Translation(s): English - Français

(!) Discussion


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

Presentation

Logical volume management provides a higher-level view of the disk storage on a computer system than the traditional view of disks and partitions. This gives the system administrator much more flexibility in allocating storage to applications and users.

Storage volumes created under the control of the logical volume manager can be resized and moved around almost at will.

Definitions

An example:

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

Good to know

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 and ?LiLo are not compatible with LVM, so /boot should be outside the storage disk managed by LVM.

LVM2 snapshots and udev on Debian

There are some caveats when creating LVM snapshots on Debian with udev, see 343671

Installation

All tools to manage an LVM volume are available in lvm2 package

sudo apt-get install lvm2

Then start the lvm service:

sudo /etc/init.d/lvm start

If needed, you can install system-config-lvm, it's a utility for graphically configuring Logical Volumes.

sudo apt-get install system-config-lvm

List of LVM commands

Physical Volumes (PV)

Create a PV

To declare the /dev/sda2 as a physical volume available for the LVM:

sudo pvcreate /dev/sda2

Remove a PV

In order to remove the Physical Volume (PV) on /dev/sda2 all data must be moved off it. To do this, make sure other physical volumes containing the same volume group have enough free space and then issue this command:

pvmove /dev/sda2

After the data is moved off the disk, remove it from the volume group (in this case, the one named myVirtualGroup1:

sudo vgreduce myVirtualGroup1 /dev/sda2

And after these preparations finally:

sudo pvremove /dev/sda2

PV commands list

Volume Groups (VG)

Create a volume group of physical volume

sudo vgcreate myVirtualGroup1 /dev/sda2

Extend a volume group

Declare another physical volume:

sudo pvcreate /dev/sda3

Then add the new PV to the VG that already exists:

sudo vgextend myVirtualGroup1 /dev/sda3

Verify VG configuration

Simply run this command:

sudo vgdisplay 

VG commands list

Logical Volumes (LV)

Create an LV

<!> Don't forget to check that you have enough space: naturally, an LV of 100 GB (Giga Bytes) doesn't fit in a 10 GB Virtual Group.

Create a logical volume in a volume group:

sudo lvcreate -n myLogicalVolume1 -L 10g myVirtualGroup1

Format the logical volume to the filesystem you want (ext4,xfs...)

sudo 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 also can check your logical volumes with:

sudo lvdisplay

Remove a LV

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

sudo lvremove myVirtualGroup1/myLogicalVolume1

You might get asked if you really want to remove an active logical volume. If so, confirm it.

LV commands

See also: