LVM (Logical Volume Manager) is the recommended way to manage disk or volume in Linux system. One of the major benefits of LVM partitions is that we can extend their size on the fly without any down time.
In this post, we will learn how to create LVM partition in linux step by step. For demonstration purpose, I have attached 10 GB disk to my Ubuntu system and will create lvm partition on it. Let’s deep dive into the steps.
Step 1) Identity Disk and Create Physical Volume (PV)
Login to Linux system and look for newly attached disk or free disk. Run ‘sudo fdisk -l’ command
$ sudo fsdik -l
As we can clearly see that we have a disk of 10 GB (/dev/sdb) which have no partition and available as raw disk. So, let’s create physical volume (pv) using following command.
$ sudo pvcreate <disk1> <disk2> … <diskN>
$ sudo pvcreate /dev/sdb Physical volume "/dev/sdb" successfully created. $
Note: In case pvcreate command is not available in Ubuntu system then install lvm2 package using following apt command.
$ sudo apt install lvm2 -y
Verify the PV status using following commands.
$ sudo pvscan Or $ sudo pvs Or $ sudo pvdisplay /dev/sdb
Output of above commands,
Step 2) Create Volume Group (VG)
Creating volume group (vg) means adding physical volume (pv) to vg. Run beneath command to create volume group,
$ sudo vgcreate <vg_name> <pv> <physical volume>
So, in our case, command would be
$ sudo vgcreate vg01 /dev/sdb Volume group "vg01" successfully created $
Verify the volume group status using following commands,
$ sudo vgscan Or $ sudo vgs Or $ sudo vgdisplay vg01
Above output confirm that volume group vg01 has been created successfully and size of each physical extent is 4 MB (this is the default size).
Step 3) Create Logical volume (LV) from Volume Group (VG)
To create a logical volume (lv) from volume group (vg), use following command
$ sudo lvcreate -L <Size> -n <lv_name> <vg_name>
In our case, I would be creating a lv of size 10 GB with name ‘lv01’.
$ sudo lvcreate -L 9.99G -n lv01 vg01 Rounding up size to full physical extent 9.99 GiB Logical volume "lv01" created. $
Verify the logical volume status by running following commands,
Perfect, above output shows that lv has been created successfully. So, to start using this lvm partition, first we must format it.
Step 4) Format LVM partition
To format LVM partition, we can use mkfs command. In this demonstration, I am formatting the lvm partition with ‘ext4’ file system.
$ sudo mkfs.ext4 <LV_Path>
$ sudo mkfs.ext4 /dev/vg01/lv01
Above output shows that lvm has been formatted with ext4 filesystem successfully. To access this file system, create a mount point using mkdir command and mount it.
$ sudo mkdir /data $ sudo mount /dev/vg01/lv01 /data
Verify the mount using following df command
$ sudo df -Th /data Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg01-lv01 ext4 9.8G 37M 9.3G 1% /data $
Great, above output confirms that LVM partition is mounted on /data directory. For permanent mounting append following entry in /etc/fstab file.
$ echo '/dev/vg01/lv01 /data ext4 defaults 0 0' | sudo tee -a /etc/fstab
That’s all from this post, I hope you have found it informative and insightful. Please do share your feedback and queries in below comments section.