Skip to content

Instantly share code, notes, and snippets.

@nhthai2005
Last active April 13, 2022 05:32
Show Gist options
  • Save nhthai2005/b59a237338b098b5130b2a408be64adb to your computer and use it in GitHub Desktop.
Save nhthai2005/b59a237338b098b5130b2a408be64adb to your computer and use it in GitHub Desktop.
How to expand swap partition after system installation
# How to expand swap partition after system installation
# Supposed that installing Ubuntu with LVM and Encrypted with LUKS default. So, current swap is 1G and /dev/sda4 is encrypted. You want to add +3G to become +4G.
# Open encrypted partition, it is /dev/sda4 in this case
sudo cryptsetup luksOpen /dev/sda4 encrypted
sudo vgscan
sudo vgchange --activate y vgubuntu
sudo pvs
sudo vgs
sudo lvs
# Check and reduce /dev/mapper/vgubuntu-root
sudo e2fsck -f /dev/mapper/vgubuntu-root
sudo resize2fs /dev/mapper/vgubuntu-root 5G
# Remember 5G > 3G to safely reduce LVM Logical Volume.
sudo lvreduce -L -3G /dev/mapper/vgubuntu-root
sudo resize2fs /dev/mapper/vgubuntu-root
# Extend /dev/mapper/vgubuntu-swap_1
sudo lvextend -l +100%FREE /dev/mapper/vgubuntu-swap_1
sudo lvs
sudo mkswap /dev/mapper/vgubuntu-swap_1
# Close encrypted partition
sudo vgchange --activate n vgubuntu
sudo cryptsetup luksClose encrypted
# Reboot and check
df -h
free -h
# Reference: https://sleeplessbeastie.eu/2020/07/29/how-to-expand-swap-partition-after-system-installation/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment