Add and Mount LVM Volume in Linux

by | Jan 12, 2015 | RHEL / CentOS

Its possible add a LVM volume on the fly. The steps would involve formatting the newly added disk with LVM and creating a mount point and adding the new volume to it. The whole process would involve creating Physical Volume followed by Volume group and then assigning logical volume and mounting it to a mount point.

First lets record the current Partitions

# df -h

Output:

[root@smtp-proxy ~]# df -h
Filesystem                      Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-lv_root   28G  5.5G   21G  21% /
tmpfs                           495M     0  495M   0% /dev/shm
/dev/sda1                       485M   32M  428M   7% /boot

After adding the disk either through VMWare vSphere Client Interface or physically adding it, 1st need to create a Physical Volume and format it using Logical Volume Manager (LVM) in Linux.
Run this command to check if the newly added disk is visible/detect to OS.

# lvmdiskscan

Output:

[root@smtp-proxy ~]# lvmdiskscan
  /dev/ram0               [      16.00 MiB]
  /dev/root               [      27.57 GiB]
  /dev/ram1               [      16.00 MiB]
  /dev/sda1               [     500.00 MiB]
  /dev/VolGroup00/lv_swap [       1.94 GiB]
  /dev/ram2               [      16.00 MiB]
  /dev/sda2               [      19.51 GiB] LVM physical volume
  /dev/ram3               [      16.00 MiB]
  /dev/ram4               [      16.00 MiB]
  /dev/ram5               [      16.00 MiB]
  /dev/ram6               [      16.00 MiB]
  /dev/ram7               [      16.00 MiB]
  /dev/ram8               [      16.00 MiB]
  /dev/ram9               [      16.00 MiB]
  /dev/ram10              [      16.00 MiB]
  /dev/ram11              [      16.00 MiB]
  /dev/ram12              [      16.00 MiB]
  /dev/ram13              [      16.00 MiB]
  /dev/ram14              [      16.00 MiB]
  /dev/ram15              [      16.00 MiB]
  /dev/sdb                [      10.00 GiB] LVM physical volume
  /dev/sdc                [      20.00 GiB]
  3 disks
  17 partitions
  1 LVM physical volume whole disk
  1 LVM physical volume

Now convert the new disk /dev/sdc to LVM

# pvcreate /dev/sdb

Output:

[root@smtp-proxy ~]# pvcreate /dev/sdc
  Physical volume "/dev/sdc" successfully created

*Notice that /dev/sdc has now got LVM Physical Volume beside it

# [root@smtp-proxy ~]# lvmdiskscan
  /dev/sdc                [      20.00 GiB] LVM physical volume

Now create a Volume Group for the Physical LVM volume

# vgcreate vgtest /dev/sdc

Output:

[root@smtp-proxy ~]# vgcreate vgtest /dev/sdc
  Volume group "vgtest" successfully created

Now verify its created and check the available Physical Extents for the next step.
Run this command

# vgdisplay

Output:

[root@smtp-proxy ~]# vgdisplay
  --- Volume group ---
  VG Name               vgtest
  System ID
  Format                lvm2
  Metadata Areas        1
  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                1
  Act PV                1
  VG Size               20.00 GiB
  PE Size               4.00 MiB
  Total PE              5119
  Alloc PE / Size       0 / 0
  Free  PE / Size       5119 / 20.00 GiB
  VG UUID               R0i4pd-w3uR-28fQ-SI7b-8MuF-jUBa-MzJzod

Now create Logical Volume by taking “Total PE” Value.
Run this command

# lvcreate -l 5119 -n lvtest vgtest

Output:

[root@smtp-proxy ~]# lvcreate -l 5119 -n lvtest vgtest
  Logical volume "lvtest" created

Now format and map the disk.

# mkfs.ext4 /dev/vgtest/lvtest

Output:

[root@smtp-proxy ~]# mkfs.ext4 /dev/vgtest/lvtest
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5241856 blocks
262092 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
160 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
# mkfs.ext4 /dev/mapper/vgtest-lvtest

 Output:

[root@smtp-proxy ~]# mkfs.ext4 /dev/mapper/vgtest-lvtest
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5241856 blocks
262092 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
160 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 30 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

 

Now the disk is ready to be assigned to a mount point. Create a folder in /mnt/

# mkdir /mnt/test_disk

Now create an entry in /etc/fstab. First check the ID of the disk.
Run this command

# blkid

Output:

[root@smtp-proxy ~]# blkid | grep /dev/mapper/vgtest
/dev/mapper/vgtest-lvtest: UUID="5e50c2a9-64bf-4795-a122-412002234c75" TYPE="ext4"

Now edit “fstab” and the below entry. Press “i” to insert text and “:wq” to save and exit the file.

# vi /etc/fstab

Add this line just above “/dev/mapper/VolGroup00-lv_swap swap”

UUID=5e50c2a9-64bf-4795-a122-412002234c75 /mnt/test_disk        ext4    defaults        1 2

* Above UUID is taken from blkid you got from above

Now re-mount all partitions in /etc/fstab

# mount -a

(OR)

# mount /mnt/test_disk

Now verify the new partition is active

# df -h

Output:

[root@smtp-proxy ~]# df -h
Filesystem                      Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-lv_root   28G  5.5G   21G  21% /
tmpfs                           495M     0  495M   0% /dev/shm
/dev/sda1                       485M   32M  428M   7% /boot
/dev/mapper/vgtest-lvtest         20G  172M   19G   1% /mnt/test_disk

Now you can see the new partition “/dev/mapper/vgtest-vo10” at mount “/mnt/test_disk” added successfully and will be persistent across reboots.

If you encounter any errors during the process, let us know via Contact Us and will get it resolved promptly.

Related Articles….