2

I want my computer to automatically hibernate when the UPS switches to the battery, so I need enough free swap space at any moment. But if I activate the swap partition, even right before the hibernation, it can be occupied by memory pages.

The only solution I found is to set /proc/sys/vm/swappiness to 0 and disable paging completely. But it's not the best option.

Is it possible to specify maximum swap usage in bytes?

2 Answers 2

0
cat /proc/sys/vm/swappiness
0

Means that swapping will be avoided unless absolutely necessary (RAM is full). Swappiness takes values between 0-100.

To make the change permanent update /etc/sysctl.conf and reboot the machine:

vm.swappiness = 10

You can't configure swappiness in bytes, but swappines 10 means that swap will be used when RAM is 90% full. Size of the swap area is given during system installation, you'd need to resize partitions to change that. The amount occupied in swap depends on RAM allocation requirements of the software you're running.

0

The only solution I found is to use a swap file which you can create at any size, say a couple gigabytes, and then mount that as your swap area.

head -c 2G /dev/urandom > your.filename

You can modify the code below which will create an encrypted swap file using a random key. You will of course need to set the $filename variable to wherever your swap file is.

filename="????"
head -c 4k /dev/urandom | sha512sum | cryptsetup open --type plain $filename /dev/mapper/swap
cryptsetup status /dev/mapper/swap
mkswap /dev/mapper/swap
swapon /dev/mapper/swap
sysctl vm.swappiness=0

Edit: If you want the swap to be accessible after reboot, just replace the random key with a known key.

You must log in to answer this question.

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