# LVM

Collection of procedures for manipulating LVM stuff


# Expand VG on same partition

You want to re-use the same partition, e.g. expand sda2 and increase the size of root_pv

sda                    8:0    0   50G  0 disk
├─sda1                 8:1    0  487M  0 part /boot
└─sda2                 8:5    0 49.5G  0 part
  ├─my_vg-root_pv  253:0    0 48.6G  0 lvm  /
  └─my_vg-swap_pv  253:1    0  980M  0 lvm  [SWAP]

# Expand the VM disk

This part will be done on the hypervisor, increase the VM's disk as needed. Once the disk has been resized, use one of the methods here (opens new window) to detect the new disk size; otherwise reboot the system for it to detect the new size.

# Expand Partition Size

Install growpart to make this easier, the alternative is to delete and recreate the partition using fdisk

yum install -y cloud-utils-growpart

Expand the disk, take note of the space between /dev/sda and 2

growpart /dev/sda 2

/dev/sda2 should now show the larger size

# Resize the PV

Run pvs before and after to see the difference. You should see a larger PV after running pvresize

pvs
pvresize /dev/sda2
pvs

Your VG should also be larger now, check with

vgs

# Expand LV

This will resize your LV to take up all of the free space now available

lvextend -l +100%FREE /dev/my_vg/root_pv

# Resize Filesystem

Depending on your chosen filesystem, use one of the following:

# XFS

Using xfs:

#xfs_growfs <mountpoint>
xfs_growfs /

# EXT3/EXT4

using EXT filesystem

resize2fs /dev/my_vg/root_pv

# Expand VG using new partition

  • If LVM exists on partitioned areas, start with this section.

  • If LVM exists on bare disks, run this command (Assuming /dev/sda is where your existing LVM resides and you just resized it):

    pvresize /dev/sda
    

    Once resized, skip to this section.

# Create or Expand the VM disk

This part will be done on the hypervisor, move on to the next part once the VM's disk has been increased in size.

# Create a new partition

Create a new partition on the newly added free space and then set the partition type to lvm

We're using /dev/sda as the disk that has been expanded

/dev/vg_vol as the existing VG to be extended

lv_part as the partition to be extended

  • fdisk /dev/sda
  • Create a new primary partition at /dev/sda3 n, p, 3
  • Check the partition table p
  • Change the partition type to Linux LVM t, 3, 8e
  • Check the partition table again p
  • Write the changes w
  • Reboot to save changes (if required)

# Create a new PV on the new partition

  • pvcreate /dev/sda3
  • Check the created PV pvdisplay

# Add the new PV to the existing VG

  • vgextend /dev/vg_vol /dev/sda3
  • vgdisplay

# Extend the LV to use the new space

  • lvextend -l +100%FREE /dev/vg_vol/lv_part

# Resize the filesystem

  • resize2fs /dev/vg_vol/lv_part

# Converting to LVM

We will copy our main root partition to a new empty disk and then extend the lvm partition to use the original disk after it has been copied over. This assumes that you are using a separate /boot partition.

# Prepare LVM Disk

This is our initial state:

# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  1.8T  0 disk
├─sda1   8:1    0  511M  0 part /boot/efi
├─sda2   8:2    0  511M  0 part /boot
├─sda3   8:3    0  1.8T  0 part /
└─sda4   8:4    0  511M  0 part [SWAP]
sdb      8:16   0  1.8T  0 disk
└─sdb1   8:17   0  511M  0 part

We'll use /dev/sdb as our new disk for creating the LVM filesystem

vg_main as our new VG

lv_root as our new LV

  • Create the new partition with fdisk /dev/sdb
# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  1.8T  0 disk
├─sda1   8:1    0  511M  0 part /boot/efi
├─sda2   8:2    0  511M  0 part /boot
├─sda3   8:3    0  1.8T  0 part /
└─sda4   8:4    0  511M  0 part [SWAP]
sdb      8:16   0  1.8T  0 disk
├─sdb1   8:17   0  511M  0 part
└─sdb2   8:18   0  1.8T  0 part
  • pvcreate /dev/sdb2
# pvcreate /dev/sdb2
  Physical volume "/dev/sdb2" successfully created
  • vgcreate vg_main /dev/sdb2
# vgcreate vg_main /dev/sdb2
  Volume group "vg_main" successfully created

Here's our state so far:

# vgdisplay
  --- Volume group ---
  VG Name               vg_main
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               1.82 TiB
  PE Size               4.00 MiB
  Total PE              476804
  Alloc PE / Size       0 / 0
  Free  PE / Size       476804 / 1.82 TiB
  VG UUID               ltBtnM-2Oxu-WVWk-YcLY-wLHy-AZiE-dm2MiR

# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sdb2
  VG Name               vg_main
  PV Size               1.82 TiB / not usable 1.07 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              476804
  Free PE               476804
  Allocated PE          0
  PV UUID               o8svsy-NUwd-2mOe-6PWx-Taop-Cf44-MxA19c

We'll use 100% of the free space on this disk

  • lvcreate --name lv_root vg_main -l 100%FREE
# lvcreate --name lv_root vg_main -l 100%FREE
  Logical volume "lv_root" created.

And our state:

# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg_main/lv_root
  LV Name                lv_root
  VG Name                vg_main
  LV UUID                9y6COH-813Z-AU6u-nAtd-CdCA-leOS-NgT57S
  LV Write Access        read/write
  LV Creation host, time ns3099345, 2018-09-01 16:28:21 +0200
  LV Status              available
  # open                 0
  LV Size                1.82 TiB
  Current LE             476804
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0

# Create a new filesystem

  • mkfs.ext4 /dev/vg_main/lv_root
# mkfs.ext4 /dev/vg_main/lv_root
mke2fs 1.42.13 (17-May-2015)
Creating filesystem with 488247296 4k blocks and 122068992 inodes
Filesystem UUID: 21a20292-bbaa-47a0-9634-138922619019
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
        102400000, 214990848

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

# Copy contents to new filesystem

  • mkdir /mnt/sdb
  • mount /dev/vg_main/lv_root /mnt/sdb
  • cp -dpRx / /mnt/sdb/

# Edit fstab

We will need to add the disk in to our copied disk

  • Grab UUID with blkid
# blkid
/dev/sda1: LABEL="EFI_SYSPART" UUID="9F4B-3971" TYPE="vfat" PARTLABEL="primary" PARTUUID="cf755262-1548-4bff-9304-ea4c5cc5035d"
/dev/sda2: LABEL="/boot" UUID="1a818491-c638-4db2-988d-db2894f76d0d" TYPE="ext4" PARTLABEL="primary" PARTUUID="064c46cc-725b-4cf1-ba80-f0e194ebae99"
/dev/sda3: LABEL="/" UUID="a77a87e5-a10d-4655-b633-48676cfa3b2a" TYPE="ext4" PARTLABEL="primary" PARTUUID="fcd69399-1f5a-49fe-9d61-03d51f734e75"
/dev/sda4: LABEL="swap-sda4" UUID="68cae6eb-bc9a-42a8-ba07-91bbeb2cbb9a" TYPE="swap" PARTLABEL="primary" PARTUUID="898360d2-9b48-482c-a669-86c529d1f2af"
/dev/sdb1: LABEL="EFI_SYSPART" UUID="A00D-3109" TYPE="vfat" PARTLABEL="primary" PARTUUID="225b4983-72a6-4131-927c-9010c21382cd"
/dev/sdb2: UUID="o8svsy-NUwd-2mOe-6PWx-Taop-Cf44-MxA19c" TYPE="LVM2_member" PARTUUID="a49e995c-a690-4b35-98cf-865047688ad3"
/dev/mapper/vg_main-lv_root: UUID="21a20292-bbaa-47a0-9634-138922619019" TYPE="ext4"
  • nano /mnt/sdb/etc/fstab

Add UUID= to the UUID itself and place it in the file

This is what the result looks like:

# <file system> <mount point>   <type>  <options>       <dump>  <pass>
#/dev/sda3      /       ext4    errors=remount-ro,relatime      0       1
UUID=21a20292-bbaa-47a0-9634-138922619019 /       ext4    errors=remount-ro,relatime      0       1
/dev/sda2       /boot   ext4    errors=remount-ro,relatime      0       1
/dev/sda4       swap    swap    defaults        0       0
proc            /proc   proc    defaults                0       0
sysfs           /sys    sysfs   defaults                0       0
/dev/sda1       /boot/efi       vfat    defaults,relatime       0       0
devtmpfs        /dev    devtmpfs        rw      0       0

# Scan the changes

  • vgscan
  • vgchange -ay
# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "vg_main" using metadata type lvm2
# vgchange -ay
  1 logical volume(s) in volume group "vg_main" now active

# Create new ramdisk

This step may be needed if your existing ramdisk is not aware of LVM

  • mkinitramfs -o /boot/initrd-$(uname -r).lvm.img
# mkinitramfs -o /boot/initrd-$(uname -r).lvm.img
W: mdadm: /etc/mdadm/mdadm.conf defines no arrays.

# ls -lah /boot
total 85M
drwxr-xr-x  4 root root 1.0K Sep  1 16:35 .
drwxr-xr-x 21 root root 4.0K Sep  1 16:04 ..
-rw-r--r--  1 root root 1.2M Aug 15 19:48 abi-4.4.0-134-generic
-rw-r--r--  1 root root 187K Aug 15 19:48 config-4.4.0-134-generic
drwxr-xr-x  3 root root 4.0K Jan  1  1970 efi
drwxr-xr-x  6 root root 1.0K Sep  1 15:58 grub
-rw-r--r--  1 root root  37M Sep  1 16:35 initrd-4.4.0-134-generic.lvm.img
-rw-r--r--  1 root root  37M Sep  1 16:04 initrd.img-4.4.0-134-generic
-rw-r--r--  1 root root  255 Aug 15 19:48 retpoline-4.4.0-134-generic
-rw-------  1 root root 3.8M Aug 15 19:48 System.map-4.4.0-134-generic
-rw-------  1 root root 6.9M Aug 15 19:48 vmlinuz-4.4.0-134-generic

# Edit grub

  • nano /boot/grub/grub.cfg
  • Add /dev/vg_main/lv_root to the root directory
  • Change initrd image to the one created earlier mkinitramfs -o /boot/initrd-$(uname -r).lvm.img

Result

---snip---

menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-a77a87e5-a10d-4655-b633-48676cfa3b2a' {
        recordfail
        load_video
        gfxmode $linux_gfx_mode
        insmod gzio
        if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
        insmod part_gpt
        insmod ext2
        set root='hd0,gpt2'
        if [ x$feature_platform_search_hint = xy ]; then
          search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2  1a818491-c638-4db2-988d-db2894f76d0d
        else
          search --no-floppy --fs-uuid --set=root 1a818491-c638-4db2-988d-db2894f76d0d
        fi
        linux   /vmlinuz-4.4.0-134-generic root=/dev/vg_main/lv_root ro  noquiet nosplash net.ifnames=0 biosdevname=0
        initrd  /initrd-4.4.0-134-generic.lvm.img

---snip---

# Reboot

Check that your server comes back up, once it does, it should look like this:

# lsblk
NAME                MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                   8:0    0  1.8T  0 disk
├─sda1                8:1    0  511M  0 part /boot/efi
├─sda2                8:2    0  511M  0 part /boot
├─sda3                8:3    0  1.8T  0 part
└─sda4                8:4    0  511M  0 part [SWAP]
sdb                   8:16   0  1.8T  0 disk
├─sdb1                8:17   0  511M  0 part
└─sdb2                8:18   0  1.8T  0 part
  └─vg_main-lv_root 252:0    0  1.8T  0 lvm  /

# df -h
Filesystem            Size  Used Avail Use% Mounted on
udev                   16G     0   16G   0% /dev
tmpfs                 3.2G   14M  3.1G   1% /run
/dev/vg_main/lv_root  1.8T  1.5G  1.7T   1% /
tmpfs                  16G     0   16G   0% /dev/shm
tmpfs                 5.0M     0  5.0M   0% /run/lock
tmpfs                  16G     0   16G   0% /sys/fs/cgroup
/dev/sda2             487M   97M  362M  22% /boot
/dev/sda1             510M  136K  510M   1% /boot/efi

You can see that the / partition is mounted on /dev/sdb2 now.

