Score:5

Resize swap partition on Ubuntu 20.04 using encrypted LVM

in flag

I have a pretty standard Ubuntu 20.04 setup, with LVM and encrypted hard drive. However, the standard setup seems to have provided a very small 900 MB swap (or did I choose that?), and it now frequently causes me problems with running out of memory, despite having 16 GB of RAM. I have plenty of available space on my 500 GB SSD, but have not been able to find clear instruction for how I can increase my swap size. I want to retain the encrypted swap as a partition, and not replace it with a file.

I looked at a few options, but didn't see anything that looked to be as straightforward as I hoped:

  • This method requires booting from a separate USB/CD -- can that be avoided? (and won't it require some kind of passphrase -- how to extract that first?)
  • This method suggests using KDE Partition Manager, but reducing the size of the main partition is not covered, and doesn't seem straightforward.
  • This method suggests removing existing space and creating a swapfile in /. I am not clear if adopting that method gives me an encrypted swap or not, and prefer to stick with the same LVM architecture provided by the Ubuntu installer, if possible.
  • Finally, I'm unsure about this one, because fdisk -l doesn't show me a /dev/sda, but instead /dev/nvme0n1, so I am not sure that the instructions are applicable to my case.
in flag
Can I confirm some numbers? Your system has 1GB of RAM and 900MB of swap?
in flag
Woops, well spotted. My RAM is 16 GB, not 1 GB. Corrected above.
Nmath avatar
ng flag
FYI: If your root filesystem is encrypted then the swapfile would be as well.
benjaoming avatar
fr flag
Did you find out why only 1GB of swap was allocated? I just did the same setup on 22.04 and also ended up with 1 GB. My system's applications repeatedly crash because of this hard memory constraint.
Score:2
mx flag

I had the same problem with Kubuntu 20.04. (I actually suspect that my system would have been more stable with no swap at all.)

Here's some info to get you started:

  1. the Kubuntu installer (based on your post and linked articles, I think the Ubuntu installer is very similar) creates an encrypted LVM volume on one of the disk partitions. Within this volume there are 2 volumes -- effectively partitions. For me, the encrypted volume is /dev/nvme0n1p3, but the name will depend on your system. The 2 partitions are:
  • /dev/vgkubuntu/root
  • /dev/vgkubuntu/swap_1
  1. you cannot resize a mounted partition. In this case, once you've booted the machine, decrypted the disk and logged in, the root disk cannot be safely unmounted. Hence the need to use the live USB or another machine.
  2. there's no real benefit of having a swap partition over a swapfile in this case -- both will be inside the encrypted partition. A swapfile can be more convenient because you can easily resize it later while logged in normally. However, it's a bit more work to set up.

Your 2 options are:

  1. resize the swap partition
  2. make a swapfile and (optionally) reclaim the swap partition (by removing it and extending the root partition)

In either case you'll have to use a live USB or move the physical disk to another machine, because you can't resize a mounted partition and you can't see the partitions within the encrypted volume without first decrypting it.

I used the live USB that I'd just used to install Kubuntu 20.04 and I chose to resize the existing partition because I'm lazy.

  1. start the live USB -- put it in the machine, start it up, mash all the keys that will get you to a way to choose the boot device, etc.
  2. wait for Kubuntu to load -- choose Try Kubuntu
  3. open the terminal emulator, sudo su to become root
  4. run lsblk to find the encrypted device name. It's probably the last and almost certainly the biggest in the list.
  5. decrypt it: cryptsetup open /dev/<device> <some new device name> e.g. cryptsetup open /dev/nvme0n1p3 cr-ext -- you will be prompted for the password. This is the same password you use to decrypt the disk during normal startup.
  6. now you can run KDE Partition Manager. You should see vgkubuntu in the devices list -- click to highlight. You should see the root and swap partitions in the partition list.
  7. Resize/Move the root partition to shrink it by however much you want to grow the swap partition. (I chose +32 GB because I'll likely have that much RAM at some point, the extra 900MB or whatever is good padding because you never know exactly how many bytes you'll end up when adding RAM, and you need swap > physical to sleep, and I'm lazy.) It wouldn't let me enter exactly the MB I wanted (current size - (32*1024)) b/c, I dunno, block sizes or something, so I put in something close and clicked the up arrow till it stopped me.
  8. Increase the swap partition to fill the space.
  9. Click Apply. Once it succeeds, you can reboot, remove the USB drive and boot normally.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.