Creating Logical Volumes on Linux System

Tutu Godfrey Oritseshutieyimi
7 min readOct 26, 2020

In this article, I would like to share with you, how to create a logical volume on Linux systems. Logical volume management is a way of creating storage space from groups of smaller partitions. It ensures efficient volume management by utilizing available smaller partitions to create a much larger volume. The grouping of partitions we give us a Logical Volume Group (LVG) from which we can create logical volumes of our desired size.

In this demonstration, I will be using the GPT partition type. You should be able to replicate this demonstration using a CentOS or Ubuntu machine. Let get started. This demonstration will exam that you have a disk named sdb present in your system. You can replace the name with a disk of your choice from the/dev directory.

First, you need to have the LVM utility installed. Check by running pvs, If it is not installed already install it by runningyum install lvm2 should do that for you.

First, you need to create partitions in the chosen disk. We will be creating two partitions of 1GB each using the GPT partition format. To create a partition run the command gdisk /dev/sdb, You will need to follow the prompt to complete the creation of the partition. To see available partitions type p , to see create a new partition enter n, to chose the default for the partition number, press enter. To choose the default for the start of the sector, press enter. for the last section type +1G and press enter. To chose the volume type, press L to see available options and enter the copy for the volume type you want to choose. Because we will be using the partition to create a logical volume, you’ll choose the Linux LVM with code 8e00 enter the code to continue. To confirm the partition has been created press p again and you should see the partition. Enter w to write/persist the partition. To create the second partition, repeat the step starting from gdisk /dev/sdb. Refer to the sample output from below on the procedure required. Alright, our partition has been created, let’s move on to creating the logical volume group.

