Translation(s): English - Español - Français -
LVM is a Logical Volume Manager for the Linux operating system.
Contents
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
PV : Physical Volumes. This means a hard disk, hard disk partition, RAID or LUNs from a SAN.
VG : Volume Groups. This is a collection of one or more Physical Volumes.
LV : Logical Volumes. LVs sit inside a Volume Group and form, in effect, a virtual partition.
PE : Physical Extents. In order to manipulate the actual data, it is divided into blocks called Physical Extents.
LE : Logical Extents. Similar to Physical Extents, but at the Logical Volume level. Physical Extents are to Physical Volumes as Logical Extents are to Logical Volumes. The size of blocks are the same for each logical volume (LV) in the same volume group (VG).
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.
- Add backup passwords
Run the following command as root:
cryptsetup luksChangeKey <device> -S <key-slot>
Notes
The -S option specifies a <key-slot> (number from 0 to 7) holding a password. You need to replace <device> with the path to your encrypted LVM volume. For example /dev/sda.
It is suggested to create at least 3 backup passwords. To add an additional backup password simply run the same command, but change the <key-slot> number. For example to set the password in slot 2 use:
cryptsetup luksChangeKey /dev/sda -S 2
- The password(s) of a encrypted LVM volume are stored inside its Linux Unified Key Setup (LUKS) header.
- Back up the passwords
Run the following command as root:
cryptsetup luksHeaderBackup <device> --header-backup-file <file>
Notes
<device> is the location to save your backup to, for example /dev/sda. And <file> is the name of your backup file, for example /media/jenn/2017-05-18_luks_sda_backup. This command backs up the passwords in all eight key-slots.
- It is suggested to store the backup file in a secure, off-line, and different location to ensure that you can access them if you lose access to LVM volume.
See the official LUKS wiki documentation for more information
Restore passwords
- Restore passwords from backup
- Run the following command as root
cryptsetup luksHeaderRestore <device> --header-backup-file <file>
- Notes
This command will delete all eight passwords, then restore all your backed up passwords. Before running this command, you should double check that you are sure you really want to proceed: there is no undo
You need to change <device> to the path to your encrypted LVM volume, for example /dev/sda. You need to change <file> to the path to your backup file, for example /media/jenn/2017-05-18_luks_sda_backup.
See the official LUKS wiki documentation for more information
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
lvmchange — Change attributes of the Logical Volume Manager.
lvmdiskscan — Scan for all devices visible to LVM2.
lvmdump — Create lvm2 information dumps for diagnostic purposes.
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
pvchange — Change attributes of a Physical Volume.
pvck — Check Physical Volume metadata.
pvcreate — Initialize a disk or partition for use by LVM.
pvdisplay — Display attributes of a Physical Volume.
pvmove — Move Physical Extents.
pvremove — Remove a Physical Volume.
pvresize — Resize a disk or partition in use by LVM2.
pvs — Report information about Physical Volumes.
pvscan — Scan all disks for Physical Volumes.
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
vgcfgbackup — Backup Volume Group descriptor area.
vgcfgrestore — Restore Volume Group descriptor area.
vgchange — Change attributes of a Volume Group.
vgck — Check Volume Group metadata.
vgconvert — Convert Volume Group metadata format.
vgcreate — Create a Volume Group.
vgdisplay — Display attributes of Volume Groups.
vgexport — Make volume Groups unknown to the system.
vgextend — Add Physical Volumes to a Volume Group.
vgimport — Make exported Volume Groups known to the system.
vgimportclone — Import and rename duplicated Volume Group (e.g. a hardware snapshot).
vgmerge — Merge two Volume Groups.
vgmknodes — Recreate Volume Group directory and Logical Volume special files
vgreduce — Reduce a Volume Group by removing one or more Physical Volumes.
vgremove — Remove a Volume Group.
vgrename — Rename a Volume Group.
vgs — Report information about Volume Groups.
vgscan — Scan all disks for Volume Groups and rebuild caches.
vgsplit — Split a Volume Group into two, moving any logical volumes from one Volume Group to another by moving entire Physical Volumes.
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
lvchange — Change attributes of a Logical Volume.
lvconvert — Convert a Logical Volume from linear to mirror or snapshot.
lvcreate — Create a Logical Volume in an existing Volume Group.
lvdisplay — Display the attributes of a Logical Volume.
lvextend — Extend the size of a Logical Volume.
lvreduce — Reduce the size of a Logical Volume.
lvremove — Remove a Logical Volume.
lvrename — Rename a Logical Volume.
lvresize — Resize a Logical Volume.
lvs — Report information about Logical Volumes.
lvscan — Scan (all disks) for Logical Volumes.
Graphical applications for managing LVM
Below are applications with a graphical user interface (GUI) that allows you to manage LVM devices
KDE PartitionManager
To install, get this KDE partitionmanager package
- Supports LVM2, PV, VG, LV, LUKS encryption, and others
Code repository at https://github.com/KDE/partitionmanager/
- Libre Source (Open Source) with LGPL 3, GPL 4, GFDL 1.2, CCO 1, CC-BY 4 licences
Blivet-GUI
To install, click on the "Debian" button at https://software.opensuse.org/download.html?project=home:vtrefny&package=blivet-gui Then choose an installation option.
- Available for Debian 10 or 11
- Support LVM2, PV, VG, LV, and others
Code repository at https://github.com/storaged-project/blivet-gui/
- Libre Source (Open Source) with GPL 2 licence
See also:
Comparison of Linux volume management solutions for Debian users
- Upstream:
https://sourceware.org/lvm2/ - Homepage, mailing list, IRC...
CategorySystemAdministration | CategoryStorage | CategoryPermalink: mentioned in the Release Notes for trixie
