Why LVM is so useful on the Cloud

Published 2011-11-17 by Jochen Lillich

The Logical Volume Manager LVM2 in Linux is a great way to become independent from physical – or virtual – disk devices.

Especially in a cloud environment like the Amazon Web Services where you can add or remove virtual disks (called “Elastic Block Storage”, EBS) at any time, LVM makes it easy to adapt a server’s storage capacity to current demand.

Preparations: Create an LVM volume

To build your filesystem, you first create an EBS volume via the Web Console or the EC2 command line tools and attach it to the EC2 instance. The default block device name is /dev/sdf, but you can choose another one if you want or need to.

This EBS volume must be marked as a Physical Volume (PV), the basic building block of our LVM storage pool:

# pvcreate /dev/sdf

The PV becomes the foundation of a Volume Group (VG). Later, you’ll be able to add other PVs to the VG, thereby growing the VG’s storage capacity.

# vgcreate vg0 /dev/sdf

Finally, from this VG, you create Logical Volumes (LV). LVs are the virtual disk devices that you’re going to format and mount. Modern filesystems like ext3/4 or XFS are able to grow in size without reformatting; this will come in handy later.

# lvcreate -L20G -n vol1 vg0
# mkfs.xfs /dev/vg0/vol1
# mkdir /vol1
# mount -t xfs /dev/vg0/vol1 /vol1

How to increase a volume’s size

If your virtual disk, the LV, is filling up, you can easily grow its capacity. Just create and attach another EBS volume (for example as /dev/sdg), add it to the VG, and finally extend first the LV and then the filesystem:

# pvcreate /dev/sdg
# vgextend vg0 /dev/sdg
# lvextend -L+20G /dev/vg0/vol1
# xfs_growfs /dev/vg0/vol1

How to replace an EBS volume

Alternatively, you could just replace the current EBS volume with a bigger one. This is easy because LVM lets you add a bigger EBS volume to the VG, then migrate all data onto it and finally remove the old volume from the VG:

# pvcreate /dev/sdg
# vgextend vg0 /dev/sdg
# pvmove /dev/sdf /dev/sdg
# lvreduce vg0 /dev/sdf
# lvextend -L+20G /dev/vg0/vol1
# xfs_growfs /dev/vg0/vol1

Now, you can detach the old EBS volume from the EC2 instance and then delete it.

Conclusion

We use virtual IT infrastructure resources from the cloud for their flexibility. By using LVM, we maintain this flexibility even on the single filesystem level.

Previous

Index

Next