How To Create LVM and Mount On Directory On Ubuntu 20.04

We can create and mount LVM disk on the directory In ubuntu 20.04 using the LVM concept. you will have to install LVM on ubuntu 20.04. Let’s see, how to mount the disk in ubuntu.

How To Create LVM and Mount On Directory On Ubuntu 20.04

As you can see the root partition is full and I don’t have any space left. So, I need to increase the space by adding the extra disk and mount on any directory using the LVM concept. Use the df -h command to check the space.

df -h

/dev/sda5 20G 19G 0 100% /

Check Out: How To Install Joomla Content Management System On CentOS 8

This is used to resizing the disk online or you can add a new disk. I’ve added extra 50GB of space and the device name is: /dev/sdb: 50 GiB

1. Select the disk and press “Enter“.

ubuntu@ubuntu:~$ sudo fdisk /dev/sdb

Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x346be65d.

Command (m for help):

2. We need to type ‘n‘ to create the new disk. I will use the full disk space to make it a primary disk. you can also choose any size according to yours.

Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p):

Using default response p.
Partition number (1-4, default 1): 
First sector (2048-104857599, default 2048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-104857599, default 104857599):

Created a new partition 1 of type 'Linux' and of size 50 GiB.

3. Check the newly created partition using ‘p‘ command and then press ‘w‘ to save it. 

Device Boot    Start         End                     Sectors                       Size                Id            Type
/dev/sdb1       2048        104857599         104855552                50G               83            Linux

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

If you want to change the type from “Linux” to “Linux LVM”, you can do that using the following commands. “8e” is a hexacode used for Linux LVM.

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): p
Disk /dev/sdb: 50 GiB, 53687091200 bytes, 104857600 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x346be65d

Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 104857599 104855552 50G 8e Linux LVM

If the LVM command not found on Ubuntu 20.04, you can install it.

ubuntu@ubuntu:~$ sudo apt install lvm2
Reading package lists... Done
Building dependency tree 
Reading state information... Done
The following additional packages will be installed:
dmeventd libdevmapper-event1.02.1 liblvm2cmd2.03 libreadline5
thin-provisioning-tools
The following NEW packages will be installed:
dmeventd libdevmapper-event1.02.1 liblvm2cmd2.03 libreadline5 lvm2
thin-provisioning-tools
0 upgraded, 6 newly installed, 0 to remove and 5 not upgraded.
Need to get 2,247 kB of archives.
After this operation, 8,883 kB of additional disk space will be used.
Do you want to continue? [Y/n] y

4. Now we have the disk and need to create the LVM. you have to use the disk to create a Psychical volume using the below commands. 

ubuntu@ubuntu:~$ sudo pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.

We need to create a volume group and logical group with the following commands. 

ubuntu@ubuntu:~$ sudo vgcreate vg /dev/sdb1                                                     ## create volume group
Volume group "vg" successfully created

ubuntu@ubuntu:~$ sudo lvcreate -l 100%FREE -n lv_vg vg                            ## create logical volume
Logical volume "lv_vg" created.

We have successfully created the LVM partition on ubuntu using the above commands. you can also check the space left or allocated on all the volumes using pvs, vgs and lvs command,

Check Out: Create Routing Table On AWS Cloud Using CLI On Windows

Mount Partition On Ubuntu 20.04:

5.  You need to find the LVM disk using fdisk -l command with sudo permission. 

Disk /dev/mapper/vg-lv_vg: 49.102 GiB, 53682896896 bytes, 104849408 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

Use the below command to format the disk with ext4 file system on ubuntu. we can’t use it without formatting because we need to make a block on the LVM partition to store the data. 

ubuntu@ubuntu:~$ sudo mkfs.ext4 /dev/mapper/vg-lv_vg
mke2fs 1.45.5 (07-Jan-2020)
Creating filesystem with 13106176 4k blocks and 3276800 inodes
Filesystem UUID: b45239fc-c015-48d8-8cdc-33503698fd3e
Superblock backups stored on blocks: 
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
4096000, 7962624, 11239424

Allocating group tables: done 
Writing inode tables: done 
Creating journal (65536 blocks): done
Writing superblocks and filesystem accounting information: done

Now the blocks are created and we need to mount it on the directory.

ubuntu@ubuntu:~$ sudo mkdir -p /opt/lvmfolder                                                                ## create a mount point
ubuntu@ubuntu:~$ sudo mount /dev/mapper/vg-lv_vg /opt/lvmfolder                      ## use command to mount
ubuntu@ubuntu:~$ sudo mount -a                                                                                               ## check for any error while mounting. if you see not output,   
                                                                                                                                                                      means working fine.

Check Out: Step To Create LVM (Logical Volume Manager) on Linux

We can check the mount point using the df -h command and also need to make it permanent because when you will reboot the system it will be gone. 

ubuntu@ubuntu:~$ sudo df -h | grep /dev/mapper/vg-lv_vg
/dev/mapper/vg-lv_vg     49G     53M       47G          1%          /opt/lvmfolder

Make an entry in /etc/fstab file to make it a permanent mount point. 

/dev/mapper/vg-lv_vg      /opt/lvmfolder        ext4        defaults       0       0

Reboot the machine and check the mount point should not be gone.

That’s it.

Share on:

I'm the founder of Curious Viral. I hope this blog will provide you complete information about Linux Technology & I would like to share my technical knowledge with you which I have learned during this period.

Other Posts You May Like...

Leave a comment