Skip to main content

Expanding an existing filesystem using LVM

Description

This example below shows you how to expand an existing filesystem that it managed by Logical Volume Manager (LVM)

Examine the existing filesystem

This command will to the host to rescan the host adapters after a new disk has been added.

echo "- - -" | tee /sys/class/scsi_host/host*/scan

/dev/mapper/ubuntu--vg-ubuntu--lv is the filesystem I want to expand.

root@dock-host-2:/mnt# df -lh
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              1.6G  1.7M  1.6G   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   63G   17G   43G  28% /
tmpfs                              7.9G     0  7.9G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          974M  163M  745M  18% /boot
tmpfs                              1.6G  4.0K  1.6G   1% /run/user/1000

Use lsblk to take a look at all devices available. In my example I am going to use /dev/sdb as the disk to expand the existing filesystem in the LVM. lsblk shows that is /dev/sdb is 128GB in size.

root@dock-host-2:~# lsblk
NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
loop0                       7:0    0  55.5M  1 loop /snap/core18/2344
loop1                       7:1    0  55.5M  1 loop /snap/core18/2409
loop2                       7:2    0 118.4M  1 loop /snap/docker/1779
loop3                       7:3    0  61.9M  1 loop /snap/core20/1494
loop4                       7:4    0 102.4M  1 loop /snap/lxd/23243
loop5                       7:5    0  61.9M  1 loop /snap/core20/1518
loop6                       7:6    0 117.2M  1 loop /snap/docker/1767
loop7                       7:7    0    47M  1 loop /snap/snapd/16292
loop8                       7:8    0    47M  1 loop /snap/snapd/16010
loop10                      7:10   0 102.4M  1 loop /snap/lxd/23270
sda                         8:0    0   128G  0 disk
├─sda1                      8:1    0     1M  0 part
├─sda2                      8:2    0     1G  0 part /boot
└─sda3                      8:3    0   127G  0 part
  └─ubuntu--vg-ubuntu--lv 253:0    0  63.5G  0 lvm  /
sdb                         8:16   0   128G  0 disk
sr0                        11:0    1  1024M  0 rom

The next step is to run a command to prep the new disk for the LVM format.

root@dock-host-2:~# 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.

Once the disk is ready we need to identify the the LVM name and use it to extend the volume with the new disk we set up.

root@dock-host-2:~# vgs
  VG        #PV #LV #SN Attr   VSize    VFree
  ubuntu-vg   1   1   0 wz--n- <127.00g 63.50g

Once we know the name of the Volume Group we can expand the Volume Group with the new disk.

root@dock-host-2:~# vgextend ubuntu-vg /dev/sdb
  Volume group "ubuntu-vg" successfully extended

After the Volume Group has been extended we can run some commands to verify the new size. You can see in the example below that we have more space in our Volume Group.

root@dock-host-2:~# vgs
  VG        #PV #LV #SN Attr   VSize   VFree
  ubuntu-vg   2   1   0 wz--n- 254.99g <191.50g

root@dock-host-2:~# vgdisplay
  --- Volume group ---
  VG Name               ubuntu-vg
  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               254.99 GiB
  PE Size               4.00 MiB
  Total PE              65278
  Alloc PE / Size       16255 / <63.50 GiB
  Free  PE / Size       49023 / <191.50 GiB
  VG UUID               CVE1jf-w4fj-FreW-Xn1p-i3gv-gzbh-GcpJdY

Now that the Volume Group is expanded we need to expand the Logical Volume and expand the Filesystem to use the new disk space. The commands below are used to check on the Logical Volume and then extend the Logical Volume.

root@dock-host-2:~# lvdisplay
  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/ubuntu-lv
  LV Name                ubuntu-lv
  VG Name                ubuntu-vg
  LV UUID                K5CEFt-q6tF-cjxB-wCFh-f970-CTy9-07KYYk
  LV Write Access        read/write
  LV Creation host, time ubuntu-server, 2021-12-19 20:48:29 +0000
  LV Status              available
  # open                 1
  LV Size                <63.50 GiB
  Current LE             16255
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0


root@dock-host-2:~# lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
  Size of logical volume ubuntu-vg/ubuntu-lv changed from <63.50 GiB (16255 extents) to 254.99 GiB (65278 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.

The last step is the expand the filesystem and verify that there is more free space.

root@dock-host-2:~# resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv
resize2fs 1.46.3 (27-Jul-2021)
Filesystem at /dev/mapper/ubuntu--vg-ubuntu--lv is mounted on /; on-line resizing required
old_desc_blocks = 8, new_desc_blocks = 32
The filesystem on /dev/mapper/ubuntu--vg-ubuntu--lv is now 66844672 (4k) blocks long.


root@dock-host-2:~# df -lh
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              1.6G  1.7M  1.6G   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv  251G   17G  224G   7% /
tmpfs                              7.9G     0  7.9G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          974M  163M  745M  18% /boot
tmpfs                              1.6G  4.0K  1.6G   1% /run/user/1000