LVM Management Cheat Sheet

Peter Boy (pboy) Version F39-F40 Last review: 2024-03-27
This article is a compilation of tasks and their solutions that are frequently encountered when administering Fedora Server Edition. For a systematic introduction and documentation see the upstream LVM project.

Status: Work in Progress

This article has not yet been finally reviewed. Some content may still be subject to change.

Exploring the systems storage devices

List of connected block devices

For example, a slightly customized installation of Fedora Server on a RAID 1 shows the following list:

[…]# lsblk
NAME                 MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINTS
sda                    8:0    0 447,1G  0 disk
├─sda1                 8:1    0    20G  0 part
│ └─md126              9:126  0    20G  0 raid1
│   ├─fedora-root    253:0    0    12G  0 lvm   /
│   └─fedora-var_log 253:1    0     3G  0 lvm   /var/log
├─sda2                 8:2    0     1G  0 part
│ └─md127              9:127  0  1023M  0 raid1 /boot
├─sda3                 8:3    0   601M  0 part
│ └─md125              9:125  0 600,9M  0 raid1 /boot/efi
└─sda4                 8:4    0 425,5G  0 part
  └─md124              9:124  0 425,4G  0 raid1
    ├─user-home      253:2    0    10G  0 lvm   /home
    └─user-libvirt   253:3    0    50G  0 lvm   /var/lib/libvirt
sdb                    8:16   0 447,1G  0 disk
├─sdb1                 8:17   0    20G  0 part
│ └─md126              9:126  0    20G  0 raid1
│   ├─fedora-root    253:0    0    12G  0 lvm   /
│   └─fedora-var_log 253:1    0     3G  0 lvm   /var/log
├─sdb2                 8:18   0     1G  0 part
│ └─md127              9:127  0  1023M  0 raid1 /boot
├─sdb3                 8:19   0   601M  0 part
│ └─md125              9:125  0 600,9M  0 raid1 /boot/efi
└─sdb4                 8:20   0 425,5G  0 part
  └─md124              9:124  0 425,4G  0 raid1
    ├─user-home      253:2    0    10G  0 lvm   /home
    └─user-libvirt   253:3    0    50G  0 lvm   /var/lib/libvirt
sdc                    8:32   0  59,6G  0 disk
├─sdc1                 8:33   0   600M  0 part
├─sdc2                 8:34   0     1G  0 part
└─sdc3                 8:35   0    58G  0 part
zram0                252:0    0     8G  0 disk  [SWAP]

Among other things, a system administrator can use it to detect new or unregistered LVM devices.

Finding the UUIDs of partitions

When mounting partitions or block devices in general you often want to know the UUID.

[…]$ sudo lsblk -o PATH,UUID
PATH                       UUID
/dev/sda
/dev/sda1                  13d9c9bb-9cbe-799e-8b29-f6cb85e1ec1f
/dev/sda2                  eadcabda-eefa-32d3-3cbd-51138b980056
/dev/sda3                  0cd0b75d-7644-747b-9a6d-46ca27f7cc97
/dev/sda4                  86c3f256-6d6a-e977-a3a1-5b3bf0229248
/dev/sdb
/dev/sdb1                  13d9c9bb-9cbe-799e-8b29-f6cb85e1ec1f
/dev/sdb2                  eadcabda-eefa-32d3-3cbd-51138b980056
...
...
/dev/zram0
/dev/mapper/fedora-root    d0346ab2-fa0f-4b38-9166-7b5da07019d3
/dev/mapper/fedora-root    d0346ab2-fa0f-4b38-9166-7b5da07019d3
/dev/mapper/fedora-var_log 04debf96-3f09-48aa-960c-b167b6756944
/dev/mapper/fedora-var_log 04debf96-3f09-48aa-960c-b167b6756944
/dev/mapper/user-home      79121558-10d2-4c7f-9248-2b4f89fbf8a0
...
...

Simply use grep to get the block device(s) for a specific UUID.

[…]$ sudo lsblk -o PATH,UUID | grep  13d9c9bb
/dev/sda1                  13d9c9bb-9cbe-799e-8b29-f6cb85e1ec1f
/dev/sdb1                  13d9c9bb-9cbe-799e-8b29-f6cb85e1ec1f

Mind you, sda1/sdb1 are part of a raid device.

Show block device attributes
[…]# blkid <DEVICE>

Example

