Recently, I had to deal with a storage issue in Linux that forced me to dig deeper into the Linux partition structure than ever before. It made me realize how totally unaware I had been of drastic changes in the Linux landscape. Once again, I encountered LVM (Logical Volume Management) on Linux. First, I'll give you the background of my specific problem, and then I'll give you the details of working with LVM.
What's the problem?
I had to have a new desktop system. I was using my current desktop as my test machine, for various reasons. Testing on my desktop machine was becoming an issue: the constant installing and/or removing of packages caused a bit of instability on the machine. Instability on my desktop production machine — used for various long-term projects — was not a good thing, so I purchased all the pieces to put together a new system. In the process of putting the new machine together, my old desktop quite literally blew up; with sparks, smoke, and explosions — it was like an episode of 24.
I was left with some RAM and a hard drive, but at least I had the intact drive. The new system is a 64-bit machine. Fortunately, I knew I could run the old 32-bit drive on the new machine and get all my information. Better yet, this was Linux; so, I could just add the old drive as a slave, boot the machine, mount the old drive, and get all my data.
Or so I thought.
The old drive was set up as a stand-alone drive on the old system using Fedora Core 6. The partition scheme on FC6 used LVM, so the drive was a part of VolGroup00. Therein lies the problem. The new drive in the new system was also labeled VolGroup00. So instead of being able to run the old tried-and-true line:
mount /dev/hdb2 /data
to get my drive mounted, I was forced to give LVM a try. I didn't realize what I was in for; but now I do, and I can share my story with you.
What exactly is LVM?
LVM is a different method of dealing with disk partitioning schemes. Instead of the traditional partitions, LVM gives the system a much more flexible method of allocation. Instead of partitions, you are dealing with storage volumes. LVM offers the following benefits to system administrators:
- Allows management of storage volumes in user-defined groups.
- Simplifies the administration of large installations.
- Accurate assessment of necessary hard-drive space is no longer relevant.
- Partition sizes are no longer static.
Why would you want to use LVM?
There are many reasons, but the simplest is this: server management is an ever-changing landscape, and so server hardware needs to be on-the-fly flexible. LVM allows exactly this. LVM volume groups can be deleted, added, resized, and renamed; all without damage to data.
Anatomy of LVM
The hierarchy of LVM looks like this:
physical drives (i.e., hda, hdb):
File Systems (i.e., ext2, reiserfs,hfs)
- Volume Group (vg): The Volume Group is the highest level abstraction used within the LVM. It gathers together a collection of Logical Volumes and Physical Volumes into one administrative unit.
- Physical Volume (pv): A physical volume is typically a hard disk, though it may just be a device that "looks" like a hard disk (e.g., a software raid device).
- Logical Volume (lv): The equivalent of a disk partition in a non-LVM system. The LV is visible as a standard block device; as such the LV can contain a file system (e.g., /home).
- Physical Extent (pe): Each physical volume is divided chunks of data, known as physical extents; these extents have the same size as the logical extents for the volume group.
- Logical Extent (le): Each logical volume is split into chunks of data, known as logical extents. The extent size is the same for all logical volumes in the volume group.
How it works together
Let's take this example into account. Volume Group 00 (or VolGroup00 in the Fedora Core space) has a physical extent size of 100 MB. Into Volume Group 00, we add two hard disk partitions, /dev/hda1 and /dev/hdb1, which will become physical volumes data1 and data2. Both data1 and data2 will be broken into 100-MB chunks, since that is the PE size of Volume Group 00.
Let's say data1 and data2 are both 40-GB drives. That means we have a total of 800 extents). With this VG broken up, a logical volume can now be created with anywhere from 1 to 800 extents.
I am going to assume you're working with a newer Linux distribution, so LVM will already be installed. Installing LVM from source requires kernel patching, re-compilation, and some serious work. It is my recommendation that if you want to employ LVM on your server, that you install a distribution such as Suse, Ubuntu, or Fedora Core.
Let's set up a situation where a server needs to have a partition expanded. Your HTTP server is running out of room on Apache's document root (we'll assume it's on a separate 100-MB /dev/hdd5 partition), and you want to add to that without having to re-install the system. With LVM, this is possible.
Of course, before you do this, you would back up the /var/www/html directory with tar:
tar cvfz backup_var_www_html.tgz /var/www/html
Since this could be mission-critical data, please check the tar file to ensure all of your data is safe. To do this, run the command:
tar tvf backup_var_www_html.tgz
which should list all of the data in the archive. Once you are sure the data is correct, move the tar to a safe place.
Now we're going to change the type of partition the /var/www/html directory resides on (assuming the original partition type was ext3). To do this, we will employ the fdisk command. As root, you will enter the command:
Once you are inside fdisk, you will use the t switch for the type. The partition number (as we mentioned earlier) is 5. The hex code for LVM is 8e. You will then use the w switch to write the new partition table.
Before you can use a disk (or partition), you have to first initialize it. To initialize a disk (or partition), you use the pvcreate command. So if you want to initialize /dev/hdd5, you would run the command:
which would create a volume group descriptor at the start of the disk where we want /var/www/html to live.
You would not want to run the command on the entire disk. Using the entire disk as a volume would cause problems because any other OS would fail to recognize the LVM metadata, see the space as free, and possibly overwrite the data. This is only a problem if the volumes are going to be accessed by other OSs (i.e., Windows writing to a Linux disk via Samba).
Now, remember we mentioned that we were going to expand /var/www/html for more room? I'll assume you have installed a new drive to be used for the new space. Let's say the new drive is labeled as /dev/hdf and is a 40-GB drive. You have to create the PV on the new disk with the pvcreate command. Let's say the new drive has been formatted with two equal partitions. We're going to use /dev/hdf1 for /var/www/html. Initialize /dev/hdf1 with pvcreate like so:
and the device will be set up.
Now we have to set up the VG that will include both /dev/hdd5 and /dev/hdf1. Do this with the vgcreate command by running:
vgcreate VolGroup00 /dev/hdd5 /dev/hdf1
You will create a VG named VolGroup00 that includes the partitions /dev/hdd5 and /dev/hdf1. Check the VGs with the vgdisplay command, which will report:
[root@localhost ~]# vgdisplay
—- Volume group —-
VG Name  VolGroup00
Metadata Areas 2
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 20.580 GB
PE Size 32.00 MB
Total PE 643
Alloc PE / Size 0 / 0
Free PE / Size 643 / 20.580 GB
So what this set up gives is 643 PEs (using the default 32-MB PE size) from a 20.580-GB VG. Of course, we want to use all of our PEs for the /var/www/html partition, so we're going to create the logical volume with all of the extents. Do this with the command:
lvcreate -l 643 VolGroup00
The Logical Volume Group has been created. But you're not finished yet. As it stands, the VG is worthless without it being formatted, its data, and without having it set up to mount at boot.
First, let's edit the /etc/fstab so that our new /var/www/html will auto-mount at boot. The new entry will look like:
/dev/VolGroup00/lvol0 /var/www/html ext3
defaults 0 0
Now, we have to create the file system with this command:
mkfs -j /dev/VolGroup00/lvol0
That's all there is to it. Now, check to make sure the system will mount properly. Run the command:
Check to make sure the volume has been mounted by running the mount command without arguments. You should see an entry that looks something like:
on /var/www/html type ext3 (rw)
If you see no problems you are ready to put your data on your new LV. Just move the archive to /var/www and run the command:
tar xvzf backup_var_www_html.tgz
which will unpack the archive in the proper place.
Your data is now in place in a much larger home!
Embrace and extend
As mentioned earlier, LVM is a very flexible system. Let's say you set up your new /var/www/html in its new larger home, but you manage run out of room again. Fear not; you can add even more physical volumes to this group. Once you set up more physical volumes (say you create /dev/hdg1 as a new PV), you can add them to the current VG with the vgextend command like so:
vgextend VolGroup00 /dev/hdg1
Of course, you can also remove PVs from a VG with the vgreduce like so:
vgreduce VolGroup00 /dev/hdg1
Note: Make sure you are not removing a PV that contains data. You can check this with the pvdisplay command. If you run:
and you see something like:
Total PE 499
Free PE 0
you will know that data is housed on this PV.
LVM is a boon to system administrators; it keeps you from worrying about running out of space on servers or losing data to drives going bad. In my opinion, LVM is flexible, reliable, and the way to go.
In my quest to get my data from identically named groups, I discovered that the Linux system I had grown to know and love was far more flexible than I had previously realized. Now, when installing Linux on a server or desktop, I won't assume Volgroup00 just a simple naming convention and ignore it at install. Nor should you.
Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.