More SWAP on LUKS?

linuxorgy

New Member
Joined
Apr 15, 2024
Messages
9
Reaction score
1
Credits
83
how to make a swap bigger on luks?
by default it is only 2 GB
I want 16GB swap


cat /etc/crypttab
nvme0n1p3_crypt UUID=5453a2d7-a805-4c46-8830-1b40b4e0820b none luks,discard
cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
/dev/mapper/vgkubuntu-root / ext4 errors=remount-ro 0 1
# /boot was on /dev/nvme0n1p2 during installation
UUID=6e64ffd7-9479-49aa-924e-189f61ea1158 /boot ext4 defaults 0 2
# /boot/efi was on /dev/nvme0n1p1 during installation
UUID=E1D2-69BA /boot/efi vfat umask=0077 0 1
/dev/mapper/vgkubuntu-swap_1 none swap sw 0 0

lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 4K 1 loop /snap/bare/5
loop1 7:1 0 373.8M 1 loop /snap/anbox/186
loop2 7:2 0 105.4M 1 loop /snap/core/16574
loop3 7:3 0 104M 1 loop /snap/core/16928
loop4 7:4 0 63.9M 1 loop /snap/core20/2182
loop5 7:5 0 63.9M 1 loop /snap/core20/2264
loop6 7:6 0 74.1M 1 loop /snap/core22/1033
loop7 7:7 0 74.2M 1 loop /snap/core22/1122
loop8 7:8 0 267.5M 1 loop /snap/firefox/3941
loop9 7:9 0 268.3M 1 loop /snap/firefox/4090
loop10 7:10 0 349.7M 1 loop /snap/gnome-3-38-2004/140
loop11 7:11 0 349.7M 1 loop /snap/gnome-3-38-2004/143
loop12 7:12 0 497M 1 loop /snap/gnome-42-2204/141
loop13 7:13 0 504.2M 1 loop /snap/gnome-42-2204/172
loop14 7:14 0 91.7M 1 loop /snap/gtk-common-themes/1535
loop15 7:15 0 103.2M 1 loop /snap/pac-vs/1
loop16 7:16 0 147M 1 loop /snap/shotcut/1389
loop17 7:17 0 40.4M 1 loop /snap/snapd/20671
loop18 7:18 0 39.1M 1 loop /snap/snapd/21184
loop19 7:19 0 417.9M 1 loop /snap/telegram-desktop/5783
loop20 7:20 0 417.9M 1 loop /snap/telegram-desktop/5791
loop21 7:21 0 310.8M 1 loop
nvme0n1 259:0 0 953.9G 0 disk
├─nvme0n1p1 259:1 0 512M 0 part /boot/efi
├─nvme0n1p2 259:2 0 1.7G 0 part /boot
└─nvme0n1p3 259:3 0 951.7G 0 part
└─nvme0n1p3_crypt 252:0 0 951.7G 0 crypt
├─vgkubuntu-root 252:1 0 929.4G 0 lvm /var/snap/firefox/common/host-hunspell
│ /
└─vgkubuntu-swap_1 252:2 0 1.9G 0 lvm [SWAP]


Filesystem Size Used Avail Use% Mounted on
tmpfs 1.6G 2.6M 1.6G 1% /run
/dev/mapper/vgkubuntu-root 914G 325G 543G 38% /
tmpfs 7.7G 298M 7.4G 4% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
efivarfs 246K 108K 134K 45% /sys/firmware/efi/efivars
/dev/nvme0n1p2 1.7G 301M 1.3G 20% /boot
/dev/nvme0n1p1 511M 6.1M 505M 2% /boot/efi
tmpfs 1.6G 108K 1.6G 1% /run/user/1000
 


You can skip all that by using a swapfile:

Code:
sudo dd if=/dev/zero of=/swapfile bs=1G count=1
sudo chmod 600 /swapfile
sudo mkswap /swapfile

echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab

sudo swapon -a


verify with:


Code:
free -m


The alternative is to shrink your luks partition, that is a bit of a hassle - source ChatGPT, I read it and I'm 99% sure this is correct, I didn't replicate it:


Step 1: Backup Your Data
Before you proceed, it's crucial to back up all data stored in the LUKS encrypted partition. Shrinking partitions, especially encrypted ones, can lead to data loss if any issues occur during the process.

Step 2: Decrypt and Open the LUKS Container
First, ensure that the LUKS container is decrypted and opened so you can access the underlying filesystem.

Code:
sudo cryptsetup luksOpen /dev/sdXn my_encrypted_volume

Replace /dev/sdXn with the device name of your LUKS partition (e.g., /dev/sda1), and my_encrypted_volume with a name you choose for the decrypted volume.

Step 3: Check and Repair the File System (if necessary)
Before resizing, it's a good idea to check the file system for errors.

