我可以扩展基于文件的磁盘映像的大小吗?

ker*_*eem 32 linux mount filesystems dd

我使用 dd 创建了一个空磁盘映像,然后使用 mkfs 使其成为真正的文件系统映像。我正在安装和使用它。我需要的是能够在需要时扩展或收缩这个基于文件的磁盘映像。是否可以通过这种方式增加磁盘映像的大小?有没有办法让这个基于文件的磁盘映像具有动态调整大小的功能,就像在虚拟机驱动器中发现的那样。

Mik*_*kov 30

首先,您必须创建一个图像文件:

# 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
Run Code Online (Sandbox Code Playgroud)

然后,您必须在其上创建一个分区——您可以使用任何您想要的工具,fdisk, parted, gparted,我更喜欢parted,因此:

# parted binary.img
Run Code Online (Sandbox Code Playgroud)

您必须先创建一个分区表,然后再创建一个大分区:

(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
Run Code Online (Sandbox Code Playgroud)

现在让我们看看:

(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
Run Code Online (Sandbox Code Playgroud)

这看起来不错的样子,

你想放大它,所以首先使用 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
Run Code Online (Sandbox Code Playgroud)

这为图像增加了 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
Run Code Online (Sandbox Code Playgroud)

如您所见,图像的大小不同(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
Run Code Online (Sandbox Code Playgroud)

并检查它:

(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
Run Code Online (Sandbox Code Playgroud)

很不错。如果你想缩小它,只需做类似的事情:

(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
Run Code Online (Sandbox Code Playgroud)

现在您可以检查分区是否较小:

(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
Run Code Online (Sandbox Code Playgroud)

是的。

如果你在有数据的情况下尝试调整分区大小,你必须注意数据的大小,因为当你缩小太多时,你会得到一个错误:

Error: Unable to satisfy all constraints on the partition
Run Code Online (Sandbox Code Playgroud)

缩小文件系统后,您还必须剪掉一些文件。但这很棘手。您可以从 parted 500M (END) 中获取值:

# dd if=./binary.img of=./binary.img.new bs=1M count=500
Run Code Online (Sandbox Code Playgroud)

但这会在文件末尾留下一些空间。我不知道为什么,但图像有效。

挂载这样的图像有一件事情——您必须知道要传递给 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)
Run Code Online (Sandbox Code Playgroud)

2048 (start) x 512 (sector size) = 1048576 ,所以你必须使用下面的命令来挂载镜像:

# mount -o loop,offset=1048576 binary.img /mnt
Run Code Online (Sandbox Code Playgroud)


dav*_*dgo 16

是的,这是可能的 - 它就像一个分区一样工作。我尝试了以下方法,效果很好:

制作原始文件,挂载,检查,卸载

dd if=/dev/zero of=test.file count=102400 
mkfs.ext3 test.file 
mount test.file /m4 -o loop
df
umount /m4
Run Code Online (Sandbox Code Playgroud)

种植它

dd if=/dev/zero count=102400 >> test.file
mount test.file /m4 -o loop
df
resize2fs /dev/loop0
df
Run Code Online (Sandbox Code Playgroud)

没有理由缩小文件不会类似地工作,但缩小文件总是比增长文件更困难(当然,需要在未安装块设备等时完成)

看看这个链接,它谈论使用 qemu-nbd 挂载 qcow2 图像


小智 8

稀疏文件是动态增长/调整磁盘映像的不错选择。

这将创建一个 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
Run Code Online (Sandbox Code Playgroud)

图像没有使用任何磁盘空间,

# du -m sparse.img
0   sparse.img
Run Code Online (Sandbox Code Playgroud)

但表观大小为 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
Run Code Online (Sandbox Code Playgroud)

您可以将其格式化并挂载为常规磁盘映像:

# 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
Run Code Online (Sandbox Code Playgroud)

现在,使用相同的命令调整大小以使用新的图像大小创建仅更改搜索参数:

dd if=/dev/zero of=sparse.img bs=1M count=0 seek=2048
Run Code Online (Sandbox Code Playgroud)

如您所见,图像现在为 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
Run Code Online (Sandbox Code Playgroud)

现在享受吧!

  • 做得很好。我认为稀疏文件没有得到充分利用。 (2认同)