Skip to content

Instantly share code, notes, and snippets.

@jeremiehuchet
Created August 19, 2024 21:16
Show Gist options
  • Select an option

  • Save jeremiehuchet/8989f10432e4506c6da184c522270fbf to your computer and use it in GitHub Desktop.

Select an option

Save jeremiehuchet/8989f10432e4506c6da184c522270fbf to your computer and use it in GitHub Desktop.
Reduce a mounted filesystem on an LVM partition

This note explain how to reduce a mounted filesystem on an LVM partition. It requires a reboot but most of the operations can be done online and the system can be used during the operations.

  1. Remount filesystem read-only
sudo mount -f -o remount,ro /home
  1. Snapshot the volume hosting the filesystem
sudo lvcreate -s data/home -L 30G -n home_resize
sudo lvdisplay data/home_resize                 
  --- Logical volume ---
  LV Path                /dev/data/home_resize
  LV Name                home_resize
  VG Name                data
  LV UUID                F1yAeM-rkxx-9PfM-7Wrf-pzQ2-qR4I-wdMML6
  LV Write Access        read/write
  LV Creation host, time tv, 2024-08-16 13:44:45 +0200
  LV snapshot status     active destination for home
  LV Status              available
  # open                 0
  LV Size                60,00 GiB
  Current LE             15360
  COW-table size         30,00 GiB
  COW-table LE           7680
  Allocated to snapshot  0,01%
  Snapshot chunk size    4,00 KiB
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:6
  1. Reduce the filesystem on the snapshot
sudo e2fsck -f /dev/mapper/data-home_resize
# reduce to the minimal possible size
sudo resize2fs -p -M /dev/mapper/data-home_resize
# or specify target size
sudo resize2fs -p /dev/mapper/data-home_resize 35G

sudo lvdisplay data/home_resize                                                                                 iotop
  --- Logical volume ---
  LV Path                /dev/data/home_resize
  LV Name                home_resize
  VG Name                data
  LV UUID                F1yAeM-rkxx-9PfM-7Wrf-pzQ2-qR4I-wdMML6
  LV Write Access        read/write
  LV Creation host, time tv, 2024-08-16 13:44:45 +0200
  LV snapshot status     active destination for home
  LV Status              available
  # open                 0
  LV Size                60,00 GiB
  Current LE             15360
  COW-table size         30,00 GiB
  COW-table LE           7680
  Allocated to snapshot  44,64%
  Snapshot chunk size    4,00 KiB
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:6

  1. Merge the snapshot with the reduced filesystem
sudo lvconvert --merge data/home_resize

  Delaying merge since origin is open.
  Merging of snapshot data/home_resize will occur on next activation of data/home.

# Here LVM cli warn this operation can't be done with an online volume
# but it can be deferred at next volume activation.
# We can reboot to trigger a re-activation.
  1. after restart, monitor snapshot merge process
sudo dmsetup status data-home
0 125829120 snapshot-merge 16/62914560 16

sudo lvs
  LV      VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home    data Owi-aos--- 60,00g             31,91 ← merge in progress
  root    data -wi-ao---- 50,00g
  swap    data -wi-ao----  8,00g
  1. Reduce the volume to a size sligtly higher than the filesystem (NOT LESS)
# my filesystem size is 32G, I reduce the volume to 35Go
sudo lvreduce -L 35G data/home
# make the filesystem grow to use all available space on the volume by omitting the size argument
sudo resize2fs -p /dev/mapper/data-home
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment