This article describes the basic logic behind the Linux logical volume manager by showing real examples of configuration and usage. Although Debian Linux will be used for this tutorial, the same commands can be applied on other Linux distributions.

In this tutorial you will learn:
  • Basic lvm concepts
  • How to manage phisical volumes, logical volumes and volume groups
Logical Volume Manager - Tutorial Scenario
Logical Volume Manager - Tutorial Scenario

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Distribution-independent
Software Userland logical volume management tools (installed by default)
Other To follow this tutorial you should be already familiar with basic partioning concepts and the linux command line
Conventions # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - requires given linux commands to be executed as a regular non-privileged user

Create Partitions

For this Linux lvm example we need an unpartitioned hard disk. From now on, we will assume it is /dev/sdb. First we need to create physical volumes. Physical volumes can be created either on raw, unpartitioned block devices, or single partitions. For the sake of this tutorial we will work on the latter.

Logical Volume
Manager List Partitions with fdisk
We can use our preferred partitioning tool to create partitions. In this example I have used cfdisk.

Create partitions with cfdiskLogical Volume Manager List Partitions with fdisk

Partitions are ready to be used.


SUBSCRIBE TO NEWSLETTER
Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


Create physical volumes

Use the pvcreate command to create physical volumes.

# pvcreate /dev/sdb1
# pvcreate /dev/sdb2

The pvdisplay command displays all physical volumes on your system.

# pvdisplay

Alternatively the following linux command should be used:

# pvdisplay /dev/sdb1 
Create physical volumes with pvcreate

Create Virtual Group

At this stage we need to create a virtual group which will serve as a container for our physical volumes. To create a virtual group with the name "mynew_vg" which will include /dev/sdb1 partition, we can issue the following linux command:

# vgcreate mynew_vg /dev/sdb1 

To include both partitions at once we can use this command:

# vgcreate mynew_vg /dev/sdb1 /dev/sdb2
Create Virtual Group

Feel free to add new physical volumes to a virtual group by using the vgextend command.

# vgextend mynew_vg /dev/sdb2 
virtual group extend


Create Logical Volumes

From our big cake (virtual group) we can cut pieces (logical volumes) which will be treated as a partitions for our linux system. To create a logical volume, named "vol01", with a size of 400 MB from the virtual group "mynew_vg" use the following linux command. Here we will:

  • create a logical volume of size 400 MB -L 400
  • create a logical volume of size 4 GB -L 4G

Let's proceed, first we will create the smaller logical volume:

# lvcreate -L 400 -n vol01 mynew_vg
lvm lvcreate

Now it's the turn of the logical volume with the size of 1GB. We will call it vol02:

# lvcreate -L 1000 -n vol02 mynew_vg
lvcreate

Note the free size in virtual group.

lvcrete free space

Create Filesystem on logical volumes

The logical volume is almost ready to use. All we need to do is to create a filesystem on it:

# mkfs.ext3 -m 0 /dev/mynew_vg/vol01 

The -m option specifies the percentage reserved for the super-user, we can set this to 0  to use all the available space (the default is 5%).

create file system on the logical volumes



Edit /etc/fstab

For the filesystem to be automatically mounted, we should add an entry for it into /etc/fstab:

edit /etc/fstab to include logical volumes

Mount logical volumes

Before we mount do not forget to create a mount point.

# mkdir /home/foobar 
mount logical volumes

Extending a logical volume

The biggest advantage of a logical volume is that it can be extended any time we are running out of space. For example, to increase the size of a logical volume and add other 800 MB of space, we can run this command:

# lvextend -L +800 /dev/mynew_vg/vol01
Extend logical volume

The command above does not actually increase the size the filesystem, but only that of the logical volume. To make the filesystem grow and use the added space we need to run:

# resize2fs  /dev/mynew_vg/vol01 

Look at the figure below to see what problems may be encountered when extending a volume:

Problem with extending a logical volume

Removing a logical volume

The command lvremove can be used to remove logical volumes. We should make sure a logical volume does not have any valuable data stored on it before we attempt to remove it. Moreover, we should make sure the volume is not mounted.

# lvdisplay 
display volume group
# lvremove /dev/mynew_vg/vol02
remove logical volume
ARE YOU LOOKING FOR A LINUX JOB?
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed on LinuxCareers.com.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.