调整磁盘映像大小

调整磁盘映像大小

我在尝试增加 ubuntu 磁盘映像的大小时遇到​​了麻烦。我知道我至少需要执行以下步骤:

$ # 1. resize the image using qemu-img
$ qemu-img resize linux-x86.img +12G
$ # this step succeeded
$ # 2. mount, chroot
$ sudo mount -o loop,offset=32256 linux-x86.img /mnt
$ sudo mount --bind /proc /mnt/proc
$ sudo mount --bind /dev /mnt/dev
$ cd /mnt
$ sudo chroot .

现在在 chroot 中执行以下步骤:

$ df -h
df: cannot read table of mounted file systems: No such file or directory
$ # fix this error 
$ cp /proc/mounts /etc/mtab
$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/loop0            496M  458M   13M  98% /
udev                  2.0G  4.0K  2.0G   1% /dev

您可以看到,尽管我已将图像大小增加到 12 GB,但可用空间只有 13M。

我尝试启动 fdisk,但分区表是空的:

$ fdisk /dev/loop0
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xa95b4787.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.


The number of cylinders for this disk is set to 1631.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): p

Disk /dev/loop0: 13.4 GB, 13421609472 bytes
255 heads, 63 sectors/track, 1631 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xa95b4787

      Device Boot      Start         End      Blocks   Id  System

Command (m for help): 

这是 /etc/mtab 供您参考:

/dev/loop0 / ext2 rw,relatime 0 0
proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
udev /dev devtmpfs rw,relatime,size=2010936k,nr_inodes=502734,mode=755 0 0

另外,“uname -a”的输出如下:

Linux ubuntu 4.2.0-27-generic #32~14.04.1-Ubuntu SMP Fri Jan 22 15:32:26 UTC 2016 x86_64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz GenuineIntel GNU/Linux

有人可以帮忙吗?

答案1

好了,您已经完成了调整大小的磁盘部分。您没有做的是调整其中的分区大小,这是与调整文件系统大小不同的一个步骤。fdisk 找不到任何分区的原因是它无法访问整个“磁盘”。基本上,当您需要 fdisk /dev/sd? 时,您所做的就是 fdisk /dev/sd?1,或者,在您的情况下,fdisk linux-x86.img。您也应该在 chrooting 之前执行此操作。

有些东西可以帮助您将分区和文件系统的大小调整合并为一步......

$ sudo udisksctl loop-setup -f linux-x86.img
Mapped file linux-x86.img as /dev/loop0.
$ sudo gparted /dev/loop0

然后您还可以挂载 /dev/loop0p1 或其他任何设备,而不必找到分区的起始字节并挂载该偏移量。

答案2

用这个删除循环设备

sudo udisksctl loop-delete -b /dev/loop0

相关内容