Using LVM

June 6, 2014

Logical Volume Manager in Linux allows you to add physical disks to groups and then assign those groups to directories on disk. You can grow or shrink groups as you need to, and groups can span multiple physical disks. I'm using Debian Wheezy.

Definitions

  • A volume group (VG) gathers together a collection of Logical Volumes and Physical Volumes
  • A physical volume (PV) is a hard disk
  • A logical volume is visible as a disk partition to software, LVM manages what happens under the covers
  • Each physical volume is divided into physical extents (PE)
  • Each logical volume is divided into logical extents (LE)

Preparing Disks

Create partitions as normal with fdisk. Once that is complete, use pvcreate to make the partition usable by LVM:

# pvcreate /dev/sdd1
# pvcreate /dev/sdc1

Create Volume Group

The next step is to create and activate a volume group. (The default volume group on Debian is named after the host name)

# vgcreate VG1 /dev/sdd1 /dev/sdc1
  Volume group "VG1" successfully created

# vgchange -a y VG1
  0 logical volume(s) in volume group "VG1" now active

Create Logical Volume and format

We now need to create a logical volume within the volume group. This creates an 800Gb logical volume called musiclv, inside VG1

# lvcreate -L800 -nmusiclv VG1
# mkfs.ext4 /dev/mapper/VG1-musiclv

Mount this logical volume

I can now add this logical volume to /etc/fstab

/dev/mapper/VG1-musiclv /music ext4 defaults 0 0

and we can mount it:

# mount /music
# df
/dev/mapper/VG1-musiclv  825698728    4113636 779642052   1% /music

Reducing Partition Size

Lets say I actually wanted a 600Gb volume, not 800Gb. Here's the process of reducing the size of an ext4 partition

Unmount:

# df
/dev/mapper/VG1-musiclv  825698728    4113636 779642052   1% /music
# umount /music

Check the file system, then resize the ext4 partition:

# e2fsck -f /dev/mapper/VG1-musiclv
e2fsck 1.42.5 (29-Jul-2012)
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/mapper/VG1-musiclv: 225/52428800 files (33.3% non-contiguous), 4318927/209715200 blocks

# resize2fs -p /dev/mapper/VG1-musiclv 600G
resize2fs 1.42.5 (29-Jul-2012)
Resizing the filesystem on /dev/mapper/VG1-musiclv to 157286400 (4k) blocks.
Begin pass 3 (max = 6400)
Scanning inode table          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/mapper/VG1-musiclv is now 157286400 blocks long.

Reduce the logical volume to the same size:

# lvreduce -L 600G /dev/mapper/VG1-musiclv 
  WARNING: Reducing active logical volume to 600.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce musiclv? [y/n]: y
  Reducing logical volume musiclv to 600.00 GiB
  Logical volume musiclv successfully resized

Mount it again:

# mount /music
# df
/dev/mapper/VG1-musiclv  619273992    4114500 583702212   1% /music

Increasing Partition Size

I now want to increase the partition once more. This is much simpler than making it smaller!

# df
/dev/mapper/VG1-musiclv  619273992    4114500 583702212   1% /music
# umount /music

Extend the LV to 900Gb:

# df
 /dev/mapper/VG1-musiclv  619273992    4114500 583702212   1% /music
# umount /music

Extend logical volume and fsck:

# lvextend -L 900G /dev/mapper/VG1-musiclv 
  Extending logical volume musiclv to 900.00 GiB
  Logical volume musiclv successfully resized

# e2fsck -f /dev/mapper/VG1-musiclv 
e2fsck 1.42.5 (29-Jul-2012)
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/mapper/VG1-musiclv: 225/39321600 files (33.3% non-contiguous), 3496527/157286400 blocks

Resize filesystem to match and fsck again:

# resize2fs -p /dev/mapper/VG1-musiclv 
resize2fs 1.42.5 (29-Jul-2012)
Resizing the filesystem on /dev/mapper/VG1-musiclv to 235929600 (4k) blocks.
Begin pass 1 (max = 2400)
Extending the inode table     XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/mapper/VG1-musiclv is now 235929600 blocks long.

# e2fsck -f /dev/mapper/VG1-musiclv 
e2fsck 1.42.5 (29-Jul-2012)
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/mapper/VG1-musiclv: 225/58982400 files (33.3% non-contiguous), 4731152/235929600 blocks

Remount and check disk size:

# mount /music
# df
/dev/mapper/VG1-musiclv  928910792    4117000 877607872   1% /music

Info

To find out what's going on with LVM, use the vgdisplay command:

# vgdisplay
  --- Volume group ---
  VG Name               VG1
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  7
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                4
  Open LV               4
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               2.73 TiB
  PE Size               4.00 MiB
  Total PE              715337
  Alloc PE / Size       290607 / 1.11 TiB
  Free  PE / Size       424730 / 1.62 TiB
  VG UUID               YM0LJE-OPr0-akXJ-Exml-KuBY-vWdt-PrfNDv

You can also find out information about each physical volume, and which VG it's in:

# pvdisplay /dev/sdd1
  --- Physical volume ---
  PV Name               /dev/sdd1
  VG Name               VG1
  PV Size               931.51 GiB / not usable 3.19 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              238466
  Free PE               0
  Allocated PE          238466
  PV UUID               71WLcN-9MA8-lgqo-Zk58-sK3K-d0ML-x13cPb

References

Tags: lvm fdisk resize