Logical Volume Management: A Practical Illustration

# Runas root
kim@kimlinux:~$ sudo su -

# List all disks
fdisk -l | grep '^Disk'

# output
root@kimlinux:~# fdisk -l | grep '^Disk'
Disk /dev/loop0: 55.33 MiB, 58007552 bytes, 113296 sectors
Disk /dev/loop1: 96.64 MiB, 101318656 bytes, 197888 sectors
Disk /dev/loop2: 62.9 MiB, 65105920 bytes, 127160 sectors
Disk /dev/loop3: 161.42 MiB, 169254912 bytes, 330576 sectors
Disk /dev/loop4: 29.91 MiB, 31342592 bytes, 61216 sectors
Disk /dev/loop5: 38.99 MiB, 40857600 bytes, 79800 sectors
Disk /dev/loop6: 39.6 MiB, 41508864 bytes, 81072 sectors
Disk /dev/loop7: 4.1 MiB, 4206592 bytes, 8216 sectors
Disk /dev/sda: 14.94 GiB, 16013942784 bytes, 31277232 sectors
Disk model: KINGSTON SNS4151
Disklabel type: gpt
Disk identifier: 3F4DAD50-B665-7A43-84B7-5D9DD359E99F
Disk /dev/sdb: 476.96 GiB, 512110190592 bytes, 1000215216 sectors
Disk model: Samsung SSD 850 
Disklabel type: gpt
Disk identifier: 1D8CE9C6-B07F-3D46-8FA3-3ABC71E0985C
Disk /dev/loop8: 226.68 MiB, 237678592 bytes, 464216 sectors
Disk /dev/loop9: 214.10 MiB, 225415168 bytes, 440264 sectors

# Partition test disk
testDisk1=/dev/sda
dd if=/dev/zero of=$testDisk1 bs=512 count=1

# output
root@kimlinux:~# dd if=/dev/zero of=$testDisk1 bs=512 count=1
1+0 records in
1+0 records out
512 bytes copied, 0.00157508 s, 325 kB/s

# Create LVM disk
pvcreate $testDisk1

# output
root@kimlinux:~# pvcreate $testDisk1
  Physical volume "/dev/sda" successfully created.

# View Existing LVM disks
root@kimlinux:~# pvscan -v
  PV /dev/sda                      lvm2 [14.91 GiB]
  Total: 1 [14.91 GiB] / in use: 0 [0   ] / in no VG: 1 [14.91 GiB]
  
# Create LVM group and assign member(s)
lvmGroup=volumeGroup1
vgcreate $lvmGroup $testDisk1

# Output:
root@kimlinux:~# vgcreate $lvmGroup $testDisk1
  Volume group "volumeGroup1" successfully created

# Remove volume group (if needed to change group name)
# vgremove $lvmGroup

# Output:
root@kimlinux:~# vgremove $lvmGroup
  Volume group "volumeGroup1" successfully removed

# Extend (if there are other 'devices' or disks)
#testDisk2=/dev/sdb3
#vgextend $lvmGroup $testDisk2

# Creating a logical volume with available disk space
volumename=vmstorage1
groupname=volumegroup1
partition=/dev/$groupname/$volumename
lvcreate -l 100%FREE -n $volumename $groupname

# Format volume
mkfs.xfs $partition # xfs is faster than ext4

# Mount logical volume to root as directory named virtualmachines
mkdir /$volumename
mount $partition /$volumename

# Sample output:
rambo@kimlinux:/home/rambo# pvscan
  PV /dev/sda    VG volumegroup1    lvm2 [14.91 GiB / 14.91 GiB free]
  PV /dev/sdb3   VG volumegroup1    lvm2 [76.64 GiB / 76.64 GiB free]
  Total: 2 [91.55 GiB] / in use: 2 [91.55 GiB] / in no VG: 0 [0   ]
rambo@kimlinux:/home/rambo# lvcreate -l 100%FREE -n vmstorage1 volumegroup1
  Logical volume "vmstorage1" created.
rambo@kimlinux:/home/rambo# lvscan
  ACTIVE            '/dev/volumegroup1/vmstorage1' [91.55 GiB] inherit
