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
Software Requirements and Conventions Used
|Category||Requirements, Conventions or Software Version Used|
|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 |
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.
We can use our preferred partitioning tool to create partitions. In this example I have used
Partitions are ready to be used.
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.
Alternatively the following linux command should be used:
# pvdisplay /dev/sdb1
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
Feel free to add new physical volumes to a virtual group by using the vgextend command.
# vgextend mynew_vg /dev/sdb2
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
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
Note the free size in virtual group.
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
-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%).
For the filesystem to be automatically mounted, we should add an entry for it into
Mount logical volumes
Before we mount do not forget to create a mount point.
# mkdir /home/foobar
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
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:
Removing a logical volume
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.
# lvremove /dev/mynew_vg/vol02