Nerveware


Logical Volume Manager

Logical Volume Manager, also known as LVM, manages storage devices for the Linux kernel. LVM enables the user to combine physical disks into a single partition. As such, a striped volume is created. In this article I will show the basic steps of creating virtual groups and logical volumes.

Installation

LVM is installable on Debian as lvm2.

apt-get install lvm2

Creating volumes

In this example I will create a logical volume from two usb sticks.

$ fdisk -l /dev/sdb Disk /dev/sdb: 7.5 GiB, 8053063680 bytes, 15728640 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes

For every physical disk a physical volume must be created. Afterwards, we can join these physical volumes into a virtual group. Virtual groups, in turn, can hold one or multiple logical volumes.

$ pvcreate /dev/sdb Physical volume "/dev/sdb" successfully created. $ vgcreate vg_usbs /dev/sdb Volume group "vg_usbs" successfully created $ lvcreate -n lv_usbs --size 512m vg_usbs Logical volume "lv_usbs" created.

An entry on the file system should be created by now. It's located in /dev/mapper/ and is, in our case, named vg_usbs-lv_usbs. The virtual group also exists, namely as /dev/vg_usbs. We can create another logical volume by executing the lvcreate command again.

$ ls -l /dev/mapper/ crw------- 1 root root 10, 236 Sep 2 16:52 control lrwxrwxrwx 1 root root 7 Sep 2 16:52 vg_usbs-lv_usbs -> ../dm-0 $ ls -l /dev/vg_usbs total 0 lrwxrwxrwx 1 root root 7 Sep 2 16:52 lv_usbs -> ../dm-0 $ lvcreate -n lv_usbs_1 --size 512m vg_usbs $ ls /dev/dm-* /dev/dm-0 /dev/dm-1

The entries in /dev/mapper are available to use as one would use disks normally. They're ready to get a file system and can be auto mounted in fstab, for which I reccomend to use blkid.

$ mkfs.ext4 /dev/mapper/vg_usbs-lv_usbs mke2fs 1.43.4 (31-Jan-2017) Creating filesystem with 131072 4k blocks and 32768 inodes Filesystem UUID: f847738f-6c61-47b3-8f44-bedac88c7027 Superblock backups stored on blocks: 32768, 98304 Allocating group tables: done Writing inode tables: done Creating journal (4096 blocks): done Writing superblocks and filesystem accounting information: done $ mount /dev/mapper/vg_usbs-lv_usbs /mnt/usb/ $ ls -la /mnt/usb lost+found $ blkid /dev/mapper/vg_usbs-lv_usbs /dev/mapper/vg_usbs-lv_usbs: UUID="f847738f-6c61-47b3-8f44-bedac88c7027" TYPE="ext4" $ echo "UUID=f847738f-6c61-47b3-8f44-bedac88c7027 /mnt/usb ext4 errors=remount-ro 0 0" >> /etc/fstab

Every volume has a convenient command to display its content.

$ pvdisplay --- Physical volume --- PV Name /dev/sdb VG Name vg_usbs PV Size 7.50 GiB / not usable 4.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 1919 Free PE 1663 Allocated PE 256 PV UUID XexhMW-m0Wx-hhBC-A3wN-t10F-6xIc-XvGuZS $ vgdisplay --- Volume group --- VG Name vg_usbs 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 0 Max PV 0 Cur PV 1 Act PV 1 VG Size 7.50 GiB PE Size 4.00 MiB Total PE 1919 Alloc PE / Size 256 / 1.00 GiB Free PE / Size 1663 / 6.50 GiB VG UUID d7psdI-BDoe-axqg-u2jX-qsdQ-rM3r-cYm9pd $ lvdisplay --- Logical volume --- LV Path /dev/vg_usbs/lv_usbs LV Name lv_usbs VG Name vg_usbs LV UUID gKI6t9-dcw2-fT50-4HfK-AQ9J-K1Is-g3OTB0 LV Write Access read/write LV Creation host, time lvm-test, 2019-09-02 16:52:57 +0200 LV Status available # open 0 LV Size 512.00 MiB Current LE 128 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 254:0 --- Logical volume --- LV Path /dev/vg_usbs/lv_usbs_1 LV Name lv_usbs_1 VG Name vg_usbs LV UUID ntX80L-0WhW-jK2O-DipI-eBdd-Vktz-pJJLHd LV Write Access read/write LV Creation host, time lvm-test, 2019-09-02 16:58:47 +0200 LV Status available # open 0 LV Size 512.00 MiB Current LE 128 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 254:1

Extending volumes

Extending volumes is as easy creating them. Below, I'll add an SD card to my volume group. Now, I will have almost 10 GB of data available. To make use of it, I will add 8G to the logical lv_usbs. $ pvcreate /dev/mmcblk0 Physical volume "/dev/mmcblk0" successfully created. $ vgextend vg_usbs /dev/mmcblk0 Volume group "vg_usbs" successfully extended $ vgdisplay --- Volume group --- VG Name vg_usbs System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 4 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 1 Max PV 0 Cur PV 2 Act PV 2 VG Size 9.36 GiB PE Size 4.00 MiB Total PE 2396 Alloc PE / Size 256 / 1.00 GiB Free PE / Size 2140 / 8.36 GiB VG UUID d7psdI-BDoe-axqg-u2jX-qsdQ-rM3r-cYm9pd $ lvextend -L +8G /dev/mapper/vg_usbs-lv_usbs Size of logical volume vg_usbs/lv_usbs changed from 512.00 MiB (128 extents) to 8.50 GiB (2176 extents). Logical volume vg_usbs/lv_usbs successfully resized. $ fdisk -l /dev/mapper/vg_usbs-lv_usbs Disk /dev/mapper/vg_usbs-lv_usbs: 8.5 GiB, 9126805504 bytes, 17825792 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Make sure you resize the filesystem to the new parition size. For EXT, use resize2fs. Use the approperiate tool for the filesystem installed. $ resize2fs /dev/mapper/vg_usbs-lv_usbs resize2fs 1.43.4 (31-Jan-2017) Resizing the filesystem on /dev/mapper/vg_usbs-lv_usbs to 2228224 (4k) blocks. The filesystem on /dev/mapper/vg_usbs-lv_usbs is now 2228224 (4k) blocks long.

Remove volumes

Remove volumes should be done in opposite order. Start by deleting the logical volumes, than the virtual groups and finally remove the physical volumes (if necessary). $ lvremove /dev/mapper/vg_usbs-lv_usbs* Do you really want to remove active logical volume vg_usbs/lv_usbs? [y/n]: y Logical volume "lv_usbs" successfully removed Do you really want to remove active logical volume vg_usbs/lv_usbs_1? [y/n]: y Logical volume "lv_usbs_1" successfully removed $ vgremove vg_usbs Volume group "vg_usbs" successfully removed $ pvremove /dev/mmcblk0 Labels on physical volume "/dev/mmcblk0" successfully wiped. $ pvremove /dev/sdb Labels on physical volume "/dev/sdb" successfully wiped.