Skip to content

Instantly share code, notes, and snippets.

@metablaster
Last active March 1, 2024 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metablaster/636997cd511dccafbdde8a19b3f187fa to your computer and use it in GitHub Desktop.
Save metablaster/636997cd511dccafbdde8a19b3f187fa to your computer and use it in GitHub Desktop.
Resize swap partition on encrypted LVM

Objective

Shrink encrypted LVM root partition (463,77GB) by 23GB and resize swap partition (1G) to 24GB

NOTE: Substitute partition names and similar with names that match your system or command output

Resize encrypted LVM root partition

The following commands are run from live USB

# Install tools
sudo apt update
sudo apt install lvm2 cryptsetup

# Load dm-crypt module
sudo modprobe dm-crypt

# List and identify partitions
lsblk
sudo fdisk --list /dev/sdb

# Unlock encrypted physical partition
sudo cryptsetup luksOpen /dev/sdb3 sdb3_crypt

# Rescan the available physical partitions
sudo vgscan --mknodes

# Make the logical volumes known to the kernel
sudo vgchange -ay

# Lists all available LVM partitions
sudo lvdisplay

# Run a file system check on root partition
sudo e2fsck -f /dev/mapper/vg-root

# Shrink file system to 439 GB down from 463 = -24GB (1GB more than needed)
sudo resize2fs /dev/mapper/vg-root 439G

# Shrink root partition by 23 GB down from 463 to 440 = -23GB
sudo lvreduce -L -23G /dev/mapper/vg-root

# Extend the file system to use all available space within the logical volume
sudo resize2fs /dev/mapper/vg-root

# Stop LVM and close the LUKS encrypted disk to ensure all the changes are saved properly
sudo vgchange -an
sudo cryptsetup luksClose sdb3_crypt

Resize swap partition

The following commands are run from system once booted up

# Lists all available LVM partitions
sudo lvdisplay

# Turn off swap
sudo swapoff -v /dev/mapper/vg-swap_1

# Extend 1GB swap with available free space
sudo lvextend -l +100%FREE /dev/mapper/vg-swap_1

# Format the new swap partition
sudo mkswap /dev/mapper/vg-swap_1

# Turn on swap
sudo swapon -v /dev/mapper/vg-swap_1

# Test if the swap logical volume was successfully extended and activated
cat /proc/swaps
free -h
lsblk

Reference

LVM Resize – How to Decrease an LVM Partition

Resizing Encrypted LVM Partitions on Linux

Chapter 15. Swap Space

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment