Answers:
首先,您必须创建一个图像文件:
# 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)。分开也可以显示图像中的可用空间。如果要查看,只需键入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
缩小文件系统后,还必须剪切掉一些文件。但这很棘手。您可以从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
parted
与resize
以下命令无关:“错误:resize命令已在parted 3.0中删除”
是的,这是可能的-它就像分区一样工作。我尝试了以下有效的方法:
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图像会谈
e2fsck -f test.file
在调整大小之前运行(在我的图像文件上)。另一件事:在存储在NTFS分区上的驱动器映像上添加70G dd ... >> drive_image
可能需要10分钟。
seek=
其他答案中的dd 参数
稀疏文件是动态增长/调整磁盘映像大小的不错选择。
这将创建一个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
现在,使用相同的命令来调整大小,以仅使用新的图像大小更改seek参数:
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
现在享受吧!