15AH, San Francisco

California, United States.

Send Your Mail At:

tianyingkejishe@sina.cn

Working Hours

Mon-Sat: 9.30am To 7.00pm

归档标题

Autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et dolore feugait.

月度归档 10月 9, 2023

【IMG】创建和修改镜像内容

一、创建新镜像文件

1、创建镜像文件

使用dd命令创建一个空的镜像文件rootfs.img,每次读取和写入的字节数是1M,大小为1M*2048;大小可根据自己需要保存文件的多少自定义。

$ dd if=/dev/zero of=rootfs.img bs=1M count=2048

2、格式化镜像文件

格式化镜像文件rootfs.img为ext4格式,具体格式化的类型还有ext3、ntfs、fat等格式可自定义。

$ mkfs.ext4 -c rootfs.img  

3、挂载镜像文件

挂载镜像文件rootfs.img到/mnt目录中。具体挂载的目录可以自定义。

$ sudo mount -t ext4 -o loop rootfs.img /mnt

4、拷贝文件到镜像

拷贝需要打包到镜像中的文件到挂载的目录中,此处表示拷贝rootfs/下的所有文件到/mnt目录中(即镜像文件中)。
$ sudo cp -rfp rootfs/* /mnt

5、取消挂载

取消镜像文件的挂载。取消挂载后就表示镜像文件制作完成。

$ sudo umount /mnt

二、修改镜像文件

1、查看镜像分区

fdisk *.img
xiaobao@Newu:~/hdd/tmp$ fdisk AB-small-S-lvm-A03-other-armbian-1.1.100.57.9.img

Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk AB-small-S-lvm-A03-other-armbian-1.1.100.57.9.img: 2.77 GiB, 2956984320 bytes, 5775360 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

Device                                             Boot  Start     End Sectors  Size Id Type
AB-small-S-lvm-A03-other-armbian-1.1.100.57.9.img1        8192  530431  522240  255M  c W95 FAT32 (LBA)
AB-small-S-lvm-A03-other-armbian-1.1.100.57.9.img2      532480 5775359 5242880  2.5G 83 Linux

2、挂载分区

sudo mount -o loop,offset=4194304 AB-small-S-lvm-A03-other-armbian-1.1.100.57.9.img /mnt/tmp/

可以看到img文件中有两个分区,第一个分区是从8192开始的,这里需要特别注意,需要转换一下:8192*512=4194304B

3、替换内容

$ sudo cp -rfp rootfs/* /mnt/tmp/

三、硬盘导出镜像工具