Logical Volume Manager

Managing disk space has always been a significant task. Running out of disk space used to be the start of a long and complex series of tasks to increase the space available to a disk partition. It also required taking the system off-line. This usually involved installing a new hard drive, booting to recovery or single-user mode, creating a partition and a filesystem on the new hard drive, using temporary mount points to move the data from the too-small filesystem to the new, larger one, changing the content of the /etc/fstab file to reflect the correct device name for the new partition, and rebooting to remount the new filesystem on the correct mount point.

LVM allows for very flexible disk space management. It provides features like the ability to add disk space to a logical volume and its filesystem while that filesystem is mounted and active and it allows for the collection of multiple physical hard drives and partitions into a single volume group which can then be divided into logical volumes.

The volume manager also allows reducing the amount of disk space allocated to a logical volume, but there are a couple requirements. First, the volume must be unmounted. Second, the filesystem itself must be reduced in size before the volume on which it resides can be reduced.

 

LVM organizes storage in 3 layers :

  • Physical Volumes (Physical block devices or other disk-like devices (for example, other devices created by device mapper, like RAID arrays) are used by LVM as the raw building material for higher levels of abstraction. Physical volumes are regular storage devices. LVM writes a header to the device to allocate it for management)
  • Volume Groups (LVM combines physical volumes into storage pools known as volume groups. Volume groups abstract the characteristics of the underlying devices and function as a unified logical device with combined storage capacity of the component physical volumes)
  • Logical Volumes (A volume group can be sliced up into any number of logical volumes. Logical volumes are functionally equivalent to partitions on a physical disk, but with much more flexibility. Logical volumes are the primary component that users and applications will interact with)
  • Extents (Physical volume are called physical extents, while the extents of a logical volume are called logical extents. A logical volume is simply a mapping that LVM maintains between logical and physical extents. Because of this relationship, the extent size represents the smallest amount of space that can be allocated by LVM)

 

In practice, it looks like this :

Fist we will prepare 2 disk (size of disk 120gb) /dev/sda and /dev/sdb for LVM (this operation will delete all data on disk) :

parted -s /dev/sda mklabel gpt mkpart primary 0% 100% set 1 lvm on
parted -s /dev/sdb mklabel gpt mkpart primary 0% 100% set 1 lvm on
pvcreate /dev/sda1
pvcreate /dev/sdb1

Create volume gorup vg_debian :

vgcreate vg_debian /dev/sda1 /dev/sdb1

We will create logical volumes (lv_root - 30gb, lv_swap - 4gb, lv_home - rest all free space) :

lvcreate -n lv_root --size 30g vg_debian
lvcreate -n lv_swap --size 4g vg_debian
lvcreate -n lv_home -l 100%FREE vg_debian

Create file system on logical volumes :

mkfs.ext4 /dev/mapper/vg_debian-lv_root
mkswap /dev/mapper/vg_debian-lv_swap
mkfs.btrfs /dev/mapper/vg_debian-lv_home

Create subvolume for home pertition on btrfs :

mount /dev/mapper/vg_debian-lv_home /mnt
cd /mnt
btrfs subvolume create @home
cd ~
umount /mnt

Example of /etc/fstab

/dev/mapper/vg_debian-lv_root / ext4 defaults 0 1
/dev/mapper/vg_debian-lv_home /home btrfs subvol=@home,defaults,noatime,space_cache,autodefrag,discard,compress=lzo 0 2
/dev/mapper/vg_debian-lv_swap none swap sw 0 0

 

Problem: In VMware I resize disk /dev/sdb to 200gb and wont to add space to /home

Solution:

  • rescan disk device /dev/sdb
  • resize partition /dev/sdb1
  • resize physical volume
  • extend logical volume lv_home
  • resize by btrfs
echo 1 > /sys/block/sdb/device/rescan
parted -s /dev/sdb resizepart 1
pvresize /dev/sdb1
lvextend /dev/mapper/vg_debian-lv_home -l +100%FREE
btrfs filesystem resize max /home

Problem: Need to reduce size of lv_home for 4gb and add to lv_root 4gb

Solution:

  • reduce lv_home logical volume for 4gb
  • rebalance + resize of btrfs file system
  • extend lv_root volume for 4gb
  • resize ext4 file system
lvresize -L-4G /dev/mapper/vg_debian-lv_home
btrfs balance start -v /home
btrfs filesystem resize max /home
lvresize -L+4G /dev/mapper/vg_debian-lv_root
resize2fs /dev/mapper/vg_debian-lv_root

 

Problem: Need to add new hard-disk to PC and resize lv_home

Solution:

  • rescan disk device /dev/sdc
  • make gpt and partitation for lvm
  • add physical hard-disk to volume group vg_debian
  • extend logical volume lv_home
  • resize xfs file system 
echo 1 > /sys/block/sdc/device/rescan
parted -s /dev/sdc mklabel gpt mkpart primary 0% 100% set 1 lvm on
pvcreate /dev/sdc1
vgextend vg_debian /dev/sda1
lvextend /dev/mapper/vg_debian-lv_home -l +100%FREE
xfs_growfs -d /dev/mapper/vg_debian-lv_home

 

Add ping

Trackback URL : https://www.jurisic.org/trackback/9

Page top