VirtFoundry Rotating Header Image

Logical Volume Expansion With Linux LVM

Sometimes we just run out of space in filesystems. Whether due to lack of understanding of application storage needs, poor planning or we just need extra space for a development effort, we find it necessary to add disk. Virtualization gives us the ability to quickly provision new virtual disks that behave identically to physical ones. This, in conjunction with the flexibility of Linux Logical Volume Management (LVM), we can easily grow volume groups, logical volumes and filesystems. It also makes it easy to practice the techniques before provisioning additional storage in customer production environments. So let’s add a disk and grow the root filesystem in a Sun VirtualBox / Fedora 10 environment.

I’m using a Fedora 10 VirtualBox guest with a roughly 9GB virtual disk. I will add a 2GB disk to the guest, boot it and then do the following:

  • Initialize the new disk as an LVM physical volume
  • Extend the volume group containing the logical volume
  • Extend the logical volume
  • Resize the filesystem on the logical volume device
  • Add a virtual disk using the VirtualBox Media Manager

    Screenshot-Create New Virtual Disk

    Screenshot-Fedora10 - Settings
    The new disk is added as primary slave and we power up the guest machine. When the guest is up, look for the disk.

    [root@F10 ~]# fdisk -l

    Disk /dev/sda: 10.7 GB, 10737418240 bytes
    255 heads, 63 sectors/track, 1305 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Disk identifier: 0×00048601

    Device Boot Start End Blocks Id System
    /dev/sda1 * 1 25 200781 83 Linux
    /dev/sda2 26 1305 10281600 8e Linux LVM

    Disk /dev/sdb: 2147 MB, 2147483648 bytes
    255 heads, 63 sectors/track, 261 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Disk identifier: 0×00000000

    Disk /dev/sdb doesn’t contain a valid partition table

    We see the 2GB disk, /dev/sdb. Notice that there’s no partition table on it. Let’s look at the filesystem usage before extending the logical volume.

    [root@F10 ~]# df -h
    Filesystem Size Used Avail Use% Mounted on
    /dev/mapper/VolGroup00-LogVol00
    8.7G 2.1G 6.2G 25% /
    /dev/sda1 190M 14M 167M 8% /boot
    tmpfs 252M 100K 251M 1% /dev/shm

    Initialize this new disk as an LVM physical volume

    [root@F10 ~]# pvcreate /dev/sdb
    Physical volume “/dev/sdb” successfully created

    View the state of LVM physical volumes

    [root@F10 ~]# pvscan
    PV /dev/sda2 VG VolGroup00 lvm2 [9.78 GB / 32.00 MB free]
    PV /dev/sdb lvm2 [2.00 GB]
    Total: 2 [11.78 GB] / in use: 1 [9.78 GB] / in no VG: 1 [2.00 GB]

    View the current volume group state

    [root@F10 ~]# vgdisplay
    — Volume group —
    VG Name VolGroup00
    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 9.78 GB
    PE Size 32.00 MB
    Total PE 313
    Alloc PE / Size 312 / 9.75 GB
    Free PE / Size 1 / 32.00 MB
    VG UUID KskOHW-jogJ-ikWo-qgN6-KEdW-bQXd-jfWHD0

    Extend VolGroup00 using the new physical volume /dev/sdb

    [root@F10 ~]# vgextend VolGroup00 /dev/sdb
    Volume group “VolGroup00″ successfully extended

    View updated volume group status

    [root@F10 ~]# vgdisplay
    — Volume group —
    VG Name VolGroup00
    System ID
    Format lvm2
    Metadata Areas 2
    Metadata Sequence No 4
    VG Access read/write
    VG Status resizable
    MAX LV 0
    Cur LV 2
    Open LV 2
    Max PV 0
    Cur PV 2
    Act PV 2
    VG Size 11.75 GB
    PE Size 32.00 MB
    Total PE 376
    Alloc PE / Size 312 / 9.75 GB
    Free PE / Size 64 / 2.00 GB
    VG UUID KskOHW-jogJ-ikWo-qgN6-KEdW-bQXd-jfWHD0

    Notice that we now have 64 free extents. So now we need to determine the logical volume that is mounted at ‘/’

    [root@F10 ~]# lvdisplay
    — Logical volume —
    LV Name /dev/VolGroup00/LogVol00
    VG Name VolGroup00
    LV UUID 9RB7rj-t2kl-3pMa-ssDt-LxU5-woiB-ML2czB
    LV Write Access read/write
    LV Status available
    # open 1
    LV Size 8.75 GB
    Current LE 280
    Segments 1
    Allocation inherit
    Read ahead sectors auto
    – currently set to 256
    Block device 253:0

    — Logical volume —
    LV Name /dev/VolGroup00/LogVol01
    VG Name VolGroup00
    LV UUID q2INDo-Me5O-G2UB-TKl8-bGLF-bzmG-lNdhhT
    LV Write Access read/write
    LV Status available
    # open 1
    LV Size 1.00 GB
    Current LE 32
    Segments 1
    Allocation inherit
    Read ahead sectors auto
    – currently set to 256
    Block device 253:1

    We need to extend LogVol00, since it’s the 8.7GB volume

    Extend the logical volume by the number of free extents (64)

    [root@F10 ~]# lvextend -l +64 /dev/VolGroup00/LogVol00
    Extending logical volume LogVol00 to 10.75 GB
    Logical volume LogVol00 successfully resized

    Resize the filesystem on the logical volume LogVol00

    The root filesystem ‘/’ is mounted on this logical volume, but with the ext3 online resize capability in the kernel, there’s no need to unmount it.

    [root@F10 ~]# resize2fs /dev/VolGroup00/LogVol00
    resize2fs 1.41.4 (27-Jan-2009)
    Filesystem at /dev/VolGroup00/LogVol00 is mounted on /; on-line resizing required
    old desc_blocks = 1, new_desc_blocks = 1
    Performing an on-line resize of /dev/VolGroup00/LogVol00 to 2818048 (4k) blocks.
    The filesystem on /dev/VolGroup00/LogVol00 is now 2818048 blocks long.

    Let’s see what the root filesystem looks like now

    [root@F10 ~]# df -h
    Filesystem Size Used Avail Use% Mounted on
    /dev/mapper/VolGroup00-LogVol00
    11G 2.1G 8.1G 21% /
    /dev/sda1 190M 14M 167M 8% /boot
    tmpfs 252M 100K 251M 1% /dev/shm

    That was pretty straight forward, and the procedure is nearly identical when provisioning SAN LUNs to Linux hosts for LVM expansion. The difference being the method used to expose new block devices to the Linux host. So before you do that, practice in a virtual machine!

    Further reading:
    LVM-HOWTO

    2 Comments

    1. David says:

      Thank you so much, this was an extremely helpful tutorial after many hours of searching for the correct procedure!

    2. Wayne says:

      I’m glad you found it useful, David. Thanks for taking the time to comment.

    Leave a Reply