linux_wiki:logical_volume_management_lvm

This is an old revision of the document!


Logical Volume Management (LVM)

General Information

LVM provides a way to create virtual disks and containers in order to provide flexibility with parition sizes and extending/shrinking space without shutting a system down.

Checklist

  • The hard disk being formatted is not in use/blank. These steps will erase all data on the target disk.

To expand storage for a logical volume, the summarized steps are:

  • Add virtual disk to VM
  • Re-Scan SCSI Bus (if needed)
  • Format the Disk with a partition
  • Create physical volume using the new disk's partition
  • Extend the volume group using the new physical volume
  • Extend the logical volume
  • Verify new LVM layout
  • Grow the file system
  • Cleanup (if a pvmove of the logical volume was done to a new disk)
    • Verify old physical volume is not used
    • Reduce the volume group by removing the physical volume
    • Remove the physical volume LVM label
    • Delete virtual disk from virtual management UI

Add disk to the system either physically or virtually if a VM.


If adding a disk to a virtual machine and the new disk does not show up:

  • Find the host bus number
    grep mpt /sys/class/scsi_host/host?/proc_name
    • Example return value:
      /sys/class/scsi_host/host2/proc_name:mptspi
    • “host2” is the important value
  • Used the returned host# to scan the SCSI bus
    echo "- - -" > /sys/class/scsi_host/host#/scan
    • Example from returned value
      echo "- - -" > /sys/class/scsi_host/host2/scan
  • The hyphens represent controller,channel,lun, so – - – indicates all controllers, all channels, and all luns should be scanned.

/dev/sdb is used in this example.

Use gdisk in order for GPT instead of MBR. (seriously, don't use fdisk…let MBR die already)

gdisk /dev/sdb

Once in gdisk:

  • Use option “o” to create a new empty GUID Partition table. (GPT)
  • “w” to write table entries
  • Go back into gdisk. (gdisk /dev/sdb)
  • “n” to create a new partition
    • Choose the defaults for partition number, start, and end blocks to fill the entire disk
    • Use code “8e00” for Linux LVM
  • “w” to write changes and exit

About: Think of physical volumes as your “virtual disks” to use in LVM.

Create a physical volume using the parition that the LVM is on.

pvcreate <device1-path> [device2-path] [deviceX-path]

Example:

pvcreate /dev/sdb1

About: Volume groups aggregate physical volumes into a usable pool of disk space.

vgcreate <vgname> <physical-device>

Example:

vgcreate vgstorage /dev/sdb1

About: Logical volumes use the disk space made available via a volume group.

lvcreate –size <size> –name <lv-name> <vg-name>

Example: Create a 100G logical volume

lvcreate --size 100G --name lvbackups vgstorage

Example: Create a logical volume that uses 100% of the volume group free extents

lvcreate --extents 100%FREE --name lvbackups vgstorage

About: So far, the only things being created were pretty much containers. Create the actual file system below.

mkfs -t <fstype> -L <label> /dev/mapper/<vgname>-<lvname>

Example:

mkfs -t ext4 -L Backups /dev/mapper/vgstorage-lvbackups

blkid
/dev/mapper/vgstorage-lvbackups: UUID="d89744ad-133c-452b-98d8-9480ef18fe77" TYPE="ext4"

Add entry to /etc/fstab by UUID

vim /etc/fstab
UUID=d89744ad-133c-452b-98d8-9480ef18fe77 /opt/backups ext4 defaults 0 2
mount -a
df -h

It is sometimes difficult to predict how much space a particular partition will need. With LVM, your paritions can grow with your needs.

Situation: I have a laptop with a 300GB hard drive and installed Linux.

A /boot partition is the only partition that can currently not be LVM.

How the disk is formatted

The disk (/dev/sda) was formatted with a GUID Paritition Table (GPT).

It has two paritions, a Linux Filesystem (to be used for /boot) and a Linux LVM.

gdisk -l /dev/sda
 
Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present
 
Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048          513766   249.9 MiB   8300  Linux filesystem
   2          514048       625142414   297.8 GiB   8E00  Linux LVM

The LVM Structure

The LVM structure was setup to allow for a good amount of room in each parition, but enough free disk space left for any of them to grow.

Physical Volume

One physical volume was created from /dev/sdb2 (the Linux LVM formatted partition from above).

pvs
  PV         VG     Fmt  Attr PSize   PFree  
  /dev/sda2  vgroot lvm2 a--  297.84g 197.84g

Volume Group

One volume group was created and the physical disk added to it.

vgs
  VG     #PV #LV #SN Attr   VSize   VFree  
  vgroot   1   5   0 wz--n- 297.84g 197.84g

Logical Volumes

The following partitions each got their own logical volume:

  • /home
  • /
  • swap
  • /tmp
  • /var

The sizes for each can be seen below:

lvs
  LV     VG     Attr      LSize  
  lvhome vgroot -wi-ao--- 50.00g                                           
  lvroot vgroot -wi-ao--- 30.00g                                           
  lvswap vgroot -wi-ao---  4.00g                                           
  lvtmp  vgroot -wi-ao---  8.00g                                           
  lvvar  vgroot -wi-ao---  8.00g  

As you can see, the partitions are using up only 100 GB of the 297.84 GB total available for the volume group.

This leaves 197.84 GB of space that can be given to any of the partions.

This information can been seen by going back to the volume group show command:

vgs
  VG     #PV #LV #SN Attr   VSize   VFree  
  vgroot   1   5   0 wz--n- 297.84g 197.84g

Partition Status

Using df -h, we can see how each LVM partition is doing space wise:

df -h
Filesystem                 Size  Used Avail Use% Mounted on
/dev/mapper/vgroot-lvroot   30G  2.7G   26G  10% /
/dev/sda1                  238M   95M  128M  43% /boot
/dev/mapper/vgroot-lvvar   7.8G  534M  6.9G   8% /var
/dev/mapper/vgroot-lvtmp   7.8G   19M  7.4G   1% /tmp
/dev/mapper/vgroot-lvhome   50G   16G   31G  34% /home

Extending Logical Volumes

Say that I have run out of space in my /home partition. Good thing I'm using LVM; I can easily make /home larger.

1) Extend the logical volume container. You can either give an exact size or an “add this much” size.

