LVM Logical Volume Manager

From xotHostWiki
Jump to: navigation, search

LVM is the Logical Volume Manager for the Linux Kernel. With it, you can use multiple hard drives which are read as one, similar to RAID 0. You can also shrink, grow, and add partitions or hard drives. It also supports online snapshots of the system.


Main parts of LVM

Physical Volume (PV)

It is a partition or hard disk on which you can have volume groups

Volume Group (VG)

It is a group of physical volumes that are used as a storage volume, such as one or more disks.

Logical Volume (LV)

It is a virtual partition that resides on a volume group. They are seen like normal partitions.

Physical Extent (PE)

It is a small part of a disk that can be assigned to a LogicalVolume.

Taking advantage of LVM on

All VPSs with CentOS, Fedora, Debian or Ubuntu come pre-configured with LVM, and ArchLinux servers can be delivered with LVM by customer request.
That gives us great flexibility on our servers, especially when getting extra hard disk space, an extra virtual hard drive, or for taking snapshots of the system while keeping it online.
We can also shrink partitions to create new ones or give space to the ones that are getting full (although shrinking must be done offline, using a virtual Live CD which can be provided by via a support ticket) to avoid data loss.
Lets discuss some of these features:

Checkyour LVM's structure

Display PV (Phisical Volume)

To display our physycal volumes we use the following command:

[root@remote-server ~]# pvdisplay

Lets assume that our physical volume is /dev/sda1

Display VG (Volume Group)

To display our Volume groups we issue the following:

[root@remote-server ~]# vgdisplay

Lets assume that our Volume Group is called VolGroup00

Display LV (Logical Volumes)

To display our Logical Volumes we use:

[root@remote-server ~]# lvdisplay

Let assume our Logical Volumes are called lvroot, lvhome and lvswap.

What you can do with LVM

Create a Logical Volume

To create a 10 GB logical volume called lvhome:

[root@remote-server ~]# lvcreate -L 10G VolGroup00 -n lvolhome

Grow a Logical Volume

Lets say we just got our 10 GB hard drive upgraded to 20 GB and we want to use that extra space to grow our lvhome partition: [root@remote-server ~]# lvextend -L 20G VolGroup00/lvhome

[root@remote-server ~]# resize2fs /dev/VolGroup00/lvhome

(this last command is done to extend and ext3 partition. Check for your particular partition type)

  • To fill all the free space on the volume group:
[root@remote-server ~]# lvextend -l +100%FREE VolGroup00/lvhome

If your filesystem does not support online growing of Logical Volume, issue the above commands using a LiveCD, and loading the LVM module:

[root@remote-server ~]# modprobe dm-mod
[root@remote-server ~]# vgchange -ay

Shrink a Logical Volume

Lets say you have your whole LVM with a single Logical Volume called lvroot, with 20GB, and you want to shrink that LV to 5 GB and create a new LV called lvhome with the remaining space:

[root@remote-server ~]# resize2fs /dev/VolGroup00/lvroot 4G
[root@remote-server ~]# lvreduce -L 5G VolGroup00/lvrootresize2fs /dev/VolGroup00/lvroot
  • Note that first we shrunk the filesystem more than needed not to accidentally cut off the end of the filesystem, and then we grow it back to the desired size.
  • Caution: Online shrinking is not recommended even if the file system supports it to avoid data loss. Do not shrink the LV to a size less than the amount of space occupied by data. You may lose your data.
  • If your filesystem does not support online growing of Logical Volume, issue the above commands using a LiveCD, and loading the LVM module:
[root@remote-server ~]# modprobe dm-mod
[root@remote-server ~]# vgchange -ay

Create a Logical Volume

Now that we have our lvroot partition the desired size (5G), we want to create our lvhome partition using the remaining space (15G). We need to issue the following:

[root@remote-server ~]# lvcreate -C y -L 10G VolGroup00 -n lvhome

or, since we know that we will be using all the remaining free space:

[root@remote-server ~]# lvcreate -l +100%FREE VolGroup00 -n lvhome

To see the created LV, use the following command:

[root@remote-server ~]# lvdisplay

Now you have to edit your /etc/fstab file so your new LV will be mounted on boot.

Add a partition to a Volume Group

If you get an extra virtual hard drive (/dev/sdb) from and you want to add it to your VG VolGroup00, first use a tool like fdisk to create a partition of the type "linux LVM" which will become /dev/sdb1

[root@remote-server ~]# fdisk /dev/sdb

Once inside of the fdisk program:

type "n" to create a new partition, using the whole space
type "t" to change the partition id
type "8e" for Linux LVM
type "w" to write the changes and exit.

Now we have our /dev/sdb1 partition of the type Linux LVM. Now we create a new Physical Volume, and then we extend our VolGroup00 VG to cover all our new hard drive:

[root@remote-server ~]# pvcreate /dev/sdb1
[root@remote-server ~]# vgextend VolGroup /dev/sdb1

With the new free space, we can proceed extending or adding Logical Volumes as described before.

Taking system snapshots

When taking a snapshot, it is important to understand that the snapshot contains hard links to the inodes of your actual data, so the snapshot can be as little as the data we modify on our system. ie. We can make a snapshot of a 15GB filesystem, and that snapshot can be as little as 1GB, as long as you modify less then 1GB on both the original and the snapshot. To create a snapshot, you create a newLogical Volume:

[root@remote-server ~]# lvcreate --size 1G --snapshot --name snap01 /dev/mapper/VolGroup00-lvhome


Back to Main Page
Go to

Personal tools