Skip to content

LVM (Logical Volume Management)


Logical Volume Manager (LVM) is a powerful tool for managing disk storage in Linux. It allows system administrators to create, resize, and manage disk partitions more flexibly than traditional partitioning methods.

LVM enables features such as volume resizing, snapshot creation, and effective management of multiple physical storage devices.

Note: For optimal use, LVM should be configured during OS installation. This allows LVM to manage nearly all disk space (except the boot partition, which cannot be in LVM).

It is also possible to install the OS without LVM and add it later. However, to use LVM for the / (root) partition, it must be set up during OS installation.

If the OS was installed without LVM, you can install the LVM package as follows:

Terminal window
sudo apt update
sudo apt -y install lvm2

Tested on the following distributions:

  • Debian 12
  • Debian 13
  • Ubuntu 22.04 LTS Server
  • Ubuntu 24.04 LTS Server


LVM is structured in three layers:

  1. Physical Volumes (PV) – Created from disks or partitions.
  2. Volume Groups (VG) – Formed by combining PVs.
  3. Logical Volumes (LV) – Defined within VGs.

Storage devices or partitions used by LVM as building blocks. These can be:

  • A complete hard drive
  • A disk partition
  • A RAID array

Collections of one or more physical volumes. VGs act as a storage pool, aggregating space from multiple PVs. Logical volumes are created within volume groups.

Abstract representations of a portion of a volume group, similar to partitions in traditional disk management. LVs can be resized and moved without affecting stored data.

There are six main types of logical volumes:

The default logical volume type. Data is stored sequentially.

Provides data redundancy by maintaining multiple copies (mirrors) of the data. Offers fault tolerance: if one mirror fails, the others remain usable.

Creates a point-in-time copy (snapshot) of an existing logical volume. Useful for backups or creating consistent volume images for testing.

Enables efficient storage allocation through thin provisioning. Space is allocated only as needed, allowing more flexible use of storage resources.

Used to cache data for another logical volume, improving performance. Typically used with SSDs to cache data from slower spinning disks.

Stripes data evenly across multiple physical volumes to enhance I/O performance. Allows parallel read and write operations.



Displays physical volume information in a configurable format, showing one line per physical volume.

Terminal window
sudo pvs
sudo pvs /dev/sdb

Scans all supported LVM block devices for physical volumes.

Terminal window
sudo pvscan

Locates and identifies physical volumes on the system and updates the LVM metadata cache.

Provides a verbose multi-line output for each physical volume.

Terminal window
sudo pvdisplay
sudo pvdisplay /dev/sdb

Display a mapping of physical extents to corresponding logical volumes:

Terminal window
sudo pvdisplay -m /dev/sdb

Initializes a physical volume for use by LVM.

Terminal window
sudo pvcreate /dev/sdb
sudo pvcreate /dev/sdb1

Warning: Ensure the device or partition is not in use, as it will be erased.

After initialization, you can create or extend volume groups using vgcreate or vgextend, respectively.

Removes LVM metadata from a physical volume.

Terminal window
sudo pvremove /dev/sdb
sudo pvremove /dev/sdb1

Warning: Ensure you are not removing a PV that is still in use. Remove any associated logical volumes and volume groups before running pvremove.

After removal, the device or partition can be repurposed (e.g., for a new partition or filesystem).

Moves allocated physical extents from one physical volume to another within the same volume group.

Move all data from /dev/sda1 to /dev/sdb1 (both must be in the same VG):

Terminal window
sudo pvmove /dev/sda1 /dev/sdb1

Resizes a physical volume.

Terminal window
sudo pvresize /dev/sdb1

Used when the underlying block device has been resized.

Changes properties (allocatable, UUID, tags, etc.) of a physical volume.

Make /dev/sdb1 allocatable:

Terminal window
sudo pvchange -x y /dev/sdb1

Deactivate (disable) the physical volume in verbose mode:

Terminal window
sudo pvchange -x y /dev/sdb1

