我使用 dd 创建了一个空磁盘映像,然后使用 mkfs 使其成为真正的文件系统映像。我安装并使用它很好。我需要的是能够在需要时扩展或缩小此基于文件的磁盘映像。是否可以通过这种方式增加磁盘映像的大小?有没有办法让此基于文件的磁盘映像具有像虚拟机驱动器中那样的动态调整大小功能。
答案1
首先你必须创建一个图像文件:
# dd if=/dev/zero of=./binary.img bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 10.3739 s, 101 MB/s
然后,您必须在其上创建一个分区——您可以使用任何您想要的工具,,,,fdisk
我更喜欢parted
,所以:gparted
parted
# parted binary.img
您必须先创建一个分区表,然后创建一个大分区:
(parted) mktable
New disk label type? msdos
(parted) mkpartfs
WARNING: you are attempting to use parted to operate on (mkpartfs) a file system.
parted's file system manipulation code is not as robust as what you'll find in
dedicated, file-system-specific packages like e2fsprogs. We recommend
you use parted only to manipulate partition tables, whenever possible.
Support for performing most operations on most types of file systems
will be removed in an upcoming release.
Partition type? primary/extended? primary
File system type? [ext2]? fat32
Start? 1
End? 1049M
现在让我们看看:
(parted) print
Model: (file)
Disk /media/binary.img: 1049MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 1049kB 1049MB 1048MB primary fat32 lba
看上去不错,
您想放大它,因此首先使用 dd 在图像中添加一些零:
# dd if=/dev/zero bs=1M count=400 >> ./binary.img
400+0 records in
400+0 records out
419430400 bytes (419 MB) copied, 2.54333 s, 165 MB/s
root:/media# ls -al binary.img
-rw-r--r-- 1 root root 1.4G Dec 26 06:47 binary.img
这给图像增加了 400M:
# parted binary.img
GNU Parted 2.3
Using /media/binary.img
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: (file)
Disk /media/binary.img: 1468MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 1049kB 1049MB 1048MB primary fat32 lba
如您所见,镜像的大小不同(1468MB)。Parted 还可以显示镜像中的可用空间。如果您想查看它,只需输入print free
而不是print
。现在您必须将额外的空间添加到文件系统:
(parted) resize 1
WARNING: you are attempting to use parted to operate on (resize) a file system.
parted's file system manipulation code is not as robust as what you'll find in
dedicated, file-system-specific packages like e2fsprogs. We recommend
you use parted only to manipulate partition tables, whenever possible.
Support for performing most operations on most types of file systems
will be removed in an upcoming release.
Start? [1049kB]?
End? [1049MB]? 1468M
并检查:
(parted) print
Model: (file)
Disk /media/binary.img: 1468MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 1049kB 1468MB 1467MB primary fat32 lba
非常棒。如果你想缩小它,只需做类似的事情:
(parted) resize 1
WARNING: you are attempting to use parted to operate on (resize) a file system.
parted's file system manipulation code is not as robust as what you'll find in
dedicated, file-system-specific packages like e2fsprogs. We recommend
you use parted only to manipulate partition tables, whenever possible.
Support for performing most operations on most types of file systems
will be removed in an upcoming release.
Start? [1049kB]?
End? [1468MB]? 500M
现在您可以检查分区是否更小:
(parted) print
Model: (file)
Disk /media/binary.img: 1468MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 1049kB 500MB 499MB primary fat32 lba
是的。
如果您在分区上有数据时尝试调整分区大小,则必须注意数据的大小,因为当您将其缩小太多时,您将收到错误:
Error: Unable to satisfy all constraints on the partition
缩小文件系统后,您还必须删除一些文件。但这很棘手。您可以从 parted 500M (END) 中获取值:
# dd if=./binary.img of=./binary.img.new bs=1M count=500
但这会在文件末尾留下一些空间。我不知道为什么,但图像可以正常工作。
挂载此类映像时,有一件事需要注意——您必须知道要传递给 mount 命令的偏移量。例如,您可以从 fdisk 获取偏移量:
# fdisk -l binary.img
Disk binary.img: 1468 MB, 1468006400 bytes
4 heads, 32 sectors/track, 22400 cylinders, total 2867200 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
Disk identifier: 0x000f0321
Device Boot Start End Blocks Id System
binary.img1 2048 2867198 1432575+ c W95 FAT32 (LBA)
2048(开始)x 512(扇区大小)= 1048576,因此您必须使用以下命令来挂载映像:
# mount -o loop,offset=1048576 binary.img /mnt
答案2
是的,这是可能的 - 它就像分区一样工作。我尝试了以下方法,有效:
制作原始文件、挂载、检查、卸载
dd if=/dev/zero of=test.file count=102400
mkfs.ext3 test.file
mount test.file /m4 -o loop
df
umount /m4
种植它
dd if=/dev/zero count=102400 >> test.file
mount test.file /m4 -o loop
df
resize2fs /dev/loop0
df
没有理由说缩小文件大小不能以类似的方式工作,但是缩小文件大小总是比增大文件大小更困难(当然,需要在未安装块设备时进行)。
看一下此链接其中讨论了使用 qemu-nbd 挂载 qcow2 镜像
答案3
稀疏文件是动态增长/调整磁盘映像大小的不错选择。
这将创建一个 1024M 的稀疏文件:
# dd if=/dev/zero of=sparse.img bs=1M count=0 seek=1024
0+0 records in
0+0 records out
0 bytes (0 B) copied, 0.000565999 s, 0.0 kB/s
该图像未占用任何磁盘空间,
# du -m sparse.img
0 sparse.img
但其表观大小为1024M。
# ls -l sparse.img
-rw-rw-r--. 1 root root 1073741824 Sep 22 14:22 sparse.img
# du -m --apparent-size sparse.img
1024 sparse.img
您可以将其格式化并安装为常规磁盘映像:
# parted sparse.img
GNU Parted 2.1
Using /tmp/sparse.img
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mktable
New disk label type? msdos
(parted) mkpartfs
WARNING: you are attempting to use parted to operate on (mkpartfs) a file system.
parted's file system manipulation code is not as robust as what you'll find in
dedicated, file-system-specific packages like e2fsprogs. We recommend
you use parted only to manipulate partition tables, whenever possible.
Support for performing most operations on most types of file systems
will be removed in an upcoming release.
Partition type? primary/extended? primary
File system type? [ext2]? fat32
Start? 1
End? 1024M
(parted) print
Model: (file)
Disk /tmp/sparse.img: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 1049kB 1024MB 1023MB primary fat32 lba
# du -m sparse.img
2 sparse.img
现在,使用相同的命令调整大小,只需将搜索参数更改为新的图像大小:
dd if=/dev/zero of=sparse.img bs=1M count=0 seek=2048
如您所见,图像现在为 2048M,您可以使用 parted 或您选择的其他工具来扩大分区。
# du -m --apparent-size sparse.img
2048 sparse.img
# parted sparse.img
GNU Parted 2.1
Using /tmp/sparse.img
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print free
Model: (file)
Disk /tmp/sparse.img: 2147MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
16.4kB 1049kB 1032kB Free Space
1 1049kB 1024MB 1023MB primary fat32 lba
1024MB 2147MB 1123MB Free Space
(parted)
# du -m sparse.img
2 sparse.img
现在享受它吧!