DEV Community

Cover image for LVM & VG in Linux
Waji
Waji

Posted on

LVM & VG in Linux

We can think LVM as a device mapper that provides logical volume management for Linux. It is used for creating single logical volumes of multiple physical volumes or entire hard disks.

👉 For example, we can consider sda1 partition for the sda disk for the / directory. If there is no space left inside this partition, normally we would consider deleting some files to make space for new files or application. However, if we use LVM, we will be connecting the ‘Volume Group’ and divide ‘Logical Volumes’ which will be connected with the ‘/’. This will allow different hard disks (sdb, sdc, etc.) to be added to this ‘Volume Group’ ultimately leading to more space in ‘/’.

💡 Just a note that we can add hard disks to the Volume Group however we cannot remove them!


LVM Hands on

Preparation for the hands on>

  • Creating 2 new disks in our Linux - 1 system.
    • 2GB Hard disk+1GB Hard disk
  • The file should contain only two entries:

    # /etc/fstab
    # Created by anaconda on Tue Jan 10 10:45:44 2023
    #
    # Accessible filesystems, by reference, are maintained under '/dev/disk'
    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
    #
    /dev/mapper/centos_linux--1-root /                       xfs     defaults        0 0
    UUID=2d2f3276-dc8a-403c-bb04-53e472b9184c /boot                   xfs     defaults        0 0
    /dev/mapper/centos_linux--1-swap swap                    swap    defaults        0 0
    
  • We can check the mount status as well using the df -h command:

    [root@Linux-1 ~]# df -h
    Filesystem                        Size  Used Avail Use% Mounted on
    devtmpfs                          475M     0  475M   0% /dev
    tmpfs                             487M     0  487M   0% /dev/shm
    tmpfs                             487M  7.6M  479M   2% /run
    tmpfs                             487M     0  487M   0% /sys/fs/cgroup
    /dev/mapper/centos_linux--1-root   17G  1.6G   16G  10% /
    /dev/sda1                        1014M  168M  847M  17% /boot
    tmpfs                              98M     0   98M   0% /run/user/0
    
  • We can also use the lsblk command to see the current hard disks:

    [root@Linux-1 ~]# lsblk
    NAME                     MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda                        8:0    0   20G  0 disk 
    ├─sda1                     8:1    0    1G  0 part /boot
    └─sda2                     8:2    0   19G  0 part 
      ├─centos_linux--1-root 253:0    0   17G  0 lvm  /
      └─centos_linux--1-swap 253:1    0    2G  0 lvm  [SWAP]
    sdb                        8:16   0    1G  0 disk 
    sdc                        8:32   0    1G  0 disk 
    sdd                        8:48   0    2G  0 disk 
    sde                        8:64   0    1G  0 disk 
    sr0                       11:0    1 1024M  0 rom
    

Hands on>

  • We will create 1 partition for each disks with the default configuration.

    Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048     2097151     1047552   83  Linux
    
    Device Boot      Start         End      Blocks   Id  System
    /dev/sdc1            2048     2097151     1047552   83  Linux
    
    Device Boot      Start         End      Blocks   Id  System
    /dev/sdd1            2048     4194303     2096128   83  Linux
    
    Device Boot      Start         End      Blocks   Id  System
    /dev/sde1            2048     2097151     1047552   83  Linux
    
    #Checking the results using lsblk command.
    
    [root@Linux-1 ~]# lsblk
    NAME                     MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda                        8:0    0   20G  0 disk 
    ├─sda1                     8:1    0    1G  0 part /boot
    └─sda2                     8:2    0   19G  0 part 
      ├─centos_linux--1-root 253:0    0   17G  0 lvm  /
      └─centos_linux--1-swap 253:1    0    2G  0 lvm  [SWAP]
    sdb                        8:16   0    1G  0 disk 
    └─sdb1                     8:17   0 1023M  0 part /sdb1
    sdc                        8:32   0    1G  0 disk 
    └─sdc1                     8:33   0 1023M  0 part 
    sdd                        8:48   0    2G  0 disk 
    └─sdd1                     8:49   0    2G  0 part 
    sde                        8:64   0    1G  0 disk 
    └─sde1                     8:65   0 1023M  0 part 
    sr0                       11:0    1 1024M  0 rom
    
  • In /dev/sdb,

    Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048     2097151     1047552   83  Linux
    
  • We can use ‘t’ and ‘L’ . Upon pressing those buttons, we will be able to see,

    Command (m for help): t
    Selected partition 1
    Hex code (type L to list all codes): L
    
     0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
     1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
     2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
     3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
     4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
     5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
     6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
     7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
     8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt
    
  • Here, we need to find the “Linux LVM”. The code is 8e for this so we will type it and see our results

    Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048     2097151     1047552   8e  Linux LVM
    

VG Hands on

To create a physical volume,

