This is just a repost of: http://joshua14.homelinux.org/blog/?p=10 (edit: link no longer works) full credit goes to the author, I’m just posting this here for ease of access.
If you’ve used Linux for a while, you probably used LVM without even realising it, especially if you have only one hard disk in your Linux machine. Modern Linux distributions use Logical Volume Management by default and if you choose default layout for your partitions, this is what you’ve been using. Frankly, I don’t like using LVM on my OS hard disk partition as most of the time you’ll never need to extend the space that the OS takes up as modern disks are plenty big enough for the Linux OS. With multi-disk servers, LVM becomes a lot more useful though.
But what is LVM exactly? Consider the following situation. You’ve organised your hard disk into regular partitions, guessing how much disk space you’ll need on each partition – the /home partition being a prime example of this. But eventually, you install more and more things into certain partitions and those partitions run out of space. Ooops. Didn’t consider that at installation time, did you? Normally about now, you’d be out of luck and your only option would be to either reinstall onto a larger hard disk and start again or to sym-link a directory under your home directory (or whatever) to a new partition. Wouldn’t it be nice to simply extend the partiton onto a new hard disk without losing all your data or to mess about with sym-linking to new separate partitions? This is where Logical Volume Management comes in.
Logical Volume Management essentially provides an abstraction from disks and partitions into Volume Groups and Logical Volumes which may include many actual disk partitions. The great thing about this is that you can add new partitions or disks to your Logical Volume if you find yourself running out of space without losing any data. This might include adding a shiny new hard disk partition but LVM doesn’t much care where the space comes from as it’s an abstraction of the actual filesystem which might span many disks and/or partitions.
**1. Partition: ** At a top level you have a regular partition. For LVM you basically create the partition of type Linux LVM (or 8e) when you partition your drive, as opposed to 83, which is a regular Linux filesystem of ext3/ext4.
**2. Physical Volume: ** A physical volume is your top level element when you’re using LVM. You create a physical volume on top of a particular LVM partition that you’ve defined. Note that you can have one or more physical volumes based on how you’ve partitioned your drives. You can use pvdisplay to show all physical volumes on your system.
**3. Volume Group: ** The next level is the volume group. A volume group is a grouping of one or more physical volumes. Thus it also has a capacity based on the capacities of its physical volumes summed up. The idea here is to be able to dynamically increase/decrease the size of a volume group by adding/removing physical volumes from it. Thereby you can add a disk for example, create a phys volume and add this disk to the volume group without having to unmount/repartition or anything like this. Common commands used here are vgcreate (to create volume group), vgextend/vgreduce (to add/remove PV’s from a volume group). You can use vgdisplay to show all volume groups on your system.
**4. Logical Volume: ** A logical volume is a chunk (or an entire) volume group’s capacity. You can have more than one logical volume created from the same volume group. The logical volume is the actual logical device on top of which you’d format a filesystem (instead of a raw partition) and its power is that it can be increased in size after the fact if you’re running out of space. You can also decrease its size; but the thing about this is that depending on circumstances you can lose information. Commonly used commands for creating a logical volume is lvcreate; lvextend/lvreduce to grow/shrink a LV. You also can use lvdisplay to show ALL logical volumes on your system. Note you can also use system-config-lvm on RHEL which allows you a GUI interface to configure LVM.
Create your partitions you wish to use in your logical volume. Here, I’m using a 500GB secondary disk that I’ve added to my system – the first being /dev/sda.
Add a new single partition that spans the entire disk by pressing ‘n’ accepting the default values. Once this is done, you need to press ‘t’ to change the default partition type to ’8e’, which is the LVM partition type rather than ’83′ which is a regular Linux filesystem partition. Write your changes to the disk by pressing ‘w’. Do this for as many partitions on as many disks as you want, but we’re just doing it on the one disk with a single 500GB partition.
Next you need to create an LVM Physical Volume (PV) on one of the partitions you just created.
The next step is to create an LVM Volume Group (VG) on that same partition called VolGroup01 with a Physical Extent (PE) size of 16MB (as I’m using a 500GB disk). In order to limit the Linux kernel memory usage, there is a limit of 65,536 physical extents (PE) per logical volume (LV). Hence, the LVM PE size will directly determine the maximum size of a logical volume (LV). For example, 4MB PE size (the default PE size) will limit single logical volume (LV) to 256GB, 16MB PE size will limit single LV to grow beyond 1TB, etc.
vgcreate -s 16M VolGroup01 /dev/hdb1
Create a 500GB logical volume (LV) called LogVol01 on volume group VolGroup01.
lvcreate -L 556000M -n LogVol01 VolGroup01.
Lastly, you need to format the Logical Volume LogVol01 as a normal filesystem as up until this point, we’ve simply been working with abstractions of partitions.
mkfs -t ext3 -v /dev/VolGroup01/LogVol01
Now you have a formatted filesystem that you can mount as a normal formatted filesystem partition. Create a mount point for the new Logical Volume something like the below.
Mount the new EXT3 file system created on logical volume LogVol01 of LVM to /mnt/lvm mount point – you can obviously use any directory location you like :)
mount -t ext3 /dev/VolGroup01/LogVol01 /mnt/lvm
So now you have 500GB on your Logical Volume under directory /mnt/lvm. Now I’m going to show you the real power of LVM, which is to increase the space used by the logical volume without destroying any data. Lets assume you’ve added a new drive called /dev/sdc, which is also 500GB. Your current 500GB drive is full up and instead of backing up and adding a new drive which is larger (say 1TB) and restoring everything, you can simply extend the logical volume to span to the new drive without losing any of your data! So let’s add another 500GB drive (/dev/sdc) and use fdisk again to partition it for LVM as above.
Right, so now you have a new disk partitioned for LVM – let’s call it /dev/sdc1. The next step is to create a physical volume on this partition.
Now you can add this physical volume to the volume group container ‘VolGroup01′.
vgextend VolGroup01 /dev/sdc1
Of course, your logical volume is still the same size as you left it, as you’ve not told the operating system to use the new space, you’ve merely added the physical volume to the volume group container. You need to use the lvextend command to extend the logical volume to span both physical volumes.
lvextend /dev/VolGroup01/LogVol01 --size +55600M
The thing to note here is that if you do a ‘df -h’ to display the size of your /mnt/lvm directory, you’ll see that it’s still only 500GB. Why is this? Because the filesystem has only been formatted to the initial size we gave it. At this point, we need to resize the filesystem across the total size of our new logical volume.
Now, if you do a ‘df -h’, you’ll see the filesystem under /mnt/lvm has 1TB of space and better yet, you didn’t lose any data! Hence the power and usefulness of LVM as opposed to traditional partitions. Enjoy!