Resize a RHEL7 QEMU/KVM image

By | February 8, 2017

At some point my RHEL7 development virtual machine run out of space.
Resizing the image is really made easy by RedHat:

STEP 1: Resize the virtual machine disk

[root@localhost VMs]# qemu-img resize RHEL7-clone.qcow2 50G
Image resized.
[root@localhost VMs]#

STEP 2: Start the VM and check the disks from inside the VM

Check the virtual disk:

[root@test1 ~]# fdisk /dev/sda
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/sda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x0005ea5f

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    41943039    20458496   8e  Linux LVM

We can see here that the virtual disk size was updated to 53.7 GB. This is good it means that the qemu-img was successful.

Check the physical volume:

[root@test1 ~]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               rhel
  PV Size               19.51 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              4994
  Free PE               10
  Allocated PE          4984
  PV UUID               mJKfU1-q4yd-XAUM-2nbd-SUeD-5Ogn-PVno7Z

We see here that we will need to extend this pv /dev/sda2.

Check the volume group

[root@test1 ~]# vgdisplay
  --- Volume group ---
  VG Name               rhel
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               19.51 GiB
  PE Size               4.00 MiB
  Total PE              4994
  Alloc PE / Size       4984 / 19.47 GiB
  Free  PE / Size       10 / 40.00 MiB
  VG UUID               NUJi9T-6QZd-vXsA-7VI4-sfP8-cjLt-np8Iiu

Check the logical volumes

 
[root@test1 ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/rhel/swap
  LV Name                swap
  VG Name                rhel
  LV UUID                TDLETn-ngo9-L8aD-vF0g-K5W3-THs8-N2Yzlf
  LV Write Access        read/write
  LV Creation host, time test1.voina.org, 2016-04-13 12:39:24 +0300
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/rhel/root
  LV Name                root
  VG Name                rhel
  LV UUID                vwgcMf-EjDs-c3te-lDkl-VKSQ-491C-SPyzPe
  LV Write Access        read/write
  LV Creation host, time test1.voina.org, 2016-04-13 12:39:25 +0300
  LV Status              available
  # open                 1
  LV Size                17.47 GiB
  Current LE             4472
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

We see here that we want to extend the root logical volume /dev/rhel/root.

STEP 3: Allocate the extra space

First we have to resize the primary partition /dev/sda2. This can be done with parted resize.
The issue here is RHEL7 does not support parted resize see https://access.redhat.com/articles/1190213

There two alternatives. One is to delete and remake /dev/sda2, that is not an option here. The other is to simply create a new partition, create anew physical volume, add it to the volume group and extend the logical volume over it.

Create primary partition /dev/sda3, allocate all the space and make it of type LVM

[root@test1 ~]# fdisk /dev/sda
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): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3): 
First sector (41943040-104857599, default 41943040): 
Using default value 41943040
Last sector, +sectors or +size{K,M,G} (41943040-104857599, default 104857599): 
Using default value 104857599
Partition 3 of type Linux and of size 30 GiB is set

Command (m for help): p

Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x0005ea5f

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    41943039    20458496   8e  Linux LVM
/dev/sda3        41943040   104857599    31457280   83  Linux

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

Command (m for help): p

Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x0005ea5f

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    41943039    20458496   8e  Linux LVM
/dev/sda3        41943040   104857599    31457280   8e  Linux LVM

STEP 5: Create a new physical disk on the newly created primary partition

First refresh the partition table. Otherwise pvcreate will complain that /dev/sda3 does not exist.

[root@test1 ~]# pvcreate /dev/sda3
  Device /dev/sda3 not found (or ignored by filtering).

So call partprobe then try again.

[root@test1 ~]# partprobe

Create the new physical volume

[root@test1 ~]# pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created

STEP 6: Extend the volume group rhel over the new physical volume.

[root@test1 ~]# vgextend rhel /dev/sda3
  Volume group "rhel" successfully extended
[root@test1 ~]# vgs
  VG   #PV #LV #SN Attr   VSize  VFree 
  rhel   2   2   0 wz--n- 49.50g 30.04g
[root@test1 ~]# pvs -o+pv_used
  PV         VG   Fmt  Attr PSize  PFree  Used  
  /dev/sda2  rhel lvm2 a--  19.51g 40.00m 19.47g
  /dev/sda3  rhel lvm2 a--  30.00g 30.00g     0 

We see here we have now 30g free in this rhel volume group.

STEP 7: Resize the root logical volume to fill all the available space

[root@test1 ~]# lvresize -l +100%FREE /dev/mapper/rhel-root
  Size of logical volume rhel/root changed from 17.47 GiB (4472 extents) to 47.50 GiB (12161 extents).
  Logical volume root successfully resized.
[root@test1 ~]# lvs
  LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root rhel -wi-ao---- 47.50g                                                    
  swap rhel -wi-ao----  2.00g 

STEP 8: Extend the file system to fill the entire logical volume.

Note that by default RHEL7 uses xfs so we have to use xfs_growfs. For other partition types you can use resizefs

[root@test1 ~]# xfs_growfs /dev/mapper/rhel-root 
meta-data=/dev/mapper/rhel-root  isize=256    agcount=4, agsize=1144832 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=4579328, 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 4579328 to 12452864
[root@test1 ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   48G  4.0G   44G   9% /
devtmpfs               3.9G     0  3.9G   0% /dev
tmpfs                  3.9G  152K  3.9G   1% /dev/shm
tmpfs                  3.9G  8.8M  3.9G   1% /run
tmpfs                  3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/sda1              497M  212M  286M  43% /boot
tmpfs                  783M   16K  783M   1% /run/user/1001

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.