You are here: Home / Debian GNU/Linux / System / Setup / Setup and use the Logical Volume Manager (LVM) on Debian

Setup and use the Logical Volume Manager (LVM) on Debian

by Pierre-Yves Landuré last modified Nov 11, 2017 09:44

LVM stands for "Logical Volume Manager". It sits between the partitions and hard disk, and provides flexibility and power to partitions management. It is particularly useful for managing partitions used by the Xen domains. It provides the ability to create partitions over several hard disks (software RAID), to resize partitions on the fly, ...

This howto is tested on:

  • Debian 5.0 Lenny
  • Debian 6.0 Squeeze
  • Debian 7.0 Wheezy

Vocabulary

LVM use a specific vocabulary. Here are some definitions:

Physical Volume
A Physical Volume or "PV" is a disk or partition "formated" by LVM. It is the storage space where are written logical volume data.
Volume group
A Volume Group or "VG" is a group of physical volumes. A volume group can contain one or more physical volumes. In order to use LVM, one must create a volume group. This concept allow to manage logical RAID with LVM.
Logical Volume
A Logical Volume or "LV" is the LVM equivalent of a partition. It is a space "somewhere in a Volume Group" where a file system can be set up.
Extent
A extent, or "Physical Extent" or "PE" is the LVM equivalent of a disk sector. The size of a Logical Volume will always be multiple of the extent size of its containing Volume Group.By default, PE size is 4 MiB.

The LVM commands are easy to use and display a very helpful usage message. Their names is guessed easily:

  • The commands managing physical volumes start with pv. List them :
    command man -k '^pv'
  • The commands managing volume groups start with vg. List them :
    command man -k '^vg'
  • The commands managing logical volumes start with lv. List them :
    command man -k '^lv'

Installation

Install the software :

command apt-get install lvm2

If, by running a LVM command, this message is displayed : « No program "*****" found for your current version of LVM. », the LVM daemon is not running. Start it:

command test -x '/etc/init.d/lvm2' && /etc/init.d/lvm2 start
command test -x '/etc/init.d/lvm' && /etc/init.d/lvm start

Physical Volume creation

The first step of setting up LVM is to create a physical volume. Choose an unused partition. Type its device name :

PV_DEV="/dev/sdb1"

Warning : All of chosen partition data will be destroyed !

It is possible to use directly a hard drive device (/dev/sdb for example) instead of a partition. It is recommended to create the physical volume on a partition with the same size as the hard drive rather than on the complete unpartitioned hard drive.

Make sure that the chosen partition is not mounted :

command umount "${PV_DEV}"

Create the physical volume on the chosen partition :

command pvcreate "${PV_DEV}"

If no error is displayed, the physical volume is created.

Volume Group creation

Once the physical(s) volume(s) created, assign-them to a volume group. Name the (new) volume group :

VG_NAME="vhd1"

Note : vhd stands for Virtual Hard Drive.

Create the volume group :

command vgcreate "${VG_NAME}" "${PV_DEV}"

List the volume groups available on the system :

command vgdisplay

Logical volumes management

Logical Volume creation

A logical volume is a reserved space in a volume group where a file system can be created (for example : swap or ext3). To create one, the containing volume group name must be known.

Name the new logical volume. For example, for the var partition of a Xen Dom0 :

LV_NAME="dom0-var"

Choose the new logical volume size :

LV_SIZE="10g"

Detect the system first volume group name :

VG_NAME="$(command vgdisplay \
| command grep "VG Name" \
| command head --lines=1 \
| command cut --characters=25-)"

Create the logical volume :

command lvcreate -n "${LV_NAME}" -L "${LV_SIZE}" "${VG_NAME}"

Create a file system on the new LV. Here is an example for ext4fs creation :

command mkfs -t ext4 "/dev/${VG_NAME}/${LV_NAME}"

Compute the device file name of the new logical volume :

echo "New logical volume (size : ${LV_SIZE}) : /dev/${VG_NAME}/${LV_NAME}"