[…]# blkid /dev/sdc1
/dev/sdc1: UUID="DC88-9268" BLOCK_SIZE="512" TYPE="vfat" PARTLABEL="EFI System Partition" PARTUUID="0a40ac7f-a2b3-49b6-b6e1-fd88311f455e"

Invoking blkid without parameters lists the properties of all block devices.

Managing the LVM configuration

As of Fedora 39, the LVM commands only take into account devices that are registered as belonging to the system. If, for example, a new device, e.g. a new partition or a new hard disk with an existing VG, is to be used on a system, this device must either be added to the devices file first, or all commands for detecting and activating VGs must be called with a specific parameter. The former is intended for permanent addition, the latter for temporary use that can no longer be used at the next boot.

List of registered devices
[…]$ sudo lvmdevices
  Device /dev/sda3 IDTYPE=sys_wwid IDNAME=naa.5000000000000000 DEVNAME=/dev/sda3 PVID=IoUGXYfv74B3YrmCoPfh9ZsWZsDrVKAN PART=3
Add a device (permanently)
[…]$ sudo lvmdevices --adddev /dev/<PART>

Managing Volume Groups (VG)

What Volume Grouops (LV) are available?
[…]$ sudo vgscan -v
What volume groups (VG) are active?
[…]$ sudo vgs

 VG            #PV #LV #SN Attr   VSize   VFree
 fedora_fedora   1   1   0 wz--n-   5,41g      0
 fedora_fedora   1   4   0 wz--n- 594,58g 229,58g
[…]$ sudo vgchange -ay
Resolving name conflicts

WARNING: VG name fedora_fedora is used by VGs fr0O3c-…-…-…-…-…-wH8yIn and xU2cHq-FK7x-…-…-…-…-XfaMuq. Fix duplicate VG names with vgrename uuid, a device filter, or system IDs.

[…]$ sudo vgs

 VG            #PV #LV #SN Attr   VSize   VFree
 fedora_fedora   1   1   0 wz--n-   5,41g      0
 fedora_fedora   1   4   0 wz--n- 594,58g 229,58g
[…]$ sudo vgrename <UUID> <NEW_NAME>
[…]$ sudo vgrename xU2cHq-…-…-…-…-…-XfaMuq  fedora_sys

Managing Logical Volumes (LV)

What logical volumes (LV) are active?
[…]$ sudo lvs
Mount an existing logical volume (LV)
[…]$ sudo mount  /dev/fedora_fedora/root   test

Access a KVM virtual disk image including LVM partitions

Using Network Block Devices (NBD)

This is a quick guide to mounting a qcow2 disk images on your host server. This is useful to reset passwords, edit files, or recover something without the virtual machine running.