# Create New PV

  • Check your disk layout fdisk -l or lsblk

DANGER

Do not delete your boot partition!

  • pvcreate /dev/sda3
# pvcreate /dev/sda3
WARNING: ext4 signature detected on /dev/sda3 at offset 1080. Wipe it? [y/n]: y
  Wiping ext4 signature on /dev/sda3.
  Physical volume "/dev/sda3" successfully created
  • Check the new PV pvdisplay
# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sdb2
  VG Name               vg_main
  PV Size               1.82 TiB / not usable 1.07 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              476804
  Free PE               0
  Allocated PE          476804
  PV UUID               o8svsy-NUwd-2mOe-6PWx-Taop-Cf44-MxA19c

  "/dev/sda3" is a new physical volume of "1.82 TiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda3
  VG Name
  PV Size               1.82 TiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               3uJ1oO-kt5i-cjnR-kHU8-1jOc-1jzx-Wa1gXj

# Extend VG

  • Extend your VG vgextend /dev/vg_main /dev/sda3
# vgextend /dev/vg_main /dev/sda3
  Volume group "vg_main" successfully extended
  • Check your VG vgdisplay
# vgdisplay
  --- Volume group ---
  VG Name               vg_main
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  3
  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               3.64 TiB
  PE Size               4.00 MiB
  Total PE              953351
  Alloc PE / Size       476804 / 1.82 TiB
  Free  PE / Size       476547 / 1.82 TiB
  VG UUID               ltBtnM-2Oxu-WVWk-YcLY-wLHy-AZiE-dm2MiR

# Extend LV

  • Extend your LV lvextend -l +100%FREE /dev/vg_main/lv_root
# lvextend -l +100%FREE /dev/vg_main/lv_root
  Size of logical volume vg_main/lv_root changed from 1.82 TiB (476804 extents) to 3.64 TiB (953351 extents).
  Logical volume lv_root successfully resized.
  • Check your LV lvs
# lvs
  LV      VG      Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv_root vg_main -wi-ao---- 3.64t
# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg_main/lv_root
  LV Name                lv_root
  VG Name                vg_main
  LV UUID                9y6COH-813Z-AU6u-nAtd-CdCA-leOS-NgT57S
  LV Write Access        read/write
  LV Creation host, time ns3099345, 2018-09-01 16:28:21 +0200
  LV Status              available
  # open                 1
  LV Size                3.64 TiB
  Current LE             953351
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0

# Resizing the filesystem

  • Resize the partition resize2fs /dev/vg_main/lv_root Before:
# df -h
Filesystem            Size  Used Avail Use% Mounted on
udev                   16G     0   16G   0% /dev
tmpfs                 3.2G   14M  3.1G   1% /run
/dev/vg_main/lv_root  1.8T  1.5G  1.7T   1% /
tmpfs                  16G     0   16G   0% /dev/shm
tmpfs                 5.0M     0  5.0M   0% /run/lock
tmpfs                  16G     0   16G   0% /sys/fs/cgroup
/dev/sda2             487M   97M  362M  22% /boot
/dev/sda1             510M  136K  510M   1% /boot/efi
# resize2fs /dev/vg_main/lv_root
resize2fs 1.42.13 (17-May-2015)
Filesystem at /dev/vg_main/lv_root is mounted on /; on-line resizing required
old_desc_blocks = 117, new_desc_blocks = 233
The filesystem on /dev/vg_main/lv_root is now 976231424 (4k) blocks long.

After:

# df -h
Filesystem            Size  Used Avail Use% Mounted on
udev                   16G     0   16G   0% /dev
tmpfs                 3.2G   14M  3.1G   1% /run
/dev/vg_main/lv_root  3.6T  1.5G  3.5T   1% /
tmpfs                  16G     0   16G   0% /dev/shm
tmpfs                 5.0M     0  5.0M   0% /run/lock
tmpfs                  16G     0   16G   0% /sys/fs/cgroup
/dev/sda2             487M   97M  362M  22% /boot
/dev/sda1             510M  136K  510M   1% /boot/efi

# Finish

Reboot your system to make sure everything comes back.

Last Updated: 2021/12/11 19:48+00:00