Skip to content

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

  1. Create PV(s)

    sudo pvcreate /dev/sdX
    
  2. 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
    
  3. Add PV(s) to VG

    sudo vgextend VG-NAME /dev/sdZ
    
  4. 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
    
  5. 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

  1. 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
    
  2. 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
    
  3. Resize filesystem

    sudo fsadm resize /dev/VG-NAME/LV-NAME
    
  4. Create pv for new drive(s). One more drive may be necessary if you want to create a mirrorlog disk.

    sudo pvcreate /dev/sdX
    
  5. Add new pv(s) to vg

    sudo vgextend /dev/VG-NAME /dev/sdX
    
  6. Create mirror using new drive(s)

    sudo lvconvert -m1 /dev/VG-NAME/LV-NAME
    

MIGRATION FROM OLD DISKS IN VG TO NEW ONES

  1. Create PV for new disk

    sudo pvcreate /dev/sdz
    
  2. Extend the VG to include the new disk

    sudo vgextend VG-NAME /dev/sdz
    
  3. Migrate extents off old disk

    sudo pvmove /dev/sdb
    
  4. Remove PV from LV

    sudo vgreduce VG-NAME /dev/sdb
    
  5. Remove PV from VG & validate

    sudo pvremove /dev/sdb
    sudo pvdisplay
    

MIGRATION OF LVM ARRAY TO NEW HOST

  1. Unmount the LVM VG(s) in question, and validate that it is indeed unmounted

    sudo umount /dev/VG-NAME/LV-NAME
    df -h
    
  2. Mark VG as inactive and verify inactive status

    sudo vgchange -an VG-NAME
    sudo lvdisplay
    
  3. Export VG and verify state

    sudo vgexport VG-NAME
    sudo vgs
    
  4. Power off hardware and transfer it to new host

  5. Import VG and verify import status

    sudo vgimport VG-NAME
    sudo vgs
    
  6. Mark VG as active and verify active status

    sudo vgchange -ay VG-NAME
    sudo vgs
    
  7. 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

  1. Remove disk from any other VGs. Use 'pvs -a' to show your PVs and their allocations

    vgreduce OLD_VG /dev/sdX
    
  2. Create pv

    pvcreate /dev/sdX
    
  3. Extend vg

    vgextend VG_NAME /dev/sdX
    
  4. 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
    
  5. Create a cache data LV

    lvcreate -n cache -l 99%PVS VG_NAME /dev/sdX
    
  6. Create the cache pool LV

    lvconvert --type cache-pool --poolmetadata VG_NAME/cache_meta VG_NAME/cache
    
  7. Create cache

    lvconvert --type cache --cachepool VG_NAME/cache VG_NAME/CACHE_TARGET
    
  8. 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:

  1. lvconvert --uncache VG_NAME/LV_NAME
  2. 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)

    1. pvcreate on each new disk.
    2. vgextend each new disk
    3. pvmove each old disk that you wish to remove
    4. lvextend if need be
    5. 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!