Checks and repairs LVM metadata on physical volumes.

Check a PV:

Terminal window
sudo pvck /dev/sdb1


This command provides volume group information in a configurable form, displaying one line per volume group.

Terminal window
sudo vgs
sudo vgs myvg

Scans all supported LVM block devices in the system for volume groups and updates the LVM metadata cache.

Terminal window
sudo vgscan

Displays volume group properties such as size, extents, number of physical volumes, and other details in a fixed format.

Terminal window
sudo vgdisplay
sudo vgdisplay myvg

Creates a new volume group.

Create a new volume group named myvg using the physical volume /dev/sdb1:

Terminal window
sudo vgcreate myvg /dev/sdb1

Prerequisite: The specified physical volumes must be initialized with pvcreate.

Note: Volume group names are case-sensitive. The new volume group’s total capacity will be the sum of the capacities of the specified physical volumes. After creation, you can proceed to create logical volumes within it.

Adds physical volumes to an existing volume group.

Add physical volume /dev/sdb2 to the volume group myvg:

Terminal window
sudo vgextend myvg /dev/sdb2

Prerequisite: The physical volumes to be added must already be initialized using pvcreate. After extending a volume group, you can use the additional space to extend existing logical volumes or create new ones.

Removes one or more physical volumes from a volume group.

Remove /dev/sdb2 from volume group myvg:

Terminal window
sudo vgreduce myvg /dev/sdb1

Note: You must first ensure the PV contains no allocated extents (use pvmove to relocate data). Otherwise, the command will fail unless forced, which can lead to data loss.

Removes a volume group.

Terminal window
sudo vgremove myvg

Prerequisites: You must first remove all logical volumes within the volume group using lvremove. Also, ensure any filesystems mounted from those logical volumes are unmounted. After removal, the physical volumes become unallocated and can be repurposed.

Changes the attributes of a volume group.

Activate (enable) the volume group myvg:

Terminal window
sudo vgchange -a y myvg

( -a y activates; -a n deactivates the volume group.)

Renames a volume group.

Terminal window
sudo vgrename oldvg newvg

Checks the consistency of volume group metadata.

Terminal window
sudo vgck

The --updatemetadata flag rewrites VG metadata to correct inconsistencies:

Terminal window
sudo vgck --updatemetadata myvg -v
  • vgcfgbackup: Backs up volume group configurations.
  • vgcfgrestore: Restores volume group configuration.
  • vgconvert: Changes volume group metadata format.
  • vgexport: Unregisters volume group(s) from the system.
  • vgimport: Registers an exported volume group with the system.
  • vgimportclone: Imports a VG from cloned PVs.
  • vgmerge: Merges volume groups.
  • vgmknodes: Creates the special device files for volume groups in /dev.
  • vgsplit: Moves PVs into a new or existing volume group.


Provides logical volume information in a configurable form, displaying one line per logical volume.

Terminal window
sudo lvs
sudo lvs /dev/vg_name/lv_name
sudo lvs /dev/myvg/mylv

To list all LVs in a specific VG:

Terminal window
sudo lvs /dev/vg_name
sudo lvs /dev/myvg

Scans for and lists all logical volumes in the system.

Terminal window
sudo lvscan

Displays logical volume properties, such as size, layout, and mapping, in a fixed format.

Terminal window
sudo lvdisplay
sudo lvdisplay myvg/mylv

To display sizes in megabytes:

Terminal window
sudo lvdisplay --unit m myvg/mylv

Creates a new logical volume within a volume group.

Create a new LV named mylv in the myvg volume group with a size of 11 GB:

Terminal window
sudo lvcreate -L 11G -n mylv myvg

Format the newly created logical volume:

Terminal window
sudo mkfs -t ext4 /dev/myvg/mylv

Mount it:

Terminal window
sudo mkdir /mnt/point
sudo mount /dev/myvg/mylv /mnt/point

Unmount it:

Terminal window
sudo umount /mnt/point