Logical Volume removal

Provide the device file path of the logical volume to be removed :

DELETED_LV="/dev/vhd1/temporary-lv"

Make sure the LV is not mounted :

command umount "${DELETED_LV}"

Remove the logical volume :

command lvremove "${DELETED_LV}"

Logical Volume up-sizing

LVM allow on the fly logical volume size change. The main difficulty come for the contained file system adjustment to the new size.

Provide the device file path for the resized logical volume :

RESIZED_LV="/dev/vhd1/dom0-home"

Choose a new size for the logical volume :

NEW_SIZE="20g"

Make sure the chosen size is greater than the actual LV_SIZE :

command lvdisplay "${RESIZED_LV}" | command grep "LV Size"

Make sure that the logical volume is not mounted :

command umount "${RESIZED_LV}"

Check the file system for errors :

command fsck -f "${RESIZED_LV}"

Up-size the logical volume :

command lvresize -L "${NEW_SIZE}" "${RESIZED_LV}"

Adjust the contained file system to the new size :

command resize2fs "${RESIZED_LV}"

Note : The resize2fs command line can only be used on ext2 family file system (ext2fs, ex3fs, ext4fs).

If needed, mount the resized logical volume :

command mount "${RESIZED_LV}"

Logical volume down-sizing

Down-sizing a file system is a delicate process. If one reduce the size of the logical volume before reducing the contained file system size, some data may be lost. Data will also be lost if the new size is less than the file system used space.

Warning : Backup your data before reducing a logical volume size.

Provide the device file path for the resized logical volume :

RESIZED_LV="/dev/vhd1/dom0-home"

Make sure that the logical volume is not mounted :

command umount "${RESIZED_LV}"

Check the file system for errors :

command fsck -f "${RESIZED_LV}"

Down-size the logical volume contained file system :

Warning : Make sure to use a file system size less than the new down-sized logical volume size, and more than the current file system used space. If an error is made, data will be definitively lost.

command resize2fs -p "${RESIZED_LV}" "1000M"

Down-size the logical volume.

Warning : Make sure to use a size greater than the filesystem reduced size (see above).

command lvresize -L "1024M" "${RESIZED_LV}"

Adjust the contained file system to the new logical volume size :

command resize2fs "${RESIZED_LV}"

If needed, mount the resized logical volume :

command mount "${RESIZED_LV}"

Logical volume backup

Snapshot creation

A LVM snapshot can be used to backup a logical volume data while it is used. A snapshot is not precisely a logical volume copy. It is a backup of the Logical Volume Physical Extents at a given time. If the data of PE change after the snapshot creation, the original PE is backed-up in the snapshot reserved space.

These features allow a snapshot to use only 15% to 20% of the space assigned to the original Logical Volume. For example, to create a snapshot of a 10 GB logical volume, 2 GB only need to be reserved for the snapshot.

Select a name for the snapshot. For example, for a snapshot of a "var" Logical Volume :

SNAPSHOT_NAME="bkp-var-20090201"

Choose the snapshot reserved space size :

SNAPSHOT_SIZE="2g"

Choose the snapshot source Logical Volume :

SNAPSHOT_SOURCE="/dev/vhd1/var"

Create the snapshot :

command lvcreate -L "${SNAPSHOT_SIZE}" -s -n "${SNAPSHOT_NAME}" "${SNAPSHOT_SOURCE}"

Creating a snapshot disk image backup

Select the snapshot to backup :

SNAPSHOT_NAME="/dev/vhd1/bkp-var-20090201"

Create the compressed disk image.

command dd if="${SNAPSHOT_NAME}"  | command gzip -9 -c > "/var/backups/${SNAPSHOT_NAME}.bz2"

To restore a Logical Volume from a compressed disk image, use :

command gunzip -c "/var/backups/some-snapshot.bz2" | command dd of="/dev/vhd1/some-lv"

One backed-up, remove the snapshot :

command lvremove "${SNAPSHOT_NAME}"

Thanks