RHCE Study Guide

This tutorial explains basic concepts of LVM in detail with examples including how to configure and manage LVM in Linux step by step.

Basic concepts of LVM

LVM stands for Logical Volume Manager. LVM is the modern way to manage the storage devices. Traditionally a hard disk is partitioned as per requirement and each partition is formatted with supported file system.

Linux LVM Example

We have already learned this approach in detail with examples in previous parts of this article.

This tutorial is the sixth part of our article “Linux Disk Management Explained in Easy Language with Examples”. You can read other parts of this article here.

Linux Disk Management Tutorial

This is the first part of this article. This part explains basic concepts of Linux disk management such as BIOS, UEFI, MBR, GPT, SWAP, LVM, RAID, primary partition, extended partition and Linux file system type.

Manage Linux Disk Partition with fdisk Command

This is the second part of this article. This part explains how to create primary, extended and logical partitions from fdisk command in Linux step by step with examples.

Manage Linux Disk Partition with gdisk Command

This is the third part of this article. This part explains how to create GPT (GUID partition table) partitions from gdisk command in Linux step by step with examples.

Linux Disk Management with parted command

This is the fourth part of this article. This part explains how to create primary, extended, logical and GPT partitions from parted command in Linux step by step with examples.

How to create SWAP partition in Linux

This is the fifth part of this article. This part explains how to create swap partition in Linux with examples including basic swap management tasks such as how to increase, mount or clear swap memory.

How to configure RAID in Linux Step by Step Guide

This is the last part of this article. This part explains basic concepts of RAID in detail with examples including how to configure and manage software RAID in Linux step by step.

This traditional approach puts several limitations on partition and file system such as we cannot add additional space in a partition which is filled up with data vice versa we cannot shrink extra space from a partition which is merely used.

LVM not only addresses all these issues but also provides several other advantages such as data snapshot which allows us to revert in previous stage if there is any failure. Let’s learn how LVM is different from classical disk management concept.

Unlike classical approach where file system is created in fixed partition, LVM creates file system in flexible logical volume. Since logical volumes are flexible in nature, we can add additional disk space in a volume which is filled up or we can shrink a volume which is merely used and has a plenty of free space. LVM works in three layers.

lvm logical layers

Bottom layer is built from available disk space. We can use any storage device for disk space for example we can use a partition form disk or can use an entire disk. This layer is known as physical volume.

Middle layer is the group of all physical volumes. This layer is known as volume group.

Top layer creates logical partition from volume group. These logical partitions are known as logical volume.

Linux LVM example

Let’s understand each layer in more details.

Physical volume

As we know physical volume is created from all available disk space. We can use any disk, RAID array, SAN disks or even a partition for disk space. When we add a disk or partition in physical volume, LVM constructs a data structures in it including a label and metadata information. The label includes information about UUID, device size and pointers. LVM stores label and metadata information in two places (in starting and in ending sectors of the device). To list all available physical volumes we can use pvs command.

pvs command

To get more detailed information about physical volume we can either use –v option with pvs command or can use pvdisplay command separately.

pvs -v command

As we can see in above output there is only one physical volume (/dev/sda2) available at this time. To view the synthetic overview of current storage configuration we can also use lsblk command.

lsblk command

As above output shows currently there are four SCSI disks (sda, sdb, sdc and sdd) attached with system. From them only sda disk is used for partitions. There are two partitions in sda; sda1 and sda2. Both partitions are created as Linux standard partition. First partition is used to store the boot partition while second partition is used to create a physical volume (/dev/sda2). Further three logical volumes (rhel-root, rhel-swap and rhel-home) are created from physical volume (/dev/sda2).

How to create a new physical volume

For this tutorial I assume that you have either non-partitioned disk space or have a separate disk for exercise. If you are using virtual program (such as VMware or Virtual Box) for the practice, I suggest you to add three new virtual hard disks. To learn how to add new virtual disk in VMware please see the first part of this tutorial.

For this exercise I have added three additional virtual disks in our system. Each disk is 2GB in size. We can list all attached hard disks with fdisk –l command.

fdisk -l

We will use two MBR partitions, two GPT partitions and one whole disk to create the physical volume.

Tips

I used three disks to demonstrate how different utilities are used to create LVM partitions. You do not need to have the same number of hard disks to follow this tutorial or to create the physical volume. You can even use a single partition to create the physical volume.

Creating 1GiB LVM partition with parted