For an ext4 file system:

Code:
sudo e2fsck -f /dev/mapper/my_encrypted_volume


Step 4: Resize the File System

You must first resize the file system contained within the LUKS container. This should be done to make the file system smaller than the target size of the LUKS container.

For an ext4 file system:

Code:
sudo resize2fs /dev/mapper/my_encrypted_volume <new-size>

Replace <new-size> with the desired size of the filesystem (e.g., 30G). This size should be smaller than the new size you plan for the LUKS partition.

Step 5: Close the LUKS Container

Once the file system has been resized, you can close the LUKS container:

Code:
sudo cryptsetup luksClose my_encrypted_volume


Step 6: Resize the LUKS Partition

Step 6.1: Note Current Partition Details

Before making changes, it's crucial to note down the current start sector of the partition you intend to resize. You can get detailed information about all partitions on the disk by using:

Code:
sudo fdisk -l /dev/sdx

Replace /dev/sdx with the appropriate device identifier for your disk.

Step 6.2: Run fdisk to Modify the Partition Table

Start fdisk on the relevant disk:

Code:
sudo fdisk /dev/sdx

Step 6.4: Delete the Existing Partition

First, you will delete the partition. Note that this does not physically erase the data, but you should have a backup if the data is important. (sidenote from me: it only removes the reference to "where the partition is located on the disk but does not overwrite any data")

In fdisk, type d to delete a partition. Then enter the number of the partition you want to delete.


Step 6.5: Recreate the Partition with a Smaller Size

After deleting the partition:

Type n to create a new partition.
Choose p for primary or e for extended, depending on your needs.
Enter the partition number, which can be the same as the one you just deleted.
Enter the original starting sector to preserve the data location (from Step 2).
Specify the new, smaller ending sector or size (e.g., +500M for 500 MB).


Step 6.6: Write Changes and Exit

After creating the new, smaller partition:

Type w to write the changes to the disk.
Exit fdisk.

Step 6.7: Verify the New Partition Setup
Check that the partition has been resized correctly and that the changes look accurate with:

Code:
sudo fdisk -l /dev/sdx



Step 7: Open the LUKS Container Again

After adjusting the partition size, open the LUKS container:

Code:
sudo cryptsetup luksOpen /dev/sdXn my_encrypted_volume

Step 8: Resize the LUKS Volume

Finally, resize the LUKS volume to use any remaining space in the new partition, if necessary:

Code:
sudo cryptsetup resize my_encrypted_volume


Step 9: Verify the File System
Check and, if necessary, resize the file system within to occupy any newly available space. For ext4:

Code:
sudo resize2fs /dev/mapper/my_encrypted_volume


Conclusion

As you can see, shrinking a LUKS encrypted partition involves several steps and is not as straightforward as resizing a non-encrypted partition. It requires careful handling of the filesystem and partition to avoid any loss of data. Always ensure you have reliable backups before proceeding with such operations.
 
By the way - swap is inherently slow and if you are already using a lot of swap, your system might not get faster by creating more swap. Unless you are sure you know what you are doing, I'd recommend to upgrade your RAM.
 
Resizing an Existing Swap File:

Since you already have a swap file but want to enlarge it, follow these steps:
First, disable the existing swap with:

sudo swapoff -a

Then resize the swap file (to 16 GB):

sudo dd if=/dev/zero of=/swapfile bs=1M count=16384
sudo mkswap /swapfile
sudo swapon /swapfile
Finally, update /etc/fstab to reflect the changes:

sudo sed -i '$ a\\/swapfile\\tnone\\tswap\\tsw\\t0\\t0' /etc/fstab
Personally I do not use a swap partition - I use a swap file if I use anything at all - I would do as @blunix has stated - use a swap file instead of swap partition

Creating a Swap File:
The easiest approach on an already running system is to create a swap file. For example, to create a 16 GB swap file, execute the following commands:

sudo dd if=/dev/zero of=/swapfile.sys bs=1M count=16384
sudo chmod 600 /swapfile.sys
sudo mkswap /swapfile.sys
sudo swapon /swapfile.sys
sudo sed -i '$ a\\/swapfile.sys\\tnone\\tswap\\tsw\\t0\\t0' /etc/fstab

The last line ensures that the swap file is automatically loaded after reboot
 
Welcome to the Forum.
m0135.gif


I would just add more Ram.
m1212.gif
 
Resizing an Existing Swap File:

Since you already have a swap file but want to enlarge it, follow these steps:
First, disable the existing swap with:



Then resize the swap file (to 16 GB):


Finally, update /etc/fstab to reflect the changes:


Personally I do not use a swap partition - I use a swap file if I use anything at all - I would do as @blunix has stated - use a swap file instead of swap partition

Creating a Swap File:
The easiest approach on an already running system is to create a swap file. For example, to create a 16 GB swap file, execute the following commands:



