How To Manage LVM in Debian

To Manage the Logical Volume in Debian Linux.

Installation of Logical Volume Management with the procedure to manage multiple disks and partitions will be discussed in this article.

To Install LVM on Debian

Root access is required to manage logical volumes. Use ' su' or ' sudo' command to achieve it.
Run the following command to install lvm2 package.

root@linuxhelp:~# apt-get install lvm2
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
thin-provisioning-tools
The following NEW packages will be installed:
lvm2
.
.
.
Setting up lvm2 (2.02.111-2.2) ...
update-initramfs: deferring update (trigger activated)
update-rc.d: warning: start and stop actions are no longer supported  falling back to defaults
Processing triggers for initramfs-tools (0.120) ...
update-initramfs: Generating /boot/initrd.img-3.16.0-4-amd64


Run the below command check the installed package.

root@linuxhelp:~# dpkg-query -s lvm2
Package: lvm2
Status: install ok installed
Priority: optional
Section: admin
Installed-Size: 1624
Maintainer: Debian LVM Team < pkg-lvm-maintainers@lists.alioth.debian.org> 
Architecture: amd64
Multi-Arch: foreign
Version: 2.02.111-2.
.
.
.
Description: Linux Logical Volume Manager
 This is LVM2, the rewrite of The Linux Logical Volume Manager.  LVM
 supports enterprise level volume management of disk and disk subsystems
 by grouping arbitrary disks into volume groups. The total capacity of
 volume groups can be allocated to logical volumes, which are accessed as
 regular block devices.
Homepage: http://sources.redhat.com/lvm2/

root@linuxhelp:~# dpkg-query -l lvm2
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name           Version      Architecture Description
+++-==============-============-============-=================================
ii  lvm2           2.02.111-2.2 amd64        Linux Logical Volume Manager

In general, LVM partition will be done by using basic tools like, cfdisk, fdisk, gparted and parted to partition and also flag the partitions to use in a LVM setup. We have 3 partition in hard disk namely sda1, sda2, sda3 and is used by the system OS. Now we have created a new partition sda5 for LVM.

root@linuxhelp:~# pvcreate /dev/sda5
  Physical volume " /dev/sda5"  successfully created

Now the physical volume is created to /dev/sda5.

To add Volume Group

Run the following command to add a Volume Group ' vg1' .

root@linuxhelp:~# vgcreate vg1 /dev/sda5
  Volume group " vg1"  successfully created

In the above command the first argument is the name of the Volume Group we have created and the second will be the name of the RAID device created using pvcreate in /dev/sda5.

Run the following command to verify the Volume group.

root@linuxhelp:~# vgdisplay
  --- Volume group ---
  VG Name               vg1
  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               10.00 GiB
  PE Size               4.00 MiB
  Total PE              2559
  Alloc PE / Size       0 / 0   
  Free  PE / Size       2559 / 10.00 GiB
  VG UUID               ei17xi-hJwh-te62-mzQJ-1Pfd-UkFY-dLTEXH
root@linuxhelp:~# vgs
VG #PV #LV #SN Attr VSize VFree
vg1 1 0 0 wz--n- 10.00g 10.00g

Now we can start creating the Logical Volumes inside the Volume Groups.
Run the following command to create the Logical Volume (LV)

root@linuxhelp:~# lvcreate -L 5G -n lv1 vg1
  Logical volume " lv1"  created

Explanation,

-L size of the LV,
-n the name of the LV.

Now create another Logical Volume, by using the following command.

root@linuxhelp:~# lvcreate -L 4G -n lv2 vg1
  Logical volume " lv2"  created

Verify the creation of the LV , by using either of the following commands:

-lvdisplay gives you the detailed output of the Logical Volumes.
-lvs gives you Less detailed output of the Logical Volumes.
Here, we are running lvdisplay command to get a detailed output.

