Increase KVM Guests Disk Space (Guests with LVM Partitions)

1) Shutdown the guest kvm.

# virsh shutdown <kvm_guest_name>

2) Move the current image.

# mv guest-kvm.img old-guest-kvm.img

3) Create a new image the same size you would like to increase the existing one by.

# qemu-image create -f raw new.raw 10G

4) Concatenate the two images.

# cat old-guest-kvm.img new.raw >> guest-kvm.img

5) Start the kvm guest.

# virsh start <kvm_guest_name>

6) Run fdisk inside VM and delete & re-create LVM partition.

# fdisk /dev/vda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p

Disk /dev/vda: 53.7 GB, 53687091200 bytes
16 heads, 63 sectors/track, 104025 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0000fb8a

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3        1018      512000   83  Linux
/dev/vda2            1019       70737    35138424   8e  Linux LVM

Command (m for help): d
Partition number (1-4): 2

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (1-104025, default 1): 1019
Last cylinder, +cylinders or +size{K,M,G} (1019-104025, default 104025):
Using default value 104025

Command (m for help): p

Disk /dev/vda: 53.7 GB, 53687091200 bytes
16 heads, 63 sectors/track, 104025 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0000fb8a

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3        1019      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2            1019      104025    51915576   83  Linux

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 8e
Changed system type of partition 2 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/vda: 53.7 GB, 53687091200 bytes
16 heads, 63 sectors/track, 104025 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0000fb8a

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3        1019      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2            1019      104025    51915576   8e  Linux LVM

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

7) Reboot the kvm guest.

# reboot

8) Resize the LVM physical volume.

# pvdisplay
  --- Physical volume ---
  PV Name               /dev/vda2
  VG Name               vg_01
  PV Size               33.51 GiB / not usable 1.87 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              8578
  Free PE               0
  Allocated PE          8578
  PV UUID               f2b8b2ef-aa92-40a0-8285-2f6c7a34d0c3

# pvresize /dev/vda2
  Physical volume "/dev/vda2" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized
# pvdisplay
  --- Physical volume ---
  PV Name               /dev/vda2
  VG Name               vg_01
  PV Size               49.51 GiB / not usable 1.80 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              12674
  Free PE               4096
  Allocated PE          8578
  PV UUID               f2b8b2ef-aa92-40a0-8285-2f6c7a34d0c3

9) Resize the LVM logical volume.

# lvresize /dev/vg_01/lv_01 -l +100%FREE

10) Grow the filesystem.

# resize2fs /dev/vg_01/lv_01
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg_01/lv_01 is mounted on /; on-line resizing required
old desc_blocks = 3, new_desc_blocks = 4
Performing an on-line resize of /dev/vg_01/lv_01 to 12873728 (4k) blocks.
The filesystem on /dev/vg_01/lv_01 is now 12873728 blocks long.

Leave a Reply