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
Whether expanding or creating new storage, the summarized steps are:
Add or expand the 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:
One Liner Method
echo "- - -" > /sys/class/scsi_host/$(grep mpt /sys/class/scsi_host/host?/proc_name | grep -o -E '(host[0-9])')/scan
Manual Method
grep mpt /sys/class/scsi_host/host?/proc_name
/sys/class/scsi_host/host2/proc_name:mptspi
echo "- - -" > /sys/class/scsi_host/host#/scan
echo "- - -" > /sys/class/scsi_host/host2/scan
If you have expanded an existing disk, rescan the scsi bus
echo 1 > /sys/class/scsi_device/<device>/rescan
Where “<device>” is your disk such as “sda”.
/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:
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
Continue on to either Expanding Storage or Creating New Storage.
Expanding storage steps.
To expand storage for a logical volume, the summarized steps are:
Performed above in “Expanding or New”:
Remaining Steps:
vgextend vglocal /dev/sdb1
pvmove --name lvhome /dev/sda2 /dev/sdb1 lvextend --resizefs /dev/vglocal/lvhome /dev/sdb1
lvextend --resizefs --extents +100%PVS /dev/vglocal/lvhome /dev/sdb1
lvextend --resizefs --size +10G /dev/vglocal/lvhome /dev/sdb1
lvextend --resizefs --extents +100%FREE /dev/vglocal/lvhome
Extending a swap volume is slightly different, as the “–resizefs” flag will not work.
lvextend --size 8G /dev/vglocal/lvswap /dev/sdb1
swapoff -a
mkswap /dev/vglocal/lvswap
swapon -a
swapon -s
lvs
vgs
pvs
Logical Volume Location(s) on Physical Volumes
If you would like to see what logical volumes reside where, a nice command to view physical extent to logical volume mappings is:
pvdisplay -m
Skip if you used the “–resizefs” flag to lvextend above.
resize2fs /dev/vglocal/lvhome
xfs_growfs /dev/vglocal/lvhome
df -h
If you have just moved all of the data of a logical volume off of an old physical volume, the old physical volume can go away if it is no longer in use.
Examples below: You have just moved everything off of /dev/sdc1 to a new disk and can now remove the old disk (/dev/sdc).
pvs -o +pv_used
vgreduce vgname olddisk
vgreduce vglocal /dev/sdc1
pvremove olddisk
pvremove /dev/sdc1
Login to the virtualization user interface and delete the old disk from inventory.
Errors During LVM Commands
root@llsrlscd01 home $ pvs /dev/sdc: read failed after 0 of 4096 at 0: Input/output error /dev/sdc: read failed after 0 of 4096 at 17179803648: Input/output error /dev/sdc: read failed after 0 of 4096 at 17179860992: Input/output error /dev/sdc: read failed after 0 of 4096 at 4096: Input/output error /dev/sdc1: read failed after 0 of 4096 at 17178755072: Input/output error /dev/sdc1: read failed after 0 of 4096 at 17178812416: Input/output error /dev/sdc1: read failed after 0 of 4096 at 0: Input/output error /dev/sdc1: read failed after 0 of 4096 at 4096: Input/output error PV VG Fmt Attr PSize PFree /dev/sda2 vglocal lvm2 a-- 19.50g 3.50g /dev/sdd1 vglocal lvm2 a-- 250.00g 0 /dev/sde1 vglocal lvm2 a-- 40.00g 0
echo 1 > /sys/block/sdX/device/delete
To create new storage for a logical volume, the summarized steps are:
Performed above in “Expanding or New”:
Remaining Steps:
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"
vim /etc/fstab UUID=d89744ad-133c-452b-98d8-9480ef18fe77 /opt/backups ext4 defaults 0 2
mount -a df -h
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}