Extends the size of a logical volume.

Extend the mylv logical volume by 2 GB:

Terminal window
sudo lvextend -L +2G myvg/mylv

Resize the filesystem to use the new space:

Terminal window
sudo resize2fs /dev/myvg/mylv

Note: resize2fs may require a clean filesystem. If prompted, run sudo e2fsck -f /dev/myvg/mylv first.

For XFS filesystems, use xfs_growfs instead of resize2fs.

Reduces the size of a logical volume.

Reduce the logical volume mylv in the volume group myvg by 1 GB:

Terminal window
sudo lvreduce -L -1G myvg/mylv

Resize the file system (you may need to unmount the LV)

Terminal window
sudo resize2fs /dev/myvg/mylv

Removes a logical volume.

Remove the logical volume mylv in the volume group myvg:

Terminal window
sudo lvremove myvg/mylv

Warning: This action is irreversible and will permanently destroy all data on the logical volume. Ensure any filesystems mounted from it are unmounted first.

Changes the attributes and status of a logical volume, such as activation state or permission flags.

Deactivate the logical volume mylv in the volume group myvg:

Terminal window
sudo lvchange -a n myvg/mylv

Activate it:

Terminal window
sudo lvchange -a y myvg/mylv

Set the logical volume to read-only:

Terminal window
sudo lvchange -p r myvg/mylv

Set it back to read/write:

Terminal window
sudo lvchange -p rw myvg/mylv

Renames a logical volume.

Terminal window
sudo lvrename myvg mylv mynewlv2
  • lvconvert: Changes logical volume layout (e.g., to/from mirrored, snapshot).
  • lvresize: A combined command that can extend or reduce a logical volume (use with the same caution as lvreduce).

5. Case Study 1 - Installing LVM and Manipulating Disks, PVs, VGs, LVs

Section titled “5. Case Study 1 - Installing LVM and Manipulating Disks, PVs, VGs, LVs”

We have a system installed without LVM.

  • Add a 20 GB disk and use it as a LV.
  • Add a 30 GB disk and extend the LV.
  • Add a 50 GB disk, extend the VG and move data to this disk.
  • Remove 20 GB and 30 GB disks.

The steps:

  1. Install LVM.
  2. Add a 20 GB disk.
  3. Create a PV from the disk.
  4. Create a VG from the PV.
  5. Create an LV within the VG.
  6. Create a filesystem on the LV.
  7. Mount the filesystem.
  8. Add a 30 GB disk.
  9. Create a PV from the 2nd disk.
  10. Extend the VG with this PV.
  11. Extend the LV.
  12. Extend the filesystem.
  13. Add a 50 GB disk.
  14. Create a PV from the 3rd disk.
  15. Extend the VG with this PV.
  16. Move data from the 20 GB and 30 GB disks to the 50 GB disk.
  17. Remove the 20 GB and 30 GB disks as PVs.
  18. Physically remove the 20 GB and 30 GB disks and verify everything works.
Terminal window
sudo apt update
sudo apt -y install lvm2

Output of lsblk -i command before adding the disk:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 22.9G 0 disk
|-sda1 8:1 0 22G 0 part /
|-sda2 8:2 0 1K 0 part
`-sda5 8:5 0 976M 0 part [SWAP]

For physical servers, you need to add the disk to the hardware. For virtual servers, you need to define it and attach it to the VM.

I’m adding a 20 GB disk to my VM.

Output of the lsblk -i command after adding the disk:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 22.9G 0 disk
|-sda1 8:1 0 22G 0 part /
|-sda2 8:2 0 1K 0 part
`-sda5 8:5 0 976M 0 part [SWAP]
sdb 8:16 0 20G 0 disk

This means my new disk is /dev/sdb. In the following steps, change /dev/sdb to your actual disk device name.

Mark /dev/sdb as a Physical Volume:

Terminal window
sudo pvcreate /dev/sdb

Create a Volume Group named myvg using /dev/sdb:

Terminal window
sudo vgcreate myvg /dev/sdb

Create the Logical Volume using the maximum available space and name it mylv:

Terminal window
sudo lvcreate -l +100%FREE -n mylv myvg

Format it as Ext4:

Terminal window
sudo mkfs -t ext4 /dev/myvg/mylv

Create a mount point:

Terminal window
sudo mkdir /mnt/mylv

Mount mylv to the mount point /mnt/mylv:

Terminal window
sudo mount /dev/myvg/mylv /mnt/mylv

To make the mount persistent, add it to /etc/fstab:

Terminal window
sudo nano /etc/fstab

Add this line to the end of the file:

/dev/myvg/mylv /mnt/mylv ext4 defaults 0 0

Check with lsblk:

Terminal window
lsblk -i

Output:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 22.9G 0 disk
|-sda1 8:1 0 22G 0 part /
|-sda2 8:2 0 1K 0 part
`-sda5 8:5 0 976M 0 part [SWAP]
sdb 8:16 0 20G 0 disk
`-myvg-mylv 254:0 0 20G 0 lvm /mnt/mylv

Check with df -h:

Terminal window
df -h

Output:

Filesystem Size Used Avail Use% Mounted on
udev 457M 0 457M 0% /dev
tmpfs 97M 544K 96M 1% /run
/dev/sda1 22G 1.8G 19G 9% /
tmpfs 481M 0 481M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/mapper/myvg-mylv 20G 44K 19G 1% /mnt/mylv
tmpfs 97M 0 97M 0% /run/user/1000

Create some large files in the filesystem:

Create a directory for files:

Terminal window
sudo mkdir /mnt/mylv/tmp

Make it writable for everyone:

Terminal window
sudo chmod 777 /mnt/mylv/tmp

Create 3 files of 500 MB each:

Terminal window
< /dev/urandom tr -dc "[:space:][:print:]" | head -c500M > /mnt/mylv/tmp/file1
< /dev/urandom tr -dc "[:space:][:print:]" | head -c500M > /mnt/mylv/tmp/file2
< /dev/urandom tr -dc "[:space:][:print:]" | head -c500M > /mnt/mylv/tmp/file3

Check the contents:

Terminal window
ls -al /mnt/mylv/tmp

I’m adding a 30 GB disk to my VM.

Output of lsblk -i after adding the disk:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 22.9G 0 disk
|-sda1 8:1 0 22G 0 part /
|-sda2 8:2 0 1K 0 part
`-sda5 8:5 0 976M 0 part [SWAP]
sdb 8:16 0 20G 0 disk
`-myvg-mylv 254:0 0 20G 0 lvm /mnt/mylv
sdc 8:32 0 30G 0 disk

This means my new disk is /dev/sdc. Change /dev/sdc to your actual disk device name in the following steps.

Mark /dev/sdc as a Physical Volume:

Terminal window
sudo pvcreate /dev/sdc

Add /dev/sdc to the Volume Group myvg:

Terminal window
sudo vgextend myvg /dev/sdc

Extend the mylv Logical Volume to use all available free space:

Terminal window
sudo lvextend -l +100%FREE myvg/mylv

Resize the filesystem to use the new space:

Terminal window
sudo resize2fs /dev/myvg/mylv

Check the new size with df -h:

Terminal window
df -h

Output:

Filesystem Size Used Avail Use% Mounted on
udev 457M 0 457M 0% /dev
tmpfs 97M 548K 96M 1% /run
/dev/sda1 22G 3.2G 18G 16% /
tmpfs 481M 0 481M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/mapper/myvg-mylv 50G 1.5G 46G 4% /mnt/mylv

Add another large file:

Terminal window
< /dev/urandom tr -dc "[:space:][:print:]" | head -c500M > /mnt/mylv/tmp/file4

Check the contents:

Terminal window
ls -al /mnt/mylv/tmp

I’m adding a 50 GB disk to my VM.

