How To Install LVM

You are here:
< All Topics

LVM or Logical Volume Manager is a disk management system used on Linux systems.

 

It provides an easy way to add, remove, and resize disk drive partitions without having to lose data. One of the biggest advantages of LVM is that you can carry out these operations without having to reboot.

 

LVM operates by creating a layer of abstraction between the operating system and the disks or existing disk partitions. You assign your drives to LVM, creating “volume groups” (VGs) and then create LVM partitions known as “logical volumes” (LVs) according to your requirements.

 

Another advantage of LVM is that the logical volumes you create can span more than one disk, something which isn’t possible with conventional hardware drive level partitioning.

 

LVM presents these logical volumes to the operating system in just the same way as conventional hard drives.

 

LVM also gives you the option to create snapshots of your logical volumes without having to first unmount the disk.

 

 

 

To Install LVM

 

LVM is included in many distributions as standard. If it isn’t on your Ubuntu or Debian-derived system, you can install it with:

 

apt-get install lvm2

 

Next, create a partition of type LVM on the disk you wish to use for LVM, using fdisk. This can also be done with gparted, but here we are using fdisk.

 

Note: Always check the precise /dev/sdX drive designations before executing these commands, as these will probably be different to your configuration!

 

root@len:/dev/mapper# fdisk -l /dev/sdb
Disk /dev/sdb: 4.56 TiB, 5000947302400 bytes, 9767475200 sectors
Disk model: My Passport 2627
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 93065373-F0C6-4DE0-ABF4-D2326AAD77AF

Device Start End Sectors Size Type
/dev/sdb1 2048 9767475166 9767473119 4.6T Linux LVM
root@len:/dev/mapper#

 

You assign the whole disk (or that part of it you want to use for lvm) to LVM as a physical volume

 

You can scan the system for block devices that LVM can see and manage by typing:

 

lvmdiskscan

 

The output will display all available block devices that LVM can interact with:

 

root@yoga:/home/kevin# lvmdiskscan
/dev/nvme0n1 [ <476.94 GiB]
/dev/nvme0n1p1 [ 260.00 MiB]
/dev/sdb1 [ <4.55 TiB]
/dev/nvme0n1p2 [ 16.00 MiB]
/dev/nvme0n1p3 [ <85.07 GiB]
/dev/nvme0n1p4 [ 1000.00 MiB]
/dev/nvme0n1p5 [ 390.62 GiB]
0 disks
7 partitions
0 LVM physical volume whole disks
0 LVM physical volumes
root@yoga:/home/kevin#

 

Note that at this point, LVM does not know about the new LVM disk. We must first create the physical LVM volume. This is in addition to the fdisk partition creation with t type lvm as above – dont confuse the two.

 

Define the device/s you want LVM to use with pvcreate:

 

root@len:/media/kevin# pvcreate /dev/sdb1
Physical volume “/dev/sdb1” successfully created.

 

This writes an LVM header to the devices to indicate that they are ready to be added to a volume group.

 

you can also check with:

 

root@yoga:/home/kevin# lvmdiskscan
/dev/nvme0n1 [ <476.94 GiB]
/dev/nvme0n1p1 [ 260.00 MiB]
/dev/sdb1 [ <4.55 TiB] LVM physical volume
/dev/nvme0n1p2 [ 16.00 MiB]
/dev/nvme0n1p3 [ <85.07 GiB]
/dev/nvme0n1p4 [ 1000.00 MiB]
/dev/nvme0n1p5 [ 390.62 GiB]
0 disks
7 partitions
0 LVM physical volume whole disks
1 LVM physical volumes
root@yoga:/home/kevin#

 

You can quickly verify that LVM has registered the physical volumes by typing:

 

root@len:/media/kevin# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvm2 — <4.55t <4.55t
root@len:/media/kevin#

 

Next add the LVM physical volume to a volume group:

 

root@len:/media/kevin# vgcreate lvmvolgroup /dev/sdb1
Volume group “lvmvolgroup” successfully created

 

root@len:/media/kevin# vgs
VG #PV #LV #SN Attr VSize VFree
lvmvolgroup 1 0 0 wz–n- <4.55t <4.55t

 

root@len:/media/kevin# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvmvolgroup lvm2 a– <4.55t <4.55t
root@len:/media/kevin#

 

You can see the physical group exists and is assigned to the volume group

 

If you had for example 2 physical volumes instead of just one and had assigned them to this volume group, then you would see PV = 2 in the above output of vgs

 

You can now create logical volumes according to your needs from this volume group pool of storage space.

 

To create logical volumes, use the lvcreate command. To specify the size directly, use the -L option. If you wish to specify the size in terms of the number of extents, you use the -l option.

 

So to create a 1TB logical volume, with the name PRIMARY_MEDIA, provisioned from the volume group lvmvolgroup, plus two logical volumes of 250GB named PRIMARY_ARCHIVE and 150GB named PRIMARY_BACKUP we enter:

 

root@len:/media/kevin# lvcreate -L 1000G -n PRIMARY_MEDIA lvmvolgroup
Logical volume “PRIMARY_MEDIA” created.
root@len:/media/kevin# lvcreate -L 250G -n PRIMARY_ARCHIVE lvmvolgroup
Logical volume “PRIMARY_ARCHIVE” created.
root@len:/media/kevin# lvcreate -L 150G -n PRIMARY_BACKUP lvmvolgroup
Logical volume “PRIMARY_BACKUP” created.
root@len:/media/kevin#

 

