Understanding LVM

Before jumping into the details of setting up LVM, you need to familiarize yourself with LVM terminology. LVM refers to a typical partition as a Physical Volume (PV). A Volume Group (VG) comprises one or more physical volumes. Each volume group must be divided into Logical Volumes (LV).

A logical volume functions like a normal partition -- they have a filesystem such as Ext3, and a mount point. You can think of the volume group as a virtual hard disk. A logical volume is thus a virtual partition on your virtual hard disk.

Getting started with LVM


We have now created a primary partition. The next step is to change the partition type to LVM. That is, we need to change the partition's system id value to 8e, instead of the default 83 assigned to new partitions:

Use t to tell fdisk you want to change the system ID, then select the partition number you want and enter type 8e as the partition type. After you hit Enter, fdisk will confirm the partition type (Linux LVM).

You can check the partition table to ensure the change in the system id. Press p to print the partition table:

Disk /dev/sda: 16.1 GB, 16106127360 bytes 255 heads, 63 sectors/track, 1958 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System /dev/sda1 * 1 1020 8193118+ 83 Linux /dev/sda2 1021 1173 1228972+ 82 Linux swap / Solaris /dev/sda3 1174 1958 3092512+ 8e Linux LVM

Of course, the actual information will differ for you. Type w and then press Enter to save the partition table. The new partition table will be used after you restart the computer.

Creating a volume group

Remember what I said about physical volumes and volume groups? A volume group comprises one or more physical volumes. Before we create a volume group, we need to initialize our physical volumes. First you'll need to reboot to ensure the new partition is used. Then open a terminal window and run this command as root, or with sudo:

pvcreate /dev/sda3

This initializes the physical volume. We will now create a volume group that uses this physical volume. You need to provide a unique name to your volume group. The command

vgcreate home2 /dev/sda3

will create a volume group called home2 that comprises /dev/sda3.

You can also use multiple physical volumes to make up a volume group. For example, vgcreate home3 /dev/sda3 /dev/sda5 will create a volume group comprising the sda3 and the sda5 physical volumes. A volume group can also span multiple hard disks: vgcreate home4 /dev/sda /dev/sdb will create a volume group spanning two hard disks. The vgscan command can be used to list all the volume groups.

Now that we have a volume group, it's time to split it into logical volumes. You can create multiple logical volumes within a volume group or use the entire space to create just one. The command

lvcreate -n downloads --size 1G home2

will create a 1GB logical volume called downloads within the home2 volume group. The -n option is used to provide a name for your logical volume.

You need both the logical volume name and the name of the volume group to mount the logical volume as discussed below. You can use the lvdisplay command to see the properties of your logical volume.

Before you can begin using the new logical volume, you need to format it and mount it. The logical volume is available as /dev/home2/downloads (that is, /dev/volumegroup/logicalvolume). Run the command mkfs.ext3 /dev/home2/downloads as the root user to format your logical volume with an Ext3 filesystem.

Next create a folder in the home directory with the mkdir /home/downloads command and mount your logical volume with the command mount -t ext3 /dev/home2/downloads /home/downloads. The mount point doesn't necessarily need to have the same name as the logical volume, but it's best you use a uniform name to avoid confusion. You can even add a line to the /etc/fstab file so that the downloads logical volume is mounted automatically after each reboot:

/dev/home2/downloads /home/downloads ext3 defaults 1 2

You can now use the downloads logical volume to store data.

Resizing logical volumes

It is safe to assume that you might run out of space on this logical volume. In such a scenario, you can increase the size of the logical volume using the lvextend command.

But, for this to work, you must have available space in your volume group. Don't despair if you've run out of space on your volume group. Jump to the next section that discusses adding physical volumes to your volume group. In the example above, I created a 1GB logical volume in a volume group with 2GB of space.

Therefore, I can extend the size of my logical volume by 1GB. Before resizing, you must unmount the logical volume. While an Ext3 filesystem can be resized on the fly, it's better to be safe. Let's unmount the drive first, using umount /home/downloads. This will unmount the logical volume if you have added an entry in /etc/fstab. If not, run umount /dev/home2/downloads to unmount it.

The command lvextend -L +1G /dev/home2/downloads will increase the size of the logical volume by 1GB, and you should see a success message like this:

Extending logical volume downloads to 2.00 GB Logical volume downloads successfully resized

You can use the lvdisplay command to make sure the size has increased. You next need to expand the filesystem to fit in the resized logical volume. The command resize2fs /dev/home2/downloads will resize the ext3 filesystem on the logical volume, and you can begin using it.

Similar to the lvextend command is the lvreduce command. It can be used to shrink the size of your logical volumes. This is considered risky, as there's a possibility you might lose the data on your logical volume. You must resize the filesystem on the logical volume using the resize2fs command before using the lvreduce command.

Using lvreduce -L -500M /dev/home2/downloads will reduce the size of the logical volume by 500 MB. lvreduce is not a very safe command, and you shouldn't drastically reduce your logical volume, without first backing up all your data.

Modifying volume groups

But what happens when your logical volume has taken up all the space in your volume group? You can add more physical volumes to an existing volume group, and then increase the size of your logical volume as discussed already.

First, unmount the logical volumes within your volume group. Assuming you already have physical volumes available, you can use the command vgextend home2 /dev/sda5 /dev/sda7 to add the physical volumes sda5 and sda7 to the existing volume group home2. The volume group home2 is now made up of sda3, sda5, and sda7.

To remove a volume group, you must first of all remove the logical volumes it contains. Unmount your logical volume, and run the command lvremove /dev/home2/downloads. This command will remove the logical volume and all the data it contains. You can now remove the volume group by using the vgremove command: vgremove home2.

-- LorenzoBusoni - 02 Oct 2007
Topic revision: r2 - 14 May 2008, LorenzoBusoni
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback