KVM disk expansion

4 minute read

Extend the KVM disk image. This time, we will expand the disk on a machine using LVM. Expand to 20GB from + 10GB to 10GB.

1. Prior confirmation

The virtual machine disk looks like this.

# df -h
File system size used Remaining used%Mount position
/dev/mapper/centos-root   8.5G  6.5G  2.1G   76% /
devtmpfs                  485M     0  485M    0% /dev
tmpfs                     496M     0  496M    0% /dev/shm
tmpfs                     496M  7.1M  489M    2% /run
tmpfs                     496M     0  496M    0% /sys/fs/cgroup
/dev/vdb                  2.6T  1.6T 1016G   62% /storage
/dev/vda1                 497M  217M  280M   44% /boot
tmpfs                     100M     0  100M    0% /run/user/1000

2. Disk image extension

First, extend the disk image file on the virtual host.
Make a backup of the image.

# cp -a /var/lib/libvirt/images/water1.img{,.bak}

Confirmation of current image

# qemu-img info water01.img
image: water01.img
file format: raw
virtual size: 10G (10737418240 bytes)
disk size: 10G

Add 10GB to a 10GB image

# qemu-img resize water01.img +10G
Image resized.

3. Disk image extension

Start the machine here and operate on the virtual machine thereafter.
Edit disk with fdisk

# fdisk /dev/vda

command(Help with m): p  #Confirmation (confirm that it is a sector unit at the same time)

disk/dev/vda: 128.8 GB,128849018880 bytes
Head 16,Sector 63,Cylinder 249660,Total 251658240 sectors
Units =Number of sectors of 1* 512 =512 bytes
Sector size(logic/Physics):512 bytes/512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disc identifier: 0x000d2e86

Device boot start / end block Id system
/dev/vda1   *        2048     1026047      512000   83  Linux
Partition 1 does not end with a cylinder boundary.
/dev/vda2         1026048    62914559    30944256   8e  Linux LVM
Partition 2 does not end with a cylinder boundary.

command(Help with m): d  #/dev/Delete vda2 once
Partition number(1-4): 2

command(Help with m): p  #/dev/Confirm that vda2 has been deleted

disk/dev/vda: 128.8 GB,128849018880 bytes
Head 16,Sector 63,Cylinder 249660,Total 251658240 sectors
Units =Number of sectors of 1* 512 =512 bytes
Sector size(logic/Physics):512 bytes/512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disc identifier: 0x000d2e86

Device boot start / end block Id system
/dev/vda1   *        2048     1026047      512000   83  Linux
Partition 1 does not end with a cylinder boundary.

command(Help with m): n  #Expanded size again/dev/Create vda2
Command action
e-extension
p primary partition(1-4)
p
Partition number(1-4): 2
First sector(63-251658239,Initial value 63): 1026048
Last sector, +Number of sectors or+size{K,M,G} (1026048-251658239,Initial value 251658239):
Use the initial value 251658239

command(Help with m): p  #Confirm that it was created

disk/dev/vda: 128.8 GB,128849018880 bytes
Head 16,Sector 63,Cylinder 249660,Total 251658240 sectors
Units =Number of sectors of 1* 512 =512 bytes
Sector size(logic/Physics):512 bytes/512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disc identifier: 0x000d2e86

Device boot start / end block Id system
/dev/vda1   *        2048     1026047      512000   83  Linux
Partition 1 does not end with a cylinder boundary.
/dev/vda2         1026048   251658239   125316096   83  Linux

command(Help with m): t  #Change format to "Linux LVM"
Partition number(1-4): 2
Hexagon code(Display code list with L command): 8e
Region system type 2 to 8e(Linux LVM)Changed to

command(Help with m): p  #Confirm that the format is "Linux LVM".

disk/dev/vda: 128.8 GB,128849018880 bytes
Head 16,Sector 63,Cylinder 249660,Total 251658240 sectors
Units =Number of sectors of 1* 512 =512 bytes
Sector size(logic/Physics):512 bytes/512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disc identifier: 0x000d2e86

Device boot start / end block Id system
/dev/vda1   *        2048     1026047      512000   83  Linux
Partition 1 does not end with a cylinder boundary.
/dev/vda2         1026048   251658239   125316096   8e  Linux LVM

command(Help with m): w  #Save disk changes
The partition table has changed!

ioctl()To reload the partition table.

warning:Partition table reload failed with error 16.:The device or resource is busy.
The kernel is still using old tables. The new table
Next reboot or part probe(8)Or kpartx(8)After running
Will be usable
The disks are syncing.

Reboot the machine and extend PV

# pvresize /dev/vda2
  Physical volume "/dev/vda2" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized

Confirm that PV has been expanded

# pvscan
  PV /dev/vda2   VG centos          lvm2 [<19.51 GiB / 10.00 GiB free]
  Total: 1 [<19.51 GiB] / in use: 1 [<19.51 GiB] / in no VG: 0 [0   ]

Expanded LV by expanded PV

# lvresize -L +10G /dev/centos/root
  Size of logical volume centos/root changed from <8.51 GiB (2178 extents) to <18.51 GiB (4738 extents).
  Logical volume centos/root successfully resized.

Expand the file system by the expanded LV

# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=256    agcount=4, agsize=557568 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=2230272, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 2230272 to 4851712

Verification. It can be confirmed that it has been expanded.

# df -h
File system size used Remaining used%Mount position
/dev/mapper/centos-root    19G  6.5G   13G   35% /
devtmpfs                  485M     0  485M    0% /dev
tmpfs                     496M     0  496M    0% /dev/shm
tmpfs                     496M  7.1M  489M    2% /run
tmpfs                     496M     0  496M    0% /sys/fs/cgroup
/dev/vdb                  2.6T  1.6T 1016G   62% /storage
/dev/vda1                 497M  217M  280M   44% /boot
tmpfs                     100M     0  100M    0% /run/user/1000