Using LVM disk storage layout on Ubuntu Server.
Beside the classic disk storage layout using partitions, Linux operating systems include the possibility to use the logical volume manager (LVM), that allows to create logical volumes out of one or multiple physical fixed disks. LVM volumes can be created on both software RAID partitions and standard partitions. Such volumes can be extended, giving greater flexibility to systems as requirements change. This tutorial shows, how to choose LVM disk storage layout at installation time of Ubuntu Server 20.04 LTS and how to add a second fixed disk to the LVM system, later.
When installing Ubuntu Server, you can choose to use LVM by selecting the Set up this disk as an LVM group checkbox of the Use an entire disk option on the Guided storage configuration page.
In an LVM context:
- A physical volume (PV) is a physical fixed disk, disk partition or software RAID partition formatted as LVM PV. On the screenshot below (showing the storage configuration as it will be after installation of the operating system), there is one PV, corresponding to /dev/sda3 (the first two partitions on the disk being used for booting the system (future mount points /boot/efi and /boot).
- A volume group (VG) is made from one or more physical volumes. After having been created, it can be extended by adding more PVs (cf. Extending an LVM group). A VG is like a virtual disk drive, from which one or more logical volumes are carved. The screenshot shows that the setup program will create an LVM volume group, called ubuntu-vg (made from /dev/sda3).
- A logical volume (LV) is similar to a partition in a non-LVM system. A LV is formatted with the desired file system (EXT3, EXT4, XFS, JFS, etc) and is then available for mounting and data storage. In our case the logical volume, called ubuntu-lv, corresponds to the entire space of the volume group (virtual disk) ubuntu-vg, i.e. to the physical volume /dev/sda3. It will be formatted using the EXT4 filesystem and mounted as /.
There are some useful utilities to view information about LVM: pvdisplay shows information about physical volumes; vgdisplay shows information about volume groups and lvdisplay shows information about logical volumes. On the screenshots below (taken after the installation of Ubuntu Server was completed), we can see
- The physical volume /dev/sda3, 18.50 GB in size, that is part of the LVM group ubuntu-vg.
- The volume group ubuntu-vg that is made from one 18.50 GB area, and being extendable.
- The logical volume ubuntu-lv, that is a 18.50 GB "partition" of the volume group ubuntu-vg (the LV path being: /dev/ubuntu-vg/ubuntu-lv).
My Ubuntu Server VMware virtual machine actually has 2 fixed disks and the following part of the tutorial shows how to do to add the second disk to the volume group ubuntu-vg in order to get a logical disk made of two areas: the third partition of the first disk (/dev/sda3) and the whole second disk (/dev/sdb). This operation is done in 4 steps:
- Create a physical volume.
- Extend the volume group (add the physical volume to the volume group).
- Extend the logical volume.
- Extend the logical volume's filesystem.
The creation of the physical volume is done by the command
sudo pvcreate /dev/sdb
You should be careful when doing this with /dev/sdb containing data; it could result in data loss!
The extension of the volume group ubuntu-vg, adding the newly created volume, is done by the command
sudo vgextend ubuntu-vg /dev/sdb
The following screenshot shows the volume group information after the volume group extension has been done. You can see that the VG now has two areas: 4735 allocated physical extends (corresponding to the 18.50 GB /dev/sda3) and 5119 free physical extends (corresponding to the 20.00 GB /dev/sdb). This is similar to a disk with an 18.50 GB partition and 20 GB of free (unallocated) space.
A logical volume is similar to a partition in a non-LVM system, we said. Thus, after having extended the volume group, we'll have to extend the logical volume
ubuntu-lv:
sudo lvextend /dev/ubuntu-vg/ubuntu-lv −l +5119
When using the lvextend command, we have to specify, beside the name of the logical volume, the size by which the LV has to be extended. This is done here by using the -l option, that needs a physical extend (PE) value as argument (another possibility is to use the -L option, that needs a size value in MB, GB...) The PE value used here (5119) corresponds to the size of the entire free PE available (what actually corresponds to the entire disk /dev/sdb).
The following screenshots show the LVM information after the logical volume extension has been done: (1) the two physical volumes /dev/sda3 and /dev/sdb; (2) the volume group ubuntu-vg, that is made of two areas, that now, from a logical point of view, form one big area of 4735 + 5119 = 9854 PE, or 18.50 + 20.00 = 38.50 GB (in fact 38.49 GB because of some not usable MB); (3) the logical volume ubuntu-lv, that may be seen as a partition of 9854 extends (38.49 GB).
So far done, the space added to the logical volume isn't usable because it hasn't yet been formatted. This may be done by extending the EXT4
filesystem using the command:
sudo resize2fs /dev/ubuntu-vg/ubuntu-lv
Note, that there is a certain risk when doing this without unmounting the logical volume. However, with the EXT4 filesystem, it normally succeeds without any problems.
To terminate the tutorial, here a screenshot of how all this looks in the display of the partitioning tool fdisk. The screenshot shows, among others, the two (physical) fixed disks /dev/sda and /dev/sdb, the first one being subdivided into 3 partitions, as well as the 38.50 GB logical LVM volume.
If you find this text helpful, please, support me and this website by signing my guestbook.