Linux Logical Volume Manager Primer
So, you're getting full up on disk space? Need to add more room? Want to do all of that without having to reformat, repartition, backup, and restore? Then LVM is the answer you seek!
LVM, short for Logical Volume Manager, is a filesystem technology which was first invented many years ago and has been present in HP-UX, Solaris, Veritas Storage Foundation, and to a certain extent in Windows for some time. Linux's answer to this requirement is now several years old and quite stable. The Logical Volume Manager allows the administrator to put together several disparate physical or logical devices into a single volume which acts as a large disk. This also allows for the dynamic re-allocation of disk space across multiple drives without even shutting down the machine. In this topic, I will show you the basics of using LVM, and in future topics, we will discuss using LVM to accomplish extremely complex setups with virtual machines, software RAID, snapshots, and even storage clustering.
To start, LVM has been included in the base Linux kernel for some time (>=2.4.x). In the 2.4 kernels, they used LVM 1. In newer 2.6 kernels, the system has been updated to LVM 2. LVM 2 offers several features over LVM 1, in that it can be clustered, handles more dynamic snapshots, and can manage much larger volumes.
LVM is a new way of looking at storage. In previous years, you have a drive and perhaps you partition it. Down the road a ways, you figure out that you're either running out of space completely, or running out of space on a particular partition. In those times, you would have to add a new physical disk or repartition your disk (requiring a reformat and reinstall). With LVM, you can add a new disk or resize and existing partition with relative ease and without even rebooting!
LVM manages storage in several tiers. You have physical volumes (PV) which are either whole disks or physical partitions. These PVs are then added to a volume group (VG), for example we'll call ours "home". These VGs are then mapped into logical extents (LE) which are kinda like partitions in the old way of doing things. The LEs can be formatted and used just like any other partition you have ever seen or used, but they also gain the benefits of LVM such as snapshots, resizing, expanding, and migration.
In many modern Linux distirbutions, LVM can be set up during the install, making the initial setup much simpler than before when you had to bootstrap your own LVM root filesystem. If you don't have the option to install LVM during the initial install, then just install the base system and try not to allocate all of your drive space. For our example, we'll go through setting up LVM on a hypothetical computer which has 2 200GB disks. We'll assume that we installed Linux on the first disk (/dev/sda) and left 150GB of unused space after installation.
On our new system, we will need to install the LVM tools. We will assume for the sake of brevity that you can find and install the LVM tools via your distribution's package manager. The tools should be named something like "lvm2". Once these tools are installed we can begin.
To begin, we have to prepare an initial physical volume (PE). Let's say that our new system is laid out with the following partitions and drives:
First, we'll create a new partition out of the 150GB of unused space on "sda" by using "fdisk". To get more information about using "fdisk" type "man fdisk" at a command prompt. Create the new partition, and set the type to "8e" which represents Linux LVM.
Now we can begin the real work of setting up the LVM storage. Start by creating a PV on the new partition using the following command:
This prepares the partition for use as part of a volume group (VG). Now, we need to create the volume group using this command:
This will create the new volume group (VG) and add our first PV to the VG. Now, we'll add the entirety of "sdb" to the volume group (VG) by typing:
The first command prepares the second hard disk (sdb) to be a physical volume (PV), and the second command extends the "home" volume group (VG) to include the entirety of "sdb". So, we now have a volume group (VG) called "home" which should consist of a 150GB partition on "sda" and the entire 200GB of "sdb". This volume group is accessed from the "/dev" filesystem under "/dev/home/".
The final step is to create logical extents (LEs) from the volume group (VG). This is done with the following command:
This command creates a new logical extent (LE) called "HomeVolume" which is 250 gigabytes in size. That means it's larger than either of the individual disks, but still leaves 100GB of unused space. We'll experiment with that space a little further on.
We have a logical extent, and that LE is accessible in the "/dev" filesystem as "/dev/home/HomeVolume". For all intents and purposes, you can use that device as you would any physical partition on a normal storage device, so we'll format it using the "ext3" filesystem.
With that formatted LE, we can now mount the filesystem using the following commands:
We can now migrate the contents of "/home" to "/mnt/newVolume", unmount "/mnt/newVolume" and then remount it at "/home". Finally, we would add these mount settings to the "/etc/fstab" file so that it is mounted at boot time.
You can already see one advantage of LVM, in that you can set up a volume which is larger than any single disk by grouping several disk devices together. That's just the beginning though. Let's say, for example, that you would like to perform an operation on your disk which could have disastrous effects and you don't want to accidentally delete anything? We can create a "snapshot" of the LE we just created and if the experiment goes wrong, we can restore the snapshot.
To create a snapshot requires only a single command.
This command will create a "frozen in time" version of your current LE. The new LE, "MySnapshot" can be mounted just like any other volume or partition. Anything you do to the real LE will not effect the snapshot. That means you could delete 100GB worth of data from the real LE, and you would still be able to access that data on the snapshot volume. Additionally, the snapshot volume is read-only and makes an excellent platform for doing backups of filesystems which change a lot. The "-L100GB" parameter specifies how much space the snapshot is allowed to use. Changes which require more space to snapshot than allotted will not be recoverable.
That's two very nice features of LVM, but the next is even more impressive. For the sake of argument, assume we are running out of space on our "HomeVolume" LE. We still have 100GB of unused space in the volume group (VG) which we can use. So, we can remove the snapshot, extend the "HomeVolume" LE, and finally expand the ext3 filesystem to fill the additional space. Here are the commands:
Pretty easy, right? Moving on to the final and, in my opinion, most impressive feature of LVM: Migration. Let's imagine that one of our hard drives (sdb) uses SMART drive diagnostic technology and it tells us that the drive is possibly going bad and needs to be replaced. We want to move all of our existing data from that drive to a new drive we installed "sdc". This new drive is 500GB, and we add it to the volume group just as before:
Now, we can tell the logical volume manager to move all data from one member of the volume group to another using a command like this:
This command tells the volume manager to migrate all data from "sdb" and distribute it across the remaining members of the volume group (VG). If we had added "/dev/sdc" to the end, that would have told the volume manager to migrate the data on "sdb" to "sdc" only.
That's my basic tutorial on using LVM, and I hope that you found it useful. For more detailed information you can check out the LVM HOWTO, or search on Google.
LVM, short for Logical Volume Manager, is a filesystem technology which was first invented many years ago and has been present in HP-UX, Solaris, Veritas Storage Foundation, and to a certain extent in Windows for some time. Linux's answer to this requirement is now several years old and quite stable. The Logical Volume Manager allows the administrator to put together several disparate physical or logical devices into a single volume which acts as a large disk. This also allows for the dynamic re-allocation of disk space across multiple drives without even shutting down the machine. In this topic, I will show you the basics of using LVM, and in future topics, we will discuss using LVM to accomplish extremely complex setups with virtual machines, software RAID, snapshots, and even storage clustering.
To start, LVM has been included in the base Linux kernel for some time (>=2.4.x). In the 2.4 kernels, they used LVM 1. In newer 2.6 kernels, the system has been updated to LVM 2. LVM 2 offers several features over LVM 1, in that it can be clustered, handles more dynamic snapshots, and can manage much larger volumes.
LVM is a new way of looking at storage. In previous years, you have a drive and perhaps you partition it. Down the road a ways, you figure out that you're either running out of space completely, or running out of space on a particular partition. In those times, you would have to add a new physical disk or repartition your disk (requiring a reformat and reinstall). With LVM, you can add a new disk or resize and existing partition with relative ease and without even rebooting!
LVM manages storage in several tiers. You have physical volumes (PV) which are either whole disks or physical partitions. These PVs are then added to a volume group (VG), for example we'll call ours "home". These VGs are then mapped into logical extents (LE) which are kinda like partitions in the old way of doing things. The LEs can be formatted and used just like any other partition you have ever seen or used, but they also gain the benefits of LVM such as snapshots, resizing, expanding, and migration.
In many modern Linux distirbutions, LVM can be set up during the install, making the initial setup much simpler than before when you had to bootstrap your own LVM root filesystem. If you don't have the option to install LVM during the initial install, then just install the base system and try not to allocate all of your drive space. For our example, we'll go through setting up LVM on a hypothetical computer which has 2 200GB disks. We'll assume that we installed Linux on the first disk (/dev/sda) and left 150GB of unused space after installation.
On our new system, we will need to install the LVM tools. We will assume for the sake of brevity that you can find and install the LVM tools via your distribution's package manager. The tools should be named something like "lvm2". Once these tools are installed we can begin.
To begin, we have to prepare an initial physical volume (PE). Let's say that our new system is laid out with the following partitions and drives:
/dev/sda
- sda1 ("/" or "root" partition)
- sda2 ("swap" partition)
- sda3 ("/boot" partition)
- sda4 (extended partition)
- sda5 ("/var" partition)
- 150GB unused space
/dev/sdb
- 200GB unused space
First, we'll create a new partition out of the 150GB of unused space on "sda" by using "fdisk". To get more information about using "fdisk" type "man fdisk" at a command prompt. Create the new partition, and set the type to "8e" which represents Linux LVM.
Now we can begin the real work of setting up the LVM storage. Start by creating a PV on the new partition using the following command:
pvcreate /dev/sda6
This prepares the partition for use as part of a volume group (VG). Now, we need to create the volume group using this command:
vgcreate home /dev/sda6
This will create the new volume group (VG) and add our first PV to the VG. Now, we'll add the entirety of "sdb" to the volume group (VG) by typing:
pvcreate /dev/sdb
and
vgextend home /dev/sdb
The first command prepares the second hard disk (sdb) to be a physical volume (PV), and the second command extends the "home" volume group (VG) to include the entirety of "sdb". So, we now have a volume group (VG) called "home" which should consist of a 150GB partition on "sda" and the entire 200GB of "sdb". This volume group is accessed from the "/dev" filesystem under "/dev/home/".
The final step is to create logical extents (LEs) from the volume group (VG). This is done with the following command:
lvcreate -L 250G -n "HomeVolume" home
This command creates a new logical extent (LE) called "HomeVolume" which is 250 gigabytes in size. That means it's larger than either of the individual disks, but still leaves 100GB of unused space. We'll experiment with that space a little further on.
We have a logical extent, and that LE is accessible in the "/dev" filesystem as "/dev/home/HomeVolume". For all intents and purposes, you can use that device as you would any physical partition on a normal storage device, so we'll format it using the "ext3" filesystem.
mkfs.ext3 /dev/home/HomeVolume
With that formatted LE, we can now mount the filesystem using the following commands:
mkdir -p /mnt/newVolume
mount -t ext3 /dev/home/HomeVolume /mnt/newVolume
We can now migrate the contents of "/home" to "/mnt/newVolume", unmount "/mnt/newVolume" and then remount it at "/home". Finally, we would add these mount settings to the "/etc/fstab" file so that it is mounted at boot time.
You can already see one advantage of LVM, in that you can set up a volume which is larger than any single disk by grouping several disk devices together. That's just the beginning though. Let's say, for example, that you would like to perform an operation on your disk which could have disastrous effects and you don't want to accidentally delete anything? We can create a "snapshot" of the LE we just created and if the experiment goes wrong, we can restore the snapshot.
To create a snapshot requires only a single command.
lvcreate -s -L100GB -n MySnapshot home/HomeVolume
This command will create a "frozen in time" version of your current LE. The new LE, "MySnapshot" can be mounted just like any other volume or partition. Anything you do to the real LE will not effect the snapshot. That means you could delete 100GB worth of data from the real LE, and you would still be able to access that data on the snapshot volume. Additionally, the snapshot volume is read-only and makes an excellent platform for doing backups of filesystems which change a lot. The "-L100GB" parameter specifies how much space the snapshot is allowed to use. Changes which require more space to snapshot than allotted will not be recoverable.
That's two very nice features of LVM, but the next is even more impressive. For the sake of argument, assume we are running out of space on our "HomeVolume" LE. We still have 100GB of unused space in the volume group (VG) which we can use. So, we can remove the snapshot, extend the "HomeVolume" LE, and finally expand the ext3 filesystem to fill the additional space. Here are the commands:
lvremove home/MySnapshot # Removes the snapshot LE
lvextend -L+100G home/HomeVolume # Extends the HomeVolume LE to encompass the additional 100GB
resize2fs /dev/home/HomeVolume # Resizes the ext3 filesystem to encompass the whole volume size.
Pretty easy, right? Moving on to the final and, in my opinion, most impressive feature of LVM: Migration. Let's imagine that one of our hard drives (sdb) uses SMART drive diagnostic technology and it tells us that the drive is possibly going bad and needs to be replaced. We want to move all of our existing data from that drive to a new drive we installed "sdc". This new drive is 500GB, and we add it to the volume group just as before:
pvcreate /dev/sdc
vgextend home /dev/sdc
Now, we can tell the logical volume manager to move all data from one member of the volume group to another using a command like this:
pvmove /dev/sdb
This command tells the volume manager to migrate all data from "sdb" and distribute it across the remaining members of the volume group (VG). If we had added "/dev/sdc" to the end, that would have told the volume manager to migrate the data on "sdb" to "sdc" only.
That's my basic tutorial on using LVM, and I hope that you found it useful. For more detailed information you can check out the LVM HOWTO, or search on Google.
Comments