Example: Extend lvhome by 10 GB

lvextend --size +10G /dev/mapper/vgroot-lvhome

2) Grow the file system to fill the new size of the logical volume. (If the size parameter is not specified, it will default to the size of the logical volume.)

resize2fs /dev/mapper/vgroot-lvhome

3) Check the volume status

Check lvs and df:

lvs
  LV     VG     Attr      LSize  Pool Origin Data%  Move Log Copy%  Convert
  lvhome vgroot -wi-ao--- 60.00g                                           
  lvroot vgroot -wi-ao--- 30.00g                                           
  lvswap vgroot -wi-ao---  4.00g                                           
  lvtmp  vgroot -wi-ao---  8.00g                                           
  lvvar  vgroot -wi-ao---  8.00g  
 
df -h
Filesystem                 Size  Used Avail Use% Mounted on
/dev/mapper/vgroot-lvroot   30G  2.7G   26G  10% /
/dev/sda1                  238M   95M  128M  43% /boot
/dev/mapper/vgroot-lvvar   7.8G  534M  6.9G   8% /var
/dev/mapper/vgroot-lvtmp   7.8G   19M  7.4G   1% /tmp
/dev/mapper/vgroot-lvhome   59G   16G   41G  29% /home

Note: If you do not resize your file system and only extend the logical volume, you will see the logical partition from the “lvs” output as larger, but it will be the old size in df -h.


Situation: You have a disk that has run out of space. You want to add a new, larger disk to a virtual machine, and delete the old disk to reclaim the resources.

  • Add the new disk to the VM.
  • Rescan the SCSI bus if disk does not show up.
  • Format it and create a LVM partition.
  • Use pvcreate to make it a physical volume.
  • Use vgextend to add the new physical volume to the same volume group that contains the disk you want to replace.
  • Move the data from the old disk, to the new:

Move Logical Volume Only

If you only need to move 1 logical volume to the new disk and not everything, follow these steps.

pvmove -n lvname olddisk newdisk

pvmove -n lvhome /dev/sdb1 /dev/sdc1

Extend the logical volume that was moved, only using the new disk's free space

lvextend /dev/mapper/vgroot-lvhome /dev/sdc1

Resize the file system (ext2,3,4 only. See other filesystem's man pages for syntax)

resize2fs /dev/mapper/vgroot-lvhome

⇒Stop Here if only moving a logical volume


Move All

If moving all data from an old physical volume to new, follow these steps instead.

pvmove olddisk newdisk

pvmove /dev/sdb1 /dev/sdc1
  • Ensure old physical volume has no space in use
pvs -o +pv_used
  • Remove old physical volume from volume group

vgreduce vgname olddisk

vgreduce vgroot /dev/sdb1
  • Remove old disk from being a physical volume

pvremove olddisk

pvremove /dev/sdb1
  • Extend logical volume
lvextend --extents +100%FREE /dev/mapper/vgroot-lvhome
  • Resize the file system (ext2,3,4 only. See other filesystem's man pages for syntax)
resize2fs /dev/mapper/vgroot-lvhome
  • Remove disk from VM using the vendor's virtual machine manager.

We don't always get a name right the first time. Luckily, LVM allows for easy renaming of logical volumes and volume groups.

Rename a Volume Group (vgrename vg-oldname vg-newname)

vgrename vgbackupmirror vgredusb

Rename a Logical Volume (lvrename vgname lv-oldname lv-newname)

lvrename vgredusb lvbackupmirror lvredbackups

LVM allows you to “freeze” the metadata of files. This allows for things like rsync to complete successfully even if files it is trying to sync are modified during the operation.

{TODO}

  • linux_wiki/logical_volume_management_lvm.1450894296.txt.gz
  • Last modified: 2019/05/25 23:50
  • (external edit)