![调整虚拟硬盘分区大小(带有 msdos 分区表的文件)](https://linux22.com/image/557401/%E8%B0%83%E6%95%B4%E8%99%9A%E6%8B%9F%E7%A1%AC%E7%9B%98%E5%88%86%E5%8C%BA%E5%A4%A7%E5%B0%8F%EF%BC%88%E5%B8%A6%E6%9C%89%20msdos%20%E5%88%86%E5%8C%BA%E8%A1%A8%E7%9A%84%E6%96%87%E4%BB%B6%EF%BC%89.png)
我的磁盘上有一个本地文件,其中包含一个 msdos 分区表和一些 linux 分区。我将其用作 qemu 的虚拟原始磁盘。
klm@lato:~/images/disk$ parted arch-linux2 p
WARNING: You are not superuser. Watch out for permissions.
Model: (file)
Disk /home/klm/images/disk/arch-linux2: 4339MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 32,3kB 107MB 107MB primary ext2 boot
2 107MB 378MB 271MB primary linux-swap(v1)
3 378MB 4100MB 3722MB primary ext4
4 4100MB 4331MB 231MB primary ext4
我想调整此文件的分区大小,GParted 通常非常容易使用。但是,当我运行
$ gparted arch-linux2
我的大多数分区都会发出警告:
e2label: No such file or directory while trying to open
/home/klm/images/disk/arch-linux2.
Couldn't find valid filesystem superblock.
为什么 gparted 无法编辑文件里面的分区?
答案1
好吧,kpartx 和符号链接来救援了!
使用部分X为每个分区创建虚拟块设备:
klm@lato:~/images/disk$ sudo kpartx -a arch-linux2
klm@lato:~/images/disk$ ls /dev/mapper/
control cryptswap1 loop0p1 loop0p2 loop0p3 loop0p4
现在您可以将 gparted 指向您的各个分区:
klm@lato:~/images/disk$ sudo gparted /dev/mapper/loop0p1
但这没什么用,因为 gparted 需要整个分区表才能调整分区大小。符号链接解决了这个问题:
klm@lato:~/images/disk$ ln -s /dev/mapper/loop0p1 arch-linux2p1
klm@lato:~/images/disk$ ln -s /dev/mapper/loop0p2 arch-linux2p2
klm@lato:~/images/disk$ ln -s /dev/mapper/loop0p3 arch-linux2p3
klm@lato:~/images/disk$ ln -s /dev/mapper/loop0p4 arch-linux2p4
这是 gparted 想要的结构:
klm@lato:~/images/disk$ ls -l
total 6275112
-rw-r--r-- 1 klm klm 4339007488 2011-07-22 15:47 arch-linux2
lrwxrwxrwx 1 klm klm 19 2011-08-04 15:23 arch-linux2p1 -> /dev/mapper/loop0p1
lrwxrwxrwx 1 klm klm 19 2011-08-04 15:23 arch-linux2p2 -> /dev/mapper/loop0p2
lrwxrwxrwx 1 klm klm 19 2011-08-04 15:23 arch-linux2p3 -> /dev/mapper/loop0p3
lrwxrwxrwx 1 klm klm 19 2011-08-04 15:23 arch-linux2p4 -> /dev/mapper/loop0p4
现在,我可以正常运行 gparted 并且没有任何警告,而且我可以调整大小!
klm@lato:~/images/disk$ sudo gparted arch-linux2