Keyboard shortcuts

Press ← or β†’ to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

🏠 Back to Blog

lvm (logical volume manager)

  • LVM creates an abstraction layer between physical storage and the file system, allowing the file system to be resized and span across multiple disks

  • Physical volumes are grouped into groups of volumes, called Volume Groups. These Volume Groups are then divided into Logical Volumes

  • LVM Acronyms

    • PV = Physical Volume, real physical storage devices

      • sudo lvmdiskscan can be used to list devices that may be used as physical volumes
        04:52:03 azureadmin@centos01 ~ β†’ sudo lvmdiskscan
        /dev/sda1  [     500.00 MiB]
        /dev/sda2  [      29.02 GiB]
        /dev/sda15 [     495.00 MiB]
        /dev/sdb1  [     <64.00 GiB]
        /dev/sdf   [       5.00 GiB]
        /dev/sdh   [       5.00 GiB]
        /dev/sdi   [       5.00 GiB]
        3 disks
        4 partitions
        0 LVM physical volume whole disks
        0 LVM physical volumes
        
      • 'sudo pvcreate /dev/sdc /dev/sdd /dev/sde can be used to create a new LVM volume from 3 disks
        04:52:06 azureadmin@centos01 ~ β†’ sudo pvcreate /dev/sdf /dev/sdh /dev/sdi
        Physical volume "/dev/sdf" successfully created.
        Physical volume "/dev/sdh" successfully created.
        Physical volume "/dev/sdi" successfully created.
        
      • sudo pvs can be used to list physical volumes used by LVM
        04:52:19 azureadmin@centos01 ~ β†’ sudo pvs
        PV         VG Fmt  Attr PSize PFree
        /dev/sdf      lvm2 ---  5.00g 5.00g
        /dev/sdh      lvm2 ---  5.00g 5.00g
        /dev/sdi      lvm2 ---  5.00g 5.00g
        
    • VG = Volume Group

      • After LVM has physical devices (pvs), you add the pvs to a volume group (vg). This tells LVM how it can use the storage capacity
      • sudo vgcreate my_volume /dev/sdf /dev/sdh will create a new VG with 2 PV
        04:58:13 azureadmin@centos01 ~ β†’ sudo vgcreate my_vol /dev/sdf /dev/sdh
        Volume group "my_vol" successfully created
        
      • Once disks are added to a volume group, they are seen by the system as one contiguous block of storage
      • You can add another disk to the volume group using vgextend
        05:00:06 azureadmin@centos01 ~ β†’ sudo vgextend my_vol /dev/sdi
        Volume group "my_vol" successfully extended
        
      • use sudo vgs to view the status of Volume Groups:
        05:00:14 azureadmin@centos01 ~ β†’ sudo vgs
        VG     #PV #LV #SN Attr   VSize   VFree
        my_vol   3   0   0 wz--n- <14.99g <14.99g
        
      • use sudo vgreduce my_vol /dev/sdi to remove a physical volume from a volume group
        05:00:39 azureadmin@centos01 ~ β†’ sudo vgreduce my_vol /dev/sdi
        Removed "/dev/sdi" from volume group "my_vol"
        
    • LV = Logical Volume

      • A logical volume is similar to a partition
      • sudo lvcreate --size 2G --name partition1 my_vol can be used to create a logical volume of 2 gigabytes
        05:02:22 azureadmin@centos01 ~ β†’ sudo lvcreate --size 2G --name partition1 my_vol
        Logical volume "partition1" created.
        
      • You can view logical volumes using sudo lvs
        05:03:49 azureadmin@centos01 ~ β†’ sudo lvs
        LV         VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
        partition1 my_vol -wi-a----- 2.00g
        partition2 my_vol -wi-a----- 6.00g
        
      • to tell a logical volume to use all space on a logical volume, use sudo lvresize
        05:03:50 azureadmin@centos01 ~ β†’ sudo lvresize --extents 100%VG my_vol/partition1
        Reducing 100%VG to remaining free space 3.99 GiB in VG.
        Size of logical volume my_vol/partition1 changed from 2.00 GiB (512 extents) to 3.99 GiB (1022 extents).
        Logical volume my_vol/partition1 successfully resized.
        
      • the path to LVs on the system can be found using lvdisplay
        05:08:32 azureadmin@centos01 ~ β†’ sudo lvdisplay  | grep "LV Path"
        LV Path                /dev/my_vol/partition1
        LV Path                /dev/my_vol/partition2
        
      • You can then add a file system to a LV using common file system management commands
        05:08:41 azureadmin@centos01 ~ β†’ sudo mkfs.xfs /dev/my_vol/partition1
        meta-data=/dev/my_vol/partition1 isize=512    agcount=4, agsize=261632 blks
                =                       sectsz=4096  attr=2, projid32bit=1
                =                       crc=1        finobt=1, sparse=1, rmapbt=0
                =                       reflink=1
        data     =                       bsize=4096   blocks=1046528, imaxpct=25
                =                       sunit=0      swidth=0 blks
        naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
        log      =internal log           bsize=4096   blocks=2560, version=2
                =                       sectsz=4096  sunit=1 blks, lazy-count=1
        realtime =none                   extsz=4096   blocks=0, rtextents=0
        Discarding blocks...Done.
        
      • If the LV contains a file system, you must take extra caution when resizing it. You must pass the --resizefs parameter to lvresize
      • sudo lvresize --resizefs --size 3G my_vol\partition1
      • XFS file system shrinking is not supported
  • If you forget what commands to use for LVM, simply open the man pages for LVM and scroll to the bottom to get a list of available commands

    • man lvm
  • sudo lvmdiskscan will show what disks are available

  • To create a physical volume:

    • sudo pvcreate /dev/sdd /dev/sde /dev/sdf
    • Example:
      [azureadmin@centos01 shares]$ sudo pvcreate /dev/sdd /dev/sde
      Physical volume "/dev/sdd" successfully created.
      Physical volume "/dev/sde" successfully created.
      
    • To list physical volumes: sudo pvs
      [azureadmin@centos01 shares]$ sudo pvs
      PV         VG Fmt  Attr PSize PFree
      /dev/sdd      lvm2 ---  5.00g 5.00g
      /dev/sde      lvm2 ---  5.00g 5.00g
      
    • After creating the physical volume, add it to a volume group:
      • sudo vgcreate my_volume /dev/sdd /dev/sde
    • List volume groups:
      [azureadmin@centos01 shares]$ sudo vgs
      VG        #PV #LV #SN Attr   VSize VFree
      my_volume   2   0   0 wz--n- 9.99g 9.99g
      
    • To expand a volume group, add a PV. Then use vgextend to add the PV to the volume group
      • sudo vgextend my_volume /dev/sdf
    • You can also remove a physical volume from the volume group:
      • sudo vgreduce my_volume /dev/sdf
    • Then you can remove the physical volume:
      • sudo pvremove /dev/sdf
    • Logical volumes are like partitions
    • you can create a new logical volume:
      • sudo lvcreate --size 3G --name partition1 my_volume
    • To grow a logical volume to use all the space it has available
      • suod lvresize --extents 100%VG my_volume/partition1

Device Mapper

  • The kernel uses a driver called the device mapper to route requests for a location on a logical volume’s block device to the true location on an actual device. After LVM has determined the structure of the logical volumes from all of the headers on the PVs, it communicates this the kernel’s device mapper driver in order to initialize the block devices for the logical volumes and load their mapping tables. It achieves this with the ioctl(2) syscall on the /dev/mapper/control device file
  • To get an inventory of mapped devices currently serviced by the device mapper, use dmsetup:
    • dmsetup info
  • There is a header at the beginning of every LVM PV that identifies the volume as well as it’s volume groups and the logical volumes within.
    • You can view the lvm header on a physical volume using dd:
      • dd if=<path to pv> count=1000 | strings | less
      • Example: dd if=/dev/sdb1 count=1000 | strings | less