As part of the RHCSA exam preparation, we already learned how to manage partitions on disk. Partitions are useful to separate disk space (for example, separating database-related files from webserver-related files), but we have a much more flexible solution that can separate or aggregate storage space.
This solution is called LVM, the Logical Volume Manager. LVM allows us to see multiple disks as one filesystem, thus overcoming the limitations of a physical disk’s site. We can also create software mirroring on disks to protect or data written to the filesystem. In this tutorial we’ll cover the basics: we’ll manage the three layers of LVM, physical volumes, volume groups and logical volumes.
In this tutorial you will learn:
- How to create and remove physical volumes
- How to assign physical volumes to volume groups
- How to create and delete logical volumes
Software Requirements and Conventions Used
Category | Requirements, Conventions or Software Version Used |
---|---|
System | Red Hat Enterprise Linux 8.1 |
Software | LVM 2.03 |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions | # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux commands to be executed as a regular non-privileged user |
With LVM, we have three layers that build on top of each other: physical volumes
(pv), which are much like partitions, only marked for LVM usage. We can create a physical volume from a disk partition or a whole raw disk. If we intend to use a disk as LVM physical volume only, we don’t need to create partition on it first. The next layer is a volume group
(vg). As the name suggests, it is simply the grouping of physical volumes. At this layer we can aggregate all underlying physical volume’s space into a bigger storage unit. On top of volume groups we can create the final layer, logical volumes
(lv). These can hold filesystems, in which we can store our data.
One of the main features of LVM is that we can add another physical volume to a volume group on the fly, and we can also modify a logical volume’s size while the system is running. So if we run out of space, we can scale without shutting down a system.
The abbreviations of the layers are good reminders when we deal with LVM. The commands that work on different layers all start with these abbreviations, for example, if we work with physical volumes, the command to create one will be pvcreate
, the command to list existing physical volumes will be pvdisplay
, and so on. Even if we need to do this infrequently, it is easy to guess or find the right command, as this naming convention is consistent across all layers.
For this tutorial, we added to disks, 2 GB is size each to our lab machine. They are recognized as /dev/sdb
and /dev/sdc
on our system, while /dev/sda
holds the operating system, which we will not touch during the following steps.
How to create and remove physical volumes
How to create physical volume
To create a physical volume, we’ll use pvcreate
. We’ll create a 2 GB physical volume on both sdb
, and sdc
.
# pvcreate /dev/sdb Physical volume "/dev/sdb" successfully created. # pvcreate /dev/sdc Physical volume "/dev/sdc" successfully created.
We can check if we did it right by listing our physical volumes. We’ll use pvdisplay
.
# pvdisplay --- Physical volume --- PV Name /dev/sda2 VG Name rhel PV Size <14.00 GiB / not usable 3.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 3583 Free PE 0 Allocated PE 3583 PV UUID rfezEa-GlgW-jWUX-Zixs-Ydw0-EsZS-nk3JDH "/dev/sdb" is a new physical volume of "2.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb VG Name PV Size 2.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID V2PUv4-O9eQ-jFvQ-UmQf-dmq3-caF9-xmxYGj "/dev/sdc" is a new physical volume of "2.00 GiB" --- NEW Physical volume --- PV Name /dev/sdc VG Name PV Size 2.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID PWTs7Q-x3wD-RyXj-ertn-Sh2N-CiXb-ayDc1q
The lab machine already uses LVM in it’s default setup (as do most distributions nowdays), this is why /dev/sda2
is shown. We are interested in our two new physical volumes, which we can now verify.
How to remove physical volume
To delete a physical volume, we have the pvremove
command. Do not remove a physical volume with data written on it that is needed.
# pvremove /dev/sdc Labels on physical volume "/dev/sdc" successfully wiped.
How to assign physical volumes to volume groups
After re-creating the dropped sdc
physical volume, we have all we need to create a volume group that can hold a logical volume. We could create a volume “group” that has only one physical volume – which is the exact case of the default sda2
– but to demonstrate storage aggregation, we’ll use both our new physical volumes to create a volume group called “data” that has 4 GB of disk space available.
We can create a volume group with vgcreate
. All we need is specifying the VG name, and the physical volumes which will be members of the volume group.
# vgcreate data /dev/sdb /dev/sdc Volume group "data" successfully created
We can list our new volume group’s properties with vgdisplay
.
# vgdisplay data --- Volume group --- VG Name data System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 1 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 2 Act PV 2 VG Size 3.99 GiB PE Size 4.00 MiB Total PE 1022 Alloc PE / Size 0 / 0 Free PE / Size 1022 / 3.99 GiB VG UUID CmM07M-16Ys-PZx2-XGvo-N1j3-nINX-fnIFIA
Note the VG size of close to 4 GB, the sum of the two physical volumes. It will not be exactly the sum of the volumes, as some space is reserved for metadata.
How to create and delete logical volumes
On top of a volume group, we can create logical volumes which in turn can hold filesystems. The default installation on many distributions separate storage this way, for example, the user’s data is held on the /home
logical volume, and system logs are stored on the /var
volume. In the event a user fills the home volume, the overall system will still be able to write logs, and thus it’s services will continue to run while the sysadmin can handle the user that gone wild.
How to create a logical volume
We’ll create two logical volumes, the first called “database” with 2 GB of size, the other called “webserver” with 500 MB of disk space. As you could guess from the above examples, the command we’ll use is lvcreate
.
# lvcreate -L 2G -n database data Logical volume "database" created. # lvcreate -L 500M -n webserver data Logical volume "webserver" created.
We can display our volume properties with lvdisplay <volume path>
, or we can list all of our logical volumes if we do not provide an argument for lvdisplay
. While this may provide a lenghtly output on some systems, it is useful in an unknown environment, as the volume paths may vary depending on distribution and version.
# lvdisplay /dev/data/database --- Logical volume --- LV Path /dev/data/database LV Name database VG Name data LV UUID D7f9An-G0dd-kEGw-OGrP-HZlA-dQlX-yBbQbi LV Write Access read/write LV Creation host, time rhel8rhcsa, 2019-12-28 16:53:24 +0100 LV Status available # open 0 LV Size 2.00 GiB Current LE 512 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:2 # lvdisplay /dev/data/webserver --- Logical volume --- LV Path /dev/data/webserver LV Name webserver VG Name data LV UUID 7Ldt79-aw0i-Oydm-4d0I-JaVe-Zd8m-xKpvrD LV Write Access read/write LV Creation host, time rhel8rhcsa, 2019-12-28 16:53:38 +0100 LV Status available # open 0 LV Size 500.00 MiB Current LE 125 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:3
If we check our volume group at this point, we can notice the space that the logical volumes using up from the volume group.
# vgdisplay data --- Volume group --- VG Name data System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 5 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 3.99 GiB PE Size 4.00 MiB Total PE 1022 Alloc PE / Size 637 / <2.49 GiB Free PE / Size 385 / 1.50 GiB VG UUID CmM07M-16Ys-PZx2-XGvo-N1j3-nINX-fnIFIA
How to delete a logical volume
To free up space, or reorganize volumes, we can drop logical volumes with lvremove
.
Do not remove logical volumes that contain valuable data. Doing so will destroy data on the volume.
There are safety belts built in, gone are the days when wiping our system did not have to wait for a prompt. At least, when using the LVM commands.
# lvremove /dev/data/webserver Do you really want to remove active logical volume data/webserver? [y/n]: y Logical volume "webserver" successfully removed
Another volume group listing shows that the 500 MB of the webserver
volume is now added back to the free size of the volume group:
# vgdisplay data --- Volume group --- VG Name data System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 6 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 0 Max PV 0 Cur PV 2 Act PV 2 VG Size 3.99 GiB PE Size 4.00 MiB Total PE 1022 Alloc PE / Size 512 / 2.00 GiB Free PE / Size 510 / 1.99 GiB VG UUID CmM07M-16Ys-PZx2-XGvo-N1j3-nINX-fnIFIA
Exercises
- Create two volume groups, and create logical volumes on both of them with the same name.
- Add another physical volume to your volume group, then create a logical volume that occupies the whole volume group. Now try to remove one of the physical volumes.
- Try to create a logical volume that has the exact size of the volume group.
- Create another physical volume on a disk, add it to your volume group, then shut down the machine and remove the disk. What does
vgdisplay
say on the missing disk? - Add the previously removed disk back again. Is it recognized on next boot?