Quickstart LVM-EN

Da PoliArch.

Introduction

This document explains how to create, modify, extend and restrict a LVM volume. It explains also how to mount it in an operating system different from the original and how to restore/recover in case of emergency.

Create a LVM volume

Create the partitions to add to the volume with fdisk or cfdisk. The partitions must be of "Linux LVM" type, that is 8E. Than it's needed to create physical volumes in these partitions:

# pvcreate /dev/sda1 /dev/sdb1

Create the Volume Group with a name at your choice (I chose backupVG0):

# vgcreate -s 32M backupVG0 /dev/sda1 /dev/sdb1

The -s option defines the extent dimension. The maximum number of extents is fixed, so for bigger volumes it's needed to set bigger extent. The default is 4M, that sets the maximum for a volume to 256GB. 16M sets the maximum to 1TB. 32M fixes the maximum to 2TB. This parameter cannot be modified later.

Then it's needed to create a Logical Volume with a name at your choice (I used backupLV0):

# lvcreate -l 100%VG -n backupLV0 backupVG0

The parameter -l controls the initial dimension of created volume, that can be smaller then the available space, and possibly increase when there will be added new Physical Volumes to the volume group. It's possible to set the exact dimension (es. 40 G), or a percentage (100%VG == use all the capability of the volume group).

Format the logical volume:

# mkfs.ext3 -m 0.1 -L backup -O dir_index /dev/backupVG0/backupLV0

/dev/backupVG0/backupLV0 is the device name associated to Logical Volume, made starting from the volume group name and the logical volume name defined before (for the logical volume, like for the partitions, it's possible to use label and uuid for reference it in configuration files). Alternatively it's possible to access the logical volume also with /dev/mapper/backupVG0-backupLV0

Mount the Logical Volume:

# mount /dev/backupVG0/backupLV0 /mnt/backup/

Modify a Logical Volume

Expand a LV

It's possible to expand a logical volume, adding new physical volumes to an existent group and extending the logical volume on the new available space, without rebooting the system. Besides with the kernel 2.6 or newer versions of e2fsprogs it's possible to resize an ext2/ext3 partition without unmounting it. This is possible because with lvm the kernel sees immediately the size increase of the volume, also if is mounted (with the physical partitions, instead, it's necessary to unmount the partition).

  • Create a "Linux LVM" partition like done before, with cfdisk;
  • Make it a new physical volume with pvcreate.
  • Add it to an existent volume group with vgextend.
  • Expand the logical volume with lvextend.
  • Resize the file system (only with ext3) with resize2fs.

Usually is a good idea to check the filesystem with e2fsck before resizing.

Decrease a LV

To reduce a logical volume to a size smaller than the size of inner file system, is in any case needed to unmount the file system and resize it before (if there is enough empty space).

  • unmount the LV
  • check the file system with e2fsck -f
  • resize the file system with resize2fs
  • reduce the LV with lvreduce

If it's needed to remove a physical volume from the volume group, we need first to move the data on another physical volume with pvmove, and then remove the physical volume with vgreduce.

Metadata Backup and restore

Introduction

The metadata describe the LV and allow to mount in the right way. Without them the LV doesn't work.

Backup

It's possible to backup the metadata with the command:

# vgcfgbackup -f backupVG0.lvm

Restore

To restore the metadata from a backup file you can use the commend vgcfgrestore. List the volume groups saved in the backup file:

# vgcfgrestore -f backupVG0.lvm -l

Restore a volume group giving his name to vgcfgrestore:

# vgcfgrestore -f backupVG0.lvm backupVG0

Insted is useful to made a backup of /etc/lvm (the equivalent file is /etc/lvm/system or /etc/lvm/backup, they are editable text files, according to your needs). To restore the metadata on physical volumes use:

# pvcreate --restorefile backupVG0

Access from a live-cd

To access the logical volume from a live-cd, must be available the lvm2 tools (available in poliarch). Identify the existent partitions, and the ones that could be PV:

# fdisk -lu

Look for existent physical volumes:

# pvscan

Look for existent volume groups:

# vgscan

Activate all the available volume groups:

# vgchange -a y

Look for logical volume:

# lvscan

Mount the logical volumes found on a directory.

Other useful LVM commands

* pvdisplay → shows the available physical volumes 
* vgdisplay → shows the available volume groups 
* vgrename → renames a volume group
* lvdisplay → shows the available logical volumes 
* lvrename → renames a logical volume
* GNU parted create pv, resize and copy lv
Warning: gparted, partitioning program derived from parted, does NOT support LVM!

More interesting stuff you need to know on LVM

LVM supports snapshots, "frozen images" of a logical volume, that you can mount and use like the original volume, the difference is that during the creation you must specify how much they can grow. The idea is that it uses the original data for the part that is equal to the original, and the differences are written on the additional space, so it's not needed on physical volumes space equal to the double of the original. The snapshots are very useful for an online backup of active systems.

# lvcreate -L500 -s -n mysnapshot /dev/backupVG0/backupLV0

creates a logical volume snapshot with the name mysnapshot

# mkdir /mnt/mysnapshot

Mounts the snapshot

# mount /dev/backupVG0/mysnapshot /mnt/mysnapshot

Unmounts the snapshot and removes it

# umount /dev/backupVG0/mysnapshot
# lvremove /dev/backupVG0/mysnapshot

Useful documentation

Man pages of LVM tools are exhaustive and very friendly. A good starting point is

# man lvm


Altre Pagine: · Home Page · Documentazione · Downloads · Screenshots · Contatti