root@linuxhelp:~# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg1/people
  LV Name                lv1
  VG Name                vg1
  LV UUID                4ElP60-SBxa-1pzd-IcLJ-TTRv-MPHx-RrfB8n
  LV Write Access        read/write
  LV Creation host, time linuxhelp, 2016-04-20 18:20:31 +0530
  LV Status              available
  # open                 0
  LV Size                5.00 GiB
  Current LE             1280
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:0
   
  --- Logical volume ---
  LV Path                /dev/vg1/lv2
  LV Name                lv2
  VG Name                vg1
  LV UUID                4ySeJI-Iyyk-a72g-Upgy-tztg-PyVP-ghivNM
  LV Write Access        read/write
  LV Creation host, time linuxhelp, 2016-04-20 18:21:30 +0530
  LV Status              available
  # open                 0
  LV Size                4.00 GiB
  Current LE             1024
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:1
   [root@linuxhelp:~#] lvs
  LV     VG    Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv1   vg1   -wi-a----- 5.00g                                                    
  lv2   vg1   -wi-a----- 4.00g   

The LV' s are configured, but it is not ready to mount and use. Format the LV with a file-system using gparted or mkfs.
Here we use CLI server, so the mkfs utility is used to accomplish this task.

root@linuxhelp:~# mkfs.ext4 -L lv1 /dev/vg1/lv1
mke2fs 1.42.12 (29-Aug-2014)
.
.
.
Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done


root@linuxhelp:~# mkfs.ext4 -L lv2 /dev/vg1/lv2
mke2fs 1.42.12 (29-Aug-2014)
.
.
.
Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done 

The above commands will write an ext4 file system to each of the LV created under the file system label (-L) on each of the LV file systems.

To create a directory

Create a new directory for each Logical Volumes, using the following command.

root@linuxhelp:~# mkdir /mnt/test
root@linuxhelp:~# mkdir /mnt/test1

Now mount the logical volumes to the new directories created using the below commands.



root@linuxhelp:~# mount /dev/vg1/lv1 /mnt/test
root@linuxhelp:~# mount /dev/vg1/lv2 /mnt/test1

The LV' s are ready to use in the directory ' /mnt/test' or ' /mnt/test1' , confirm it using the lsblk command.

root@linuxhelp:~# lsblk
NAME             MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0                2:0    1    4K  0 disk 
sda                8:0    0   30G  0 disk 
??sda1             8:1    0  9.3G  0 part /
??sda2             8:2    0  3.7G  0 part [SWAP]
??sda5             8:5    0   10G  0 part 
  ??vg1-lv1 254:0    0    5G  0 lvm  /mnt/test
  ??vg1-lv2   254:1    0    4G  0 lvm  /mnt/test1
sr0               11:0    1  3.7G  0 rom  /media/cdrom0

To get Universally Unique Identifier (UUID)

Run the following command to obtain the Universally Unique Identifier value and also the file system type, which is required to use, to start, to setup persistent mounting.

root@linuxhelp:~# blkid /dev/vg1/*
/dev/vg1/lv1: LABEL=" lv1"  UUID=" e88e067c-bf1b-462a-a04b-b48bb93b1a09"  TYPE=" ext4" 
/dev/vg1/lv2: LABEL=" lv2"  UUID=" 594881b0-a9e4-4867-b6a7-5450a6f278de"  TYPE=" ext4" 

The above UUID values for each of the LVs created must be sent to ' /etc/fstab' and this can be achieved by running the following command.

root@linuxhelp:~# blkid /dev/vg1/* > >  /etc/fstab


Make sure, this command uses > > (DOUBLE greater than symbols). If suppose > (a single greater than symbol) is used, the contents of the file will be over-written.

To open the /etc/fstab file with a text editor run the following command.

root@linuxhelp:~# vim /etc/fstab
# /etc/fstab: static file system information.
#
# Use ' blkid'  to print the universally unique identifier for a
# device  this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
#              
# / was on /dev/sda1 during installation
UUID=678bde07-b627-4113-b332-038322a15465 /               ext4    errors=remount-ro 0       1
# swap was on /dev/sda2 during installation
UUID=6251a6f5-b5f4-428d-bcc0-23e90a0b5e11 none            swap    sw              0       0
/dev/sr0        /media/cdrom0   udf,iso9660 user,noauto     0       0

/dev/vg1/lv1: LABEL=" lv1"  UUID=" e88e067c-bf1b-462a-a04b-b48bb93b1a09"  TYPE=" ext4" 
/dev/vg1/lv2: LABEL=" lv2"  UUID=" 594881b0-a9e4-4867-b6a7-5450a6f278de"  TYPE=" ext4" 

To format the /etc/fstab file remove ' /dev/vg1/lv1' and ' /dev/vg1/lv2' . Enter the mount points and the absolute path to the mount point in the field should be placed after the UUID.

The third field, should be the file system, ' ext4' and fourth field is set as ' defaults' and the last two (< dump> < pass> ) will be set as zeros.

# /etc/fstab: static file system information.
#
# Use ' blkid'  to print the universally unique identifier for a
# device  this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
#              
# / was on /dev/sda1 during installation
UUID=678bde07-b627-4113-b332-038322a15465 /               ext4     errors=remount-ro  0       1
# swap was on /dev/sda2 during installation
UUID=6251a6f5-b5f4-428d-bcc0-23e90a0b5e11 none            swap     sw                 0       0
/dev/sr0        /media/cdrom0   udf,iso9660 user,noauto                               0       0

UUID=e88e067c-bf1b-462a-a04b-b48bb93b1a09       /mnt/test     ext4    defaults      0       0

UUID=594881b0-a9e4-4867-b6a7-5450a6f278de       /mnt/test1       ext4    defaults       0       0
~      


To confirm the mount points, use the ' mount' command with an argument which allows the mount utility to load all the mounts automatically.

root@linuxhelp:~# mount -a

To check the above process, run lsblk command.

root@linuxhelp:~# lsblk
NAME             MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
fd0                2:0    1    4K  0 disk 
sda                8:0    0   30G  0 disk 
??sda1             8:1    0  9.3G  0 part /
??sda2             8:2    0  3.7G  0 part [SWAP]
??sda5             8:5    0   10G  0 part 
  ??vg1-lv1      254:0     0    5G   0 lvm  /mnt/test
  ??vg1-lv2      254:1     0    4G   0 lvm  /mnt/test1
sr0               11:0    1  3.7G  0  rom   /media/cdrom0

Here the LVs are accessible through /mnt/test and /mnt/test1 for the system user to migrate the data, re-sizing the data or add more data using LVM volumes.

FAQ
Q
How to Resize the Logical Volumes?
A
Use "lvreduce" option along with size to be reduced for reducing Logical volume size.
Q
What if I wanted to create XFS file system instead of ext4 after LVM creation?
A
Use the same command "mkfs.xfs path to LVM" after LVM creation.
Q
What package is required to do LVM setup in rpm based machines?
A
No need external package you can straight away go ahead and continue with LVM.
Q
Major categories while partitioning LVM?
A
PV, VG, LV (i.e) physical volume, volume group logical volume to be created while doing LVM.
Q
I have created a Volume group and got created successfully but how to check with the settings?
A
Use "vgdisplay" to check out Volume group.