👉 pvcreate /dev/sdb

👉 pvcreate /dev/sdc

👉 pvcreate /dev/sdd

👉 pvcreate /dev/sde

To create the Volume Group,

vgcreate VG /dev/sdb /dev/sdc /dev/sdd

[root@Linux-1 ~]# vgcreate VG /dev/sdb /dev/sdc /dev/sdd
Volume group "VG" successfully created
Enter fullscreen mode Exit fullscreen mode

We can use vgdisplay command to see our VGs,

[root@Linux-1 ~]# vgdisplay
  --- Volume group ---
  VG Name               centos_linux-1
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <19.00 GiB
  PE Size               4.00 MiB
  Total PE              4863
  Alloc PE / Size       4863 / <19.00 GiB
  Free  PE / Size       0 / 0   
  VG UUID               7hBnc9-6dXE-9f1q-rpmW-SuBd-TZv0-TVBa6X

  --- Volume group ---
  VG Name               VG
  System ID             
  Format                lvm2
  Metadata Areas        3
  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                3
  Act PV                3
  VG Size               <3.99 GiB
  PE Size               4.00 MiB
  Total PE              1021
  Alloc PE / Size       0 / 0   
  Free  PE / Size       1021 / <3.99 GiB
  VG UUID               DdSDJQ-EpWo-uy8S-Pb3m-9AWP-tzBw-vaLSov
Enter fullscreen mode Exit fullscreen mode

Now we will create the logical volumes for this VG,

This will create a logical volume ‘LV-1’ using 1GB of the VG’s space.

[root@Linux-1 ~]# lvcreate -L 1GB -n LV-1 VG
  Logical volume "LV-1" created.
Enter fullscreen mode Exit fullscreen mode

This will create a logical volume ‘LV-2’ using 50% of the Volume Group’s total space.

[root@Linux-1 ~]# lvcreate -l +50%VG -n LV-2 VG
WARNING: xfs signature detected on /dev/VG/LV-2 at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/VG/LV-2.
  Logical volume "LV-2" created.
Enter fullscreen mode Exit fullscreen mode

This will create a logical volume ‘LV-3’ using 100% of the remaining of the Volume Group.

[root@Linux-1 ~]# lvcreate -l +100%FREE -n LV-3 VG
  Logical volume "LV-3" created.
Enter fullscreen mode Exit fullscreen mode

We can use ‘lvscan’ here,

[root@Linux-1 ~]# lvscan
  ACTIVE            '/dev/centos_linux-1/swap' [2.00 GiB] inherit
  ACTIVE            '/dev/centos_linux-1/root' [<17.00 GiB] inherit
  ACTIVE            '/dev/VG/LV-1' [1.00 GiB] inherit
  ACTIVE            '/dev/VG/LV-2' [1.99 GiB] inherit
  ACTIVE            '/dev/VG/LV-3' [1020.00 MiB] inherit
Enter fullscreen mode Exit fullscreen mode

We have logical volumes created inside the VG. Now we can just give them the filesystem,

[root@Linux-1 ~]# mkfs.xfs /dev/VG/LV-1
meta-data=/dev/VG/LV-1           isize=512    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=262144, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

[root@Linux-1 ~]# mkfs.xfs /dev/VG/LV-2
meta-data=/dev/VG/LV-2           isize=512    agcount=4, agsize=130560 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=522240, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

[root@Linux-1 ~]# mkfs.xfs /dev/VG/LV-3
meta-data=/dev/VG/LV-3           isize=512    agcount=4, agsize=65280 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=261120, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=855, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
Enter fullscreen mode Exit fullscreen mode

We can use blkid to see the filesystem type and short summary for our partitions

[root@Linux-1 ~]# blkid
/dev/sda1: UUID="2d2f3276-dc8a-403c-bb04-53e472b9184c" TYPE="xfs" 
/dev/sda2: UUID="UJrgq8-Rei9-528e-Hg0W-2QHD-n7Jp-OL4L13" TYPE="LVM2_member" 
/dev/sdb: UUID="gsms5M-xl9S-DxUo-BO4O-Qf3V-35ch-LXUOaB" TYPE="LVM2_member" 
/dev/sdc: UUID="l59wcF-iURW-suWZ-dfC6-Gf2z-5T0A-qb8xaK" TYPE="LVM2_member" 
/dev/sdd: UUID="BV4Gjx-jMBs-4K3V-sinB-lc7R-7DEM-bYYGXS" TYPE="LVM2_member" 
/dev/sde: UUID="UdVQLK-TlsI-cDsh-isfT-tuAT-yURe-np6RZW" TYPE="LVM2_member" 
/dev/mapper/centos_linux--1-root: UUID="8f826410-dc1d-4aba-bc6d-36d25621e5cf" TYPE="xfs" 
/dev/mapper/centos_linux--1-swap: UUID="ad01e948-9fdd-43ad-84e5-ce4ab14a995c" TYPE="swap" 
/dev/mapper/VG-LV--1: UUID="048f9bc6-f8f0-46b3-95f1-c7b1900adda6" TYPE="xfs" 
/dev/mapper/VG-LV--2: UUID="3074670a-13de-47ed-abc0-08b3319fb20e" TYPE="xfs" 
/dev/mapper/VG-LV--3: UUID="96062612-1a8a-4a21-bfc6-c16ccd3e96a6" TYPE="xfs"
Enter fullscreen mode Exit fullscreen mode

