File Systems: Part 3 -- Logical Volumes
This chapter discusses file systems in Linux.
Introduction
We have studied partitions and some benefits of partitions. However there is a drawback to partitions and that is, it is not easy to change sizes once they are in place. If we allocate some space for say one filesystem and only use a bit of it then the space is wasted. LVM ( Logical Volume Manager ) is a layer on top of the partitions and the file systems are instead mounted as volumes on this layer. Later on the sizes of these volumes can be changed. These volumes are called logical volumes. Of course the raw data will still be on a partition or partitions. The logical volumes are indicated by "lv" or "vg" .[amittal@hills ~]$ df Filesystem 1K-blocks Used Available Use% Mounted on devtmpfs 6010896 0 6010896 0% /dev tmpfs 6028268 0 6028268 0% /dev/shm tmpfs 6028268 591104 5437164 10% /run tmpfs 6028268 0 6028268 0% /sys/fs/cgroup /dev/mapper/vg00-root 52416000 34184640 18231360 66% / /dev/mapper/vg00-tmp 5232640 69968 5162672 2% /tmp /dev/mapper/vg00-var 26201600 1054556 25147044 5% /var /dev/mapper/vg01-users 52403200 14810120 37593080 29% /users /dev/mapper/vg02-students 524027908 61175032 462852876 12% /students /dev/sda1 2086912 318876 1768036 16% /boot /dev/mapper/vg00-logs 5232640 69740 5162900 2% /logs We can see the logical volumes with the "vg" in the device name.
LVM Process
1) A decision is made about which physical devices (entire disks and/or disk partitions) are to be devoted to the LVM. The partitions are created as usual. 2) Each device is marked for use by the LVM. The devices are then called physical volumes. 3) The physical volumes are grouped into 'pools', called volume groups. One or more volume groups are created from the physical volumes. The size of each volume group is the sum of the sizes of the physical volumes it contains. The volume group is then considered to be a contiguous space the size of the sum of the physical volumes in it, divided into equal-sized units called extents. 4) Each volume group is divided into logical volumes. Each logical volume contains an integral number of extents of the volume group, not necessarily contiguous. 5) A filesystem is created on the logical volumes by the LVM and they are mounted just like a disk partition. later, the size of the logical volume may be increased by adding unused extents from its volume group. Entire logical volumes can also be removed, returning their extents to the volume group. 6) If a volume group runs out of space, unused or newly-created physical volumes can be added to it.
Creating volume
You might not have a LVM installed on your Linux system. This can be done as follows:apt-get update apt-get install lvm2The creation is done with the "pvcreate" command. We specify the device name of the partition as the argument. An entire disk can also be a physical volume.
Creating volume group
This is done with the "vgcreate" command.vgcreate volumegroupname physicalvolume1 [ physicalvolume2 ... ]
Creating logical volume
This is the last step wehre we create the logical volume from the volume group.lvcreate --name logicalvolumename --size N volumegroupnameThis will create device file entry in "/dev/volume group name/logical volume name" . We can use this device file to create the file system and mount it.
Example
Let's see an example using our flash drive. We first use the "fdisk" to create 3 partitions of 5 Gb each. We will then create 2 logical volumes of 4Gb and 7Gb .
... Using default response p. Partition number (1-4, default 1): First sector (2048-61341663, default 2048): Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-61341663, default 61341663): +5G Created a new partition 1 of type 'Linux' and of size 5 GiB. Command (m for help): n Partition type p primary (1 primary, 0 extended, 3 free) e extended (container for logical partitions) Select (default p): Using default response p. Partition number (2-4, default 2): First sector (10487808-61341663, default 10487808): Last sector, +/-sectors or +/-size{K,M,G,T,P} (10487808-61341663, default 61341663): +5G Created a new partition 2 of type 'Linux' and of size 5 GiB. ... Check the partitions on the flash drive. root@ajkumar08-PC:/dev# fdisk -l Disk /dev/sda: 149.05 GiB, 160041885696 bytes, 312581808 sectors Disk model: WDC WD1600BEVS-2 Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0xdb985c72 Device Boot Start End Sectors Size Id Type /dev/sda1 * 2048 310579199 310577152 148.1G 83 Linux /dev/sda2 310581246 312580095 1998850 976M 5 Extended /dev/sda5 310581248 312580095 1998848 976M 82 Linux swap / Solaris Disk /dev/sdb: 29.25 GiB, 31406948352 bytes, 61341696 sectors Disk model: Cruzer Glide Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0x00000000 Device Boot Start End Sectors Size Id Type /dev/sdb1 2048 10487807 10485760 5G 83 Linux /dev/sdb2 10487808 20973567 10485760 5G 83 Linux /dev/sdb3 20973568 31459327 10485760 5G 83 Linux root@ajkumar08-PC:/dev#The partition's device file names are listed using fdisk. We now create the physical volumes.
root@ajkumar08-PC:/dev# pvcreate /dev/sdb1 /dev/sdb2 /dev/sdb3 Physical volume "/dev/sdb1" successfully created. Physical volume "/dev/sdb2" successfully created. Physical volume "/dev/sdb3" successfully created.We now create the volume group with the simple name "vg01".
root@ajkumar08-PC:/dev# vgcreate vg01 /dev/sdb1 /dev/sdb2 /dev/sdb3 Volume group "vg01" successfully created root@ajkumar08-PC:/dev# root@ajkumar08-PC:/dev# root@ajkumar08-PC:/dev# lvcreate --name lv01 --size 4G vg01 Logical volume "lv01" created. root@ajkumar08-PC:/dev# lvcreate --name lv02 --size 7G vg01 Logical volume "lv02" created. root@ajkumar08-PC:/dev# lvscan ACTIVE '/dev/vg01/lv01' [4.00 GiB] inherit ACTIVE '/dev/vg01/lv02' [7.00 GiB] inherit root@ajkumar08-PC:/dev# pvscan PV /dev/sdb1 VG vg01 lvm2 [<5.00 GiB / 1020.00 MiB free] PV /dev/sdb2 VG vg01 lvm2 [<5.00 GiB / 0 free] PV /dev/sdb3 VG vg01 lvm2 [<5.00 GiB / 2.99 GiB free] Total: 3 [<14.99 GiB] / in use: 3 [<14.99 GiB] / in no VG: 0 [0 ] root@ajkumar08-PC:/dev# vgscan Found volume group "vg01" using metadata type lvm2We now create 2 logical volumes with names "lv01" and "lv02" of sizes 4Gb and 7Gb. We confirm it by using lvscan utility.
The status of physical and logical volumes and of volume groups can be examined using the nine commands {pv,vg,lv}{s,scan,display} To extend an existing volume group or logical volume, simply use {vg,lv}extend In fact, there are a huge number of LVM commands. To see them, execute the following commands for physical volume commands: man -k pv | grep '^pv' for volume group commands: man -k vg | grep '^vg' for logical volume commands: man -k lv | grep '^lv' root@ajkumar08-PC:/dev# lvdisplay --- Logical volume --- LV Path /dev/vg01/lv01 LV Name lv01 VG Name vg01 LV UUID T16dhL-BFOk-TcXE-Nyh3-B515-evMX-Gwnm57 LV Write Access read/write The command "lvdisplay" shows detailed output. We now create the file systems : root@ajkumar08-PC:/dev# mkfs -t ext4 /dev/vg01/lv01 mke2fs 1.46.2 (28-Feb-2021) Creating filesystem with 1048576 4k blocks and 262144 inodes Filesystem UUID: 22cdd15a-0d2a-426f-8592-c23c2c243a66 Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 Allocating group tables: done Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done root@ajkumar08-PC:/dev# root@ajkumar08-PC:/dev# mkfs -t ext4 /dev/vg01/lv02 Now we create the folders for mounting. root@ajkumar08-PC:/home/ajay# ls a.out Documents FLASH Music Public Templates v02 Desktop Downloads m1.sh Pictures scripts v01 Videos root@ajkumar08-PC:/home/ajay# mount /dev/vg01/lv01 /home/ajay/v01 root@ajkumar08-PC:/home/ajay# mount /dev/vg01/lv02 /home/ajay/v02 root@ajkumar08-PC:/home/ajay# Create a file on logical volume 1 . root@ajkumar08-PC:/home/ajay/v01# ls lost+found root@ajkumar08-PC:/home/ajay/v01# date > 1.txt root@ajkumar08-PC:/home/ajay/v01#
Resizing
It may be possible and possibly easier to do this via GUI on the Linux system. We shall explore the command line commands. To increase the size of the logical volume we need to first increase the size of the logical volume using "lvextend" and then increase the size of the file system using "resize2fs" or "xfs_growfs". The "resize2fs" command works with the ext systems.Let's try to extend the logical volume of 4GB to 5Gb.
root@ajkumar08-PC:/home/ajay/v01# pvscan PV /dev/sdb1 VG vg01 lvm2 [<5.00 GiB / 1020.00 MiB free] PV /dev/sdb2 VG vg01 lvm2 [<5.00 GiB / 0 free] PV /dev/sdb3 VG vg01 lvm2 [<5.00 GiB / 2.99 GiB free] Total: 3 [<14.99 GiB] / in use: 3 [<14.99 GiB] / in no VG: 0 [0 ] root@ajkumar08-PC:/home/ajay/v01# lvscan ACTIVE '/dev/vg01/lv01' [4.00 GiB] inherit ACTIVE '/dev/vg01/lv02' [7.00 GiB] inherit root@ajkumar08-PC:/home/ajay/v01# df Filesystem 1K-blocks Used Available Use% Mounted on udev 946176 0 946176 0% /dev tmpfs 192808 1432 191376 1% /run /dev/sda1 151741000 4517244 139442944 4% / tmpfs 964028 0 964028 0% /dev/shm tmpfs 5120 4 5116 1% /run/lock tmpfs 192804 128 192676 1% /run/user/1000 /dev/mapper/vg01-lv01 4046560 28 3820436 1% /home/ajay/v01 /dev/mapper/vg01-lv02 7127068 24 6743660 1% /home/ajay/v02 root@ajkumar08-PC:/home/ajay/v01# lvextend -r -L +1G /dev/vg01/lv01 Size of logical volume vg01/lv01 changed from 4.00 GiB (1024 extents) to 5.00 GiB (1280 extents). Logical volume vg01/lv01 successfully resized. resize2fs 1.46.2 (28-Feb-2021) Filesystem at /dev/mapper/vg01-lv01 is mounted on /home/ajay/v01; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 1 The filesystem on /dev/mapper/vg01-lv01 is now 1310720 (4k) blocks long. root@ajkumar08-PC:/home/ajay/v01# pvscan PV /dev/sdb1 VG vg01 lvm2 [<5.00 GiB / 0 free] PV /dev/sdb2 VG vg01 lvm2 [<5.00 GiB / 0 free] PV /dev/sdb3 VG vg01 lvm2 [<5.00 GiB / <2.99 GiB free] Total: 3 [<14.99 GiB] / in use: 3 [<14.99 GiB] / in no VG: 0 [0 ] root@ajkumar08-PC:/home/ajay/v01# lvscan ACTIVE '/dev/vg01/lv01' [5.00 GiB] inherit ACTIVE '/dev/vg01/lv02' [7.00 GiB] inherit root@ajkumar08-PC:/home/ajay/v01# df Filesystem 1K-blocks Used Available Use% Mounted on udev 946176 0 946176 0% /dev tmpfs 192808 1428 191380 1% /run /dev/sda1 151741000 4517268 139442920 4% / tmpfs 964028 0 964028 0% /dev/shm tmpfs 5120 4 5116 1% /run/lock tmpfs 192804 128 192676 1% /run/user/1000 /dev/mapper/vg01-lv01 5078688 28 4810624 1% /home/ajay/v01 /dev/mapper/vg01-lv02 7127068 24 6743660 1% /home/ajay/v02 root@ajkumar08-PC:/home/ajay/v01#The "lvextend" has a handy option "-r" that extends the underlying filesystem after the logical volume is extended. We confirm the size change by using "pvscan", "lvscan" and "df" commands. Now we use "lvreduce" to shrink the logical volume. We need to be careful because data might be lost if our size parameters are not correct. We can use the "-r" option to reduce the filesystem but we use "resize2fs" to show how the command is used.
root@ajkumar08-PC:/home/ajay# umount /dev/vg01/lv01 root@ajkumar08-PC:/home/ajay# resize2fs /dev/vg01/lv01 4G resize2fs 1.46.2 (28-Feb-2021) Please run 'e2fsck -f /dev/vg01/lv01' first. //We follow the advice and run "e2fsck" . root@ajkumar08-PC:/home/ajay# e2fsck -f /dev/vg01/lv01 e2fsck 1.46.2 (28-Feb-2021) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/vg01/lv01: 12/327680 files (0.0% non-contiguous), 41055/1310720 blocks //Reduce the file system to 4Gb root@ajkumar08-PC:/home/ajay# resize2fs /dev/vg01/lv01 4G resize2fs 1.46.2 (28-Feb-2021) Resizing the filesystem on /dev/vg01/lv01 to 1048576 (4k) blocks. The filesystem on /dev/vg01/lv01 is now 1048576 (4k) blocks long. //Reduce the logical volume by 1Gb to get the original size of 4Gb. root@ajkumar08-PC:/home/ajay# lvreduce -L -1G /dev/vg01/lv01 WARNING: Reducing active logical volume to 4.00 GiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce vg01/lv01? [y/n]: y Size of logical volume vg01/lv01 changed from 5.00 GiB (1280 extents) to 4.00 GiB (1024 extents). Logical volume vg01/lv01 successfully resized. root@ajkumar08-PC:/home/ajay# mount /dev/vg01/lv01 /home/ajay/v01 root@ajkumar08-PC:/home/ajay# lvscan ACTIVE '/dev/vg01/lv01' [4.00 GiB] inherit ACTIVE '/dev/vg01/lv02' [7.00 GiB] inherit root@ajkumar08-PC:/home/ajay#