Note: This is an RHCSA 7 exam objective.
Prerequisites
Before dealing with physical volumes, volume groups and logical volumes, you need to know how to create partitions on disk.
LVM Presentation
LVM stands for Logical Volume Manager. It’s the system component that is able to deal with partitions in a modern way.
Traditionally, Unix disks were divided into slices called partitions. It was possible to decide the size of each partition but, once done, you couln’t change it without destroying all data on it.
With LVM, you can allocate disk space in a flexible manner.
LVM relies on three main concepts:
- physical volume or pv: a pv is a whole disk or a partition of a disk,
- volume group or vg: a vg contains one or more pv; a pv can only belong to one vg,
- logical volume or lv: a lv represents a portion of a vg; a lv can only belong to one vg; it’s on a lv that you can create a file system.
LVM brings flexibility compared to the previous system only based on partitions:
- you can easily increase the size of a logical volume if you’ve got some space available in the associated volume group,
- if you don’t have any available space anymore, you can add a new disk as a physical volume, add it to the volume group and increase the size of your logical volume: the logical volume will span two or more disks without any additional operations,
- creating or extending logical volumes don’t require any downtime.
Configuration Report
Before starting any operation, you need to know the current configuration. Type:
# lsblk -a NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT vda 252:0 0 6G 0 disk ├─vda1 252:1 0 390M 0 part /boot └─vda2 252:2 0 5.5G 0 part ├─rhel-swap 253:0 0 552M 0 lvm [SWAP] └─rhel-root 253:1 0 3G 0 lvm /
With the lsblk command, you get quickly the following information:
- it is a virtual machine (vda, sda would indicate a physical server),
- there is only one disk /dev/vda of 6GB,
- the disk is divided into two partitions (vda1 and vda2) respectively with a size of 390MB and 5.5GB,
- the vda1 partition is mounted under /boot,
- the vda2 partition consists in two logical volumes (lvm) swap and root in a volume group called rhel,
- the swap logical volume is used by the system as a swapping area ([SWAP]) of 552MB,
- the root logical volume is mounted under / with a size of 3GB,
- there is around 2GB of free available space (5.5GB–552MB–3GB=2GB) in the vda2 partition,
- none of the partitions are in Read-Only mode (RO=0) or ReMovable (RM=0).
Note1: lsblk should be the first command to type when dealing with partitions.
Note2: By default, the lsblk command doesn’t display empty devices. The -a option corrects this problem.
Physical Volume Management
To create a physical volume (here /dev/vda), type:
# pvcreate /dev/vda
To remove a physical volume not belonging to any volume group (here /dev/vda), type:
# pvremove /dev/vda
To get the list of the physical volumes, type:
# pvs
Volume Group Management
To create a volume group (here called vg using a physical volume /dev/vda) with a physical extent size of 8MB, type:
# vgcreate -s 8m vg /dev/vda
To add a physical volume (here /dev/vdb) to an existing volume group (here vg), type:
# vgextend vg /dev/vdb
To remove a physical volume (here /dev/vdb) from an existing volume group (here vg), type:
# vgreduce vg /dev/vdb
To remove an existing volume group (here called vg), type:
# vgremove vg
To get the list of the volume groups, type:
# vgs
Logical Volume Management
To create a logical volume (here called lv_vol with a size of 1GB in the vg volume group), type:
# lvcreate --size 1G --name lv_vol vg
To remove a logical volume (here called lv_vol in the vg volume group), type:
# lvremove /dev/vg/lv_vol
To get the list of the logical volumes, type:
# lvs
Additional Resources
You can also watch Ralph Nyberg‘s video about LVM (18min/2015) or Red Hat Training‘s Resizing a volume group (5min/2015).
Beyond the RHCSA objectives, you can read this tutorial about Creating a Thinly Provisioned Logical Volume.
On my LFCS exam, they had asked us to do some work with LVM – I won’t discuss what they actually asked because that would be a cheating… But I will say that users should know what packages are needed… Sometimes, even though they are default packages, they aren’t installed… If you know what I mean…
You should add the lvresize command,which is quite useful.
Ex: If you have a logical volume with name “lvol” in volume group “vg_new” and you wish to resize it:
lvresize -L 10G -r /dev/vg_new/lvol0
The command changes the size of the volume to 10 GB, resizes the file system automatically.Works in both up and down directions ( -L option specifies the amount of space you want for your lvol and it skips the usage of “lvextend” and “lvreduce”)
N.B.:
*”lvresize” must be used only when the volume is “umount”ed!
* “-r” option resizes ext2,ext3,ext4 in both directions,”xfs” file system supports only growing!
This information is already mentioned here -> http://www.certdepot.net/sys-extend-ext4-formatted-logical-volumes/
Hi CertDepot,
On Sanders video tutorial he mentioned that lv resizing operation is important to follow, else, it can create a big mess and it is very difficult to fix.
When reducing volume group logical volume must be reduce first.
Don’t have any idea if it is mentioned on other topics here.
We’ll I just want to share it anyways. Thanks
lv_resizing is explained here.
Thanks.
Hi,
Can you please help on the below two queries:
1-Is shrinking LVM is an RHCSA objective?
2-Is creating a LUKS encrypted partitions an RHCSA objective?
3-If yes, how we can do both for the RHCSA perspective?
1- You are supposed to know how to do it.
2- LUKS encrypted partitions were an RHCSA 6 objective.
3- You only need to be prepared for the point 1.
Just a tip: If pvcreate and similars doesn’t work, you have to check with rpm -q lvm2 to see if lvm is installed.
Interesting. Thanks.
Hi, I have read somewhere that in the exam they ask to create lv in terms of PE unit. It is normally 1PE = 4 Mib. Is there any exception of this conversion or it will always be 4 Mib ?
The size depends on the volume group configuration (see the vgcreate command).
Thanks, I see there is this an -s option to define the size. Without it defaults to 4 Mib.
“vgdisplay” command is more helpful in this regard, it shows the actual PE in use on this vg
Hi guys probably a stupid question. I have 2 LVs configured and only one mounted. My scope is to check available space on them. If I do lvdisplay I see lv size, and with df -h I see free space of the mounted one. My question is….how can I see the free space of LV not mounted?
The lvs command should solve your problem.
Partially solved used df -h /pathtolv but I wonder if the same can be accomplished with lvs or lvdisplay?
The lvs command will give you the size that has been allocated to the volume, but it won’t know the amount of free space that is available, because free disk space depends on how much the filesystem on the volume is in use.
If you use ext4 filesystem, you can do “tune2fs -l” on the volume, and calculate the following:
Free blocks x Block size = You will get free disk space.
Lisenet thank you, useful. What’s the same (if possible) for XFS?
Try xfs_info.
Hello CertDepot, with lvs I see LSsize but not free space. Am I missing some command options?
No, You are right: my answer wasn’t good enough!
absolutely no problem!