Now we can make 3 directories for these logical volumes to be mounted,

[root@Linux-1 ~]# mkdir /data1
[root@Linux-1 ~]# mkdir /data2
[root@Linux-1 ~]# mkdir /data3

[root@Linux-1 ~]# mount /dev/VG/LV-1 /data1
[root@Linux-1 ~]# mount /dev/VG/LV-2 /data2
[root@Linux-1 ~]# mount /dev/VG/LV-3 /data3
Enter fullscreen mode Exit fullscreen mode

Confirming the mount,

[root@Linux-1 ~]# df -h
Filesystem                        Size  Used Avail Use% Mounted on
devtmpfs                          475M     0  475M   0% /dev
tmpfs                             487M     0  487M   0% /dev/shm
tmpfs                             487M  7.6M  479M   2% /run
tmpfs                             487M     0  487M   0% /sys/fs/cgroup
/dev/mapper/centos_linux--1-root   17G  1.6G   16G  10% /
/dev/sda1                        1014M  168M  847M  17% /boot
tmpfs                              98M     0   98M   0% /run/user/0
/dev/mapper/VG-LV--1             1014M   33M  982M   4% /data1
/dev/mapper/VG-LV--2              2.0G   33M  2.0G   2% /data2
/dev/mapper/VG-LV--3             1017M   33M  985M   4% /data3
Enter fullscreen mode Exit fullscreen mode

If we want this mount to happen automatically, we can just mention them in the /etc/fstab file

For now, we will just umount these volumes.

[root@Linux-1 ~]# umount /data1
[root@Linux-1 ~]# umount /data2
[root@Linux-1 ~]# umount /data3
Enter fullscreen mode Exit fullscreen mode

We now want to add /dev/sde to this VG.

First,

[root@Linux-1 ~]# pvcreate /dev/sde
  Physical volume "/dev/sde" successfully created.
Enter fullscreen mode Exit fullscreen mode

Then we will use the ‘vgextend’ command,

[root@Linux-1 ~]# vgextend VG /dev/sde
  Volume group "VG" successfully extended
Enter fullscreen mode Exit fullscreen mode

We can see the VG

[root@Linux-1 ~]# vgdisplay VG
  --- Volume group ---
  VG Name               VG
  System ID             
  Format                lvm2
  Metadata Areas        4
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               0
  Max PV                0
  Cur PV                4
  Act PV                4
  VG Size               4.98 GiB
  PE Size               4.00 MiB
  Total PE              1276
  Alloc PE / Size       1021 / <3.99 GiB
  Free  PE / Size       255 / 1020.00 MiB
  VG UUID               DdSDJQ-EpWo-uy8S-Pb3m-9AWP-tzBw-vaLSov
Enter fullscreen mode Exit fullscreen mode

Now if we see using lvscan,

[root@Linux-1 ~]# lvscan
  ACTIVE            '/dev/centos_linux-1/swap' [2.00 GiB] inherit
  ACTIVE            '/dev/centos_linux-1/root' [<17.00 GiB] inherit
  ACTIVE            '/dev/VG/LV-1' [1.00 GiB] inherit
  ACTIVE            '/dev/VG/LV-2' [1.99 GiB] inherit
  ACTIVE            '/dev/VG/LV-3' [1020.00 MiB] inherit
Enter fullscreen mode Exit fullscreen mode

We want to add the extended logical volume to the LV-3,

[root@Linux-1 ~]# lvextend -l +100%FREE /dev/VG/LV-3
  Size of logical volume VG/LV-3 changed from 1020.00 MiB (255 extents) to 1.99 GiB (510 extents).
  Logical volume VG/LV-3 successfully resized.
Enter fullscreen mode Exit fullscreen mode

Now, if we mount the LV-3 into the data3 directory, we can find,

[root@Linux-1 ~]# mount /dev/VG/LV-3 /data3

[root@Linux-1 ~]# df -h
Filesystem                        Size  Used Avail Use% Mounted on
devtmpfs                          475M     0  475M   0% /dev
tmpfs                             487M     0  487M   0% /dev/shm
tmpfs                             487M  7.6M  479M   2% /run
tmpfs                             487M     0  487M   0% /sys/fs/cgroup
/dev/mapper/centos_linux--1-root   17G  1.6G   16G  10% /
/dev/sda1                        1014M  168M  847M  17% /boot
tmpfs                              98M     0   98M   0% /run/user/0
/dev/mapper/VG-LV--3             1017M   33M  985M   4% /data3
Enter fullscreen mode Exit fullscreen mode