Create a 1GiB MBR primary partition and set its type to LVM with following steps.

  • Run parted /dev/sdd command. This command will initiate parted command on /dev/sdd disk.
  • Use mklab command with msdos value to set the type of disk to MBR.
  • Use mkpart command to create a 1GiB partition.
  • Use set command with lvm on value to change the partition type to lvm.

Following figure illustrates above steps

create partition with parted command

To learn parted command and its sub command in detail see the fourth part of this article which explains parted command in detail with examples.

Creating 1 GiB LVM partition with fdisk command

Create a 1GiB logical partition and set its type to LVM with following command

  • Run fdisk /dev/sdd command to initiate fdisk utility.
  • Type n and press Enter key to create new partition.
  • Since logical partition can be created only in extended partition, we have to create extended partition first. Type e and press Enter key to create to extended partition.
  • Type 4 and press Enter key to put the extended partition in last at partition table.
  • Press Enter key to keep the default starting sector.
  • Type +1G and press Enter key to assign 1GiB space in this partition.
  • Type n again to create logical partition
  • Keep default logical partition number
  • Keep default starting sector and press Enter key
  • Keep default ending sector to assign all space in this partition and press Enter key

Following figure illustrates above steps

fdisk logical partition

At this time partition is marked as Linux Standard Type. Let’s change its type to LVM.

  • Type t and press Enter key
  • Type partition number which partition type we want to change
  • Type 8e and press Enter key
  • Type w to save the change and exit from fdisk

fdisk logical partition

Second part of this article explains fdisk command and its sub command in detail with examples.

Creating 1GiB LVM partition with gdisk command

Create a 1GiB GPT partition and set its type to LVM with following command

  • Run gdisk /dev/sdc command to initiate gdisk command.
  • Type n to create new partition
  • Keep default partition number
  • Use default starting point
  • Use +1G to assign 1GiB space in this partition
  • Type 8e00 to set partition type as LVM partition
  • Type p to verify the creation of partition

Following figure illustrate above steps

gdisk lvm partition

Use w command to save the changes, when asked type y to confirm the changes.

gdisk save partition

Second part of this article explains gdisk and its sub command in detail with examples.

Let’s create one more GPT partition with remaining space in /dev/sdc disk

create partition with gdisk command

Finally run parprobe command to update the kernel about this disk configuration change.

partprobe command

Let’s summaries what we have prepared so far for physical volumes

LVM device Disk Type LVM device Type Partition Type
/dev/sdb Blank Whole Disk None
/dev/sdc1 GPT GPT Partition Linux LVM
/dev/sdc2 GPT GPT Partition Linux Standard
/dev/sdd1 MBR Primary Partition Linux LVM
/dev/sdc5 MBR Logical Partition Linux LVM

We can use lsblk command again to verify the above configuration

lsblk command

That’s all setup we need to create the physical volume.

Tips

I created above configuration to explain how different disk utilities mark a partition which will be used in LVM. A partition with LVM marking can be added directly in volume group by skipping physical volume. I will explain this method in some other tutorial. For this tutorial I am following standard procedure where you must have to create physical volume before creating volume group. When we create physical volume by following standard procedure, the partition type marking (8e, 8e00, lvm set on) or hard disk type (GPT or MBR) do not make any difference. For LVM they all are the equal source of free space. The only thing which you should consider is that LVM cleans the source of space before putting its own label in it. It is just like formatting a disk or partition before using it, once formatted all data will be wiped out from it.

How to create LVM physical volume

Following command is used to create the LVM physical volume from available LVM devices.

#pvcreate –v [LVM devices]

pvcreate command

We can use pvs command again to verify the creation of physical volumes.

pvsdisplay command

Volume Group

Volume group is the second layer in LVM structure. It is built upon the physical layer. Basically volume group is the group of physical volumes. We can add physical volume in existing volume group or can create a new volume group for physical volume.

Following command is used to create a new volume group.

#vgcreate [volume group name] [physical volume name]

vgcreate command

To explain how to add a new physical volume in existing volume group I skipped one physical volume in above example.

To add a new physical volume in existing volume group following command is used

vgcreate command

To verify the creation of volume group we can use vgdisplay command

vgdisplay command

While creating a new volume group, LVM automatically divides all available space into smaller logical pieces known as Physical Extent. A PE is the smallest unit which can be allocated in logical volume. Default size of a PE is 4MB.

Logical Volume

This is the third and last layer in LVM. Logical volumes are just like regular partitions but have more advance features in comparison with regular partitions. A logical volume can be resized as per requirement. Unlike a regular partition which cannot be expanded outside the disk, a logical volume can be expanded in multiple disks.

