The Sysadmin Notebook  

Sitemap

LVM2

Logical Volume Manager

Contents

LVM2 allows you to create a Volume Group from one or more physical volumes or disks. The volume group can then be divided into logical volumes by assigning space from the physical volumes. Logical volumes equate to partitions and can be used to contain filesystems

The 'lvm' command can be run to begin an interactive lvm session where commands can be issued to manage a logical volume

Gathering Information

Top Bottom

Three commands can be used to display detailed information about Volume Groups, Physical Volumes and Logical Volumes:

Summary information can be displayed via:

The vgscan, pvscan and lvscan display the same information in a different format.

Creating and Moving Volume Groups

Top Bottom

This section deals with adding a brand new harddisk to your system, managed by LVM. We are going to create a new volume group to hold media files on disk /dev/sdb.

Partition The Disk

Top Bottom

use cfdisk to create three partitions of type 8E - Linux LVM


                                cfdisk (util-linux-ng 2.16)

                              Disk Drive: /dev/sdb
                       Size: 500107862016 bytes, 500.1 GB
             Heads: 255   Sectors per Track: 63   Cylinders: 60801

    Name        Flags      Part Type  FS Type          [Label]        Size (MB)
 ------------------------------------------------------------------------------
    sdb1                    Primary   Linux LVM                       149996.21 
    sdb2                    Primary   Linux LVM                       149996.21
    sdb3                    Primary   Linux LVM                       200112.84

Initialise The Physical Volumes

Top Bottom

Use pvcreate to initialise the physical volumes for use with LVM:

lvm> 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

Add the PVs to a Volume Group

Top Bottom

Use vgcreate to add the physical volumes to a new volume group called 'media':

lvm> vgcreate media /dev/sdb1 /dev/sdb2 /dev/sdb3
  Volume group "media" successfully created

Add Logical Volumes to the Volume Group

Top Bottom

Use lvcreate to create the Logical Volumes that will contain the filesystems of the volume group:

lvm> lvcreate -L10G -ndocuments media
  Logical volume "documents" created
lvm> lvcreate -L100G -nmusic media
  Logical volume "music" created
lvm> lvcreate -L100G -nvideo media
  Logical volume "video" created
lvm> lvcreate -L15G -npictures media
  Logical volume "pictures" created

Add Filesystems to the Logical Volumes

Top Bottom

Use mkfs to create the filesystems:

mkfs -t ext4 /dev/media/documents
mkfs -t ext4 /dev/media/music
mkfs -t ext4 /dev/media/video
mkfs -t ext4 /dev/media/pictures

With the filesystems prepared, they can now be mounted as /dev/media/documents, /dev/media/music, /dev/media/video and /dev/media/pictures for the device name.

Migrating a Volume Group to Another System

Top Bottom

To migrate a Volume Group to another system the Volume Group must be exported from the old system and then imported to the new system. Prior to exporting the VG, umount the filesystems, then de-activate the Volume Group:

lvm> vgchange -an media
  4 logical volume(s) in volume group "media" now inactive
lvm> vgexport media
  Volume group "media" successfully exported

With the disks attached to the new system, use vgscan to see if the Volume group is visible and lvscan to see which LVs are active:

lvm> vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "media" using metadata type lvm2
  Found volume group "vg_zadoc" using metadata type lvm2
lvm> pvscan
  PV /dev/sdb1   VG media      lvm2 [139.69 GiB / 39.69 GiB free]
  PV /dev/sdb2   VG media      lvm2 [139.69 GiB / 114.69 GiB free]
  PV /dev/sdb3   VG media      lvm2 [186.37 GiB / 86.37 GiB free]
  PV /dev/sda2   VG vg_zadoc   lvm2 [465.25 GiB / 0    free]
  Total: 4 [931.00 GiB] / in use: 4 [931.00 GiB] / in no VG: 0 [0   ]
lvm> lvscan
  inactive          '/dev/media/music' [100.00 GiB] inherit
  inactive          '/dev/media/video' [100.00 GiB] inherit
  inactive          '/dev/media/documents' [10.00 GiB] inherit
  inactive          '/dev/media/pictures' [15.00 GiB] inherit
  ACTIVE            '/dev/vg_zadoc/lv_root' [50.00 GiB] inherit
  ACTIVE            '/dev/vg_zadoc/lv_home' [409.41 GiB] inherit
  ACTIVE            '/dev/vg_zadoc/lv_swap' [5.84 GiB] inherit

To import and activate the Volume group use vgimport and vgchange:

lvm> vgimport media
  Volume group "media" successfully imported
lvm> vgchange -ay media
  4 logical volume(s) in volume group "media" now active

Reducing the Size of a Logical Volume

Top Bottom

To reduce the size of a Logical Volume, the filesystem must first be reduced using resize2fs, before attempting to issue the lvreduce command. A disk check is also required beforehand:

unmount /dev/system/test
e2fsck -f /dev/system/test
resize2fs /dev/system/test 8G
lvreduce -L 8G /dev/system/test
mount /dev/system/test /media/test

Extending the Size of a Logical Volume

Top Bottom

When increasing the size of a Logical Volume, the filesystem is resized after the LV is extended

Increase LV to 12G:

lvm lvextend -L 12G /dev/system/test
e2fsck -f /dev/system/test
resize2fs /dev/system/test 

Increase LV by 2G:

lvm lvextend -L +2G /dev/system/test
e2fsck -f /dev/system/test
resize2fs /dev/system/test

Increase a volume by 20 physical extents:

lvm lvextend -l +20 /dev/system/test
e2fsck -f /dev/system/test
resize2fs /dev/system/test

Creating a Snapshot Logical Volume

Top Bottom

A snapshot logical volume allows you to create an exact copy of a volumes data at the time it was created:

lvcreate -L 5G -s -n homebackup /dev/system/home