Ubuntu LVM2 – Smart Storage

Overview

There are plenty of exceptions where hardware and software abstractions are used to create modular and flexible designs in a modern OS. Examples can be seen from Microsoft’s .Net Framework to Virtualization.

Logical Volume Management or LVM is no exception. It allows the separation of physical disk storage devices to a software layer allowing for partitions to span across multiple disks to only appear as one drive to the user.

What Is LVM

LVM allows a volume to be resized with less dependency no physical hard disks. In LVM, physical hard disks are referred to as physical volumes of PV. Each hard disk may contain more than one volume just like a hard disk can be partitioned many times.

A Logical Volume (LV) is a software managed volume. This allows the LV to extend or contract in size as and when needed. As time goes on more disks can be added and the LV can expand to take advantage of the disk without referring to each new additional drive as a separate drive e.g in Windows world E:\ currently exists with 100GB with a new drive added, it must be mounted on another drive letter e.g F:\ with 200GB. Using LVM E:\ will remain as a drive but the second drive will be added to E:\ making it appear as though drive E:\ has 300 GB, 100 GB from the original drive and 200 GB from the new drive.

Similarly the drives can be removed from the LVM if the drive is to be removed or used for another purpose. Reducing the size is more dangerous than expanding one and also all the existing data needs to fit onto the remaining drive space.

LVM should be used from the start as it requires the disk to be formatted / partitioned. LVM can consist of one disk and more can be added later.

History

I have encountered LVMs when Fedora first came out but never really understood / utilized them. It did not help that Ubuntu never had any user friendly way to configure them and Gparted, my preferred disk partition manager did not support LVM.

Fedora / Redhat had always supported LVM very well and now Ubuntu is starting to catch up. The GUI still has some way to go but it’s improving all the time.

LVM has also moved to version 2 with the Linux 2.6 kernel and is the version this article will document.

Install LVM And Tools

The remainder of the article will document setting up and configuring LVM on a Ubuntu 10.04 system. LVM is used in other Linux distributions and the commands may be common across all platforms but some may be Ubuntu specific.

  1. Install LVM and GUI configuration tool
    sudo apt-get install lvm2 system-config-lvm
  2. Install a partition editor Gparted
    sudo apt-get install gparted

Preparing Disk For LVM

  1. Ensure the hard disk / partition is in the machine. If not then shutdown the system, attach the disk and start the computer up again.
  2. Start Gparted
  3. System > Administration > Gparted

  4. Select the disk in the upper right drop down list.
  5. If it’s a new device a partition table needs to be created
    Device > Create partition table...
  6. Create an unformatted partition
    Right click on the disk allocation > New
  7. Select unformatted as the File System and change the partition size if necessary / not using the whole disk. Leave the rest as is unless
  8. Click OK to create the partition.
  9. Note the partition number in the list below the graph. In the picture below this is /dev/sdb1
  10. Repeat above steps to prepare any other hard disks to be used in LVM.
  11. Create / prepare the physical volume (hard disks)
    sudo pvcreate /dev/sdb1
    where /dev/sdb1 is the partition. This can be obtained from Gparted. Multiple partitions can be created by adding each one after the other in pvcreate command. e.g sudo pvcreate /dev/sdb1 /dev/sda1 /dev/sdc1

Creating Volume Groups

  1. Create a Logical Volume group. Each group can have multiple volumes but a group will share the same physical volumes / hard disks. To create a LV, it must be associated with at least one physical volume.
    sudo vgcreate lvm1 /dev/sdb1
    where lvm1 is my group name and /dev/sdb1 is the PV allocated to lvm1.

Create Logical Volumes

  1. Create a volume in the group.
    sudo lvcreate -L931.5G -nhome lvm1
    where -L99G is the size of the volume. In this example it is 931.51 GB. Different units can be used such as MB. -nhome is the name of the volume and lvm1 is the volume group.
  2. Repeat above step to add as many volumes as you wish. Each volume is treated as a individual “drive” changing the volume name each time as a minimum.

    Although Gparted now shows the partition as being formated to lvm2, it is actually not formatted. Just like creating a partition it does not format the drive for use, creating a volume so far has only created the space for the LV.

  3. Format LV to EXT4 (any file system can be used)
    mount -t ext4 -j /dev/lvm1/home
    Note that you specify the logical volume and not the physical volume. -t is the file format and -j means use journalling.

Mounting Logical Volumes

  1. To mount the new volume it will be located in /dev/[volume group]/[volume] for example /dev/lvm1/home The full command to mount the drive would be sudo mount /media/home /dev/lvm1/home

LVM GUI Tool

As part of the install of LVM there was a GUI tool installed as well system-config-lvm. To start the tool go to Applications > System Tools > Logical Volume Management

So far in the example used in this post, one LVM group with one physical volume has been create. This can be seen in the LVM tool:

Adding More Space to a Volume

A new drive or partition would be needed to extend the volume group size. How the spare space is allocated to each volume can be managed separately.

  1. Prepare the new disk / partition as in the above sections depending on the state of the drives. See Preparing Disk For LVM
  2. Add the newly prepared disk to the LVM group
    sudo vgextend lvm1 /dev/sdd1
    where lvm1 is the group name and /dev/sdd1 is the partition name.
    The disk has been added to the group however the free space have not been allocated to volumes yet.
  3. To allocate the free space to a volume it needs to be extended to take up the freespace:
    sudo lvextend -L+931.51G /dev/lvm1/home
    where -L+931.51G is how much to allocate and /dev/lvm1/home is the volume to make bigger.

Removing Disk From LVM

Ensure the data currently on the LVM can fit onto any disks that remain attached to a LVM group. It is advised that a backup is taken before continuing.

  1. Umount the LV
    sudo umount /dev/lvm1/home
  2. Perform a check disk and fix any disk errors
    sudo e2fsck -f /dev/lvm1/home
  3. Resize the volume
    lvreduce /dev/lvm1/home -L931.5G
  4. Commit the resize to disk and not just the LVM
    sudo resize2fs /dev/lvm1/home
  5. Remove partition from LVM group
    sudo pvmove /dev/sdd1

Migrating LVM

If you need to format and reinstall your OS or move your LVM storage to a new machine it needs to be prepared for that prior to moving it.

  1. Unmount the volume
    sudo umount /dev/lvm1/home
  2. Make the volume inactive
    vgchange -an lvm1
  3. Export the group so that it can be read by another system
    sudo vgexport lvm1
  4. Power down and remove / format system disk.
  5. Import LVM
    sudo vgimport lvm1
  6. Reactivate the group
    sudo vgchange -ay lvm1

Useful Commands

Summary

LVM is a very clean way to allow for volumes to be expanded without impacting current software or setups. It helps to alleviate problems when disk drives run out of space and is a bonus to allow drives to be kept rather than replaced. It is advised that the process is done at the beginning of an install to avoid migrating the data from a non-LVM disk to a LVM space. It took several hours to copy just under 1TB from one system to the other.

SettingUpLVM-WithoutACleanInstall

How do i remove a disk from an LVM?

Moving a volume group to another system

About Danny

I.T software professional always studying and applying the knowledge gained and one way of doing this is to blog. Danny also has participates in a part time project called Energy@Home [http://code.google.com/p/energyathome/] for monitoring energy usage on a premise. Dedicated to I.T since studying pure Information Technology since the age of 16, Danny Tsang working in the field that he has aimed for since leaving school. View all posts by Danny → This entry was posted in Linux and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

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

All comments must go through an approval and anti-spam process before appearing on the website. Please be patience and do not re-submit your comment if it does not appear.

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