43

I did a clean install of Ubuntu 18.04 Desktop.

I used the graphical installer and chose "Encrypt the new Ubuntu installation for security".

It used by default LVM and created a partition for the swap instead of the file. Here is sudo swapon -s result:

eviatan89@leviatan89-K55VD:~$ sudo swapon -s
Filename                Type        Size    Used    Priority
/dev/dm-2                               partition   1003516 999448  -2

I need to increase the size as I am having lots of problems running low on RAM.

As curiosity, problems come when using Cassandra and Firefox with several open tabs (including YouTube). My system got 6GB of RAM.

Thanks a lot for your help!

7
  • centos.org/docs/5/html/5.1/Deployment_Guide/… worked for me (18.04 full disk encryption)
    – olejorgenb
    Jul 24, 2018 at 13:34
  • 1
    I'm wandering if we can just make the partition bigger. LVM should make it easy, right? Has anyone tried it? Couldn't find any answer with this option. Jun 24, 2019 at 7:30
  • I tried resizing the partition using the disks app but the option wasn't available. Not even after I "stopped" it. I tried also to boot from a thumb drive, which doesn't mount anything on the hard drive, but the resize option wasn't available for non of the LVM partition. Might this be because the "main" partition is locked? After unlocking it, the option was still unavailable. Jun 24, 2019 at 9:03
  • @AlikElzin-kilaka: yes, you need to use kpvm to resize the swap volume, and indeed, it's very easy. Dec 4, 2019 at 2:45
  • @DanDascalescu Extend logical volume... is disabled for my swap_1 partition, so is Reduce logical volume... for root. What to do?
    – Bojan P.
    Dec 13, 2019 at 15:18

7 Answers 7

48

The easiest solution would be to add a swap file. If you are already encrypting your root file system, I would not bother with an encrypted swap file, which is only a little more difficult, but it is slower. The advantage of a swap file is that you can remove it later to regain the disk space. And the disk is already encrypted!

The steps are straightforward. First, make the file. For example, this would make 1GB of new swap:

sudo dd if=/dev/zero of=/swapfile bs=1024 count=1024k

The of=/swapfile tells dd to put the new swap file in /swapfile. You can call it anything you want. You can add multiple swap files, too. For recent Linux kernels, the speed is the same as a swap partition.

Then, you need to format the swap file as swap space, like so:

sudo mkswap /swapfile

This command will give you some output like:

Setting up swapspace version 1, size = 1048576 KiB

no label, UUID=83352590-ef57-49f5-84c4-7fb847e4e4e0

And that's your new swap file. Finally, you need to activate the swap on your machine using the following command:

sudo swapon /swapfile

Now, sudo swapon -s should show you both the swap partition and the swap file.

I then recommend adding some security by changing permissions as follows:

sudo chown root:root /swapfile
sudo chmod 0600 /swapfile

If all seems good so far, you can add the swap file permanently by adding the the following line to /etc/fstab using your favorite editor:

/swapfile       none    swap    sw      0       0

You can add multiple swap files, of course. And you can remove the swap file by using sudo swapoff /swapfile.

Hope this helps.

7
  • 4
    Thanks! It worked. This is the dd command I used for adding 4G swap file: sudo dd if=/dev/zero of=/swapfile bs=1024 count=4096k
    – leviatan89
    May 3, 2018 at 19:30
  • 5
    The block size parameter 'bs' is in bytes. So a count of 1024k or 1 million blocks of 1KB each is 1 GB.
    – Martin W
    Aug 17, 2018 at 14:17
  • 2
    Since the OP uses LVM, it would be even easier to simply resize the swap volume using kpvm. Dec 4, 2019 at 2:50
  • 1
    @DanDascalescu …easier unless OP prefers not to install KDE libs for this issue alone Dec 21, 2019 at 14:00
  • 1
    Be careful with the dd command. The count param is a multiplier of the block size (bs) so if you type bs=1G count=32G it will try to allocate 32,000 1GB blocks. Apr 12, 2021 at 13:35
8

Adding to the top answer. Since I do not have the reputation to comment. Apologies.

In case you are trying to increase swap space and already have swap space allocated.

Warning: Close applications that use swap space.

First, do this or else you will get a Error:

sudo swapoff -a

And then proceed as instructed above.

Also, the above process will erase the previous swap space, so if you have 2 Gigs of swap and want an additional 6 Gigs, you will have to allocate a fresh 9 Gigs of swap space. Or name the swap file to something different from the other swap file(s).

sudo dd if=/dev/zero of=/swapfile2 bs=1024 count=6144k

Error:

~ $sudo dd if=/dev/zero of=/swapfile bs=1024 count=6144k                     
dd: failed to open '/swapfile': Text file busy

NOTE: This is a suggested Extension to @Martin W's answer

1
4

Note that if your installation uses LVM, you may already have a swap volume. Check using kvpm. If that's not the case, here are concise steps to create a new 4GB swap file. First close any applications using swap space (or restart your machine). Then:

sudo swapoff -a                                    # Turn off all swap space.
sudo rm /swapfile                                  # Delete current swap file.
sudo dd if=/dev/zero of=/swapfile bs=1G count=4    # Make a new 4GB swap file.
sudo chown root:root /swapfile                     # Set owner to root, group root
sudo chmod 0600 /swapfile                          # Set permission to root
sudo mkswap /swapfile                              # Convert file to swap format
sudo swapon /swapfile                              # Enable swap space

Run htop or another system monitor to check that the new swap is in effect. If all looks good, make the swap file permanent by adding the following line to /etc/fstab:

/swapfile       none    swap    sw      0       0
3
  • If I need 12gb of swap do I need to just change count = 12288 right?
    – Jeeth
    Apr 29, 2019 at 15:57
  • 1
    Indeed, 4096*3=12288 Aug 26, 2019 at 9:02
  • With LVM, you don't need a swap file. LVM makes it easy to resize the swap volume using kpvm. Dec 4, 2019 at 2:51
1

It used by default LVM and created a partition for the swap instead of the file.

That is not actually a partition; rather, it's an LVM (Logical Volume Management) volume. You can see LVM volumes using kvpm:

kpvm screenshot with swap volume

Thus the correct answer to your question is not to create another swap destination (a swap file), but to resize the swap volume using kvpm: right click on the swap volume, and choose Extend logical volume.

If that option is not available, you may need to reduce the size of another volume first.

3
  • Will it work when using zfs on ubuntu 19.10? Dec 9, 2019 at 0:54
  • 3
    Extend logical volume... is disabled for my swap_1 partition, so is Reduce logical volume... for root. What to do?
    – Bojan P.
    Dec 13, 2019 at 15:17
  • Can kvpm reduce the size of other volumes?
    – mwfearnley
    Jan 25, 2021 at 17:36
0

This answer is based on Ubuntu 22.04 LTS

Decrease root and increase swap (backup important data!)

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]
-1

If you are running mint 19.X, here are some concise steps:

sudo dd if=/dev/zero of=/swapfile bs=1024 count=10240k # make 10G swapfile
sudo chown root:root /swapfile
sudo chmod 0600 /swapfile
sudo mkswap /swapfile

edit /etc/fstab

comment out line: /dev/mapper/mint--vg-swap_1 none swap sw 0 0

add line: /swapfile none swap sw 0 0

Then reboot.

-1

GUI approach On ubuntu

  1. Open Disks. Then go the the drive you want to add swap on.
  2. Click on add partition, then choose the number of GB you need
  3. Click on next, then as on type choose other, then next.
  4. Choose Linux swap partition and proceed On completion, mount the drive as swap. By clicking on the mount button

Checkout the image

0

You must log in to answer this question.

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