Suggest a StoryDo you have a news tip? We would like to hear from you.

Create LVM Partition with XFS/Ext4 File Systems on RHEL and Debian

Spread the love

Partitioning hard disk with old traditional methods is still commonly used by many users. However, taking advantage of LVM (Logical Volume Manager) to create LVM partition and manage multiple hard disks specially on a scalable severs running Linux operating system with the ability to extend or reduce the size of the logical volume and the volume groups, creating snapshot backups of logical volume, encrypting entire system based on LVM and more.

Here we will go through creating an LVM disk with XFS, Ext4, Ext3, and Ext2 file systems. We will include more tutorials in the future for more common advanced scenarios you might face on a production server or even personal machine.

1.0 Create LVM Partition on RHEL / Debian

I am using CentOS 7.5 as a testing environment for that tutorials. So, let’s attach 2 new disks with 200 MB disk space for each. Once we have attached the disk it will be recognized on by fdisk and we will start partitioning it.

1.1 Checking currently attached Disks

Running fdisk command to check the size and the properties of the new attached disks to our machine.

fdisk -l
### or by using the following command to specify disks
fdisk -l /dev/sdb /dev/sdc
Terminal Output Sample

Disk /dev/sdb: 209 MB, 209715200 bytes, 409600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/sdc: 209 MB, 209715200 bytes, 409600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Currently we don’t have any logical volumes except for the root and swap on the machine. So, to verify that we could use the following commands.

1.2 Display Physical Volumes, Volume Groups and Logical Volumes

To view currently available physical volumes on the machine, which shows the physical volume path “PV”, associated volume group “VG”, file system format “Fmt”, physical volume attributes “Attr”, Physical volume size “PSize”, and free size of physical volume “PFree”.

pvs
Terminal Output Sample
PV         VG      Fmt  Attr PSize   PFree 
/dev/sda2  centos  lvm2 a--   <3.00g     0 

To view currently available volume groups on the machine, which shows the name of the volume group “VG”, number of associated physical volumes “PV”, number of used logical volumes used by this volume group “VG”, the number of snapshot the volume group contains “SN”, Volume group attributes “Attr”, Volume group size “VSize”, and the free space of the volume group “VFree”.

vgs
Terminal Output Sample
VG      #PV #LV #SN Attr   VSize   VFree 
centos    1   2   0 wz--n-  <3.00g     0 

To view currently available logical volumes on the machine. As it displays the name of the logical volume, associated volume group, attributes and the logical volume disk size.

lvs
Terminal Output Sample
VG      #PV #LV #SN Attr   VSize   VFree 
centos    1   2   0 wz--n-  <3.00g     0 

1.3 Creating a Physical volume for newly attached Disks

Here we will create 2 new physical volumes of /dev/sdb and /dev/sdc to be able join both of them in the upcoming step in a single Volume Group.

pvcreate /dev/sdb
pvcreate /dev/sdc

pvcreate-command-Centos-7-5-Screenshot-1

pvcreate-command-Centos-7-5-screenshot-2

To verify that you have created physical volumes of both disks. You could run pvs command.

pvs

pvdisplay-Centos-7-5-Screenshot-LVM

So far in the output of pvs command it shows that /dev/sdb and /dev/sdc aren’t associated with any volume groups. So, let’s check the following step to create volume group for these disks.



1.4 Creating a Volume Group for Physical Volumes

vgcreate test-vg /dev/sdb /dev/sdc

vgcreate-create-volume-group-centos-7-5-screenshot

vgcreate command is followed by the desired Volume Group name and Physical volumes paths.

Verifying the creation of the volume group using vgdisplay command.

vgdisplay test-vg

vgdisplay-display-current-volume-group-centOS-7-5-Screenshot

As show on the terminal output, it combines the size of both disks in the volume group.

1.5 Creating a Logical Volume from the current Volume Group

So, we are almost there. Here we will create a new logical volume using recently created volume group with the follow command.

I specified that I want to create a logical volume with 300MB size, instead of using the entire volume group. So, I could demonstrate how to extend a logical volume with the rest of the available space of the volume group on the upcoming tutorial.

lvcreate -n new-lv -L 300M test-vg

lvcreate-create-lvm-disk-centos-7-5-screenshot

As shown in the previous command “-n” option to specify the new logical volume name followed by the logical volume name, “-L” to Specify the desired logical volume size, and final the name of the volume group we would like to use for creating the logical volume.

1.6 Build a File System for a Logical Volume

We will build the file system for the newly created logical volume using mkfs command. Here, we could create EXT2, EXT3, EXT4 and XFS file systems. Choose which file system you need.

mkfs.xfs /dev/test-vg/new-lv
mkfs.ext4 /dev/test-vg/new-lv
mkfs.ext3 /dev/test-vg/new-lv
mkfs.ext2 /dev/test-vg/new-lv

Create-xfs-filesystem-centos-7-5-screenshot

After running mkfs command it should display the new properties of the new logical volume with the new file system.

Finally you could mount the new logical volume to a new directory as shown down below.

mount /dev/test-vg/new-lv /new-disk

Verifying that you had successfully mounted the disk with the df command. Which will display disk path and where it’s mounted along with more information about disk size, used and available disk space.

df -h

1.7 Manual Pages for LVM

man command-name

Manual pages are quite useful and comes with several examples to guide you through the creation of LVM partition. So, you could use the man command followed by any of the main commands mentioned earlier.

In the next post we will go through how to extend a logical volume on without affecting currently available data.


Spread the love
2 Comments

Leave a Reply