Output of lsblk -i after adding the disk:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 22.9G 0 disk
|-sda1 8:1 0 22G 0 part /
|-sda2 8:2 0 1K 0 part
`-sda5 8:5 0 976M 0 part [SWAP]
sdb 8:16 0 20G 0 disk
`-myvg-mylv 254:0 0 30G 0 lvm /mnt/mylv
sdc 8:32 0 30G 0 disk
`-myvg-mylv 254:0 0 30G 0 lvm /mnt/mylv
sdd 8:48 0 50G 0 disk

This means my new disk is /dev/sdd. Change /dev/sdd to your actual disk device name in the following steps.

Mark /dev/sdd as a Physical Volume:

Terminal window
sudo pvcreate /dev/sdd

Add /dev/sdd to the Volume Group myvg:

Terminal window
sudo vgextend myvg /dev/sdd

5.16. Move Data from the 20 GB and 30 GB Disks to the 50 GB Disk

Section titled “5.16. Move Data from the 20 GB and 30 GB Disks to the 50 GB Disk”

Move data from the 20 GB disk (sdb) to the 50 GB disk (sdd):

Terminal window
sudo pvmove /dev/sdb /dev/sdd

Move data from the 30 GB disk (sdc) to the 50 GB disk (sdd):

Terminal window
sudo pvmove /dev/sdc /dev/sdd

Note: These operations may take some time, depending on the amount of data.

5.17. Remove the 20 GB and 30 GB Disks as PVs

Section titled “5.17. Remove the 20 GB and 30 GB Disks as PVs”

First, remove them from the Volume Group:

Terminal window
sudo vgreduce myvg /dev/sdb
sudo vgreduce myvg /dev/sdc

Now, remove the Physical Volume metadata from the disks:

Terminal window
sudo pvremove /dev/sdb
sudo pvremove /dev/sdc

5.18. Physically Remove the 20 GB and 30 GB Disks and Verify

Section titled “5.18. Physically Remove the 20 GB and 30 GB Disks and Verify”

At this step, we physically remove (or detach) the 20 GB and 30 GB disks from the system.

Output of lsblk -i after removing the disks:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 22.9G 0 disk
|-sda1 8:1 0 22G 0 part /
|-sda2 8:2 0 1K 0 part
`-sda5 8:5 0 976M 0 part [SWAP]
sdb 8:16 0 50G 0 disk
`-myvg-mylv 254:0 0 30G 0 lvm /mnt/mylv

As you can see, /dev/sdd has become /dev/sdb, but this is not a problem. Our LVM configuration is still working correctly.

Check the contents to confirm data integrity:

Terminal window
ls -al /mnt/mylv/tmp


A snapshot creates a point-in-time copy of a Logical Volume (LV). When files are changed on the original volume after the snapshot is taken, the original data blocks are preserved in the snapshot space. This allows you to revert to the snapshot state if needed.

Because only changed data blocks are stored, snapshots typically require much less space than the original volume.

Important: If a snapshot becomes full (runs out of space to track changes), it becomes invalid and is automatically dropped, which can lead to data loss. Snapshots are resizable to prevent this.

We have a system installed with LVM.

  • Add a 20 GB disk and use it to extend the system (/) disk.
  • Add a 30 GB disk and add it to the existing Volume Group.
  • Create a snapshot of the root filesystem using space from the new 30 GB disk.
  • Make significant changes to the system and revert using the snapshot.

The steps:

  1. Add a 20 GB disk.
  2. Create a PV from the disk.
  3. Extend the VG with this PV.
  4. Extend the root LV.
  5. Extend the filesystem.
  6. Add a 30 GB disk.
  7. Create a PV from the disk.
  8. Extend the VG with this PV.
  9. Create a snapshot of the system (root) disk.
  10. Make some changes to the system disk.
  11. Mount the snapshot and check its content.
  12. Extend the snapshot.
  13. Revert to the snapshot.

Output of lsblk -i command before adding the disk:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 20G 0 disk
|-sda1 8:1 0 487M 0 part /boot
|-sda2 8:2 0 1K 0 part
`-sda5 8:5 0 19.5G 0 part
|-myvg-root 254:0 0 18.6G 0 lvm /
`-myvg-swap_1 254:1 0 980M 0 lvm [SWAP]

Note on Naming:

When installing with LVM, Debian lets you choose the VG name (I chose myvg). Debian names the root LV root. Ubuntu uses ubuntu-vg and lv-root by default.

In this section, replace myvg with your actual Volume Group name (ubuntu-vg for Ubuntu) in all commands.

Output of sudo pvs before adding the disk:

PV VG Fmt Attr PSize PFree
/dev/sda5 myvg lvm2 a-- <19.52g 0

Output of sudo lvscan before adding the disk:

ACTIVE '/dev/myvg/root' [18.56 GiB] inherit
ACTIVE '/dev/myvg/swap_1' [980.00 MiB] inherit

Output of df -h before adding the disk:

Filesystem Size Used Avail Use% Mounted on
udev 458M 0 458M 0% /dev
tmpfs 97M 544K 96M 1% /run
/dev/mapper/myvg-root 19G 1.6G 16G 10% /
tmpfs 481M 0 481M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda1 455M 98M 333M 23% /boot
tmpfs 97M 0 97M 0% /run/user/1000

For physical servers, add the disk to the hardware. For virtual servers, define and attach it to the VM.

I’m adding a 20 GB disk to my VM.

Output of lsblk -i after adding the disk:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 20G 0 disk
|-sda1 8:1 0 487M 0 part /boot
|-sda2 8:2 0 1K 0 part
`-sda5 8:5 0 19.5G 0 part
|-myvg-root 254:0 0 18.6G 0 lvm /
`-myvg-swap_1 254:1 0 980M 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk

This means my new disk is /dev/sdb. Change /dev/sdb to your actual disk device name in the following steps.

Mark /dev/sdb as a Physical Volume:

Terminal window
sudo pvcreate /dev/sdb

Add /dev/sdb to the Volume Group myvg:

Terminal window
sudo vgextend myvg /dev/sdb

Extend the root LV to use all available free space:

Terminal window
sudo lvextend -l +100%FREE myvg/root

Resize the filesystem to use the new space:

Terminal window
sudo resize2fs /dev/myvg/root

Output of df -h after expanding:

Terminal window
Filesystem Size Used Avail Use% Mounted on
udev 458M 0 458M 0% /dev
tmpfs 97M 548K 96M 1% /run
/dev/mapper/myvg-root 38G 1.6G 35G 5% /
tmpfs 481M 0 481M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda1 455M 98M 333M 23% /boot
tmpfs 97M 0 97M 0% /run/user/1000

I’m adding a 30 GB disk to my VM.

Output of lsblk -i command after adding the disk:

Terminal window
sda 8:0 0 20G 0 disk
|-sda1 8:1 0 487M 0 part /boot
|-sda2 8:2 0 1K 0 part
`-sda5 8:5 0 19.5G 0 part
|-myvg-root 254:0 0 38.6G 0 lvm /
`-myvg-swap_1 254:1 0 980M 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
`-myvg-root 254:0 0 38.6G 0 lvm /
sdc 8:32 0 30G 0 disk

