Resizing an LVM disk

This post describes how to increase an LVM disk under Linux.

user@host:/home/user# sudo qemu-img resize /path/to/disk-image.qcow2 +10G
Image resized.

Now start the VM and log in as root user. Edit the disk, in this example /dev/vda. Print and note down the start and end-block of the partition to resize.

[root@guestvm user]# fdisk /dev/vda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p

Disk /dev/vda: 26.8 GB, 26843545600 bytes, 52428800 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 label type: dos
Disk identifier: 0x000cf726

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1050623      524288   83  Linux
/dev/vda2         1050624    31457279    15203328   8e  Linux LVM

Delete the LVM partition and re-create it from the same start block but with a bigger size. Set the partition type to 8e for a LVM partition.

Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 is deleted

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2):
First sector (1050624-52428799, default 1050624): 1050624
Last sector, +sectors or +size{K,M,G} (1050624-52428799, default 52428799):
Using default value 52428799
Partition 2 of type Linux and of size 24.5 GiB is set

Command (m for help): t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Check if the result looks OK and save the partition table.

Command (m for help): p

Disk /dev/vda: 26.8 GB, 26843545600 bytes, 52428800 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 label type: dos
Disk identifier: 0x000cf726

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1050623      524288   83  Linux
/dev/vda2         1050624    52428799    25689088   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.

Reboot to make sure the change is activated.

reboot

Back in the VM, resize the physical volume.

[root@guestvm user]# pvresize /dev/vda2
  Physical volume "/dev/vda2" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized

Make sure there is now some free space.

[root@guestvm user]# pvs
  PV         VG   Fmt  Attr PSize   PFree
  /dev/vda2  vg00 lvm2 a--  <24.50g 10.00g


[root@guestvm user]# lvs
  LV      VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv_root vg00 -wi-ao---- <14.50g

Finally, extend the logical volume. The -r option also resizes the file system.

[root@guestvm user]# lvextend -r -l +100%FREE /dev/vg00/lv_root
  Size of logical volume vg00/lv_root changed from <14.50 GiB (3711 extents) to <24.50 GiB (6271 extents).
  Logical volume vg00/lv_root successfully resized.
meta-data=/dev/mapper/vg00-lv_root isize=512    agcount=4, agsize=950016 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=3800064, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
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 3800064 to 6421504

Now the size of the logical volume has increased as expected.

[root@guestvm user]# lvs
  LV      VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv_root vg00 -wi-ao---- <24.50g