Following command is used to create a new logical volume.

 #lvcreate –n [LV Name] –L [LV Size] [VG Name from which LV will create]
 

lvcreate command

If command detects any file system signature, it will confirm the action before creating the logical volume. Accept the change by typing y here.

Let’s create two more logical volumes

lvcreate command

We can verify the creation of Logical Volume with lvs command

lvcreate command

We cannot use a logical volume until it has a valid file system. A file system creates necessary data structure in logical volume for files and directories.

Formatting Logical Volumes

Following command is used to format a logical volume.

#mkfs –t [File System] /dev/[VG Name form which logical volume is created]/[LV name]

Formatting logical volume lv01rhcetest with ext3 file system

formatting logial volume with ext3

Formatting logical volume lv02rhcetest with ext4 file system

formatting with ext4

Formatting logical volume lv03rhcetest with xfs file system

fromatting with xfs file system

Logical volumes are ready to use. In order to use them we have to mount them somewhere in Linux file system. Linux file system (primary directory structure) starts with root (/) directory and everything goes under it or its subdirectories. We have to mount logical volumes somewhere under this directory tree. We can mount logical volumes temporary or permanently.

Temporary mounting LV (Logical volume)

Following command is used to mount the partition temporary.

#mount [what to mount] [where to mount]

Mount command accepts several options and arguments which I will explain separately in another tutorial. For this tutorial this basic syntax is sufficient.

what to mount :- This is the partition.

where to mount :- This is the directory which will be used to access the mounted resource.

Once mounted, whatever action we will perform in mounted directory will be performed in mounted resources. Let’s understand it practically.

  • Create a mount directory in / directory
  • Mount /dev/vg01rhcetest/lv01rhcetest logical volume
  • List the content
  • Create a test directory and file
  • List the content again
  • Un-mount the /dev/vg01rhcetest/lv01rhcetest logical volume and list the content again
  • Now mount the /dev/vg02rhcetest/lv02rhcetest logical volume and list the content
  • Again create a test directory and file. Use different name for file and directory
  • List the content
  • Un-mount the /dev/vg02rhcetest/lv02rhcetest logical volume and list the content again

Following figure illustrates this exercise step by step

temporary mounting logical volume

As above figure shows whatever action we performed in mount directory was actually performed in respective logical volumes.

Temporary mount option is good for logical volumes which we access occasionally. If we access LV on regular basis then this approach will not helpful. Each time we reboot the system all temporary mounted resources are get un-mounted automatically. So if you have a LV which is going to be used regularly, mount it permanently.

Mounting logical volume permanently

Each resource in file system has a unique ID called UUID. When mounting a LV permanently we should use UUID instead of its name. From version 7, RHEL also uses UUID instead of device name.

The UUID stands for Universally Unique Identifier. It is a 128-bit number, expressed in hexadecimal (base 16) format.

If you have a static environment, you may use device name. But if you have dynamic environment, you should always use UUID. In dynamic environment device name may change each time when system boot. For example we attached an additional SCSI disk in system; it will be named as /dev/sdb. We mounted this disk permanently with its device name. Now suppose someone else removed this disk and attached new SCSI disk in the same slot. New disk will also be named as /dev/sdb. Since name of old disk and new disk is same, new disk will be mounted at the place of old disk. This way, device name could create a serious problem in dynamic environment. But this issue can solve with UUID. No matter how we attach the resource with system, its UUID will remain always fix.

If you have static environment, you may consider device name to mount the partition. But if you have dynamic environment, you should always use UUID.

To know the UUID of all partitions we can use blkid command. To know the UUID of a specific partition we have to use its name as argument with this command.

blkid command

Once we know the UUID, we can use it instead of device name. We can also use copy and paste option to type the UUID.

  • Use blkid command with LV name to print the UUID of partition.
  • Copy the UUID of LV.
  • Use mount command to mount the LV. Use paste option instead of typing UUID.

Following figure illustrates above steps

mount partition through uuid

When system boots, it looks in /etc/fstab file to find out the devices (partitions, LVs or swap) which need to be mount in file system automatically. By default this file has entry for those partitions, logical volumes and swap space which were created during the installation. To mount any additional LV (Logical Volume) automatically we have to make an entry for that LV in this file. Each entry in this file has six fields.

default fstab file

Number Filed Description
1 What to mount Device which we want to mount. We can use device name, UUID and label in this filed to represent the device.
2 Where to mount The directory in main Linux File System where we want to mount the device.
3 File system File system type of device.
4 Options Just like mount command we can also use supported options here to control the mount process. For this tutorial we will use default options.
5 Dump support To enable the dump on this device use 1. Use 0 to disable the dump.
6 Automatic check Whether this device should be checked while mounting or not. To disable use 0, to enable use 1 (for root partition) or 2 (for all partitions except root partition).

