How to expand swap partition after system installation

Expand encrypted swap partition after system installation.

Operating system version.

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu Focal Fossa (development branch)
Release:        20.04
Codename:       focal
You cannot reduce ext4 file system in online mode, so boot live Ubuntu OS from the USB or CD.

The operation is quite simple:

  • Boot live OS.
  • Open LUKS device.
  • Activate LVM Logical Volumes.
  • Check and repair root filesystem.
  • Shrink root filesystem.
  • Reduce the size of root LVM Logical Volume.
  • Expand root filesystem to fit its LVM Logical Volume.
  • Extend swap LVM Logical Volume.
  • Deactivate LVM Logical Volumes on LUKS device.
  • Close LUKS device.
  • Reboot.

I will operate on /dev/sda hard disk.

$ sudo sfdisk -l /dev/sda
Disk /dev/sda: 447.13 GiB, 480103981056 bytes, 937703088 sectors
Disk model: KINGSTON SA400S3
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x139b1ddb

Device     Boot   Start       End   Sectors   Size Id Type
/dev/sda1  *       2048   1499135   1497088   731M 83 Linux
/dev/sda2       1501182 937701375 936200194 446.4G  5 Extended
/dev/sda5       1501184 937701375 936200192 446.4G 83 Linux

Swap is located on the encrypted /dev/sda5 partition.

$ lsblk /dev/sda -o name,size,type,fstype
NAME                    SIZE TYPE  FSTYPE
sda                   447.1G disk  
|-sda1                  731M part  ext4
|-sda2                    1K part  
`-sda5                446.4G part  crypto_LUKS

Open /dev/sda5 LUKS device and set up the encrypted mapping.

$ sudo cryptsetup luksOpen /dev/sda5 encrypted
Enter passphrase for /dev/sda5: ****************

Search for all LVM Volume Groups.

$ sudo vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "vgubuntu" using metadata type lvm2

Activate LVM Logical Volumes on found vgubuntu LVM Volume Group.

$ sudo vgchange --activate y vgubuntu
  2 logical volume(s) in volume group "vgubuntu" now active

Display information about LVM Physical Volumes.

$ sudo pvs
  PV                    VG       Fmt  Attr PSize    PFree 
  /dev/mapper/encrypted vgubuntu lvm2 a--  <446.40g 20.00

Display information about LVM Volume Groups.

$ sudo vgs
  VG       #PV #LV #SN Attr   VSize    VFree 
  vgubuntu   1   2   0 wz--n- <446.40g 20.00m

Display information about LVM Logical Volumes.

$ sudo lvs
  LV     VG       Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root   vgubuntu -wi-a----- <445.43g                                                    
  swap_1 vgubuntu -wi-a-----  976.00m 

Check and repair file system on root LVM Logical Volume.

$ sudo e2fsck -f /dev/mapper/vgubuntu-root 
e2fsck 1.45.3 (14-Jul-2019)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/mapper/vgubuntu-root: 213658/29196288 files (0.2% non-contiguous), 3639411/116765696 blocks

Shrink that file system to safely reduce LVM Logical Volume.

$ sudo resize2fs /dev/mapper/vgubuntu-root  400G
resize2fs 1.45.3 (14-Jul-2019)
Resizing the filesystem on /dev/mapper/vgubuntu-root to 104857600 (4k) blocks.
The filesystem on /dev/mapper/vgubuntu-root is now 104857600 (4k) blocks long.

Reduce the size of the root LVM Logical Volume.

$ sudo lvreduce -L -15G /dev/mapper/vgubuntu-root 
  WARNING: Reducing active logical volume to <430.43 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vgubuntu/root? [y/n]: y
  Size of logical volume vgubuntu/root changed from <445.43 GiB (114029 extents) to <430.43 GiB (110189 extents).
  Logical volume vgubuntu/root successfully resized.

Expands filesystem located on root LVM Logical Volume to use all available space within it.

$ sudo resize2fs /dev/mapper/vgubuntu-root  
resize2fs 1.45.3 (14-Jul-2019)
Resizing the filesystem on /dev/mapper/vgubuntu-root to 112833536 (4k) blocks.
The filesystem on /dev/mapper/vgubuntu-root is now 112833536 (4k) blocks long.

Extend swap LVM Logical Volume to use all available free space that was freed when you reduced the size of the root LVM Logical Volume.

$ sudo lvextend -l +100%FREE /dev/mapper/vgubuntu-swap_1 
  Size of logical volume vgubuntu/swap_1 changed from 976.00 MiB (244 extents) to 15.97 GiB (4089 extents).
  Logical volume vgubuntu/swap_1 successfully resized.

Display information about LVM Volume Groups.

$ sudo  lvs
  LV     VG       Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root   vgubuntu -wi-a----- <430.43g                                                    
  swap_1 vgubuntu -wi-a-----   15.97g 
Notice the difference.

Set up swap partition.

$ sudo mkswap /dev/mapper/vgubuntu-swap_1 
mkswap: /dev/mapper/vgubuntu-swap_1: warning: wiping old swap signature.
Setting up swapspace version 1, size = 16 GiB (17150504960 bytes)
no label, UUID=69477769-296e-4a26-ba40-03f9c0e4f772

Deactivate LVM Logical Volumes on the vgubuntu LVM Volume Group located on an encrypted LUKS device.

$ sudo vgchange --activate n vgubuntu
  0 logical volume(s) in volume group "vgubuntu" now active

Close /dev/sda5 LUKS device and remove encrypted mapping.

$ sudo cryptsetup luksClose encrypted

Reboot. Boot regular Ubuntu operating system to verify.

$ free -h
              total        used        free      shared  buff/cache   available
Mem:           15Gi       2.9Gi       6.5Gi       613Mi       6.1Gi        11Gi
Swap:         975Mi          0B       975Mi
Total:         16Gi       2.9Gi       7.4Gi

Several of these steps can be omitted, but are described here to make things clear.