13

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]
5
  • What is the device type? (plain, luks, etc.)
    – JasonF4
    Jan 3, 2020 at 23:10
  • The drive is luks encrypted.
    – xendi
    Jan 3, 2020 at 23:11
  • Can we see the output of lsblk?
    – Kaz Wolfe
    Jan 4, 2020 at 6:28
  • Added it. It appears I had a misconception. I didn't realize that gparted doesn't deal with LVM. I think I need to shrink my root LVM to give more room for my cryptswap but I don't have a flash drive on hand to boot to. I don't think the root vol can be shrunk "Live"
    – xendi
    Jan 4, 2020 at 7:09
  • I updated the answer based on the error you got. Also, you didn't allow for the size of the header in your attempted resizing of mint--vg-swap_1 (2MiB). mint--vg-swap_1 needs to be 2MiB larger than cryptswap (so cryptswap wouldn't fit in there). I don't think you can use that resize command on the container. See my updated answer.
    – JasonF4
    Jan 4, 2020 at 14:25

2 Answers 2

10

This answer is based on Ubuntu 22.04 LTS

lslbk
   # └─sda6                  8:6    0 464,6G  0 part  
   #   └─sda6_crypt        253:0    0 464,5G  0 crypt 
   #     ├─vgubuntu-root   253:1    0 463,6G  0 lvm   /
   #     └─vgubuntu-swap_1 253:2    0   980M  0 lvm   [SWAP]

One thing that was not addressed before is, that, in order to INCREASE the lvm swap size, the lvm root size has to be DECREASED first!

(At least when lsblk (like above) shows that root + swap make up the whole encrypted device.)


TL;DR (But backup important data!)

Here we decrease root -40G AND increase swap +40G:

# Boot Ubuntu Live/Flash/"Try Ubuntu" AND open terminal
# Run subsequent commands as superuser
sudo -i
    # `sudo`    => Execute a command as another user.
    # `sudo -i [-u user]` => Run an interactive session with substitute user, default is root.
    # `sudo [-u user] command` => Run a command with a substitute user, default is root.

# Encrypted device should NOT be unlocked
lsblk # => list block devices
    # └─sda6 => no `crypt`/`lvm``
# Unlock encrypted device
cryptsetup open /dev/sda6 crypt # Enter passphrase
    # `cryptsetup` => Manage dm-crypt + LUKS encrypted volumes.
    # `cryptsetup open <device> <name>` => Opens encrypted lv as <name>

# Get logical volume identifiers
lsblk
    # └─sda6                  8:6    0 464,6G  0 part
    #   └─sda6_crypt        253:0    0 464,5G  0 crypt
    #     ├─vgubuntu-root   253:1    0 463,6G  0 lvm   /
    #     └─vgubuntu-swap_1 253:2    0   980M  0 lvm   [SWAP]

# Shrink logical root volume AND filesystem
lvresize --verbose --resizefs -L -40G /dev/mapper/vgubuntu-root
    # `lvresize` <volume> => resize a logical volume
    #   --verbose  => Give more info.
    #   --resizefs => Resize filesystem AND LV with fsadm(8).
    #   -L         => Specifies the new size of the LV, 
    #                 +/- add/subtracts to/from current size, g|G is GiB.

# Check filesystem of logical root volume for errors
e2fsck -f /dev/mapper/vgubuntu-root
    # `e2fsck`<fs-path> => Check a Linux ext2/ext3/ext4 file system
    #   -f => Force checking even if the file system seems clean.

# Increase swapsize
lvresize --verbose -L +40G /dev/mapper/vgubuntu-swap_1

After rebooting, Ubuntu should start normally, swap should be adjusted size:

lslbk
    # └─sda6                  8:6    0 464,6G  0 part  
    #   └─sda6_crypt        253:0    0 464,5G  0 crypt 
    #     ├─vgubuntu-root   253:1    0 423,6G  0 lvm   /
    #     └─vgubuntu-swap_1 253:2    0    41G  0 lvm   [SWAP]

If the system monitor still only has the initial 1G it is necessary to rewrite the logical swap device:

swapon --show
    # NAME      TYPE      SIZE USED PRIO
    # /dev/dm-2 partition 976M   0B   -2

swapoff -v /dev/dm-2
    # swapoff /dev/dm-2

mkswap /dev/dm-2
    # mkswap: /dev/dm-2: warning: wiping old swap signature.
    # Setting up swapspace version 1, size = 41 GiB (43973079040 bytes)
    # no label, UUID=...

swapon -a
    # Turn on swapspace
3
  • I tried to increase my swap size using this, however I am getting Cannot use device /dev/sda6 which is in use (already mapped or mounted). which I imagine is because I only have one device and I am already using it. What would be some possible solutions?
    – Stromwerk
    Mar 12, 2023 at 5:57
  • 1
    @Stromwerk 1. If you ask for advice on a failed command obviously we exactly need to know which command you are talking about. 2. If it is lvresize /dev/sda6 seems wrong since this answer is for an encrypted swap that should reside under /dev/mapper/. 3. In general resizing should be done from a Linux live disk, not from the system you want to change, then non /dev/sdaX should be mounted. 4. Ask yourself why sda6 is mounted. 5. Maybe search for how to unmount an sdaX device. Mar 12, 2023 at 13:43
  • The command is lvresize --verbose --resizefs -L -15G /dev/mapper/vgubuntu-root (I have it as -15G as I only want to change my swap size by that much). I understand now the solution is most likely to use a Linux live disk, which I can't easily obtain at the moment. Thank you
    – Stromwerk
    Mar 12, 2023 at 23:28
4
+50
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

7
  • Will this persist between boots? How do I calculate the sectors? I assume I need to print the current sector size and divide by total desired size?
    – xendi
    Jan 4, 2020 at 0:57
  • The manual says --size, -b <number of 512 byte sectors> so I tried cryptsetup resize cryptswap1 --size 7812500 which resulted in Device /dev/mapper/mint--vg-swap_1 is too small.
    – xendi
    Jan 4, 2020 at 6:13
  • Added attempt info to question.
    – xendi
    Jan 4, 2020 at 6:22
  • Using cyrptsetup resize will persist on boot as long as you increased the swap size, rather than decreasing it. (Yes, it's weird.)
    – JasonF4
    Jan 4, 2020 at 14:19
  • I updated the answer based on the feedback you added to the question.
    – JasonF4
    Jan 4, 2020 at 14:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .