Logical Volume Management#

As seen in the previous chapter the size of filesystems are limited to the size of a single harddisk. By using Logical Volume Management we can work around this limitation as it an abstraction layer between one or more harddisks and filesystems.

The Basics#

On our example system we can see that we already has been deployed with a volume group called systemvg on partition /dev/sda2, but both /dev/sdb and /dev/sdc are still unused when we check with lsblk.

# 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   11G  0 part
  ├─systemvg-root 253:0    0    4G  0 lvm  /
  ├─systemvg-var  253:1    0    4G  0 lvm  /var
  ├─systemvg-home 253:2    0 1024M  0 lvm  /home
  └─systemvg-swap 253:3    0    2G  0 lvm  [SWAP]
sdb                 8:16   0    8G  0 disk
sdc                 8:32   0    8G  0 disk
sr0                11:0    1 1024M  0 rom

A harddisk or partition can be prepared with the pvcreate command and verified with the command pvdisplay. In the example below harddisk /dev/sdb is prepared and verify the new physical volume. The pvdisplay command shows the maximum size that can be allocated and how much space has been allocated.

# pvcreate /dev/sdb
Physical volume "/dev/sdb" successfully created.

# pvdisplay /dev/sdb
  "/dev/sdb" is a new physical volume of "8.00 GiB"
    --- NEW Physical volume ---
    PV Name               /dev/sdb
    VG Name
    PV Size               8.00 GiB
    Allocatable           NO
    PE Size               0
    Total PE              0
    Free PE               0
    Allocated PE          0
    PV UUID               QFLoSk-uwBO-xSrP-nyLI-21wj-dCJ1-0pdLFy

One or more physical volumes can be combined into volume group with the command vgcreate. After creating a new volume group called datavg that contains the physical volume on /dev/sdb the specifications can be dispayed with command vgdisplay. Running vgdisplay and pvdisplay will the allocated space both within the volume group and physical volume.

# vgcreate datavg /dev/sdb
  Volume group "datavg" successfully created

# vgdisplay datavg
  --- Volume group ---
  VG Name               datavg
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <8.00 GiB
  PE Size               4.00 MiB
  Total PE              2047
  Alloc PE / Size       0 / 0
  Free  PE / Size       2047 / <8.00 GiB
  VG UUID               pfKpWs-Qb3Z-rQMw-1EqO-TYRw-ZhkF-FhiSwM

# pvdisplay /dev/sdb
  --- Physical volume ---
  PV Name               /dev/sdb
  VG Name               datavg
  PV Size               8.00 GiB / not usable 4.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              2047
  Free PE               2047
  Allocated PE          0
  PV UUID               QFLoSk-uwBO-xSrP-nyLI-21wj-dCJ1-0pdLFy

Creating a logical volume called fs01 with the lvcreate command and allocating space for it in volume group ** datavg**. Running the command vgdisplay and pvdisplay will show that the allocated and remaining space.

# lvcreate -L 100M datavg -n fs01

# vgdisplay datavg
  --- Volume group ---
  VG Name               datavg
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <8.00 GiB
  PE Size               4.00 MiB
  Total PE              2047
  Alloc PE / Size       25 / 100.00 MiB
  Free  PE / Size       2022 / <7.90 GiB
  VG UUID               pfKpWs-Qb3Z-rQMw-1EqO-TYRw-ZhkF-FhiSwM

# pvdisplay /dev/sdb
  --- Physical volume ---
  PV Name               /dev/sdb
  VG Name               datavg
  PV Size               8.00 GiB / not usable 4.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              2047
  Free PE               2022
  Allocated PE          25
  PV UUID               QFLoSk-uwBO-xSrP-nyLI-21wj-dCJ1-0pdLFy

The device mapper in Linux now provides a block device that can be accessed like a partition to create a filesystem and mount it.

# ls -lL /dev/mapper/
total 0
crw-------. 1 root root  10, 236 Dec 16 21:24 control
brw-rw----. 1 root disk 253,   2 Dec 16 21:43 datavg-fs01
brw-rw----. 1 root disk 253,   0 Dec 16 21:24 systemvg-root
brw-rw----. 1 root disk 253,   1 Dec 16 21:24 systemvg-swap

# mkfs.xfs /dev/mapper/datavg-fs01
meta-data=/dev/mapper/datavg-fs01 isize=512    agcount=4, agsize=6400 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1    bigtime=0 inobtcount=0
data     =                       bsize=4096   blocks=25600, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=1368, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
Discarding blocks...Done.

# mount /dev/mapper/datavg-fs01 /mnt

# df -h /mnt
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/datavg-fs01   95M  6.0M   89M   7% /mnt

After unmounten the filesystem the logical volume can de removed from the volume group with the lvremove command. The vgdisplay command also shows that the allocated space has been freed and is available to be used for another logical volume.

# umount /mnt

# lvremove /dev/mapper/datavg-fs01
Do you really want to remove active logical volume datavg/fs01? [y/n]: y
  Logical volume "fs01" successfully removed.

# vgdisplay datavg
  --- Volume group ---
  VG Name               datavg
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  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               <8.00 GiB
  PE Size               4.00 MiB
  Total PE              2047
  Alloc PE / Size       0 / 0
  Free  PE / Size       2047 / <8.00 GiB
  VG UUID               pfKpWs-Qb3Z-rQMw-1EqO-TYRw-ZhkF-FhiSwM

When volume group datavg is also removed with the vgremove command and /dev/sdb isn’t part of a volume group anymore as the line with VG Name is empty. After this the LVM-label on disk can be remove with pvremove and the physical volume is completely gone.

# vgremove datavg
  Volume group "datavg" successfully removed

# pvdisplay /dev/sdb
"/dev/sdb" is a new physical volume of "8.00 GiB"
--- NEW Physical volume ---
PV Name               /dev/sdb
VG Name
PV Size               8.00 GiB
Allocatable           NO
PE Size               0
Total PE              0
Free PE               0
Allocated PE          0
PV UUID               QFLoSk-uwBO-xSrP-nyLI-21wj-dCJ1-0pdLFy

# pvremove /dev/sdc1

Extending#

# vgextend vg_name /dev/sdc2
# pvmove /dev/sdc1 /dev/sdc2
# vgreduce vg_name /dev/sdc2
# lvextend -L +512M /dev/vg_name/lv_name
# lvextend -L +128 /dev/vg_name/lv_name
# lvextend -l+100%FREE /dev/vg_name/lv_name
# xfs_growfs /dev/vg_name/lv_name
# resize2fs /dev/vg_name/lv_name

paulin