LVM

From HPCWIKI
Jump to navigation Jump to search

LVM(Logical volume management) creates an easy to use layer over multiple physical disks. We can combine multiple disks and create logical storage volumes.

LVM features are[1]

  1. No restriction on disk size
  2. Increased disk throughput
  3. Mirroring volumes for business critical data
  4. Volume snapshots
  5. Easy backups and restores using snapshots
  6. Easy data relocation
  7. Resizing storage pools (add or remove disks) without reformatting disks

LVM Terminologies

Type Description Example
Physical Volumes (PV) Actual disks (e.g. /dev/sda, /dev,sdb, /dev/vdb and so on) display physical volumes

$sudo pvs

Volume Groups (VG) Physical volumes are combined into volume groups. (e.g. my_vg = /dev/sda + /dev/sdb.)
Logical Volumes (LV) A volume group is divided up into logical volumes

(e.g. my_vg divided into my_vg/data, my_vg/backups, my_vg/home, my_vg/mysqldb and so on)

Create LVM with two or more disk

    #assuming we have two device sdb and sdc 
    _pvs=( /dev/sdb /dev/sdc )
    for _pv in "${_pvs[@]}" 
    do  
        echo "Create PV (Physical Volume) : $_pv"
        pvcreate $_pv
    done 

    # Create VG (Volume Group) using PVs
    sudo vgcreate $_vgname /dev/sdb /dev/sdc

    # Create LV (Logical Volume) using VG
    sudo lvcreate -n $_lvname -l 100%free --wipesignatures y --yes --zero y $_vgname 

    # Format LVM Partition depending on the filesystem
    #sudo mkfs.ext4 /dev/$_vgname/$_lvname
    #sudo mkfs.xfs  /dev/$_vgname/$_lvname

    # mount LVS to mount point, example /mnt/storage 
    sudo mkdir -p /mnt/storage
    sudo mount /dev/$_vgname/$_lvname /mnt/storage
    
    # add LVM into fstab 
    echo '/dev/$_vgname/$_lvname  /mnt/storage  ext4  defaults 0 0' | sudo  tee -a /etc/fstab
    sudo mount -a

Remove LVM

    #unmount before to remove 
    sudo umount /mnt/storage

    #disable LVM
    sudo lvchange -an /dev/$_vgname/$_lvname
    #delete LVM
    sudo lvremove -f /dev/$_vgname/$_lvname

    #delete VGs
    sudo vgremove -f $_vgname

    # Delete physical volumes
    _pvs=( /dev/sdb /dev/sdc )
    for _pv in "${_pvs[@]}"
    do
        echo "Delete PV (Physical Volume) : $_pv"
        sudo pvremove -f $_pv
    done

Display LVM status

PV status

# pvs
  PV         VG   Fmt  Attr PSize   PFree
  /dev/sda3  rl   lvm2 a--   <2.00t    0 
  /dev/sdb   vg01 lvm2 a--  <20.00g    0 
  /dev/sdc   vg01 lvm2 a--  <10.00g    0 


# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sdb
  VG Name               vg01
  PV Size               20.00 GiB / not usable 4.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              5119
  Free PE               0
  Allocated PE          5119
  PV UUID               jkuGmw-Dcvm-GTo3-DMUj-h9xu-bG2I-l70qO0
   
  --- Physical volume ---
  PV Name               /dev/sdc
  VG Name               vg01
  PV Size               10.00 GiB / not usable 4.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              2559
  Free PE               0
  Allocated PE          2559
  PV UUID               zOtlUy-Gh4t-KIVd-DLaq-qS9X-7SN4-d634eV
   
  --- Physical volume ---
  PV Name               /dev/sda3
  VG Name               rl
  PV Size               <2.00 TiB / not usable 0   
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              524031
  Free PE               0
  Allocated PE          524031
  PV UUID               enc5P4-hS0a-Tdpx-SUVj-8BSf-hsnM-ClWfmH

VG status

# vgs
  VG   #PV #LV #SN Attr   VSize  VFree
  rl     1   3   0 wz--n- <2.00t    0 
  vg01   2   1   0 wz--n- 29.99g    0 


# vgdisplay 
  --- Volume group ---
  VG Name               vg01
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               29.99 GiB
  PE Size               4.00 MiB
  Total PE              7678
  Alloc PE / Size       7678 / 29.99 GiB
  Free  PE / Size       0 / 0   
  VG UUID               QeeTHC-n8LG-5jqB-Z5yo-0cdg-f8Y5-dRJyjp
   
  --- Volume group ---
  VG Name               rl
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <2.00 TiB
  PE Size               4.00 MiB
  Total PE              524031
  Alloc PE / Size       524031 / <2.00 TiB
  Free  PE / Size       0 / 0   
  VG UUID               fVQYRJ-kNSC-XAec-TxNi-BF0U-GbIA-LaImvg

LV status

# lvs
  LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home rl   -wi-ao---- <1.93t                                                    
  root rl   -wi-ao---- 70.00g                                                    
  swap rl   -wi-ao----  2.71g                                                    
  lv01 vg01 -wi-ao---- 29.99g  
  
# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/vg01/lv01
  LV Name                lv01
  VG Name                vg01
  LV UUID                D6mc3r-lbtH-9gqc-3HjF-01Fg-7zrh-hPJzpC
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2024-01-11 01:28:30 -0500
  LV Status              available
  # open                 1
  LV Size                29.99 GiB
  Current LE             7678
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:3
   
  --- Logical volume ---
  LV Path                /dev/rl/swap
  LV Name                swap
  VG Name                rl
  LV UUID                cnVSkz-QhIp-UpeE-9Cb2-2neR-d6tc-YnJ5jy
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2024-01-10 21:19:57 -0500
  LV Status              available
  # open                 2
  LV Size                2.71 GiB
  Current LE             695
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/rl/home
  LV Name                home
  VG Name                rl
  LV UUID                eRA0eB-7PLr-JHaR-P8l7-UMoP-V1mz-eVVEJJ
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2024-01-10 21:19:58 -0500
  LV Status              available
  # open                 1
  LV Size                <1.93 TiB
  Current LE             505416
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
   
  --- Logical volume ---
  LV Path                /dev/rl/root
  LV Name                root
  VG Name                rl
  LV UUID                yRgfqu-8slv-AzW8-LaUp-Mkto-fSJh-MOIAG4
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2024-01-10 21:19:59 -0500
  LV Status              available
  # open                 1
  LV Size                70.00 GiB
  Current LE             17920
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

References