Check with:

 

root@len:/media/kevin# vgs -o +lv_size,lv_name
VG #PV #LV #SN Attr VSize VFree LSize LV
lvmvolgroup 1 3 0 wz–n- <4.55t 3.18t 1000.00g PRIMARY_MEDIA
lvmvolgroup 1 3 0 wz–n- <4.55t 3.18t 250.00g PRIMARY_ARCHIVE
lvmvolgroup 1 3 0 wz–n- <4.55t 3.18t 150.00g PRIMARY_BACKUP

 

Now that we have logical volumes, we can use them as normal block devices.

 

The logical devices are available within the /dev directory just like other storage devices. You can access them in two places:

 

/dev/volume_group_name/logical_volume_name

 

/dev/mapper/volume_group_name-logical_volume_name

 

The next step is to format the logical volumes we just created.

 

So to format our four logical volumes with the Ext4 filesystem, we can type:

 

mkfs.ext4 /dev/lvmvolgroup/PRIMARY_MEDIA

mkfs.ext4 /dev/lvmvolgroup/PRIMARY_ARCHIVE

mkfs.ext4 /dev/lvmvolgroup/PRIMARY_BACKUP

 

root@len:/media/kevin# mkfs.ext4 /dev/lvmvolgroup/PRIMARY_MEDIA

 

mke2fs 1.45.5 (07-Jan-2020) Creating filesystem with 262144000 4k blocks and 65536000 inodes Filesystem UUID: 136f78a6-0aaf-47fc-9509-db182db94c41

 

Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 102400000, 214990848

Allocating group tables: done Writing inode tables: done

Creating journal (262144 blocks): done

Writing superblocks and filesystem accounting information: done

 

root@len:/media/kevin# mkfs.ext4 /dev/lvmvolgroup/PRIMARY_ARCHIVE

mke2fs 1.45.5 (07-Jan-2020) Discarding device blocks: done

Creating filesystem with 65536000 4k blocks and 16384000 inodes Filesystem UUID: 3ce12992-7189-4e6e-8088-b41201b88efc

Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424, 20480000, 23887872

Allocating group tables: done Writing inode tables: done

Creating journal (262144 blocks): done Writing superblocks and filesystem accounting information: done

 

root@len:/media/kevin# mkfs.ext4 /dev/lvmvolgroup/PRIMARY_BACKUP mke2fs 1.45.5 (07-Jan-2020) Discarding device blocks: done

Creating filesystem with 39321600 4k blocks and 9830400 inodes Filesystem UUID: 923c6784-67bc-4a8a-8280-d3dfac3c48d1

Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424, 20480000, 23887872

Allocating group tables: done Writing inode tables: done

Creating journal (262144 blocks): done Writing superblocks and filesystem accounting information: done

 

You can then mount the newly created and formatted logical volumes:

 

root@yoga:/home/kevin# mount -t ext4 /dev/lvmvolgroup/PRIMARY_MEDIA /media/kevin/PRIMARY_MEDIA

 

Check with df:

 

/dev/mapper/lvmvolgroup-PRIMARY_MEDIA 1031069848 77852 978546812 1% /media/kevin/136f78a6-0aaf-47fc-9509-db182db94c41

/dev/mapper/lvmvolgroup-PRIMARY_ARCHIVE 256981444 61468 243796392 1% /media/kevin/3ce12992-7189-4e6e-8088-b41201b88efc

/dev/mapper/lvmvolgroup-PRIMARY_BACKUP 153769424 61468 145827252 1% /media/kevin/923c6784-67bc-4a8a-8280-d3dfac3c48d1

 

In the device directory /dev/mapper/ you will see the following devices created:

 

root@len:/home/kevin# ll /dev/mapper

 

total 0 drwxr-xr-x 2 root root 140 Aug 6 23:00 ./

drwxr-xr-x 23 root root 4920 Aug 6 23:00 ../ crw——- 1 root root 10, 236 Aug 7 2020 control

lrwxrwxrwx 1 root root 7 Aug 7 2020 lvmvolgroup-PRIMARY_ARCHIVE -> ../dm-1

lrwxrwxrwx 1 root root 7 Aug 6 23:00 lvmvolgroup-PRIMARY_BACKUP -> ../dm-2
lrwxrwxrwx 1 root root 7 Aug 7 2020 lvmvolgroup-PRIMARY_MEDIA -> ../dm-0

 

To remove an LVM volume group

 

Make sure that no logical volumes are present in the volume group, see later section for how to do this.

 

Next, deactivate the volume group:

 

vgchange -a n my_volume_group

 

Now you actually remove the volume group:

 

vgremove my_volume_group

 

To resize an LVM logical volume

 

To enlarge a logical volume

 

First perform a file system check with e2fsck.

 

e2fsck -f </dev/volume_group/logical_volume>

 

then:

 

lvextend -L +100g /dev/lvmvolgroup/PRIMARY_MEDIA
resize2fs /dev/lvmvolgroup/PRIMARY_MEDIA

 

To reduce the size of a logical volume

 

There are two ways to use the command lvreduce:

 

To reduce to 50G

 

lvreduce -L 50G </dev/vg/disk-name>

resize2fs </dev/vg/disk-name>

 

To reduce by 50G

 

lvreduce -L -50G </dev/vg/disk-name>

resize2fs </dev/vg/disk-name>

Disclosure: The above article may include affiliate links for products and services for which this site may receive remuneration.
Table of Contents