15.1 LVM

Logical Volume Management or LVM are to make the handeling of disks and partitions more flexible and be able to have potentialy more space then is physically possible with physical disks. With LVM you can combine 1 or several disks or partitions into a Volume Group or VG. This can be split (partitioned) up into Logical Volumes or LV.

The steps are as follows:

  1. Have or create partition or partitions we can use on a disk.

  2. Designate the disk as a LVM Physical Volume or PV

  3. Create a Volume Group or VG on the PV

  4. Create Logical Volumes, LV on the VG

  5. Set a Filesystem on the LV

  6. Mount or add in fstab

See the flow in the image below: disk_management2.png

1. Create a Partition

For us to designate a Physical Volume or PV, we need a block device. A block device is a kind of file which represents a device of some kind, with data that can be read or written to it in blocks. Usually also with the abilities to seek forward and backward for the data. You can find them at /dev. To summarize: disks and partitions are block devices.

In our case, we will make two partitions on /dev/vdc of 500 Megabyte, using the fdisk command.

fdisk /dev/vdc
n       - new partition
p       - primairy partition
[enter] - select logical next number
[enter] - select logical partition start sector
+500M    - 500 Megabyte in size
t       - to label the partion
8e      - selects LVM label
w       - write partition

Do this *twice!!

Refresh kernel block device registry:

partprobe

Now that we have a partition (two partitions of 500M), we can designate this as a physical volume or PV

2. Designate the disk as a Physical Volume

We can designate a block device as a Physical Volume with the pvcreate command:

pvcreate /dev/vdc1 /dev/vdc2

Result:

[root@rhcsa ~]# pvcreate /dev/vdc1 /dev/vdc2
  Physical volume "/dev/vdc1" successfully created.
  Physical volume "/dev/vdc2" successfully created.

This will have labeled the device as a Physical Volume, with the standard extend size 4.00 MiB per extend.

You can check the status of the pv with pvdisplay:

pvdisplay

Result:

