Extend an ext4 LVM root Volume

Our root partition is filling up and we want to increase its size on LVM.

General disclaimer applies, no liability will be accepted for any loss or damage, use at your own risk and do frequent backups!

Software

Software used in this article:

  1. Oracle Linux 6.6
  2. lvm2, e2fsck, resize2fs

Before We Begin

We are using Oracle Linux 6.6 on VirtualBox in this example (VirtualBox is not covered here).

Disk partition table looks like this:

# fdisk -l | grep sd
Disk /dev/sda: 12.9 GB, 12884901888 bytes
/dev/sda1   *    1     52    409600  83  Linux
/dev/sda2       52   1567  12172288  8e  Linux LVM

We have a single 13GB disk and two partitions on it.

Root partition usage can be seen below. It’s got around 600MB of free space.

# df -h /
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_oracle-lv_root
                      7.0G  6.0G  631M  91% /

Disks scans for physical volumes:

# pvscan
  PV /dev/sda2   VG vg_oracle   lvm2 [11.61 GiB / 0 free]
  Total: 1 [11.61 GiB] / in use: 1 [11.61 GiB] / in no VG: 0 [0]
# pvs
  PV         VG        Fmt  Attr PSize  PFree
  /dev/sda2  vg_oracle lvm2 a--  11.61g    0

Volume groups and logical volumes are as below:

# vgs
  VG        #PV #LV #SN Attr   VSize  VFree
  vg_oracle   1   4   0 wz--n- 11.61g    0
# lvs
  LV      VG        Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv_home vg_oracle -wi-ao----   1.95g                                          
  lv_root vg_oracle -wi-ao----   7.21g                                          
  lv_swap vg_oracle -wi-ao---- 500.00m                                          
  lv_var  vg_oracle -wi-ao----   1.95g

Extend root LVM

Extend the disk on VirtualBox and check the disk partition table:

# fdisk -l | grep sd
Disk /dev/sda: 32.2 GB, 32212254720 bytes
/dev/sda1   *    1     52    409600  83  Linux
/dev/sda2       52   1567  12172288  8e  Linux LVM

It’s now got 32GB of disk space.

Modify disk partition table so that the second partition (the LVM one) is extended. Be very careful here.

# fdisk /dev/sda
>> d (delete)
>> 2
>> n (create a new one)
>> p
>> 2
>> [enter] make sure the first cylinder matches the one you deleted before (i.e. 52)
>> [enter] last cylinder should be default to use all space available
>> p
>> t (change filesystem type to LVM)
>> 2
>> 8e
>> p
>> w (save changes)

Partitions information will be re-read after reboot:

# reboot

Alternatively to system reboot, you can read disk (see https://access.redhat.com/solutions/57542) and try to add a new partition to the system (use at your own risk):

# partx -v -a /dev/sda

After reboot, resize /dev/sda2 physical volume:

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

Check physical volume and volume group information:

# pvs
  PV         VG        Fmt  Attr PSize  PFree
  /dev/sda2  vg_oracle lvm2 a--  29.61g 18.00g
# vgs
  VG        #PV #LV #SN Attr   VSize  VFree
  vg_oracle   1   4   0 wz--n- 29.61g 18.00g

Extend the logical root volume by 10GB:

# lvextend --size +10GB /dev/mapper/vg_oracle-lv_root
  Size of logical volume vg_oracle/lv_root changed from 7.21 GiB (1846 extents) to 17.21 GiB (4406 extents).
  Logical volume lv_root successfully resized

You should really check the filesystem at this point, however, the results printed by e2fsck are not valid if the filesystem is mounted:

# e2fsck -n /dev/mapper/vg_oracle-lv_root

Resize an ext4 filesystem on the root volume:

# resize2fs /dev/mapper/vg_oracle-lv_root
resize2fs 1.43-WIP (20-Jun-2013)
Filesystem at /dev/mapper/vg_oracle-lv_root is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/mapper/vg_oracle-lv_root is now 4511744 blocks long.

Check root partition usage:

# df -h /
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_oracle-lv_root
                       17G  6.0G   11G  38% /

Related Posts

Using Linux LVM on Amazon EC2 Ubuntu Server

Leave a Reply

Your email address will not be published. Required fields are marked *