Let’s make some directories to mount the LVs which we have created recently

mkdir rhacelab

Take the backup of fstab file and open it for editing

vim etc/fstab file

Make entries for logical volumes and save the file.

fstab uuid entry

For demonstration purpose I used both device name and UUID to mount the logical volumes. After saving always check the entries with mount –a command. This command will mount everything listed in /etc/fstab file. So if we made any mistake while updating this file, we will get an error as the output of this command.

If you get any error as the output of mount –a command, correct that before rebooting the system. If there is no error, reboot the system.

mount command

The df –h command is used to check the available space in all mounted partitions. We can use this command to verify that all partitions are mounted correctly.

df -h command

Above output confirms that we have successfully mounted all partitions.

Resizing Logical Volume

As I mentioned in starting of this tutorial logical volumes are flexible in nature. It means we can resize them easily. Following command is used to add additional space in LV.

#lvresize –L +[Space which we want to add] –r /dev/[VG where LV is created]/[LV Name]

Let’s fill-up the one LV with some dummy data to simulate the situation where LV is filled up with data. We can use dd command to copy the null bytes from zero device file to a test file located in LV.

dd command

To understand dd command in detail, see the third part of this article.

As we can see in above output LV (lv03) is running out the space. Let’s extend it by adding 1GB additional space.

lv resize

Tips

Additional space will be taken from volume group. So make sure you have sufficient free space in volume group. You can use vgdisplay or vgs command to view the available free space in volume group. If require, you can add additional space in volume group with vgextend command. If file system supports, you can shrink a logical volume which has additional free space. The ext3 and ext4 file system support shrink operation while the xfs file system does not support shrink operation.

Same command is used to shrink the extra space from logical volume except the sign of size. To reduce the size we have to use - (minus) sign instead of + (plus) sign.

#lvresize –L -[Space which we want to reduce] –r /dev/[VG where LV is created]/[LV Name]

This command will work only if file system supports. For example xfs file system does not support shrink option.

xfs lv reszie fail

The ext file system supports both options.

ext lvm reszie

How to delete logical volumes

We cannot delete a mounted logical volume. Un-mount all logical volumes which we created in this exercise

umount partition

Use following command to remove logical volume

#lvremove –f [Use if want to remove LV with permission]  /dev/[VG Name where LV was created]/[LV Name]

lvremove command

Once all logical volumes removed we can safely remove volume group with following command

#vgremove [VG name]

vgremove

After removing volume group use following command to remove LVM signature from physical volumes.

#pvremove [PV names]

pvremove command

Finally remove the mount directory and copy the original fstab file back.

If you haven’t taken the backup of original fstab file, remove all entries from this file which you made.

resotre fstab

Now reboot the system and use df –h command again to verify that all partitions which we created in this exercise are gone.

df -h command

LVM command cheat sheet

You may associate LVM commands with their functions. LVM structure builds with three layers Physical Volumes (pv), Volume Group (vg) and Logical Volume (lv). Usually commands used to manage the layer starts with respective initials for example commands used to manage the physical volume starts with pv. Now merge initial with operation to get the command.

Layer Initial Operation Command Description
Physical Layer pv s (Show) pvs List all physical volume.
Volume Group vg s (Show) vgs List all volume group
Logical Volume lv s (Show) lvs List all logical volume
Physical Layer pv create pvcreate Create physical volume from supplied disk or partition
Volume Group vg create vgcreate Create volume group from specified physical volume
Logical Volume lv create lvcreate Create logical volume from specified volume group
Physical Layer pv display pvdisplay Display physical volume
Volume Group vg display vgdisplay Display volume group
Logical Volume lv display lvdisplay Display logical volume
Physical Layer pv resize pvresize Resize a physical volume
Volume Group vg resize vgresize Resize a volume group
Logical Volume lv resize lvresize Resize a logical volume
Physical Layer pv remove pvremove Remove a disk or partition from LVM
Volume Group vg remove vgremove Remove volume group
Logical Volume lv remove vgremove Remove a logical volume
  • You can use –f switch in command to perform an action without confirmation.
  • You can use –v switch to print real time action in output.

That’s all for this part. In next part we will learn how to create and manage RAID Array.

Share this Share This Article with Friends

Stay updateStay Update With US

More Articles For YouYou May Also Like