[root@lcsa-centos /]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.10
Partition table scan:
MBR: not present
BSD: not present
APM: not present
GPT: not present
Creating new GPT entries.Command (? for help): n
Partition number (1-128, default 1): 1
First sector (34-20971486, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-20971486, default = 20971486) or {+-}size{KMGTP}: +1G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 8e00
Changed type of partition to 'Linux LVM'
Command (? for help): p
Disk /dev/sdb: 20971520 sectors, 10.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): D036EAAA-B93F-4E93-A9EC-F8FBE42170A8
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 20971486
Partitions will be aligned on 2048-sector boundaries
Total free space is 18874301 sectors (9.0 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 2099199 1024.0 MiB 8E00 Linux LVM
Command (? for help): n
Partition number (2-128, default 2):
First sector (34-20971486, default = 2099200) or {+-}size{KMGTP}:
Last sector (2099200-20971486, default = 20971486) or {+-}size{KMGTP}: +1
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 8e00
Changed type of partition to 'Linux LVM'
Command (? for help): p
Disk /dev/sdb: 20971520 sectors, 10.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): D036EAAA-B93F-4E93-A9EC-F8FBE42170A8
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 20971486
Partitions will be aligned on 2048-sector boundaries
Total free space is 18874300 sectors (9.0 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 2099199 1024.0 MiB 8E00 Linux LVM
2 2099200 2099200 512 bytes 8E00 Linux LVM
Command (? for help): d
Partition number (1-2): 2
Command (? for help): p
Disk /dev/sdb: 20971520 sectors, 10.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): D036EAAA-B93F-4E93-A9EC-F8FBE42170A8
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 20971486
Partitions will be aligned on 2048-sector boundaries
Total free space is 18874301 sectors (9.0 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 2099199 1024.0 MiB 8E00 Linux LVM
Command (? for help): n
Partition number (2-128, default 2):
First sector (34-20971486, default = 2099200) or {+-}size{KMGTP}:
Last sector (2099200-20971486, default = 20971486) or {+-}size{KMGTP}: +1G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 8e00
Changed type of partition to 'Linux LVM'
Command (? for help): pDisk /dev/sdb: 20971520 sectors, 10.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): D036EAAA-B93F-4E93-A9EC-F8FBE42170A8
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 20971486
Partitions will be aligned on 2048-sector boundaries
Total free space is 18874301 sectors (9.0 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 2099199 1024.0 MiB 8E00 Linux LVM
Command (? for help): n
Partition number (2-128, default 2):
First sector (34-20971486, default = 2099200) or {+-}size{KMGTP}:
Last sector (2099200-20971486, default = 20971486) or {+-}size{KMGTP}: +1G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 8e00
Changed type of partition to 'Linux LVM'
Command (? for help): p
Disk /dev/sdb: 20971520 sectors, 10.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): D036EAAA-B93F-4E93-A9EC-F8FBE42170A8
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 20971486
Partitions will be aligned on 2048-sector boundaries
Total free space is 16777149 sectors (8.0 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 2099199 1024.0 MiB 8E00 Linux LVM
2 2099200 4196351 1024.0 MiB 8E00 Linux LVM
Command (? for help): wFinal checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.
[root@lcsa-centos /]# cat proc/partitions
major minor #blocks name
8 16 10485760 sdb
8 17 1048576 sdb1
8 18 1048576 sdb2
8 0 20971520 sda
8 1 204800 sda1
8 2 20764672 sda2

After creating the partitions, we need to mark the partition as a physical disk. you can do that by using the pvcreate command. Here how to do that

For the first partition

[root@lcsa-centos /]# pvcreate /dev/sdb1 
Physical volume "/dev/sdb1" successfully created.

For the second partition

[root@lcsa-centos /]# pvcreate /dev/sdb2
Physical volume "/dev/sdb2" successfully created.

To see available physical volumes

[root@lcsa-centos /]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvm2 --- 1.00g 1.00g
/dev/sdb2 lvm2 --- 1.00g 1.00g

A Logical Volume Group (LVG) will be used to hold the logical volumes we will be creating later. To create the LVG we’ll need one or more physical volumes. We already created two physical volumes. Let make use of them to create our LVG.

[root@lcsa-centos /]# vgcreate vgexam /dev/sdb1 /dev/sdb2
Volume group "vgexam" successfully created

To see information about the LVG just created run the command below

[root@lcsa-centos /]# vgdisplay vgexam
--- Volume group ---
VG Name vgexam
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 1.99 GiB
PE Size 4.00 MiB
Total PE 510
Alloc PE / Size 0 / 0
Free PE / Size 510 / 1.99 GiB
VG UUID f03wL2-ULWS-uY7v-iHqR-9ovf-h90K-x3359V

Let’s now create a logical volume using the LVG. In the command below we are creating a logical volume named lvexamof size 400 megabyte from the LVG.

[root@lcsa-centos /]# lvcreate -L 400M -n lvexam /dev/vgexam
Logical volume "lvexam" created.

A directory with a name /dev/vgexam/lvexam will be created to reference the logical volume. Let see the property of the directory. The link directory ../dm-0 is the internal representation of the logical volume by the Kernel. Whereas /dev/vgexam/lvexam is what we see and interact with.

[root@lcsa-centos /]# ls -l /dev/vgexam/lvexam 
lrwxrwxrwx. 1 root root 7 Oct 26 19:04 /dev/vgexam/lvexam -> ../dm-0

Now we need to make a filesystem from the logical volume and mount it on the system so it is accessible to users. You can use ext4 filesystem or xfs filesystem. In this demonstration, I'm using ext4.

[root@lcsa-centos /]# mkfs.ext4 /dev/vgexam/lvexam 
mke2fs 1.42.9 (28-Dec-2013)
Discarding device blocks: done
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=4 blocks, Stripe width=4 blocks
102400 inodes, 409600 blocks
20480 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=34078720
50 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

Great filesystem created! You can mount the filesystem temporarily on /mnt as follow mount /dev/vgexam/lvexam /mnt, but I want to persist the mount, so we need to edit /etc/fstab. Also, I want to mount it on a directory named examso we need to create the directory. mkdir /exam should do that for us. Now open vi /etc/fstab and add the line below.

/dev/vgexam/lvexam      /exam                   ext4    defaults 0      0

At the point, you have done what is required to mount the filesystem created from the logical volume. The filesystem will be automatically mounter when you reboot reboot to reboot the system. If a reboot is not convenient for you simply run mount -a and the filesystem should be mount. You can now start storing data in the /exam directory.

Thank you for following to the end and I hope it is helpful. If so please leave some like and share.

--

--

Tutu Godfrey Oritseshutieyimi

I am a Fullstack Software engineer with commitment to delivering great products. Enthusiastic about DevOps and Linux system administration