Ubuntu – Increase size of encrypted swap

bootcryptswapswap

Ubuntu 18.04 is by default creating a random encrypted swap during each boot using dm-crypt/Swap encryption. For me, this is creating a 1gb swap but I cannot find where to increase the size of the swap it creates on each boot. How can we increase the swap size allocated during each boot? I do not want to switch to a static swapfile. There is no /swapfile or partition.

This appears to be a new feature where the swap is dynamically created each time the system boots. How can I increase it?

from /etc/crypttab:

nvme0n1p3_crypt UUID=1e092df8-5c37-4409-8ac8-361402244f69 none luks,discard
cryptswap1 UUID=aba1b825-a77b-4384-a40f-fdc6fdaaced0 /dev/urandom swap,offset=1024,cipher=aes-xts-plain64

from /etc/fstab:

/dev/mapper/cryptswap1 none swap sw 0 0
# swapon -s
Filename                Type        Size    Used    Priority
/dev/dm-3                               partition   1003004 601088  -2

htop

gparted does not show any swap partition. This is the only drive.

gparted

In an answer here, it is suggested to do:

sudo swapoff -a  
sudo cryptsetup resize cryptswap1 --size (in sectors)  
sudo mkswap /dev/cryptswap1  
sudo swapon -a  

So I run: cryptsetup resize cryptswap1 --size 7812500. This results in the error: Device /dev/mapper/mint--vg-swap_1 is too small.

So I try: cryptsetup resize /dev/mapper/mint--vg-swap_1 --size 7812500

This does not produce an error but parted --list; fdisk -l shows no change:

Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/cryptswap1: 979.5 MiB, 1027080192 bytes, 2006016 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

and trying to again run cryptsetup resize cryptswap1 --size 7812500 produces Device /dev/mapper/mint--vg-swap_1 is too small.

I have also tried: resize2fs /dev/mapper/mint--vg-swap_1 -s 4G which produced:

resize2fs 1.44.1 (24-Mar-2018)
resize2fs: Device or resource busy while trying to open /dev/mapper/mint--vg-swap_1
Couldn't find valid filesystem superblock.
# lsblk
NAME                  MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
loop0                   7:0    0  54.6M  1 loop  /snap/core18/1279
loop1                   7:1    0  89.1M  1 loop  /snap/core/8213
loop2                   7:2    0  89.1M  1 loop  /snap/core/8268
loop3                   7:3    0 497.4M  1 loop  /snap/wickrme/246
loop4                   7:4    0 497.2M  1 loop  /snap/wickrme/247
loop5                   7:5    0  54.6M  1 loop  /snap/core18/1288
nvme0n1               259:0    0 465.8G  0 disk  
├─nvme0n1p1           259:1    0   512M  0 part  /boot/efi
├─nvme0n1p2           259:2    0   732M  0 part  /boot
└─nvme0n1p3           259:3    0 464.6G  0 part  
  └─nvme0n1p3_crypt   253:0    0 464.6G  0 crypt 
    ├─mint--vg-root   253:1    0 463.6G  0 lvm   /
    └─mint--vg-swap_1 253:2    0   980M  0 lvm   
      └─cryptswap1    253:3    0 979.5M  0 crypt [SWAP]

Best Answer

sudo swapoff -a  
sudo cryptsetup resize cryptswap1 --size (in sectors)  
sudo mkswap /dev/mapper/cryptswap1  
sudo swapon -a  

If --size is left out, it will use the whole device.

UPDATE - Based on feedback. Reboot and make sure mint--vg-swap_1 is still 980M.
IF IT IS, do the steps below. -- Try manually increasing the size of the container first (I don't think that should have been necessary). Your comment says you are trying to make the swap 4G (+3G), so:

sudo swapoff -a  
sudo lvresize -L+3G /dev/mapper/mint--vg-swap_1
sudo cryptsetup resize cryptswap1 --size 7812500
sudo mkswap /dev/mapper/cryptswap1  
sudo swapon -a  

On the other hand, if mint--vg-swap_1 is now 4G, then run this (allow for the size of the header):

sudo swapoff -a  
sudo cryptsetup resize cryptswap1 --size 7808404   
sudo mkswap /dev/mapper/cryptswap1  
sudo swapon -a  

Reference