LVM Basic Operations
Overview of Logical Volume Management (LVM)
Logical volume management (LVM) creates a layer of abstraction over physical storage, which helps you to create logical storage volumes. This provides much greater flexibility in a number of ways than using physical storage directly.
In addition, the hardware storage configuration is hidden from the software so it can be resized and moved without stopping applications or unmounting file systems. This can reduce operational costs.
The following are the components of LVM – they are illustrated on the diagram shown at Figure 1:
- Physical volume: A physical volume (PV) is a partition or whole disk designated for LVM use. For more information, see Managing LVM physical volumes.
- Volume group: A volume group (VG) is a collection of physical volumes (PVs), which creates a pool of disk space out of which logical volumes can be allocated. For more information, see Managing LVM volume groups.
- Logical volume: A logical volume represents a mountable storage device. For more information, see Managing LVM logical volumes.
Physical Volumes (PV)
The physical volume (PV) is a partition or whole disk designated for LVM use. To use the device for an LVM logical volume, the device must be initialized as a physical volume.
If you are using a whole disk device for your physical volume, the disk must have no partition table. For DOS disk partitions, the partition id should be set to 0x8e using the fdisk
or cfdisk
command or an equivalent. If you are using a whole disk device for your physical volume, the disk must have no partition table. Any existing partition table must be erased, which will effectively destroy all data on that disk. You can remove an existing partition table using the wipefs ‑a <PhysicalVolume>`
command as root.
Display PV
pvdisplay
– Display various attributes of physical volume(s). pvdisplay
shows the attributes of PVs, like size, physical extent size, space used for the VG descriptor area, etc.
sudo pvdisplay
--- Physical volume ---
PV Name /dev/nvme0n1p3
VG Name vg_name
PV Size <930.54 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 238216
Free PE 155272
Allocated PE 82944
PV UUID BKK3Cm-CAzY-rNah-o3Ox-L7FY-aNIY-ysUSIL
pvs
– Display information about physical volumes. pvs
is a preferred alternative of pvdisplay
that shows the same information and more, using a more compact and configurable output format.
sudo pvs
PV VG Fmt Attr PSize PFree
/dev/nvme0n1p3 vg_name lvm2 a-- 930.53g 626.53g
The command lsblk
also outputs useful information about PV-VG-LV relations.
lsblk -M
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
nvme0n1 259:0 0 931.5G 0 disk
├─nvme0n1p1 259:1 0 512M 0 part /boot/efi
├─nvme0n1p2 259:2 0 488M 0 part /boot
└─nvme0n1p3 259:3 0 930.5G 0 part
┌─> ├─vg-lv_1-real 254:0 0 60G 0 lvm
│ │ └─vg-lv_1 254:1 0 60G 0 lvm /
┌─>│ ├─vg-lv_2-real 254:2 0 60G 0 lvm
│ │ │ └─vg-lv_2 254:7 0 60G 0 lvm /home
└┬>│ ├─vg-lv_2_ss_at_date_220912-cow 254:3 0 60G 0 lvm
│ └┬> ├─vg-lv_1_ss_at_date_220908-cow 254:4 0 60G 0 lvm
│ │ └─vg-swap_1 254:6 0 4G 0 lvm [SWAP]
│ └──vg-lv_1_ss_at_date_220908 254:5 0 60G 0 lvm
└─────vg-lv_2_ss_at_date_220912 254:9 0 60G 0 lvm
pvscan
– List all physical volumes. The pvscan
command scans all supported LVM block devices in the system for physical volumes. You can define a filter in the lvm.conf
file so that this command avoids scanning specific physical volumes.
sudo pvscan
PV /dev/nvme0n1p3 VG vg_name lvm2 [930.53 GiB / 566.53 GiB free]
Total: 1 [930.53 GiB] / in use: 1 [930.53 GiB] / in no VG: 0 [0 ]
Create PV
The physical volume could be created on the entire drive or on a partition. It is preferable to partition the disk – create a single partition that covers the whole disk to label as an LVM physical volume, – otherwise some tools like CloneZilla wont handle the drive correctly when LVM is installed directly on the drive (without partitioning), etc.. How to partition the disk from the CLI is described in the article Linux Basic Partitioning.
Create Physicals volume by the partition /dev/nvme0n1p3
.
sudo pvcreate /dev/nvme0n1p3
Physical volume "/dev/nvme0n1p3" successfully created.
Another example: Create Physicals volume by the partition /dev/vda1
and /dev/vdb1
.
sudo pvcreate /dev/vda1 /dev/vdb1
Physical volume "/dev/vda1" successfully created.
Physical volume "/dev/vdb1" successfully created.
Remove PV
If a device is no longer required for use by LVM, you can remove the LVM label by using the pvremove
command. Executing the pvremove
command zeroes the LVM metadata on an empty physical volume.
sudo pvremove /dev/nvme0n2p1
Labels on physical volume "/dev/nvme0n2p1" successfully wiped.
Then use pvs
and verify if the required volume is removed. If the physical volume you want to remove is currently part of a volume group, you must remove it from the volume group with the vgreduce
command. For more information, see Removing physical volumes from a volume group.
Volume Groups (VG)
A volume group (VG) is a collection of physical volumes (PVs), which creates a pool of disk space out of which logical volumes (LVs) can be allocated.
Within a volume group, the disk space available for allocation is divided into units of a fixed-size called extents. An extent is the smallest unit of space that can be allocated. Within a physical volume, extents are referred to as physical extents.
A logical volume is allocated into logical extents of the same size as the physical extents. The extent size is thus the same for all logical volumes in the volume group. The volume group maps the logical extents to physical extents.
Display VG
vgdisplay
– Display volume group information. The vgdisplay
command displays volume group properties such as size, extents, number of physical volumes, and other options in a fixed form. If you do not specify a volume group, all existing volume groups are displayed.
sudo vgdisplay
--- Volume group ---
VG Name vg_name
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 19
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 6
Open LV 4
Max PV 0
Cur PV 1
Act PV 1
VG Size 930.53 GiB
PE Size 4.00 MiB
Total PE 238216
Alloc PE / Size 93184 / 364.00 GiB
Free PE / Size 145032 / 566.53 GiB
VG UUID NWWfr6-xxfN-0d22-muPn-Rgqj-vAcA-z5P5jV
The following example shows the output of the vgdisplay
command for the volume group vg_name
.
sudo vgdisplay vg_name
vgs
– Display information about volume groups. It is a preferred alternative of vgdisplay
that shows the same information and more, using a more compact and configurable output format.
sudo vgs
VG #PV #LV #SN Attr VSize VFree
vg_name 1 6 2 wz--n- 930.53g 566.53g
vgscan
– Search for all volume groups. vgscan
scans all supported LVM block devices in the system for VGs.
sudo vgscan
Found volume group "vg_name" using metadata type lvm2
Create VG
Create Volume group named vg_name
by using the /dev/nvme0n1p3
Physical volume.
sudo vgcreate vg_name /dev/nvme0n1p3
Volume group "vg_name" successfully created.
Another example: Create Volume group named another_vg
by using /dev/vda1
and /dev/vdb1
Physical volumes.
sudo vgcreate another_vg /dev/vda1 /dev/vdb1
Volume group "another_vg" successfully created.
Extend VG
vgextend
– Add physical volumes to a volume group. vgextend
adds one or more PVs to a VG. This increases the space available for LVs in the VG.
Also, PVs that have gone missing and then returned, e.g. due to a transient device failure, can be added back to the VG without re-initializing them (see --restoremissing
).
If the specified PVs have not yet been initialized with pvcreate
, vgextend
will initialize them. In this case pvcreate
options can be used, e.g. --labelsector
, --metadatasize
, --metadataignore
, --pvmetadatacopies
, --dataalignment
, --dataalignmentoffset
.
Remove VG
Create LVM logical volume at lvm-vm-group
:
sudo lvcreate -n vm-win-01 -L 60g lvm-vm-group
Logical volume "vm-win-01" created.
vm-win-01
is the name of the logical device, it is mater of your choice.
Check the result:
lsblk | grep -P 'sdb|lvm'
├─sdb1 8:17 0 1G 0 part
└─sdb2 8:18 0 231.9G 0 part
└─lvm--vm--group-vm--win--01 253:0 0 60G 0 lvm
## Extend VG which is located on /dev/sda to encompass also /dev/sdb
lsblk # check the devices
sudo pvcreat /dev/sdb # create phisical volume at /dev/sdb
sudo pvdisplay # check
sudo vgextend vg_name /dev/sdb # extend the existing volume group
df -hT # check
sudo vgdisplay # check
# At this point you can create a new logical volume or extend an existing one
## Extend logical volume - note there mus have enoug free space in the volume group where the existing LV is located
sudo lvextend -L +10G /dev/mapper/lv_name
sudo rsize2fs /dev/mapper/lv_name
df -h
### for swap file sudo mkswap /dev/mapper/xxxx-swap_1
### then edit /etc/fstab and chenge the UUID if it is mounted by it...
## Exten to all available space and resize the FS in the same time
sudo lvextend --reziefs -l +100%FREE /dev/mapper/lv_name
df -h
# Shrink logical volume
1 lsblk
2 sudo apt update
3 sudo apt install gnome-disk-utility
4 ll
5 lsblk
6 ll /dev/kali-x-vg
7 ll /dev/kali-x-vg/root
8 ll /dev/dm-0
9 sudo lvreduce --resizefs -L 60G kali-x-vg/root
10 lsblk
11 df -h
## Create a new volume group on newly attached block device /dev/sdc
lsblk # check
sudo pvcreat /dev/sdbc # crrate phisical volume
sudo vgcreate vg_name /dev/sdbc # create volume group
sudo vgdisplay # check
## Create a new logical volume
sudo lvcreate vg_name -L 5G -n lv_name # create 5GB logical volume
sudo lvdisplay # check
sudo mkfs.ext4 /dev/mapper/vg_name-lv_name # format the logical folume as Ext4
# mount the new logical volume
sudo mkdir -p /mnt/volume # create mount point
sudo mount /dev/mapper/vg_name-lv_name /mnt/volume # mount the volume
df -h # check
# mount it permanently via /etc/fstab (we can munt it by using the mapped path, but it is preferable to use UUID)
sudo blkid /dev/mapper/vg_name-lv_name # get the UUID (universal unique identifier) of the logical volume
> /dev/mapper/vg_name-lv_name: UUID="b6ddc49d-...-...c90" BLOCK_SIZE="4096" TYPE="ext4"
sudo cp /etc/fstab{,.bak} # backup the fstab file
sudo umount /mnt/volume # unmount the nel volume
df -h # check
sudo nano /etc/fstab
># <file system> <mount point> <type> <options> <dump> <pass>
> UUID=b6ddc49d-...-...c90 /mnt/volume ext4 defaults 0 2
# Test /etc/fsstab for errors, by remount everything listed inside
sudo mount -a # no output means everything is correctly mounted
df -h # check
Snapshots of Logical Volumes
Create a Snapshot
lvcreate
– Create a logical volume.
-s
,--snapshot
– Create a snapshot. Snapshots provide a "frozen image" of an origin LV. The snapshot LV can be used, e.g. for backups, while the origin LV continues to be used. This option can create a COW (copy on write) snapshot, or a thin snapshot (in a thin pool.) Thin snapshots are created when the origin is a thin LV and the size option is NOT specified.-n
,--name
– Specifies the name of a new LV. When unspecified, a default name oflvol#
is generated, where#
is a number generated by LVM.
Create 5GB snapshot of a logical volume named lv_name
from volume group named vg_name
.
sudo lvcreate /dev/mapper/vg_name-lv_name -L 5G -s -n lv_name_ss_at_date_$(date +%y%m%d)
sudo lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv_name vg_name owi-aos--- 60.00g
lv_name_ss_at_date_220908 vg_name swi-a-s--- 5.00g lv_name 0.01
Mount a Snapshot
The snapshot could be mounted in order to fetch files in their snapshot state. In this case it is good to mount them in read only mode.
sudo mkdir /mnt/snapshot
sudo mount -r /dev/mapper/vg_name-lv_name_ss_at_date_220908 /mnt/snapshot
You can use a mounted snapshot (of the root filesystem) to create a backup while the OS is running.
Restore a Snapshot
Note after the restoring of a snapshot with merge option in use the snapshot will be removed, so you must create another snapshot for a later use of the same state of the logical volume.
For restoring a snapshot we will need to use the commands lvconvert
and lvchange
.
LvConvert: Convert-merge the snapshot with the source LV
lvconvert
– Change logical volume layout.
--merge
– An alias for--mergethin
,--mergemirrors
, or--mergesnapshot
, depending on the type of LV (logical volume).--mergesnapshot
– Merge COW snapshot LV into its origin. When merging a snapshot, if both the origin and snapshot LVs are not open, the merge will start immediately. Otherwise, the merge will start the first time either the origin or snapshot LV are activated and both are closed. Merging a snapshot into an origin that cannot be closed, for example a root filesystem, is deferred until the next time the origin volume is activated. When merging starts, the resulting LV will have the origin's name, minor number and UUID. While the merge is in progress, reads or writes to the origin appear as being directed to the snapshot being merged. When the merge finishes, the merged snapshot is removed. Multiple snapshots may be specified on the command line or a @tag may be used to specify multiple snapshots be merged to their respective origin.
sudo umount /lv_name-mountpoint
sudo lvconvert --merge /dev/mapper/vg_name-lv_name_ss_at_date_220908
If it is a root filesystem, and the source logical volume is not operational at all, we need to boot in live Linux session in order to perform the steps, because it must be unmounted. It it is not a root filesystem, we need to unmount the logical volume first.
LvChange: Deactivate, reactivate and remount the LV
lvchange
– Change the attributes of logical volume(s).
-a|
,--activate y|n|ay
– Change the active state of LVs. An active LV can be used through a block device, allowing data on the LV to be accessed.y
makes LVs active, or available.n
makes LVs inactive, or unavailable…
sudo lvchange -an /dev/mapper/vg_name-lv_name
sudo lvchange -ay /dev/mapper/vg_name-lv_name
If we are working within a live Linux session – i.e. we are restoring a root filesystem, – these steps will be performed at the next reboot. Otherwise we need to run the following steps in order to apply the changes without reboot.
sudo mount -a # remount via /etc/fstab
df -h # list the mounted devices
Remove a Snapshot
lvremove
– Remove logical volume(s) from the system.
sudo lvremove /dev/vg_name/lv_name_ss_at_date_220908
sudo lvremove /dev/mapper/vg_name-lv_name_ss_at_date_220908
References
- Red Hat Docs: Red Hat > [9] > Configuring and managing logical volumes
- Learn Linux TV: Linux Logical Volume Manager (LVM) Deep Dive Tutorial
- Learn Linux TV at YouTube: Linux Logical Volume Manager (LVM) Deep Dive Tutorial
- Linux Hint: LVM: How to List and Selectively Remove Snapshots
- DigitalOcean: How To Use LVM To Manage Storage Devices on Ubuntu 18.04
- Ubuntu Community Wiki: Ubuntu Desktop LVM
- SleepLessBeastie's notes: How to fix device excluded by a filter