LVM CHEATSHEET
LVM is the "Logical Volume Manager". Its a suite of tools that make for easy and powerful management of disks and pools of data.
GLOSSARY
ACRONYM | NAME | DESCRIPTION |
---|---|---|
pv | physical volume | The physical disk or partition in array |
vg | volume group | The name of the whole array |
lv | logical volume | The name of a virtual disk within the array. More than one LV may be in a VG |
CREATING A NEW LV
-
Create PV(s)
sudo pvcreate /dev/sdX
-
Create VG - give it the disk names for the PVs at hand, one or more PVs can be added initially
sudo vgcreate VG-NAME /dev/sdX /dev/sdY
-
Add PV(s) to VG
sudo vgextend VG-NAME /dev/sdZ
-
Create LV-NAME
#Non-mirrored LV - (100MB in example, and %100 afterwards) in VG-NAME sudo lvcreate -L 100M -n LV-NAME VG-NAME sudo lvcreate -l 100%FREE -n LV-NAME VG-NAME #Create a single mirror lv. #-- You need .1% roughly for log file if keeping on same set. #-- Presuming 100G VG. #-- You can add "--nosync" to the command to skip initial sync. sudo lvcreate -L 49G -m1 -n LV-NAME VG-NAME
-
Format lv as ext4 or xfs
sudo mkfs.ext4 /dev/VG-NAME/LV-NAME && e2label /dev/VG-NAME/LV-NAME labelname sudo mkfs.xfs /dev/VG-NAME/LV-NAME
MIRROR EXISTING LV
-
Reduce filesystem
#Use 'df' to see where things are mounted sudo umount LV-MOUNTPOINT #Resize filesystem on LV sudo fsadm resize /dev/VG-NAME/LV-NAME NEWSIZE #OPTIONAL : #Resize both LV and filesystem to 5G (POTENTIALLY UNSAFE) #-- Skip to step 4 afterwards sudo lvreduce --resizefs -L 5G /dev/VG-NAME/LV-NAME
-
Reduce LV
#Reduce LV by 5G (Sufficient for ~ 5TB @ .1% rule) sudo lvreduce -L -5G /dev/VG-NAME/LV-NAME #Reduce LV to 5G sudo lvreduce -L 5G /dev/VG-NAME/LV-NAME
-
Resize filesystem
sudo fsadm resize /dev/VG-NAME/LV-NAME
-
Create pv for new drive(s). One more drive may be necessary if you want to create a mirrorlog disk.
sudo pvcreate /dev/sdX
-
Add new pv(s) to vg
sudo vgextend /dev/VG-NAME /dev/sdX
-
Create mirror using new drive(s)
sudo lvconvert -m1 /dev/VG-NAME/LV-NAME
MIGRATION FROM OLD DISKS IN VG TO NEW ONES
-
Create PV for new disk
sudo pvcreate /dev/sdz
-
Extend the VG to include the new disk
sudo vgextend VG-NAME /dev/sdz
-
Migrate extents off old disk
sudo pvmove /dev/sdb
-
Remove PV from LV
sudo vgreduce VG-NAME /dev/sdb
-
Remove PV from VG & validate
sudo pvremove /dev/sdb sudo pvdisplay
MIGRATION OF LVM ARRAY TO NEW HOST
-
Unmount the LVM VG(s) in question, and validate that it is indeed unmounted
sudo umount /dev/VG-NAME/LV-NAME df -h
-
Mark VG as inactive and verify inactive status
sudo vgchange -an VG-NAME sudo lvdisplay
-
Export VG and verify state
sudo vgexport VG-NAME sudo vgs
-
Power off hardware and transfer it to new host
-
Import VG and verify import status
sudo vgimport VG-NAME sudo vgs
-
Mark VG as active and verify active status
sudo vgchange -ay VG-NAME sudo vgs
-
Mount VG and verify mount status
sudo mkdir -p /mnt/VG-NAME mount /dev/VG-NAME/LV-NAME /mnt/VG-NAME/LV-NAME df -h
LV CACHE
PREFACE
To add a cache to an existing VG we will perform a few steps to create the cache and metadata LVs and correctly tag these two LVs as cache mode. This guide will presume this is a fresh drive not in any existing VG. If it is, remove it first, then the drive can be utilized.
CREATING THE CACHE LVs
-
Remove disk from any other VGs. Use 'pvs -a' to show your PVs and their allocations
vgreduce OLD_VG /dev/sdX
-
Create pv
pvcreate /dev/sdX
-
Extend vg
vgextend VG_NAME /dev/sdX
-
Create lv for cache metadata in lv. For YMB, use a value for Y that is .1% of total cache (1GB for a 1TB disk, etc)
lvcreate -n cache_meta -L YMB VG_NAME /dev/sdX
-
Create a cache data LV
lvcreate -n cache -l 99%PVS VG_NAME /dev/sdX
-
Create the cache pool LV
lvconvert --type cache-pool --poolmetadata VG_NAME/cache_meta VG_NAME/cache
-
Create cache
lvconvert --type cache --cachepool VG_NAME/cache VG_NAME/CACHE_TARGET
-
Verify
#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
LV_NAME VG_NAME Cwi-aoC--- <3.42t [cache] [LV_NAME_corig] 0.02 0.62 0.00
BREAK LVM CACHE
To remove the LVM cache is straighforward:
- lvconvert --uncache VG_NAME/LV_NAME
- vgreduce VG_NAME /dev/sdX
Done!
QUICK TASKS HOWTO
-
Add a disk to the VG and extend the LV out to make use of it
sudo pvcreate /dev/sdX sudo vgextend VG_NAME /dev/sdX sudo lvextend -l +100%FREE VG_NAME/LV_NAME sudo fsadm resize /dev/VG-NAME/LV-NAME
-
Remove disk from VG. Note, you will need to have the available space to hold all LV's in the VG after disk removal.
sudo pvmove /dev/sdX sudo vgreduce VG_NAME /dev/sdX sudo pvremove /dev/sdX
-
Swap smaller disk(s) for larger one(s)
- pvcreate on each new disk.
- vgextend each new disk
- pvmove each old disk that you wish to remove
- lvextend if need be
- extend filesystem if need be
REFERENCES
TROUBLESHOOTING
CANNOT CREATE PV
If you get the message:
$ sudo pvcreate /dev/sdb /dev/sdc
Device /dev/sdb not found (or ignored by filtering).
Device /dev/sdc not found (or ignored by filtering).
The solution is to recreate the disklabel from gpt to msdos:
$ sudo parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel msdos
Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? y
(parted) select /dev/sdc
Using /dev/sdc
(parted) mklabel msdos
Warning: The existing disk label on /dev/sdc will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? y
(parted) q
Information: You may need to update /etc/fstab.
You likely will need a reboot, but then the disks should be able to be turned into a PV without issue:
$ sudo pvcreate /dev/sdb
WARNING: dos signature detected on /dev/sdb at offset 510. Wipe it? [y/n]: y
Wiping dos signature on /dev/sdb.
Physical volume "/dev/sdb" successfully created.
$ sudo pvcreate /dev/sdg
WARNING: dos signature detected on /dev/sdg at offset 510. Wipe it? [y/n]: y
Wiping dos signature on /dev/sdg.
Physical volume "/dev/sdg" successfully created.
All fixed!