A prerequisite is the installation of the package qemu-tools. For example, qemu-nbd uses qemu-img.

  1. Preparation

    1. Ensure that the nbd kernel driver is loaded.

      […]$ sudo lsmod | grep nbd

      If the output is empty or doesn’t include nbd, load it.

      […]$ sudo modprobe nbd max-part=6
      […]$ sudo lsmod | grep nbd
      nbd                    73728  0

      The command allows for 6 partitions to work on. Enlarge the number if you need more. The command modinfo nbd provides more information about available configuration options.

    2. Install the qemu-img package it not already done.

      […]$ sudo dnf install qemu-img
    3. You should not use your original VM disk image, but create a copy in another directory. Make this directory the default.

  2. Connect the VM image as network block device

    With all requirements in place you can connect your VM disk image to the first network block device, nbd0, and use fdisk to explore what partitions are available.

    […]$ sudo qemu-nbd --connect=/dev/nbd0   ./<IMAGE_NAME>.[qcow2|raw]
    […]$ sudo fdisk /dev/nbd0 -l

    For the F40 Beta VM it would be something like

    […]$ sudo qemu-nbd --connect=/dev/nbd0   Fedora-Server-KVM-40_Beta-1.9.x86_64.qcow2
    
    […]$ fdisk /dev/nbd0 -l
    Disk /dev/nbd0: 7 GiB, 7516192768 bytes, 14680064 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: gpt
    Disk identifier: E2669689-B476-46ED-8AF0-3720AD743AC0
    
    Device        Start      End  Sectors Size Type
    /dev/nbd0p1    2048     4095     2048   1M BIOS boot
    /dev/nbd0p2    4096  2101247  2097152   1G Linux extended boot
    /dev/nbd0p3 2101248 14678015 12576768   6G Linux LVM
  3. Mount the partition from the VM

    1. For standard partitions you can create a mount point and mount the filesystem.

      […]$ sudo mkdir /mnt/vm0p2
      […]$ sudo mount /dev/nbd0p2 /mnt/vm0p2
      […]$ sudo ls -al /mnt/vm0p2
      dr-xr-xr-x. 6 root root     4096 19. Mär 11:43 .
      drwxr-xr-x. 3 root root       19 20. Mär 12:39 ..
      -rw-r--r--. 1 root root   271869 26. Feb 01:00 config-6.8.0-0.rc6.49.fc40.x86_64
      drwxr-xr-x. 3 root root     4096 19. Mär 11:40 efi
      drwx------. 5 root root     4096 19. Mär 11:43 grub2
      ...
      ...

      If you want to be sure that nobody modifies the disk image you may mount it read-only as

      […]$ sudo mkdir /mnt/vm0p2
      […]$ sudo mount -ro /dev/nbd0p2 /mnt/vm0p2
      […]$ sudo ls -al /mnt/vm0p2
      dr-xr-xr-x. 6 root root     4096 19. Mär 11:43 .
      drwxr-xr-x. 3 root root       19 20. Mär 12:39 ..
      -rw-r--r--. 1 root root   271869 26. Feb 01:00 config-6.8.0-0.rc6.49.fc40.x86_64
      drwxr-xr-x. 3 root root     4096 19. Mär 11:40 efi
      drwx------. 5 root root     4096 19. Mär 11:43 grub2
      ...
      ...
    2. With an LVM partition you must first activate the Volume Group in the partition, check für Logical Volumes and mount the intended LV.

      […]$ sudo vgscan  --devices /dev/nbd0p3
        Found volume group "sysvg" using metadata type lvm2
      
      […]$ sudo vgchange --devices /dev/nbd0p3 -ay  sysvg
      1 logical volume(s) in volume group "sysvg" now active
      
      […]$ sudo lvscan --devices /dev/nbd0p3
        ACTIVE            '/dev/sysvg/root' [<6,00 GiB] inherit
      
      […]$ sudo mkdir  /mnt/vm0root
      […]$ sudo mount /dev/sysvg/root  /mnt/vm0root
      […]$ sudo ls -al /mnt/vm0root/
      dr-xr-xr-x.  18 root root  235 19. Mär 11:40 .
      drwxr-xr-x.   4 root root   34 20. Mär 16:22 ..
      dr-xr-xr-x.   2 root root    6 24. Jan 01:00 afs
      lrwxrwxrwx.   1 root root    7 24. Jan 01:00 bin -> usr/bin
      drwxr-xr-x.   2 root root    6 19. Mär 11:40 boot
      drwxr-xr-x.   2 root root    6 19. Mär 11:40 dev
      ...
      ...

      To make sure that nobody changes the disk image, you can also mount it read-only in this case by appending the parameter -o ro to the mount command. But mind that as soon as the Logical Volume is mounted, metadata on the volume changes, even being mounted read only. The user data and the size of the image file of course remain unchanged, but a comparison with the original file shows a difference.

  4. Do everything you want to do with the mounted directories

    You can read in the filesystems as you are used to any directorys and even write to the file system.

    You can even chroot to jail the filesystem and execute commands therein.

    […]$ sudo chroot /mnt/mv0p2/
    […]# ls -al
  5. After completion, reset everything in reverse order

    Unmount the file systems and disconnect the nbd

    […]$ sudo umount /mnt/vm0p2
    […]$ sudo qemu-nbd --disconnect /dev/nbd0
    […]$ sudo rmdir /mnt/vm0p2

    or if you mounted a LVM partition use

    […]$ sudo umount /mnt/vm0root
    […]$ sudo vgchange --devices /dev/nbd0p3 -an  sysvg
    […]$ sudo qemu-nbd --disconnect /dev/nbd0
    […]$ sudo rmdir /mnt/vm0root

    If you had to load the nbd module, unload it.

    […]$ sudo rmmod nbd

Troubleshooting

The nbd module can not be found
[…]$ sudo modprobe nbd max_part=6
modprobe: FATAL: Module nbd not found.

Probably your kernel does not compile nbd as a module. Try to proceed without this step. It it fails, too, you don’t have nbd support available at all.