This means my new disk is /dev/sdc. Change /dev/sdc to your actual disk device name in the following steps.

Mark /dev/sdc as a Physical Volume:

Terminal window
sudo pvcreate /dev/sdc

Add /dev/sdc to the Volume Group myvg:

Terminal window
sudo vgextend myvg /dev/sdc

Important: Snapshots must be created within the same Volume Group as the original Logical Volume.

Create a 2 GB snapshot named mysnapshot of the root LV:

Terminal window
sudo lvcreate --type snapshot -n mysnapshot -L 2G --snapshot /dev/myvg/root

Tip: Monitor snapshot usage with sudo lvs -o lv_name,lv_size,snap_percent,origin. If the usage nears 100%, extend it immediately.

To demonstrate the snapshot, we’ll make significant changes by installing software:

Terminal window
sudo apt update
sudo apt install -y apache2 mariadb-server

Check the snapshot status to see if it’s tracking changes:

Terminal window
sudo lvs -o lv_name,lv_size,origin

6.11. Mount the Snapshot and Check Its Content

Section titled “6.11. Mount the Snapshot and Check Its Content”

Create a temporary mount point:

Terminal window
sudo mkdir /mnt/mysnapshot

Mount the snapshot (read-only is safer for inspection):

Terminal window
sudo mount -o ro /dev/myvg/mysnapshot /mnt/mysnapshot

Browse /mnt/mysnapshot to see the state of the root filesystem at the time the snapshot was taken (you won’t see the newly installed packages).

Unmount it:

Terminal window
sudo umount /mnt/mysnapshot

If the snapshot is running low on space (check with sudo lvs), extend it to allow more changes to be tracked:

Terminal window
sudo lvextend -L+1G /dev/myvg/mysnapshot

Reverting merges the snapshot back into the original volume, restoring it to the snapshot’s state.

⚠️ Critical for Root Volume: Since the root (/) filesystem cannot be unmounted while the system is running, the merge is scheduled for the next boot.

Schedule the merge:

Terminal window
sudo lvconvert --merge myvg/mysnapshot

You will see a message like: “Delayed merge scheduled for activation after next activation.”

Reboot the system. The merge will occur during the early boot process:

Terminal window
sudo reboot

Note: The snapshot LV (mysnapshot) will no longer exist after a successful merge.


7. Case Study 3 - Export and Import of LVM

Section titled “7. Case Study 3 - Export and Import of LVM”

It is possible to export and import entire Volume Groups (with their Logical Volumes) between systems. The process involves: unmounting the LVs, deactivating and exporting the VG, moving the physical disks to the new system, then importing, activating, and remounting the VG.

We have two systems installed without LVM: srva and srvb. We will create an LVM setup on srva, fill it with data, and then move the entire setup to srvb.

  • A. Prepare the first system (srva)
    • -1. Install LVM on srva.
    • -2. Add 20 GB disk to srva.
    • -3. Create a PV from the disk.
    • -4. Create a VG from the PV.
    • -5. Create 2 LVs within the VG.
    • -6. Create filesystems in the LV.
    • -7. Mount the filesystems.
    • -8. Put test data on the filesystems.
  • B. Export
    • -9. Unmount the logical volumes.
    • -10. Deactivate all logical volumes in the VG.
    • -11. Export the VG and Unplug the disk.
  • C. Prepare the second system (srvb)
    • -12. Install LVM on srvb.
    • -13. Plug the disk into srvb.
  • D. Import
    • -14. Import the volume group.
    • -15. Activate the volume group.
    • -16. Mount the logical volumes.
Terminal window
sudo apt update
sudo apt -y install lvm2

Output of lsblk -i before adding the disk:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 22.9G 0 disk
|-sda1 8:1 0 22G 0 part /
|-sda2 8:2 0 1K 0 part
`-sda5 8:5 0 976M 0 part [SWAP]

For physical servers, add the disk to the hardware. For virtual servers, define and attach it to the VM.

I’m adding a 20 GB disk to my VM.

Output of lsblk -i after adding the disk:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 22.9G 0 disk
|-sda1 8:1 0 22G 0 part /
|-sda2 8:2 0 1K 0 part
`-sda5 8:5 0 976M 0 part [SWAP]
sdb 8:16 0 20G 0 disk

This means my new disk is /dev/sdb. Change /dev/sdb to your actual disk device name in the following steps.

Mark /dev/sdb as a Physical Volume:

Terminal window
sudo pvcreate /dev/sdb

Create a Volume Group named myvg using /dev/sdb:

Terminal window
sudo vgcreate myvg /dev/sdb

First LV is 10 GB:

