RHCSA - Configure Local Storage: Create & Delete Logical Volumes
In LVM, physical storage devices such as hard drives or solid-state drives are combined into Volume Groups (VGs). These VGs are then further divided into Logical Volumes. Logical Volumes function as virtual partitions, offering flexibility and dynamism in managing storage space. They can be resized, extended, or reduced in real-time, providing a dynamic approach to storage allocation and allowing administrators to efficiently manage storage resources without disrupting the underlying data. Logical Volumes play a crucial role in LVM's ability to abstract the physical storage from the logical storage layer, making it easier to manage complex storage configurations and enabling features such as snapshotting, mirroring, and striping.
Lesson Setup
To actively participate in the exercises, make sure to attach an additional empty disk to your system. While the exercises assume that the additional disk is 1G in size, feel free to use whatever disk size is available to you.
Do not use a disk with data on as following the exercises will mean that data will be deleted.
Once the disk is attached, you should be able to see it by running the lsblk
command, which in this example shows as the sdb
device:
After rebooting, the disk may not retain the same device name, making it essential to always run lsblk
to verify that you are working with the correct disk.
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 600M 0 part /boot/efi
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 17G 0 part
├─os_vg-root 253:0 0 15G 0 lvm /
└─os_vg-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 1G 0 disk
You will now need to create a volume group using the /dev/sdb
device so that you can practice creating and deleting logical volumes.
Wipe the /dev/sdb
disk clean if it has been used in previous lesson:
sudo wipefs --all /dev/sdb*
sudo sed -i '/\/dev\/sdb/d' /etc/lvm/devices/system.devices
Create volume group logical_vg
using disk /dev/sdb
:
sudo vgcreate logical_vg /dev/sdb
Physical volume "/dev/sdb" successfully created.
Volume group "logical_vg" successfully created
Confirm the volume group exists:
sudo vgs
VG #PV #LV #SN Attr VSize VFree
logical_vg 1 0 0 wz--n- 1020.00m 1020.00m
Creating Logical Volumes
The lvcreate
commands is used to create a logical volume within a volume group. The -L
flag is used to specify the size, and unlike vgcreate
a flag is required when specifying the name you want the logical volume to be called (-n
).
Create a logical volume of size 100MB called first_lv
within volume group logical_vg
:
sudo lvcreate -L 100MB -n first_lv logical_vg
Logical volume "first_lv" created.
Confirm the logical volume was created using lvs
:
sudo lvs
The output shows the volume group of the logical volume and the size of the logical volume:
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
first_lv logical_vg -wi-a----- 100.00m
Create a second logical volume of size 200MB called second_lv
within volume group logical_vg
:
sudo lvcreate -L 200MB -n second_lv logical_vg
Logical volume "second_lv" created.
Confirm the logical volume was created using lvs
:
sudo lvs
The output shows there are now two logical volumes present:
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
first_lv logical_vg -wi-a----- 100.00m
second_lv logical_vg -wi-a----- 200.00m
Create a third logical volume of size 300MB called third_lv
within volume group logical_vg
:
sudo lvcreate -L 300MB -n third_lv logical_vg
Logical volume "third_lv" created.
Confirm the logical volume was created using lvs
:
sudo lvs
The output shows there are now two logical volumes present:
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
first_lv logical_vg -wi-a----- 100.00m
second_lv logical_vg -wi-a----- 200.00m
third_lv logical_vg -wi-a----- 300.00m
Deleting Logical Volumes
To delete logical volumes you use the lvremove
command. You can use the -y
flag to automatically answer yes to the confirmation message.
In these exercises the logical volumes are not mounted anywhere, but if they were, you would want to ensure they are not in use by unmounting them from the filesystem. The -f
flag to lvremove
would force the removal though.
Delete logical volume third_lv
from volume group logical_vg
:
sudo lvremove -y logical_vg/third_lv
Logical volume "third_lv" successfully removed.
Confirm the deletion using the lvs
command:
sudo lvs
The output shows there are now two logical volumes present:
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
first_lv logical_vg -wi-a----- 100.00m
second_lv logical_vg -wi-a----- 200.00m
Delete all logical volumes from volume group logical_vg
in one go:
sudo lvremove -y logical_vg
Logical volume "first_lv" successfully removed.
Logical volume "second_lv" successfully removed.
Confirm the logical_vg
volume group contains no logical volumes with the vgs
command:
sudo vgs
The #LV column shows there are no logical volumes within the volume group:
VG #PV #LV #SN Attr VSize VFree
logical_vg 1 0 0 wz--n- 1020.00m 1020.00m