LVM

From Briki
Revision as of 18:03, 3 November 2007 by Admin (talk | contribs) (10 revision(s))
Jump to: navigation, search

Overview

  • One or more Physical Volumes (eg. /dev/sda2) are joined together in a Volume Group (eg. /dev/vg1)
  • A Volume Group is split into one or more Logical Volumes (eg. /dev/vg1/media)

Creating a new volume group & logical volume

To create a volume group (/dev/vg1) consisting of 1 physical volume (/dev/sda2), and split into only 1 logical volume (/dev/vg1/media):

  • Setup partition as a physical volume:
sudo pvcreate /dev/sda2
  • Create a volume group consisting of only this physical volume:
sudo vgcreate vg1 /dev/sda2
  • Check volume group has been created successfully:
vgdisplay

This should show 1 volume group consisting of 0 LVs (see Cur LV) and 1 PV (see Cur PV). Note the number of free extents in the volume group.

  • Create a logical volume with this number of free extents:
sudo lvcreate -l <num-extents> --name media vg1
  • Format the logical volume as ext3:
mkfs.ext3 /dev/vg1/media
  • Mount the volume by adding the following line to /etc/fstab:
/dev/vg1/media   /var/media   ext3   defaults    0  2
sudo mount /dev/vg1/media

Adding a physical volume to a volume group and logical volume

For example, to add /dev/sdb2 to /dev/vg1/media

  • Setup partition as a physical volume:
sudo pvcreate /dev/sdb2
  • Create a volume group consisting of only this physical volume:
sudo vgextend vg1 /dev/sdb2
  • Check volume group has been changed successfully, and note the number of new free extents:
vgdisplay
  • Unmount the logical volume:
sudo umount /dev/vg1/media
  • Extend the logical volume:
sudo lvextend -l +<num-extents> /dev/vg1/media
  • Check that the volume sizes look correct
sudo vgdisplay
  • Resize the ext3 filesystem on /dev/vg1/media
sudo resize2fs /dev/vg1/media
  • Mount the logical volume:
sudo mount /dev/vg1/media

Resizing a physical volume

For example, deleting /dev/sda3 and extending /dev/sda2 to use the free space. Note here that we are extending a physical volume, not just a logical one.

  • Unmount any logical volumes which use /dev/sda2
sudo umount /dev/vg1/media
  • Use fdisk to delete /dev/sda3, delete old /dev/sda2 and recreate as a larger partition (this will not actually delete any data from /dev/sda2)
$ sudo fdisk /dev/sda

The number of cylinders for this disk is set to 38913.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p

Disk /dev/sda: 320.0 GB, 320072933376 bytes
255 heads, 63 sectors/track, 38913 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x08270827

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1       16708   134206976    7  HPFS/NTFS
/dev/sda2           16708       20532    30716282+  83  Linux
/dev/sda3           20533       38913   147645380    7  HPFS/NTFS

Command (m for help): d
Partition number (1-4): 3

Command (m for help): p

Disk /dev/sda: 320.0 GB, 320072933376 bytes
255 heads, 63 sectors/track, 38913 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x08270827

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1       16708   134206976    7  HPFS/NTFS
/dev/sda2           16708       20532    30716282+  83  Linux

Command (m for help): d
Partition number (1-4): 2

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First sector (268414015-625142447, default 268414015):
Using default value 268414015
Last sector or +size or +sizeM or +sizeK (268414015-625142447, default 625142447):
Using default value 625142447

Command (m for help): p

Disk /dev/sda: 320.0 GB, 320072933376 bytes
255 heads, 63 sectors/track, 38913 cylinders, total 625142448 sectors
Units = sectors of 1 * 512 = 512 bytes
Disk identifier: 0x08270827

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *          63   268414014   134206976    7  HPFS/NTFS
/dev/sda2       268414015   625142447   178364216+  83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.
  • If fdisk warned you that the kernel still uses the old table, you will need to reboot now. Remember to unmount relevant logical volumes again after rebooting.
  • Resize the LVM physical volume to match the new partition size
sudo pvresize /dev/sda2
  • Check how many free extents exist in the volume group - you should see extents corresponding to the increase in size of your physical partition
sudo vgdisplay
  • Add this many extents to /dev/vg1/media
sudo lvextend -l +<num-extents> /dev/vg1/media
  • Check that the volume sizes look correct
sudo vgdisplay
  • Resize the ext3 filesystem on /dev/vg1/media
sudo resize2fs /dev/vg1/media
  • Remount affected filesystems
sudo mount /dev/vg1/media
  • Check that everything looks ok :)

Reactivating a volume group

If things have gone a bit wrong, and you've had to manually fix stuff (with fdisk, and the like), then to reactivate the volume group, and get it to recognise the fixed LVs, simply run

sudo vgchange -a y  /dev/vg1

Sample vgdisplay Output

  --- Volume group ---
  VG Name               vg1
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               170.10 GB
  PE Size               4.00 MB
  Total PE              43545
  Alloc PE / Size       43545 / 170.10 GB
  Free  PE / Size       0 / 0
  VG UUID               TACrPR-xO6J-mHz0-hiMn-ax3G-R3a4-dv2ivp