Logical Volume Management Practice

Creating a Physical Volume (PV):

  • Create a partition using fdisk, and change the hex code of it to
  • Save and exit the fdisk and update the partition table using fdisk /dev/sda command.
  • Create 3 Physical volumes from 3 physical disks (/dev/sda8, /dev/sda9, /dev/sda10).
[root@localhost ~]# fdisk -l

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1050623      524288   83  Linux
/dev/sda2         1050624    42993663    20971520   83  Linux
/dev/sda3        42993664    84936703    20971520   83  Linux
/dev/sda4        84936704   209715199    62389248    5  Extended
/dev/sda5        84938752   101715967     8388608   83  Linux
/dev/sda6       101718016   105912319     2097152   83  Linux
/dev/sda7       105914368   105914868         250+  83  Linux
/dev/sda8       105918464   106942463      512000   83  Linux
/dev/sda9       106944512   107968511      512000   83  Linux
/dev/sda10      107970560   108994559      512000   83  Linux
[root@localhost ~]# fdisk /dev/sda Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): t Partition number (1-8, default 8): 7 Hex code (type L to list all codes): 8e Changed type of partition 'Linux' to 'Linux LVM'
  • Create a PV on newly created partition i.e. /dev/sda8.
  • Verify it by pvs or pvdisplay command
  • Syn: #pvcreate <partition name1> <partition name2>
  • #pvcreate /dev/sda8 /dev/sda9 /dev/sda10
[root@localhost Downloads]# pvcreate /dev/sda8 /dev/sda9 /dev/sda10
  Physical volume "/dev/sda8" successfully created.
  Physical volume "/dev/sda9" successfully created.
  Physical volume "/dev/sda10" successfully created.
[root@localhost Downloads]# pvs  
  PV         VG Fmt  Attr PSize   PFree  
  /dev/sda10 lb lvm2 a--  496.00m 496.00m
  /dev/sda8  lb lvm2 a--  496.00m 444.00m
  /dev/sda9  lb lvm2 a--  496.00m 496.00m
  • The above command will list all the PVs in the system, if you want to see the details only for a particular PV, then use
    #pvdisplay <partition name> i.e. #pvdisplay /dev/sda8 /dev/sda9 /dev/sda10
Creating a Volume Group (VG)
  • After creating a PV, the next step is to create a Volume Group or VG
  • To create a VG the syntax is
    #vgcreate <name for the VG> <partition name1> <partition name2>
    #vgcreate lb /dev/sda8 /dev/sda9 /dev/sda10
  • Verify it by vgs or vgdisplay command
[root@localhost Downloads]# vgcreate lb /dev/sda8 /dev/sda9 /dev/sda10
  Volume group "lb" successfully created
[root@localhost Downloads]# vgs
  VG #PV #LV #SN Attr   VSize VFree
  lb   3   1   0 wz--n- 1.45g 1.40g
Logical Volume Creation:
  • Once we are ready with a Volume Group then it’s the time to create a Logical Volume LV
  • The syntax for creating an LV is
  • #lvcreate –L <size of LV> <VG name> -n <name for LV>
  • #lvcreate -L +50M lb -n linux
  • Verify it by lvs or lvdisplay command
[root@localhost Downloads]# lvcreate -L +50M lb -n linux
  Rounding up size to full physical extent 52.00 MiB
  Logical volume "linux" created.
[root@localhost Downloads]# lvs
  LV    VG Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  linux lb -wi-ao---- 52.00m  

Adding File system to the LV and Mounting it:

  • As per now we have our VG created so is our LV. In order make it accessible we need to format it with a file system like ext4 or ext3 or vfat.
  • The syntax for formatting an LV is exactly like formatting a normal partition, Instead of /dev/partition name we use the path of LV that will be something like /dev/vg/lv
    #mkfs.ext4 /dev/lb/linux
[root@localhost Downloads]# mkfs.ext4 /dev/lb/linux 
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
13328 inodes, 53248 blocks
2662 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33685504
7 block groups
8192 blocks per group, 8192 fragments per group
1904 inodes per group
Superblock backups stored on blocks: 
	8193, 24577, 40961

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
[root@localhost Downloads]# blkid /dev/lb/linux /dev/lb/linux: UUID=”6647f1dc-633e-48a4-abcf-d92ae176176a” TYPE=”ext4″ 
  • Mounting an LV is exactly same like a normal partition, again the path for mounting will be /dev/vg/lv
  • Create a directory over which the LV should be mounted.
  • #mkdir /directory name
  • #mount </dev/vgname/lvname> /directory name
  • #mount /dev/lb/linux /lvm
[root@localhost Downloads]# mkdir /lvm
[root@localhost Downloads]# mount /dev/lb/linux /lvm

Deleting/Removing an LV:

  • To Delete/Remove an LV, first unmount the file system.
  • use the command lvremove i.e.
  • #umount /lvm
  • #lvremove <Vg name> <lv name>
  • #lvremove /dev/lb/linux (it will prompt to you to continue, press y to continue)
  • Verify it by using lvs or lvdisplay command
[root@localhost ~]# umount /lvm/
[root@localhost ~]# lvremove /dev/lb/linux 
Do you really want to remove active logical volume lb/linux? [y/n]: y
  Logical volume "linux" successfully removed
[root@localhost ~]# lvs
[root@localhost ~]# lvdisplay 
[root@localhost ~]# 

As we was having only one LV and that is now deleted, that’s why it is not showing any LVs after executing lvg lvdisplay command.

Deleting a Volume Group:
  • To delete the volume a group, make sure that if there is any LV in it, it should not be mounted. Because while removing a vg it will also remove LV’s inside it. In our case we have no LV in our volume group, so we will not be concerned about it.
  • To delete a VG, use the following command.
  • #vgremove <vgname>
  • #vgremove lb
[root@localhost ~]# #vgremove lb
    Volume group "lb" successfully removed
[root@localhost ~]# 
Deleting a Physical Volume:
  • Deleting a PV is very simple. The only thing we should check that the PV we are going to delete should not belong to any volume group. We can only delete a PV which is free.
  • The syntax to delete a PV is
  • #pvremove <PV name1> <PV name2> <PV name3>
  • #pvremove /dev/sda8 /dev/sda9 /dev/sda10 (To remove multiple PVs in one command)
[root@localhost ~]# #pvremove /dev/sda8 /dev/sda9 /dev/sda10
    Labels on physical volume "/dev/sda8" successfully wiped
    Labels on physical volume "/dev/sda9" successfully wiped
    Labels on physical volume "/dev/sda10" successfully wiped