The last line ensures that the swap file is automatically loaded after reboot
did not work on luks, got mail
 
@linuxorgy
The procedure to increase swap file on a LUKS encrypted LVM drive is lengthy and not straightforward, many things can go very wrong.
You'll need to do this from live USB

Below is my gist which provides hints only that i used myself, it's not a complete tutorial so be careful and make sure you understand what it does.

See "reference" section at the end for user firendly tutorials.
 
Last edited:
Well you can totally do this - I just think if you ask in a forum about it, you should'nt ;)

I did this once or twice and I'm 99% sure what I copied from chatgpt above works. But as said, I wouldn't do it, I would just add a swap file.
Or download more RAM.
 
sudo swapon -s
sudo swapoff /dev/dm-2
sudo fallocate -l 16g /swapfile
chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

nano /etc/fstab
/swapfile none swap sw 0 0

solved
 
sudo swapoff /dev/dm-2

You could have kept this, doesn't hurt to have more than one swap.

sudo fallocate -l 16g /swapfile

The dd version described above might have given better results initially, but this is generally ok too.

Out of curiosity - is your box performing better now?
 
sudo swapon -s
sudo swapoff /dev/dm-2
sudo fallocate -l 16g /swapfile
chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

nano /etc/fstab
/swapfile none swap sw 0 0

solved
You did not deal with encrypted LVM right? because that looks too easy.
 
no lvm, just this, after reboot all good, now I can open countless chromes, I am happy
Cool feeling ha?
What I like most about large swap is the ability to hibernate system, with low swap size like 2GB there is no way to hibernate.
I hope you accounted to for that too? how large is your swap partition now and how much RAM do you have?
 
ram always stays at ~13 GB of 16 GB, and rest is dumped to nvme swap file.
me happy puppy long time now :D
before that my kubuntu would freeze to death if I open 10 or even less chrome windows

free -h
total used free shared buff/cache available
Mem: 15Gi 4.1Gi 8.1Gi 819Mi 3.1Gi 10Gi
Swap: 17Gi 0B 17Gi



opened up 15 heavy pages in chrome + vmware 8 GB ram image + terminals

free -h
total used free shared buff/cache available
Mem: 15Gi 3.3Gi 192Mi 8.2Gi 11Gi 3.5Gi
Swap: 17Gi 6.6Gi 11Gi


flying magic!

god bless NVMe

never thought about hiber though, might test someday, I usually shutdown laptop when travelling, to be safe



closeed some chromes and vmware

free -h
total used free shared buff/cache available
Mem: 15Gi 2.3Gi 9.3Gi 601Mi 3.7Gi 12Gi
Swap: 17Gi 3.7Gi 14Gi


so I guess this is a sum of default 2GB + 16 GB swaps

Code:
free -h
               total        used        free      shared  buff/cache   available
Mem:            15Gi       1.9Gi       9.7Gi       618Mi       3.7Gi        12Gi
Swap:           17Gi       3.5Gi        14Gi


lsblk
NAME                   MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINTS
loop0                    7:0    0     4K  1 loop  /snap/bare/5
loop1                    7:1    0 373.8M  1 loop  /snap/anbox/186
loop2                    7:2    0 105.4M  1 loop  /snap/core/16574
loop3                    7:3    0   104M  1 loop  /snap/core/16928
loop4                    7:4    0  63.9M  1 loop  /snap/core20/2182
loop5                    7:5    0  63.9M  1 loop  /snap/core20/2264
loop6                    7:6    0  74.1M  1 loop  /snap/core22/1033
loop7                    7:7    0  74.2M  1 loop  /snap/core22/1122
loop8                    7:8    0 267.5M  1 loop  /snap/firefox/3941
loop9                    7:9    0 268.3M  1 loop  /snap/firefox/4090
loop10                   7:10   0 349.7M  1 loop  /snap/gnome-3-38-2004/140
loop11                   7:11   0 349.7M  1 loop  /snap/gnome-3-38-2004/143
loop12                   7:12   0 504.2M  1 loop  /snap/gnome-42-2204/172
loop13                   7:13   0 505.1M  1 loop  /snap/gnome-42-2204/176
loop14                   7:14   0  91.7M  1 loop  /snap/gtk-common-themes/1535
loop15                   7:15   0 103.2M  1 loop  /snap/pac-vs/1
loop16                   7:16   0   147M  1 loop  /snap/shotcut/1389
loop17                   7:17   0  39.1M  1 loop  /snap/snapd/21184
loop18                   7:18   0  38.7M  1 loop  /snap/snapd/21465
loop19                   7:19   0 417.9M  1 loop  /snap/telegram-desktop/5791
loop20                   7:20   0 419.8M  1 loop  /snap/telegram-desktop/5820
loop21                   7:21   0 310.8M  1 loop
nvme0n1                259:0    0 953.9G  0 disk
├─nvme0n1p1            259:1    0   512M  0 part  /boot/efi
├─nvme0n1p2            259:2    0   1.7G  0 part  /boot
└─nvme0n1p3            259:3    0 951.7G  0 part
  └─nvme0n1p3_crypt    252:0    0 951.7G  0 crypt
    ├─vgkubuntu-root   252:1    0 929.4G  0 lvm   /var/snap/firefox/common/host-hunspell
    │                                             /
    └─vgkubuntu-swap_1 252:2    0   1.9G  0 lvm   [SWAP]