In this current mounted state,

[root@Linux-1 ~]# xfs_growfs /dev/VG/LV-3
meta-data=/dev/mapper/VG-LV--3   isize=512    agcount=4, agsize=65280 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=261120, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=855, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 261120 to 522240
Enter fullscreen mode Exit fullscreen mode

Now if we check,

[root@Linux-1 ~]# df -h
Filesystem                        Size  Used Avail Use% Mounted on
devtmpfs                          475M     0  475M   0% /dev
tmpfs                             487M     0  487M   0% /dev/shm
tmpfs                             487M  7.6M  479M   2% /run
tmpfs                             487M     0  487M   0% /sys/fs/cgroup
/dev/mapper/centos_linux--1-root   17G  1.6G   16G  10% /
/dev/sda1                        1014M  168M  847M  17% /boot
tmpfs                              98M     0   98M   0% /run/user/0
/dev/mapper/VG-LV--3              2.0G   33M  2.0G   2% /data3
Enter fullscreen mode Exit fullscreen mode

Let’s add this to the auto-mount.


[root@Linux-1 ~]# vi /etc/fstab

# /etc/fstab
# Created by anaconda on Tue Jan 10 10:45:44 2023
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos_linux--1-root /                       xfs     defaults        0 0
UUID=2d2f3276-dc8a-403c-bb04-53e472b9184c /boot                   xfs     defaults        0 0
/dev/mapper/centos_linux--1-swap swap                    swap    defaults        0 0

/dev/VG/LV-1    /data1          xfs     defaults        0 0
/dev/VG/LV-2    /data2          xfs     defaults        0 0
/dev/VG/LV-3    /data3          xfs     defaults        0 0
Enter fullscreen mode Exit fullscreen mode

After the reboot,

[root@Linux-1 ~]# df -h
Filesystem                        Size  Used Avail Use% Mounted on
devtmpfs                          475M     0  475M   0% /dev
tmpfs                             487M     0  487M   0% /dev/shm
tmpfs                             487M  7.6M  479M   2% /run
tmpfs                             487M     0  487M   0% /sys/fs/cgroup
/dev/mapper/centos_linux--1-root   17G  1.6G   16G  10% /
/dev/mapper/VG-LV--3              2.0G   33M  2.0G   2% /data3
/dev/mapper/VG-LV--1             1014M   33M  982M   4% /data1
/dev/mapper/VG-LV--2              2.0G   33M  2.0G   2% /data2
/dev/sda1                        1014M  168M  847M  17% /boot
tmpfs                              98M     0   98M   0% /run/user/0
Enter fullscreen mode Exit fullscreen mode

Deleting the VG and its volumes.

Let’s umount and remove the auto-mount for these 3 volumes.

Removing the local volumes from the VG,

[root@Linux-1 ~]# lvremove /dev/VG/LV-1
Do you really want to remove active logical volume VG/LV-1? [y/n]: y
  Logical volume "LV-1" successfully removed

[root@Linux-1 ~]# lvremove /dev/VG/LV-2
Do you really want to remove active logical volume VG/LV-2? [y/n]: y
  Logical volume "LV-2" successfully removed

[root@Linux-1 ~]# lvremove /dev/VG/LV-3
Do you really want to remove active logical volume VG/LV-3? [y/n]: y
  Logical volume "LV-3" successfully removed
Enter fullscreen mode Exit fullscreen mode

Let’s check now,

[root@Linux-1 ~]# lvscan
  ACTIVE            '/dev/centos_linux-1/swap' [2.00 GiB] inherit
  ACTIVE            '/dev/centos_linux-1/root' [<17.00 GiB] inherit
Enter fullscreen mode Exit fullscreen mode

Removing the VG,

[root@Linux-1 ~]# vgremove VG
  Volume group "VG" successfully removed

[root@Linux-1 ~]# vgdisplay VG
  Volume group "VG" not found
  Cannot process volume group VG
Enter fullscreen mode Exit fullscreen mode

Removing the physical volumes,

[root@Linux-1 ~]# pvremove /dev/sdb
  Labels on physical volume "/dev/sdb" successfully wiped.

[root@Linux-1 ~]# pvremove /dev/sdc
  Labels on physical volume "/dev/sdc" successfully wiped.

[root@Linux-1 ~]# pvremove /dev/sdd
  Labels on physical volume "/dev/sdd" successfully wiped.

[root@Linux-1 ~]# pvremove /dev/sde
  Labels on physical volume "/dev/sde" successfully wiped.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)