[root@rhcsa ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/vdc1
  VG Name               plato
  PV Size               500.00 MiB / not usable 4.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              124
  Free PE               124
  Allocated PE          0
  PV UUID               Il1ImX-zOLu-G9gz-APwS-2SZ6-KWb0-vHE7y9
   
  --- Physical volume ---
  PV Name               /dev/vdc2
  VG Name               plato
  PV Size               500.00 MiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              124
  Free PE               124
  Allocated PE          0
  PV UUID               WsBS2y-x8fr-caYd-iFOA-G05q-NHwB-OM4H2y
   
  --- Physical volume ---
  PV Name               /dev/vda2
  VG Name               cl
  PV Size               <8.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              2047
  Free PE               0
  Allocated PE          2047
  PV UUID               aq53k3-yETG-pEsz-COhh-W902-2drm-vdOJPL

3. Create a Volume Group

Next up we need to create a new Volume Group of the Physical Volume or volumes.

We will create a Volume Group named plato: this is done with the vgcreate command.

vgcreate plato /dev/vdc1 /dev/vdc2

Result:

[root@rhcsa ~]# vgcreate plato /dev/vdc1 /dev/vdc2
  Volume group "plato" successfully created

This will create a Volume Group for us, on which we can now create Logical Volumes.

We can check the display status of the VG with vgdisplay.

vgdisplay plato
[root@rhcsa ~]# vgdisplay plato
  --- Volume group ---
  VG Name               plato
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               992.00 MiB
  PE Size               4.00 MiB
  Total PE              248
  Alloc PE / Size       0 / 980.00 MiB
  Free  PE / Size       248 / 12.00 MiB
  VG UUID               44avDc-4ODt-xaeZ-MGAL-tEzx-efEC-c2QWl2

4. Create Logical Volumes

We will create two Logical Volumes for us to experiment with: cave and phaedrus. This can be done with the lvcreate command. If we want to give a new name to the volume, we use the -n switch to allow for a name input. The -L input is for how big you want the volume to be in (M or G).

lvcreate -n cave -L 500M plato

Result:

[root@rhcsa ~]# lvcreate -n cave -L 500M plato
  Logical volume "cave" created.

This will create a device called /dev/plato/cave of 500M, currently without a file system on it.

Next up, we want to create phaedrus. But instead of determining the size, we want to create phaedrus by the amount of extends. 120 extends of 4MB; this should be about 480M (120 Extends * 4Mb = 480Mb). Extends are done with the small -l switch.

lvcreate -n phaedrus -l 120 plato

Result:

[root@rhcsa ~]# lvcreate -n phaedrus -l 120 plato
  Logical volume "phaedrus" created.

Let’s check our results with the (you might have guessed it) lvdisplay command:

lvdisplay plato

Result:

[root@rhcsa ~]# lvdisplay plato
  --- Logical volume ---
  LV Path                /dev/plato/cave
  LV Name                cave
  VG Name                plato
  LV UUID                h14a4t-kktR-Y5cp-DPT0-JDFe-GUeS-vNJVjW
  LV Write Access        read/write
  LV Creation host, time rhcsa.greateracademy.local, 2020-08-10 11:41:15 -0400
  LV Status              available
  # open                 0
  LV Size                500.00 MiB
  Current LE             125
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:3
   
  --- Logical volume ---
  LV Path                /dev/plato/phaedrus
  LV Name                phaedrus
  VG Name                plato
  LV UUID                LRrN1e-X7es-W0Bb-H22u-XZcS-vHnC-fjhl6D
  LV Write Access        read/write
  LV Creation host, time rhcsa.greateracademy.local, 2020-08-10 11:46:29 -0400
  LV Status              available
  # open                 0
  LV Size                480.00 MiB
  Current LE             120
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:4

Now that we have our Logical Volumes, the next step is to add filesystems on them.

5. Set a Filesystem on the LV

The next part is creating a filesystem on the Logical Volume so we can start using the space for files. We will make one ext4 and another xfs.

We can do this like other excersizes with the mkfs command. We will show you 2 methods:

mkfs.xfs /dev/mapper/plato-cave

Result:

[root@rhcsa ~]# mkfs.xfs /dev/mapper/plato-cave 
meta-data=/dev/mapper/plato-cave isize=512    agcount=4, agsize=32000 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=128000, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=1368, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

And the second example:

mkfs.ext4 /dev/plato/phaedrus

Result:

[root@rhcsa ~]# mkfs.ext4 /dev/plato/phaedrus 
mke2fs 1.44.3 (10-July-2018)
Discarding device blocks: done                            
Creating filesystem with 491520 1k blocks and 122880 inodes
Filesystem UUID: eb3056b5-b333-4149-b070-84d3fd14aea6
Superblock backups stored on blocks: 
	8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

Now that we added filesystems to the LV, we can now persistently mount them with /etc/fstab.

6. Mount or add in fstab

To add them to the /etc/fstab, we need to add the blockID to the fstab like we did before. We will add plato-cave to /mnt/cave and plato-phaedrus to /mnt/phaedrus.

Let’s make the directories for the mounts:

mkdir -p /mnt/cave ; mkdir -p /mnt/phaedrus

Now let’s add the blockid’s to the /etc/fstab.

First we create a backup:

cp /etc/fstab /etc/fstab.bak

Next we will push the cave and the phaedrus blkid’s into the /etc/fstab:

blkid | grep cave >> /etc/fstab; blkid | grep phaedrus >> /etc/fstab

Edit the /etc/fstab with vim:

vim /etc/fstab

It should look like this:

/dev/mapper/cl-root                             /               xfs     defaults        0 0
UUID=8503adac-d001-46c0-92b5-d2c14d46c916       /boot           ext4    defaults        1 2
/dev/mapper/cl-swap                             swap            swap    defaults        0 0
/dev/mapper/plato-cave: UUID="6711d453-c6cd-4b81-92c5-eafc877d9670" TYPE="xfs"
/dev/mapper/plato-phaedrus: UUID="eb3056b5-b333-4149-b070-84d3fd14aea6" TYPE="ext4"

Make the changes to make it look as following:

/dev/mapper/cl-root                             /               xfs     defaults        0 0
UUID=8503adac-d001-46c0-92b5-d2c14d46c916       /boot           ext4    defaults        1 2
/dev/mapper/cl-swap                             swap            swap    defaults        0 0
UUID=6711d453-c6cd-4b81-92c5-eafc877d9670       /mnt/cave       xfs     defaults        0 0
UUID=eb3056b5-b333-4149-b070-84d3fd14aea6       /mnt/phaedrus   ext4    defaults        0 0

We can now mount the devices using mount command:

mount -va

Result:

[root@rhcsa ~]# mount -va
/                        : ignored
/boot                    : already mounted
swap                     : ignored
mount: /mnt/cave does not contain SELinux labels.
       You just mounted an file system that supports labels which does not
       contain labels, onto an SELinux box. It is likely that confined
       applications will generate AVC messages and not be allowed access to
       this file system.  For more details see restorecon(8) and mount(8).
/mnt/cave                : successfully mounted
mount: /mnt/phaedrus does not contain SELinux labels.
       You just mounted an file system that supports labels which does not
       contain labels, onto an SELinux box. It is likely that confined
       applications will generate AVC messages and not be allowed access to
       this file system.  For more details see restorecon(8) and mount(8).
/mnt/phaedrus            : successfully mounted

If you wish to fix the selinux messages from your mount command:

restorecon -RFvv /mnt/

Result:

[root@rhcsa ~]# restorecon -RFvv /mnt/
Relabeled /mnt/phaedrus from system_u:object_r:unlabeled_t:s0 to system_u:object_r:mnt_t:s0
Relabeled /mnt/cave from system_u:object_r:unlabeled_t:s0 to system_u:object_r:mnt_t:s0

and lsblk to check the results of what you made:

lsblk

Result:

[root@rhcsa ~]# lsblk
NAME               MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda                  8:0    0    9G  0 disk 
├─vda1               8:1    0    1G  0 part /boot
└─vda2               8:2    0    8G  0 part 
  ├─cl-root        253:0    0  6.1G  0 lvm  /
  ├─cl-swap        253:1    0  924M  0 lvm  [SWAP]
vdb                  8:16   0   10G  0 disk 
vdc                  8:32   0    5G  0 disk 
├─vdc1               8:33   0  500M  0 part 
│ └─plato-cave     253:3    0  500M  0 lvm  /mnt/cave
└─vdc2               8:34   0  500M  0 part 
  ├─plato-cave     253:3    0  500M  0 lvm  /mnt/cave
  └─plato-phaedrus 253:4    0  480M  0 lvm  /mnt/phaedrus
sr0                 11:0    1 1024M  0 rom  

Now you have created, added and mounted all the devices. Well done!