rambo@kimlinux:/home/rambo# mkfs.xfs $partition
meta-data=/dev/volumegroup1/vmstorage1 isize=512    agcount=4, agsize=5999872 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=23999488, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=11718, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
rambo@kimlinux:/home/rambo# mount $partition /$volumename
mount: /vmstorage1: mount point does not exist.
rambo@kimlinux:/home/rambo# mkdir /$volumename
rambo@kimlinux:/home/rambo# mount $partition /$volumename/
rambo@kimlinux:/home/rambo# df -h
Filesystem                           Size  Used Avail Use% Mounted on                         
udev                                  16G     0   16G   0% /dev                               
tmpfs                                3.2G  1.8M  3.2G   1% /run                               
/dev/sdb2                            393G  9.9G  363G   3% /
tmpfs                                 16G  613M   16G   4% /dev/shm
tmpfs                                5.0M  4.0K  5.0M   1% /run/lock
tmpfs                                 16G     0   16G   0% /sys/fs/cgroup
tmpfs                                 16G  8.0K   16G   1% /tmp
/dev/loop1                            97M   97M     0 100% /snap/core/9804
/dev/loop2                           4.2M  4.2M     0 100% /snap/notepad-plus-plus/238
/dev/loop4                            63M   63M     0 100% /snap/gtk-common-themes/1506
/dev/loop3                            40M   40M     0 100% /snap/remmina/4324
/dev/loop5                            30M   30M     0 100% /snap/snapd/8790
/dev/loop0                            56M   56M     0 100% /snap/core18/1885
/dev/loop6                            60M   60M     0 100% /snap/powershell/137
/dev/loop7                            39M   39M     0 100% /snap/remmina/4309
/dev/loop8                           162M  162M     0 100% /snap/gnome-3-28-1804/128
/dev/loop9                           227M  227M     0 100% /snap/wine-platform-runtime/145
/dev/loop10                          215M  215M     0 100% /snap/wine-platform-5-stable/5
/dev/sdb1                            300M  7.8M  292M   3% /boot/efi
tmpfs                                3.2G  8.0K  3.2G   1% /run/user/1000
/dev/mapper/volumegroup1-vmstorage1   92G  686M   91G   1% /vmstorage1
# More illustrations...
# Runas root
ali@kimlinux:~$ sudo su
[sudo] password for ali:

# check lvm devices
ali@kimlinux:/home/ali# pvscan
  PV /dev/sda    VG volumegroup1    lvm2 [14.91 GiB / 0    free]
  PV /dev/sdb3   VG volumegroup1    lvm2 [76.64 GiB / 0    free]
  Total: 2 [91.55 GiB] / in use: 2 [91.55 GiB] / in no VG: 0 [0   ]
  
# check mount  
ali@kimlinux:/home/ali# ls /vmstorage1/
Windows1

# check logical volumes
ali@kimlinux:/home/ali# lvscan
  ACTIVE            '/dev/volumegroup1/vmstorage1' [91.55 GiB] inherit

# unmount logical volume
ali@kimlinux:/home/ali# umount /dev/volumegroup1/vmstorage1 
umount: /dev/volumegroup1/vmstorage1: not mounted.

# remove logical volume
ali@kimlinux:/home/ali# lvremove /dev/volumegroup1/vmstorage1 
Do you really want to remove and DISCARD active logical volume volumegroup1/vmstorage1? [y/n]: y
  Logical volume "vmstorage1" successfully removed

# check volume groups
ali@kimlinux:/home/ali# vgscan
  Found volume group "volumegroup1" using metadata type lvm2
  
# recreating logical volumes
ali@kimlinux:/home/ali# volumename=vmstorage1
ali@kimlinux:/home/ali# groupname=volumegroup1
ali@kimlinux:/home/ali# partition=/dev/$groupname/$volumename

# create other volume
ali@kimlinux:/home/ali# lvcreate -l 100%FREE -n $volumename $groupname
  Logical volume "vmstorage1" created.

# check results
ali@kimlinux:/home/ali# lvscan
  ACTIVE            '/dev/volumegroup1/swap' [32.00 GiB] inherit
  ACTIVE            '/dev/volumegroup1/vmstorage1' [59.55 GiB] inherit

# creating file system for the other volume
ali@kimlinux:/home/ali# mkfs.xfs $partition
meta-data=/dev/volumegroup1/vmstorage1 isize=512    agcount=4, agsize=3902720 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=15610880, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=7622, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

# mount other volume onto its directory
ali@kimlinux:/home/ali# mount $partition /$volumename
Posted on Categories Linux

Leave a Reply

Your email address will not be published. Required fields are marked *