Mount a disk image file via loop device

This method is particularly convenient if the image is in raw format and the partition to be mounted directly contains a file system.

  1. Check the format of the image file

    You may skip this if you are sure about the format or if qemu-img is not installed. Using the example of the ARM image invoke

    […]$ sudo qemu-img info ./Fedora-Server-KVM-40_Beta-1.9.x86_64.raw
    image: ./Fedora-Server-40_Beta-1.10.aarch64.raw
    file format: raw
    virtual size: 7 GiB (7516192768 bytes)
    disk size: 4.35 GiB
    Child node '/file':
        filename: ./Fedora-Server-40_Beta-1.10.aarch64.raw
        ...
        ...
  2. Determine the start block of the relevant partition

    […]$ sudo fdisk -lu ./Fedora-Server-40_Beta-1.10.aarch64.raw
    Disk ./Fedora-Server-40_Beta-1.10.aarch64.raw: 7 GiB, 7516192768 bytes, 14680064 sectors
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: dos
    Disk identifier: 0x117d0501
    
    Device                                    Boot   Start      End  Sectors  Size Id Type
    ./Fedora-Server-40_Beta-1.10.aarch64.raw1 *       2048  1230847  1228800  600M  6 FAT16
    ./Fedora-Server-40_Beta-1.10.aarch64.raw2      1230848  3327999  2097152    1G 83 Linux
    ./Fedora-Server-40_Beta-1.10.aarch64.raw3      3328000 14680063 11352064  5,4G 8e Linux LVM
  3. Mount the image at the start position of the relevant partition

    As an example we mount partition 2.

    The value for the starting block (sectors by default) must first be multiplied by 512. 1230848 (starting sector) times 512 (sector size) is 630 194 176. So you should run

    […]$ sudo mkdir /mnt/vm0part2
    […]$ sudo mount  Fedora-Server-40_Beta-1.10.aarch64.raw  /mnt/vm0part2 -o loop,offset=630194176

    Now, you can work in the subdirectry just as in any other subdirectory.

  4. When finished, reset everything

    […]$ sudo umount /mnt/vm0part2
    […]$ sudo rmdir /mnt/vm0part2

Mount a disk image file using libguestfs utilities

You might try libguestfs, which can mount many VM disk images automagically.

Advanced LVM management tasks

Display the position of the LV in a VG

You may use this information to optimize the LV performance.

[…]# pvs --segments -o+lv_name,seg_start_pe,segtype
   PV         VG    Fmt  Attr PSize   PFree   Start  SSize  LV         Start Type
  /dev/md125 usrvg lvm2 a--    1,76t 552,34g      0   5120 libvirt        0 linear
  /dev/md125 usrvg lvm2 a--    1,76t 552,34g   5120   8960 ourea_sys      0 linear
  /dev/md125 usrvg lvm2 a--    1,76t 552,34g  14080 204800 ourea_srv      0 linear
  /dev/md125 usrvg lvm2 a--    1,76t 552,34g 218880   8960                0 free
  /dev/md125 usrvg lvm2 a--    1,76t 552,34g 227840  25600 srv            0 linear
  /dev/md125 usrvg lvm2 a--    1,76t 552,34g 253440   8960 pontos_sys     0 linear
  /dev/md125 usrvg lvm2 a--    1,76t 552,34g 262400  12800 pontos_usr     0 linear
  /dev/md125 usrvg lvm2 a--    1,76t 552,34g 275200  38400                0 free
  /dev/md125 usrvg lvm2 a--    1,76t 552,34g 313600  10240 pgsql          0 linear
  /dev/md125 usrvg lvm2 a--    1,76t 552,34g 323840  56320                0 free
  /dev/md125 usrvg lvm2 a--    1,76t 552,34g 380160  17920 libvirt     5120 linear
  /dev/md125 usrvg lvm2 a--    1,76t 552,34g 398080  25600 machines       0 linear
  /dev/md125 usrvg lvm2 a--    1,76t 552,34g 423680  37720                0 free
  /dev/md126 sysvg lvm2 a--  <60,01g <34,01g      0   1536 var_log        0 linear
  /dev/md126 sysvg lvm2 a--  <60,01g <34,01g   1536   5120 root           0 linear
  /dev/md126 sysvg lvm2 a--  <60,01g <34,01g   6656   8706                0 free