Terminal window
sudo lvcreate -L 10G -n mylv1 myvg

Second LV uses the remaining space (~10 GB):

Terminal window
sudo lvcreate -l +100%FREE -n mylv2 myvg
Terminal window
sudo mkfs -t ext4 /dev/myvg/mylv1
sudo mkfs -t ext4 /dev/myvg/mylv2

Create mount points:

Terminal window
sudo mkdir /mnt/mylv1
sudo mkdir /mnt/mylv2

Mount the LVs:

Terminal window
sudo mount /dev/myvg/mylv1 /mnt/mylv1
sudo mount /dev/myvg/mylv2 /mnt/mylv2

Make the mount points writable for everyone (for testing):

Terminal window
sudo chmod 777 /mnt/mylv1
sudo chmod 777 /mnt/mylv2

Create 2 files of 100 MB for each LV:

Terminal window
< /dev/urandom tr -dc "[:space:][:print:]" | head -c100M > /mnt/mylv1/d1f1
< /dev/urandom tr -dc "[:space:][:print:]" | head -c100M > /mnt/mylv1/d1f2
< /dev/urandom tr -dc "[:space:][:print:]" | head -c100M > /mnt/mylv2/d2f1
< /dev/urandom tr -dc "[:space:][:print:]" | head -c100M > /mnt/mylv2/d2f2

Check the contents:

Terminal window
ls -al /mnt/mylv1
ls -al /mnt/mylv2
Terminal window
sudo umount /mnt/mylv1
sudo umount /mnt/mylv2

7.10. Deactivate All Logical Volumes in the VG

Section titled “7.10. Deactivate All Logical Volumes in the VG”

Deactivate the entire Volume Group:

Terminal window
sudo vgchange -an myvg

Verify with pvscan (it should show the PV as belonging to myvg but not active):

Terminal window
sudo pvscan

Export the Volume Group (this removes it from the system’s LVM metadata cache):

Terminal window
sudo vgexport myvg

Now you can safely unplug or detach the 20 GB disk from srva.

Output of lsblk -i after removing the disk from srva:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 22.9G 0 disk
|-sda1 8:1 0 22G 0 part /
|-sda2 8:2 0 1K 0 part
`-sda5 8:5 0 976M 0 part [SWAP]
Terminal window
sudo apt update
sudo apt -y install lvm2

Output of lsblk -i on srvb before adding the disk:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 22.9G 0 disk
|-sda1 8:1 0 22G 0 part /
|-sda2 8:2 0 1K 0 part
`-sda5 8:5 0 976M 0 part [SWAP]

For physical servers, add the disk to the hardware. For virtual servers, define and attach it to the VM.

I’m adding the 20 GB disk to my srvb VM.

Output of lsblk -i after adding the disk to srvb:

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 22.9G 0 disk
|-sda1 8:1 0 22G 0 part /
|-sda2 8:2 0 1K 0 part
`-sda5 8:5 0 976M 0 part [SWAP]
sdb 8:16 0 20G 0 disk

This means the disk is now /dev/sdb on srvb.

Scan for and import the exported Volume Group myvg:

Terminal window
sudo vgimport myvg

Activate the Volume Group and its logical volumes:

Terminal window
sudo vgchange -ay myvg

Create mount points and mount the LVs:

Terminal window
sudo mkdir /mnt/mylv1 /mnt/mylv2
sudo mount /dev/myvg/mylv1 /mnt/mylv1
sudo mount /dev/myvg/mylv2 /mnt/mylv2

Verify the data is intact:

Terminal window
ls -al /mnt/mylv1
ls -al /mnt/mylv2


The following LVM subjects are not covered in this tutorial:

  • Striped logical volumes
  • RAID logical volumes
  • Thin-provisioned logical volumes
  • Cache logical volumes
  • Using shared storage (e.g., with iSCSI or Fibre Channel)
  • Fine-grained control of logical volume activation
  • Controlling physical extent allocation policies
  • LVM object tags