Version 5 (modified by JaimeV, 13 months ago) (diff)


How to extend a logical volume on a KVM guest

Sometimes a file system on our guests fill up, meaning that data cannot be written to parts of the file system. For example, vg_stone0-var will have no remaining space making it impossible to write to the /var directory.

Step One: figure out file system and logical volume size

In order to determine if this is the case, you can run

# df -h

The output will look like this:

0 stone:~# df -h
Filesystem            Size  Used Avail Use% Mounted on
                      3.8G  957M  2.7G  27% /
tmpfs                1007M     0 1007M   0% /lib/init/rw
udev                 1001M  108K 1001M   1% /dev
tmpfs                1007M     0 1007M   0% /dev/shm
/dev/vda1             236M   22M  202M  10% /boot
                      961M   19M  894M   3% /tmp
                      6.9G  4.6G  2.1G  69% /var
                       30G   24G  4.3G  85% /home
0 stone:~# 

If the fifth column says 100% then the file system is full. In order to check if any space exists on the logical volume, you can run:

# lvdisplay

The output should look something like this.

0 stone:~# lvdisplay /dev/vg_stone0/var
  --- Logical volume ---
  LV Name                /dev/vg_stone0/var
  VG Name                vg_stone0
  LV UUID                quZZX4-zRSb-sGyH-1V1b-nBx9-A2rX-oGau0V
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                7.00 GiB
  Current LE             1792
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:3
0 stone:~# 

You will want to check the LV Size and make sure it's the same size as the df -h command above. If it's larger than the output of df -h, then more space has been allotted to the drive and you can skip to step 3.

If you've determined that the LV Size needs extending from the proceeding step, you'll need to check and see if the volume group has available space:

0 stone:~# vgs
  VG        #PV #LV #SN Attr   VSize  VFree
  vg_stone0   1   5   0 wz--n- 49.76g 7.52g
0 stone:~# 

If you do not see enough VFree space, you'll need to follow the instructions on extend-disk-on-kvm-guest.

Also check which physical device(s) are assigned to your logical volume group. On servers using lvm caching more than on physical device may be assigned to the same volume group.

0 stone:~# pvs
  PV         VG        Fmt  Attr PSize    PFree 
  /dev/vda2  vg_stone0 lvm2 a--  <249.76g 78.80g

Step Two: Extend the logical volume

Once you've done that, then execute the following command for the logical volume. Make sure that the number after --size is the total size of the logical volume you want. In the below example /dev/vda2 is the physical volume based on spinning disk used for volume group vg_stone0. When extending logical volumes for servers that have lvm cache enabled we want to be sure that the extents for the space we add come from the physical device based on spinning disk.

# lvextend --size 10GB /dev/mapper/vg_stone0-var /dev/vda2

Resizing cached logical volumes

# lvextend --size +40G /dev/mapper/vg_server0-home
  Unable to resize logical volumes of cache type.

If the logical volume you need to extend is an lvm cache volume disable caching for this volume before extending the origin logical volume, resizing the filesystem and enabling the cache again.

Step Three: Extend the file system

Once you've extended the logical volume, you need to extend the file system as well. The following command will extend the file system.

resize2fs /dev/mapper/LVNAME

You may be asked to run e2fsck when running the above command. If so try to umount the file system:

# umount /home

Some file systems will be too busy to umount (for example, /var will likely not umount) and your only recourse will be to fsck (see Step Uh Oh).

e2fsck /dev/mapper/LVNAME

When running this command, you may be asked to allow e2fsck to fix parts of the file system. Generally, you should answer yes to such requests.

Then rerun resize2fs command. If it succeeds, you should have extended the logical volume. Repeat Step One and make sure everything looks correct.

Step Uh Oh

If you were asked to run e2fsck and you cannot umount the file system or if you have done all of the above and the file system seems to be acting in an unusual manner. You may need to run fsck to fix the file system. The easiest way to accomplish this is to restart the guest with a -F option.

# shutdown -hF now