当我们需要扩展模板镜像的虚拟磁盘大小时,比如原来的虚拟磁盘大小为20G,现在我们想将其扩展到30G,那么我们可以根据如下步骤来操作。
整个流程可以分为三个阶段:
1、扩展KVM镜像磁盘文件大小到30G。
2、扩展磁盘分区大小为30G。
3、扩展文件系统大小为30G。
假若当前有一个名为 test_extend.img 的模板镜像,其格式为 qcow2, virtual_size为 20G
1、首先可以使用qemu-img来查看该模板镜像的元信息:
root@cason:~/image# qemu-img info test_extend.img image: test_extend.img file format: qcow2 virtual size: 20G (21474836480 bytes) disk size: 309M cluster_size: 65536 Format specific information: compat: 1.1 lazy refcounts: false root@cason:~/image#
2、由于需要使用到qemu-nbd,因此我们需要先确认nbd模板已经被load:
root@cason:~/image# lsmod | grep nbd root@cason:~/image#
执行如上命令发现没有任何输出,则表示当前系统并没有加载nbd模块
3、加载nbd模板,并再次确认(若没有nbd模块,则可以参考这里进行编译安装):
root@cason:~/image# modprobe nbd max_part=8 root@cason:~/image# lsmod | grep nbd nbd 17603 0 root@cason:~/image#
此时nbd模板已经正常加载了
4、通过qemu-img命令来扩展虚拟磁盘的virtual_size为 30G:
root@cason:~/image# qemu-img resize test_extend.img 32212254720 Image resized. root@cason:~/image# qemu-img info test_extend.img image: test_extend.img file format: qcow2 virtual size: 30G (32212254720 bytes) disk size: 309M cluster_size: 65536 Format specific information: compat: 1.1 lazy refcounts: false root@cason:~/image#
5、下面通过qemu-nbd挂载test_extend.img到/dev/nbd0设备上:
root@cason:~/image# qemu-nbd -c /dev/nbd0 ./test_extend.img root@cason:~/image# ll /dev/nbd* | grep nbd0 brw-rw---- 1 root disk 43, 0 11月 20 18:09 /dev/nbd0 brw-rw---- 1 root disk 43, 1 11月 20 18:09 /dev/nbd0p1 root@cason:~/image#
挂载成功后,在/dev/下会看到如上信息,其中/dev/nbd0p1表示该虚拟磁盘仅有一个分区
6、下面开始通过fdisk来扩展虚拟磁盘分区大小:
root@cason:~/image# fdisk /dev/nbd0 Command (m for help): p Disk /dev/nbd0: 32.2 GB, 32212254720 bytes 255 heads, 63 sectors/track, 3916 cylinders, total 62914560 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: 0x000e8e8d Device Boot Start End Blocks Id System /dev/nbd0p1 * 2048 41943039 20970496 83 Linux Command (m for help): d Selected partition 1 Command (m for help): p Disk /dev/nbd0: 32.2 GB, 32212254720 bytes 255 heads, 63 sectors/track, 3916 cylinders, total 62914560 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: 0x000e8e8d Device Boot Start End Blocks Id System Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): Using default value 1 First sector (2048-62914559, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-62914559, default 62914559): Using default value 62914559 Command (m for help): p Disk /dev/nbd0: 32.2 GB, 32212254720 bytes 255 heads, 63 sectors/track, 3916 cylinders, total 62914560 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: 0x000e8e8d Device Boot Start End Blocks Id System /dev/nbd0p1 2048 62914559 31456256 83 Linux Command (m for help): a Partition number (1-4): 1 Command (m for help): p Disk /dev/nbd0: 32.2 GB, 32212254720 bytes 255 heads, 63 sectors/track, 3916 cylinders, total 62914560 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: 0x000e8e8d Device Boot Start End Blocks Id System /dev/nbd0p1 * 2048 62914559 31456256 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. root@cason:~/image#
如此以来,我们已经将该虚拟磁盘的分区大小扩展到了30G
7、至此,我们还需要将文件系统扩展到30G:
root@cason:~/image# e2fsck -fp /dev/nbd0p1 /dev/nbd0p1: Deleted inode 131076 has zero dtime. FIXED. /dev/nbd0p1: 18489/1310720 files (0.2% non-contiguous), 281286/5242624 blocks root@cason:~/image# resize2fs /dev/nbd0p1 resize2fs 1.42.9 (4-Feb-2014) Resizing the filesystem on /dev/nbd0p1 to 7864064 (4k) blocks. The filesystem on /dev/nbd0p1 is now 7864064 blocks long. root@cason:~/image# qemu-nbd -d /dev/nbd0
/dev/nbd0 disconnected
至此,我们就完成了扩展虚拟磁盘大小的操作。