Pain-free disk space management with LVM


Author: Shashank Sharma

Managing disk space used to be a royal pain for admins and users. Running out of disk space often meant reinstalling Linux or spending a few hours with tools like Parted to resize partitions. However, using the Logical Volume Manager (LVM) tools, you can grow, shrink, and manage disk space with very little hassle.

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

You need to install the lvm2 package before you can start with LVM. Debian and Fedora users can use apt-get and yum respectively to install lvm2.

LVM was added to the Linux kernel in the 2.4.x branch, so almost all Linux distributions support it. Several distributions, like Fedora and Debian, allow you to use LVM when partitioning during the installation itself. Also, some distributions such as Fedora and SUSE offer graphical tools to manage your LVM layout.

In this article, I will focus on how you can create a LVM layout and then mount it under your home directory, so as to use it in future. My root partition (/) is sda1, and sda2 is swap. I also have some unused space available on my disk, which will be used to create a partition. If you already have a spare partition, you can simply change its system ID to LVM using fdisk (or another partitioning tool) and then move on to creating the volume group.

To start partitioning, run fdisk on the target disk — in this case, /dev/sda. You could also use GParted, if you are more comfortable with a graphical tool.

fdisk /dev/sda

You’ll see a message like this, with a prompt for a command:

The number of cylinders for this disk is set to 1958.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): 

Note that the number of cylinders will vary. You’ll want to enter n as the command to create a new partition. Then, enter p to create a primary partition, and then give it a partition number, and you’ll probably want to accept the default values for the first and last cylinder.

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.


Since converting to LVM, I have slept peacefully knowing that anytime I run out of disk space, I can simply add another partition to my volume group and increase the size of my logical volumes.

The fact that the partition I add to the existing volume group doesn’t need to be on the same physical hard disk is also an advantage. Remember, with LVM you can not only increase the size of a logical volume, but also the size of the volume group. Obviously, to increase the size of the logical volume you must have free space in the volume group and to increase the size of the volume group you must have unused physical volumes.


  • High Performance Computing
  • Backup & Data Recovery