我正在创建一个空文件...
dd if=/dev/zero of=${SDCARD} bs=1 count=0 seek=$(expr 1024 \* ${SDCARD_SIZE})
Run Code Online (Sandbox Code Playgroud)
...然后将其转换为驱动器映像...
parted -s ${SDCARD} mklabel msdos
Run Code Online (Sandbox Code Playgroud)
...并在其上创建分区
parted -s ${SDCARD} unit KiB mkpart primary fat32 ${IMAGE_ROOTFS_ALIGNMENT} $(expr ${IMAGE_ROOTFS_ALIGNMENT} \+ ${BOOT_SPACE_ALIGNED})
parted -s ${SDCARD} unit KiB mkpart primary $(expr ${IMAGE_ROOTFS_ALIGNMENT} \+ ${BOOT_SPACE_ALIGNED}) $(expr ${IMAGE_ROOTFS_ALIGNMENT} \+ ${BOOT_SPACE_ALIGNED} \+ $ROOTFS_SIZE)
Run Code Online (Sandbox Code Playgroud)
如何使用mkfs.ext和mkfs.vfat 不安装此图像?
要创建具有多个分区的映像,不需要任何花哨的工具或 root 访问权限的解决方案是首先创建文件系统,然后将它们连接起来。
truncate -s $IMAGE_ROOTFS_ALIGNMENT disk
truncate -s $BOOT_SPACE_ALIGNED part1
mkfs.fat part1
cat part1 >>disk
truncate -s $ROOTFS_SIZE part2
mkfs.ext4 part2
cat part2 >>disk
Run Code Online (Sandbox Code Playgroud)
然后运行parted或fdisk创建分区。
这种方法的缺点是生成的图像不会稀疏。
为了扩展@gilles 提供的答案,这里有一种创建包含格式化文件系统的磁盘映像的方法,方法是首先在文件中创建一个文件系统(在本例中为 ESP 类型),然后将其组装成一个有效的磁盘映像;不需要 root、mounts 或 loop 设备:
diskimg=diskimg # filename of resulting disk image
size=$((260*(1<<20))) # desired size in bytes, 260MB in this case
alignment=1048576 # align to next MB (https://www.thomas-krenn.com/en/wiki/Partition_Alignment)
size=$(( (size + alignment - 1)/alignment * alignment )) # ceil(size, 1MB)
# mkfs.fat requires size as an (undefined) block-count; seem to be units of 1k
mkfs.fat -C -F32 -n "volname" "${diskimg}".fat $((size >> 10))
# insert the filesystem to a new file at offset 1MB
dd if="${diskimg}".fat of="${diskimg}" conv=sparse obs=512 seek=$((alignment/512))
# extend the file by 1MB
truncate -s "+${alignment}" "${diskimg}"
# apply partitioning
parted --align optimal "${diskimg}" mklabel gpt mkpart ESP "${offset}B" '100%' set 1 boot on
Run Code Online (Sandbox Code Playgroud)
当在支持稀疏文件的文件系统上使用时,上述方法具有稀疏的副作用;生成的“262MB”文件在磁盘上占用的空间不到 200kB:
du -h --apparent diskimg; du -h diskimg
262M diskimg
196K diskimg
Run Code Online (Sandbox Code Playgroud)
对于 FAT 文件系统,Mtools实用程序支持对文件的偏移量进行操作(ext2/4/etc 可能也这样做?)。这使它更容易,您只需创建分区映像并直接处理它:
diskimg=diskimg
size=$((260*(1<<20))) # desired size in bytes, 260MB in this case
# align to next MB (https://www.thomas-krenn.com/en/wiki/Partition_Alignment)
alignment=1048576
size=$(( (size + alignment - 1)/alignment * alignment ))
# image size is gpt + filesystem size + gpt backup
truncate -s $((size + 2*alignment)) "${diskimg}"
parted --machine --script --align optimal "${diskimg}" mklabel gpt mkpart ESP "${alignment}B" '100%' set 1 boot on
mformat -i "${diskimg}"@@"${alignment}" -t $((size>>20)) -h 64 -s 32 -v "volname"
Run Code Online (Sandbox Code Playgroud)
这是生成的图像文件的图表:
您想格式化磁盘映像文件中的分区,而不是整个映像文件。在这种情况下,您需要使用losetup告诉 linux 将映像文件用作环回设备。
注意:losetup需要 root 权限,因此必须以 root 身份或使用 sudo 运行。/dev/loop*它使用/创建的设备也需要 root 权限才能访问和使用。
例如(作为根)
# losetup /dev/loop0 ./sdcard.img
# fdisk -l /dev/loop0
Disk /dev/loop0: 1 MiB, 1048576 bytes, 2048 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: 0x54c246ab
Device Boot Start End Sectors Size Id Type
/dev/loop0p1 1 1023 1023 511.5K c W95 FAT32 (LBA)
/dev/loop0p2 1024 2047 1024 512K 83 Linux
# file -s /dev/loop0p1
/dev/loop0p1: data
# mkfs.vfat /dev/loop0p1
mkfs.fat 3.0.28 (2015-05-16)
Loop device does not match a floppy size, using default hd params
# file -s /dev/loop0p1
/dev/loop0p1: DOS/MBR boot sector, code offset 0x3c+2, OEM-ID "mkfs.fat", sectors/cluster 4, root entries 512, sectors 1023 (volumes <=32 MB) , Media descriptor 0xf8, sectors/FAT 1, sectors/track 32, heads 64, serial number 0xfa9e3726, unlabeled, FAT (12 bit)
Run Code Online (Sandbox Code Playgroud)
最后,从环回设备中分离图像:
# losetup -d /dev/loop0
Run Code Online (Sandbox Code Playgroud)
有关man losetup更多详细信息,请参阅。