(!) Discussion

Translation(s): English - Español - Français -


LVM is a Logical Volume Manager for the Linux operating system.

Overview

Logical volume management with LVM provides a more flexible way to manage the disk space on a system than the traditional approach of dividing a disk into partitions. LVM gives the system administrator much more flexibility in allocating storage, because partitions controlled by LVM be resized and moved around, and a single partition can reside on more than one physical hard disk.

Definitions

The most important terms for users are the PVs, VGs and LVs.

An example:

||-------------------------OS----------------------------||
||-------------------------LVM---------------------------||
||  LV-1 (/)    |LV-2 (swap)|  LV 3 (/home) | LV-4 (/tmp)|| Logical Volumes (LV)
||------------------------------------------|------------||
||                  VG 1                    |    VG 2    || Volume Groups (VG)
||------------------------------------------|------------||
||  /dev/sda2 |    /dev/sda3    | /dev/sdb2 | /dev/sdd4  || Physical Volumes (PV)
||-------------------------------------------------------||

Encrypted LVM

When formatting an LVM volume, you can choose whether to encrypt the data on it or not. Using encrypted LVM is recommended if you are worried about your computer or disk storage being stolen.

<!> If you choose the encrypted LVM option, it is suggested to consider creating backup passwords to ensure you can recover from data loss: Otherwise if you forget the password you will permanently lose all your valuable data :(

Backing up passwords for encrypted LVM

Creating backup passwords is a two step process. First you create a backup password and then you back them up.

  1. Add backup passwords

Run the following command as root:

cryptsetup luksChangeKey <device> -S <key-slot>

Notes

cryptsetup luksChangeKey /dev/sda -S 2
  1. Back up the passwords

Run the following command as root:

cryptsetup luksHeaderBackup <device> --header-backup-file <file>

Notes

Restore passwords

  1. Restore passwords from backup
    • Run the following command as root

cryptsetup luksHeaderRestore <device> --header-backup-file <file>

Using Encrypted LVM as a swap partition

If you use encrypted LVM partitions then you will probably want to encrypt your swap partition as well, because the LVM encryption key is stored in memory and may be written to swap: anyone with physical access may be able to retrieve the encryption keys for all encrypted partitions by accessing the swap partition. The Debian installer will warn against creating an LVM encrypted partition unless the swap partition is encrypted (see Source)

Labels

You should use labels for Logical Volume (LV) identification in /etc/fstab, instead of UUIDs or the kernel naming rules (/dev/sda) in order to avoid conflicts with the restoration of volume snapshots.

/boot

Grub v2 supports booting from an LVM partition since DebianSqueeze.

If you plan to encrypt your root filesystem /boot may need to be located in a separate, unencrypted, Logical Volume or partition. Refer to the Cryptsetup documentation for more information.

GRUB v1 and LILO were not compatible with LVM, if you use one of those legacy bootloaders /boot could not be on the storage disk managed by LVM.

LVM cached volumes

To use lvmcache(7) your system must have the thin-provisioning-tools installed for its cache_check tool, otherwise cached volumes will not be available at boot. This could result in the system being unbootable without manual intervention.

Installation

All tools to manage an LVM volume are available in the lvm2 package which can be installed by running

apt install lvm2

List of LVM commands

Physical Volumes (PV)

Create a PV

To add a hard disk or partition, such as /dev/sda2, to LVM, you create a physical volume, by running the following as root:

pvcreate /dev/sda2

You can now add this PV to a volume group (or create a new VG, see below).

Remove a PV

In order to remove a physical disk from LVM Physical Volume (PV) all data must first be moved off it. To do this, make sure any other physical volumes in the same volume group have enough free space and then issue this command as root:

pvmove /dev/sda2

After the data is moved, remove the disk from the volume group. Assuming the VG is named vol-group, run the following as root:

vgreduce vol-group /dev/sda2

And finally remove the PV by running this as root:

pvremove /dev/sda2

List of PV commands

Volume Groups (VG)

Create a volume group

Assuming /dev/sda2 has been set up as an LVM Physical volume, root can add it to a new volume group by running:

vgcreate myVirtualGroup1 /dev/sda2

Extend a volume group

A volume group can contian multiple physical volumes: after running

pvcreate /dev/sda3

you can add the new PV to an existing VG that already exists:

vgextend VG /dev/sda3

Renaming a volume group

Renaming a volume group can be tricky if it contains the root or swap partition. In that case it should be insured that the old names are still accessible via /dev/mapper until a full reboot cycle is complete.

vgrename somename-vg vgsomename
cd /dev/mapper
ls somename-vg*
## somename--vg-lv1
## somename--vg-lv2
...

ln -s vgsomename-lv1   somename--vg-lv1
ln -s vgsomename-lv2   somename--vg-lv2
...

# if logical volumes of the group are contained in /etc/fstab:
sed -i 's#/dev/mapper/somename--vg-#/dev/vgsomename/#g' /etc/fstab

# if logical volumes of the group are contained in /boot/grub/grub.cfg:
sed -i 's#/dev/mapper/somename--vg-#/dev/vgsomename/#g' /boot/grub/grub.cfg  # this is only good for the initramfs run and the initial next reboot
update-initramfs -c -k all

reboot
update-grub #creating final /boot/grub/grub.cfg

List VGs

Simply run this command:

vgdisplay

List of VG commands

Logical Volumes (LV)

Create an LV

<!> Don't forget to check that you have enough space: naturally, an LV of 100 GB does not work if you only have 10 GB of space. Use vgdisplay to see the free space in each volume group. All these commands must be run as root.

Create a logical volume in a volume group:

lvcreate --name myLogicalVolume1 --size 10g myVirtualGroup1

Create a filesystem on the logical volume:

mkfs -t ext4 /dev/myVirtualGroup1/myLogicalVolume1

You can test to see if it's working:

mkdir /test
sudo mount /dev/myVirtualGroup1/myLogicalVolume1 /test
df -h

You can list your logical volumes with:

sudo lvdisplay

Remove a LV

To remove a logical volume, make sure it is not in use. Then simply issue this command to remove the logical volume myLogicalVolume1 in volume group myVirtualGroup1:

lvremove myVirtualGroup1/myLogicalVolume1

Increase the size of a partition using LVM

To increase the size of a partition, such as /boot stored in a logical volume /dev/vg/boot of a volume group vg that has some unused space:

lvextend --resizefs --verbose --size +3G /dev/vg/boot

This can be done with the partition still mounted. If you do not have free space in the volume group you will need to shrink one of the other LVs in the VG or add another physical volume.

Shrinking a partition using LVM

To shrink a partition, such as /tmp stored on a logical volume /dev/vg/tmp of a volume group vg

lvreduce --resizefs --verbose --size -1G /dev/vg/tmp

This assumes there was 1GB of unused space on the partition. The commmand will unmount the partition and remount it when finished: you will need to ensure nothing is using the filesystem before starting (for the root partition you may need to boot into a rescue disk and resize from there)

List of LV commands

Graphical applications for managing LVM

Below are applications with a graphical user interface (GUI) that allows you to manage LVM devices

KDE PartitionManager

Blivet-GUI

See also:


CategorySystemAdministration | CategoryStorage | CategoryPermalink: mentioned in the Release Notes for trixie