11

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.
4
  • 1
    Can I confirm some numbers? Your system has 1GB of RAM and 900MB of swap? 🤔
    – matigo
    Jul 4, 2021 at 5:08
  • Woops, well spotted. My RAM is 16 GB, not 1 GB. Corrected above.
    – jdpipe
    Jul 4, 2021 at 5:09
  • 1
    FYI: If your root filesystem is encrypted then the swapfile would be as well.
    – Nmath
    Jul 4, 2021 at 6:53
  • 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.
    – benjaoming
    Jun 10, 2022 at 13:24

4 Answers 4

8

This method requires booting from a separate USB/CD

To shrink the root volume it has to be unmounted, to have the root volume unmounted you basically can not use the system.

This method suggests using KDE Partition Manager, but reducing the size of the main partition is not covered

KDE Partition Manager might be dangerous: LVM partition destroyed and Normal partition destroyed.

This method suggests removing existing space and creating a swapfile in /

Seems unnecessary.


TL;DR (backup important data!) - Decrease root and increase swap

This answer is based on Ubuntu 22.04 LTS

Copied my answer from Increase size of encrypted swap.

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

# Boot Ubuntu Live/Flash/"Try Ubuntu" AND open terminal
# Run subsequent commands as superuser
sudo su
    # `sudo`    => Execute a command as another user.
    # `sudo su [user]` => Run a command with 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=...
3
  • Thanks, this worked, right up to the last lsblk when I rebooted. However, when I run a vm in vmware that requires more swap, I get the same message as before. So I do, sudo swapon --show, and I see that same result as before I made changes, only 1.9G of swap (which is what vmware is reporting): sudo swapon --show /dev/dm-2 partition 1.9G 0B -2 lsblk ... vgubuntu-swap1 253:2 0 41.9G 0 lvm [SWAP]
    – B Phillips
    Aug 24, 2022 at 4:54
  • @BPhillips Are you sure changes to the vm system are persistent? Aug 24, 2022 at 13:00
  • @BPhillips I now faced the same problem on a debian device. In my case it was necessary to rewrite the logical swap device via mkswap. I suspect specific Ubuntu versions might do that automatically when booting. Jan 1, 2023 at 20:42
3

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.
2

Sadly I do not have enough points to post a comment to the ubuntu lvresize answer, so I will have to add this in a second answer.

After resizing the swap partition you still have to do the following, so that the added space is actually being used (at least I had to do so on Ubuntu 22-04):

sudo swapoff /dev/dm-2
sudo mkswap /dev/dm-2
sudo swapon /dev/dm-2
sudo swapon --show
1

Since it's not very safe to resize a mounted partition, consider booting into another partition and resizing your Ubuntu partitions from there.

If you have Windows on another partition:
Boot into windows and use an app like AOMEI or Minitool Partition Wizard (Not advertising. I've simply used them, that's all) etc. to resize the Ubuntu partitions.


If you don't have windows on your PC:

  1. Create a bootable USB stick
  2. Shut down the computer, and plug in the said USB stick if it's not already plugged in.
  3. Press the power button, at the same time press your machine's button for entering BIOS settings (usually it's F12 or F10 or F2)
  4. In BIOS settings, change the boot order so that the Bootable USB stick is first to boot.
  5. then follow the instructions on this answer

You must log in to answer this question.

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