LVM - Logical Volume Manager

Install logical volumes (LVM)

Prepare and install volumes

Prepare a single disk or a disk raid:

lvm
lvm> pvcreate <physvol>
lvm> vgcreate <physvol> // or several physical volumes: <physvol1> <physvol2> <physvol3>
lvm> lvcreate <volgroup> <physical>
lvm> quit
mkfs.ext4 /dev/mapper/<volgroup-logvol>
mount /dev/mapper/<volgroup-logvol> <dir>

Auto-mount during boot:

vim /etc/fstab
/dev/mapper/<volgroup-logvol> <dir> ext3 noatime,user_xattr 0 0

Replace LVM disk to upgrade capacity

This example will copy /dev/sde1 mounted on /backup to /dev/sdf1 mounted on /replace and afterwards change mount point, logical volume and volume group names to the source names.

Prepare new disk

  • fdisk /dev/sdf
  • n to create a new partition, select 1 and accept all defaults
  • w to write the new partition table and quit
  • mkfs.ext3 /dev/sdf1
  • lvm
  • lvm > pvcreate /dev/sdf1
  • lvm > vgcreate vg_replace /dev/sdf1
  • lvm > vgdisplay vg_replace to check ### of free extents
  • lvm > lvcreate -l### vg_replace (do not use -LxxGB, use size with -l### which is number of extents)
  • lvm > lvrename vg_replace lvol0 lv_replace
  • lvm > vgcfgbackup to backup volume group configurations
  • lvm > exit
  • mkfs -t ext3 /dev/vg_replace/lv_replace
  • mkdir /replace
  • mount /dev/vg_replace/lv_replace /replace

Copy content and assign new disk

  • rsync -avH /backup/* /replace/
  • for an entire BackupPC structure
    rsync -avxHAWX --info=progress2 /backup/ /new-backup/
  • verify the structure between source and destination
    rsync -avcn /backup/ /new-backup/
  • umount /backup
  • umount /new-backup
  • lvm
  • lvm > lvrename vg_backup/lv_backup lv_backup1
  • lvm > lvchange vg_backup/lv_backup1 -an
  • lvm > vgrename vg_backup vg_backup1
  • lvm > lvchange vg_backup1/lv_backup1 -ay
  • lvm > lvrename vg_replace/lv_replace lv_backup
  • lvm > lvchange vg_replace/lv_backup -an
  • lvm > vgrename vg_replace vg_backup
  • lvm > lvchange vg_backup/lv_backup -ay
  • lvm > exit
  • mount /dev/vg_backup/lv_backup /backup -o noatime,user_xattr

Setting up LVM on top of a Linux Software Raid

  • prepare the new disks with a partition (see above), do not create the filesystem yet
  • mdadm –create –verbose /dev/md5 –level=1 –raid-devices=2 /dev/sde /dev/sdf
  • watch the progress with cat /proc/mdstat
  • when finished, restart the computer (shutdown -r now)

Merge 2 volume groups

  1. Unmount and remove LV0 and LV1 from VG1 with umount/lvremove
  2. Remove VG1 with vgremove
  3. Unmount LV0 and LV1 from VG0 with umount
  4. Extend VG0 with any available PVs if necessary
  5. Mount LV0 and LV1 on VG0 with mount
lvcreate -l<extents> -n testlv testvg
mkfs.ext4 /dev/mapper/vg-lv

Increase size of physical volume and/or logical volume

  1. Replace hard disks in a RAID1 array, or change from a RAID1 to a RAID5 array, the resulting array being MD127 (see mdadm - Software RAID)
  2. Use pvresize to extend the physical volume to consume all available space
    pvresize /dev/md127
  3. Use lvresize to extend the logical volume to consume all available space
    lvresize -l +100%FREE /dev/mapper/<vg name>-<lv name>
  4. Or, unmount the volume, delete the logical volumes and the volume group first, then create a new logical volume consuming all available space
    lvremove /dev/mapper/<vg name>-<lv name>
    vgremove <vg name>
    lvcreate -l 100%FREE -n <lv name>  <vg name>

Increase / decrease size of logical volume

Decrease

  1. sudo umount /srv/media
  2. sudo lvresize –resizefs –size -1024GB /dev/vg_data/lv_media

Increase

  1. sudo lvresize –resizefs –size +1024GB /dev/vg_data/lv_home

Remove a disk from a logical volume