df -h
Filesystem                  Size  Used Avail Use% Mounted on
tmpfs                       1.6G  2.8M  1.6G   1% /run
/dev/mapper/vgkubuntu-root  914G  342G  527G  40% /
tmpfs                       7.7G  159M  7.5G   3% /dev/shm
tmpfs                       5.0M  4.0K  5.0M   1% /run/lock
efivarfs                    246K  109K  133K  46% /sys/firmware/efi/efivars
/dev/nvme0n1p2              1.7G  301M  1.3G  20% /boot
/dev/nvme0n1p1              511M  6.1M  505M   2% /boot/efi
tmpfs                       1.6G   84K  1.6G   1% /run/user/1000


fdisk -l
Disk /dev/loop0: 4 KiB, 4096 bytes, 8 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


Disk /dev/loop1: 373.78 MiB, 391933952 bytes, 765496 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


Disk /dev/loop2: 105.41 MiB, 110526464 bytes, 215872 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


Disk /dev/loop3: 103.99 MiB, 109043712 bytes, 212976 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


Disk /dev/loop4: 63.91 MiB, 67010560 bytes, 130880 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


Disk /dev/loop5: 63.95 MiB, 67051520 bytes, 130960 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


Disk /dev/loop6: 74.11 MiB, 77713408 bytes, 151784 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


Disk /dev/loop7: 74.21 MiB, 77819904 bytes, 151992 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


Disk /dev/nvme0n1: 953.87 GiB, 1024209543168 bytes, 2000409264 sectors
Disk model: SAMSUNG MZVL21T0HCLR-00B00             
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 85232725-F4F2-4DDA-98A8-1CFC262BFA01

Device           Start        End    Sectors   Size Type
/dev/nvme0n1p1    2048    1050623    1048576   512M EFI System
/dev/nvme0n1p2 1050624    4550655    3500032   1.7G Linux filesystem
/dev/nvme0n1p3 4550656 2000408575 1995857920 951.7G Linux filesystem


Disk /dev/mapper/nvme0n1p3_crypt: 951.68 GiB, 1021862477824 bytes, 1995825152 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


Disk /dev/mapper/vgkubuntu-root: 929.41 GiB, 997950750720 bytes, 1949122560 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


Disk /dev/mapper/vgkubuntu-swap_1: 1.91 GiB, 2046820352 bytes, 3997696 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


Disk /dev/loop8: 267.46 MiB, 280453120 bytes, 547760 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


Disk /dev/loop9: 268.25 MiB, 281284608 bytes, 549384 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


Disk /dev/loop10: 349.69 MiB, 366678016 bytes, 716168 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


Disk /dev/loop11: 349.7 MiB, 366682112 bytes, 716176 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


Disk /dev/loop12: 504.15 MiB, 528642048 bytes, 1032504 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


Disk /dev/loop13: 505.09 MiB, 529625088 bytes, 1034424 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


Disk /dev/loop14: 91.69 MiB, 96141312 bytes, 187776 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


Disk /dev/loop15: 103.23 MiB, 108244992 bytes, 211416 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


Disk /dev/loop16: 147.04 MiB, 154181632 bytes, 301136 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


Disk /dev/loop17: 39.1 MiB, 40996864 bytes, 80072 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


Disk /dev/loop18: 38.73 MiB, 40615936 bytes, 79328 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


Disk /dev/loop19: 417.91 MiB, 438210560 bytes, 855880 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


Disk /dev/loop20: 419.77 MiB, 440164352 bytes, 859696 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


Disk /dev/loop21: 310.8 MiB, 325902336 bytes, 636528 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
 
Last edited:
@linuxorgy
16GB RAM requires 24GB swap file for hibernate function to work without any issues, especially if you consume a lot of RAM and want to hibernate all of it.
But if that's not your concern that's fine, 17GB swap is good too but keep in mind you'll be able to hibernate when used RAM is no more that 14GB or so.
 
interesting.... will remember, just set swap to 32GB and forget

what are these loops doing there?
Kubuntu 22.04
 
I think there is a way to use your gpu's memory for swap somehow. might be worth a google search.
 


Latest posts

Top