admin 发表于 2019-12-31 00:59:46

kvm扩容

当我们需要扩展模板镜像的虚拟磁盘大小时,比如原来的虚拟磁盘大小为20G,现在我们想将其扩展到30G,那么我们可以根据如下步骤来操作。

整个流程可以分为三个阶段:

1、扩展KVM镜像磁盘文件大小到30G。

2、扩展磁盘分区大小为30G。

3、扩展文件系统大小为30G。

假若当前有一个名为 test_extend.img 的模板镜像,其格式为 qcow2, virtual_size为 20G

1、首先可以使用qemu-img来查看该模板镜像的元信息:

复制代码
root@# 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@ #
复制代码
2、由于需要使用到qemu-nbd,因此我们需要先确认nbd模板已经被load:

root@# lsmod | grep nbd
root@#
   执行如上命令发现没有任何输出,则表示当前系统并没有加载nbd模块

3、加载nbd模板,并再次确认(若没有nbd模块,则可以参考这里进行编译安装):

root@ # modprobe nbd max_part=8
root@ # lsmod | grep nbd
nbd                  176030
root@ #
   此时nbd模板已经正常加载了

4、通过qemu-img命令来扩展虚拟磁盘的virtual_size为 30G:

复制代码
root@ # qemu-img resize test_extend.img 32212254720
Image resized.
root@# 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@#


5、下面通过qemu-nbd挂载test_extend.img到/dev/nbd0设备上:

root@   # qemu-nbd -c /dev/nbd0 ./test_extend.img
root@   # 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@# 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   IdSystem
/dev/nbd0p1   *      2048    41943039    20970496   83Linux

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   IdSystem

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   IdSystem
/dev/nbd0p1            2048    62914559    31456256   83Linux

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   IdSystem
/dev/nbd0p1   *      2048    62914559    31456256   83Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
root@#
复制代码
   如此以来,我们已经将该虚拟磁盘的分区大小扩展到了30G

7、至此,我们还需要将文件系统扩展到30G:

复制代码
root@# 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
页: [1]
查看完整版本: kvm扩容