This site is now 100% read-only, and retired.

Enabling swap on debian based system

Posted by tuwid on Tue 8 Dec 2015 at 18:01

Tags:

Recently I found myself without a swap partition (having installed another OS and reworked my disk-setup) so I wanted to share a quick guide to enable swap again.

As a recap "swap" space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM.

You can check to see if you have swap partitions defined by consulting /etc/fstab: $ grep swap /etc/fstab UUID=14a0f7b9-dabb-4296-b0e7-013527a7d82d none swap sw 0 0

If one is defined you can check that it is being used by the system:

$ swapon -s
Filename Type Size Used Priority
/dev/sda6 partition 1004020 215532 -1

If one is defined but not in use the obvious thing to do is check that it is formatted as a swap partition:

$ sudo fdisk -l /dev/sda
[..snipped..]
/dev/sda5 3842 6595 22121473+ 83 Linux
/dev/sda6 6596 6720 1004031 82 Linux swap / Solaris

If it is a swap partition, ready it for use and turn it on. At this point, you might see any errors that prevented its use.

su -
mkswap /dev/sda6
swapon /dev/sda6

You can now check that the partition is being used using the "swapon -s" command we saw above.

Add an entry to /etc/fstab to have this swap partition loaded at bootup. You can replace the "UUID=xxx" part from above with "/dev/sda6"; so the entry would look like this:

/dev/sda6 none swap sw 0 0

If you didn't define a swap partition earlier, then you'd have to create one, or point swap to a regular file (which is a little less efficient, but probably sufficient).

 

 


Re: Enabling swap on debian based system
Posted by Steve (217.155.xx.xx) on Tue 8 Dec 2015 at 18:05
[ View Weblogs ]

For what it is worth I tend to find using a static partition for swap is wasteful - so I tend to configure my systems with LVM, and allocate a swap partition in that.

If I'm not using LVM then a simple file is sufficient, since a system actually using swap is probably already broken. This is how I'd set it up:

$ su -
# dd if=/dev/zero of=/swap.file bs=1024k count=1
# mkswap -f /swap.file
# echo "/swap.file none swap sw 0 0 " >> /etc/fstab
# swapon -a

--
Steve

[ Parent ]

Re: Enabling swap on debian based system
Posted by Anonymous (72.22.xx.xx) on Wed 9 Dec 2015 at 18:14
On many systems, I literally have more than enough RAM. Isn't that a nice state of being?

But just in case of... something, I might want swapspace some day.

Usually that means a 1GB swap file, as in Steve's example, but sometimes I want system uptime more than I want performance:

# apt-get install swapspace

will install a daemon that will dynamically create, mount, and remove swapspace.

[ Parent ]

Re: Enabling swap on debian based system
Posted by ntropia (137.131.xx.xx) on Wed 9 Dec 2015 at 19:36

I didn't know about this daemon, and in principle it sounds nice.

Although, the size-variable file makes feels like we're getting back to Windows and its disk-fragmented nightmare. I know that any decent filesystem available on Linux tend to have negligible fragmentation, but isn't the whole point of a partition to keep the swap separated by the other filesystems?

@Steve: kinda of a silly question, but what's the advantage of a swap partition in LVM?

[ Parent ]

Re: Enabling swap on debian based system
Posted by jchris (91.55.xx.xx) on Sat 23 Apr 2016 at 09:23
A system actually using swap is already broken? I beg to differ, because swap can be useful: swap is a safety belt on servers which might e.g. be 'slashdotted' for some reason, and, last but not least, you need swap to hibernate your system. And I prefer hibernation over "suspend to ram" because it tends to survive power outages (e.g. low battery on laptops) much better.

[ Parent ]

Re: Enabling swap on debian based system
Posted by Anonymous (88.111.xx.xx) on Sat 14 Jan 2017 at 15:52
Much quicker to use fallocate:
$ fallocate 2G

[ Parent ]

Re: Enabling swap on debian based system
Posted by naoliv (200.145.xx.xx) on Thu 10 Dec 2015 at 12:53
[ View Weblogs ]

[ Parent ]

Re: Enabling swap on debian based system
Posted by Anonymous (173.3.xx.xx) on Tue 29 Dec 2015 at 02:14
Thanks for the article. I found it useful. One small suggestion.

The manual of swapon says -s is deprecated in favour of --show as the latter provides better control on output data. It might be a good idea update the article to swapon --show?

[ Parent ]

Re: Enabling swap on debian based system
Posted by Anonymous (90.208.xx.xx) on Thu 11 May 2017 at 12:59
Swapping is quite an old technique to increase the amount of available system memory. However, what caught my attention recently was the Linux kernel's ability to compress swap pages on-the-fly with lz4.

While working with gcc 4.6 and the early implementation of Link-Time Optimization (LTO) could the compiler occasionally consume huge amounts of memory, sometimes more than 32GB. This made it necessary for me to look closer at swapping again.

For unknown reasons had my Debian installer decided two prioritise my two swap partitions differently. I had to edit the fstab and assign both an equal priority to get the two partitions, each on a separate disk, to work like a 0-RAID swap device. Without it would the kernel have used one partition more often than the other. This only made a minor improvement, but it's still worth mentioning.

Here is how one sets the priorities of swap devices in the fstab:

/dev/sda1 none swap pri=0 0 0
/dev/sdb1 none swap pri=0 0 0

The solution with two disks however didn't provide enough throughput for the huge memory consumption and so I've enabled swap space compression in the kernel to see if this was of any benefit.

The impact was tremendous.

Not only did the compression reduce the amount of data that's being transferred to and from the disks, but because of it did it also reduce the time for the transfer. The swap speed increased by 2-3 times. The lz4 compressor isn't the most efficient compressor in terms of compression ratio, but it is by far the fastest available, and the time spend by the CPU to compress and decompress the data is negligible.

To enable compression does it need to be configured into the kernel if it already isn't. Then all it needs is to activate it during boot on the kernel command line. The best place for this is /etc/default/grub and add it there:

GRUB_CMDLINE_LINUX_DEFAULT="quiet zswap.enabled=1 zswap.compressor=lz4 zswap.zpool=z3fold"

It will enable swap space compression with lz4 as the compressor and use the "z3fold" pool, meaning, it can store up to 3 compressed memory pages where it would otherwise have stored a single, uncompressed memory page.

It still won't replace real RAM, but it makes memory swapping much more viable than it used to be.

[ Parent ]

Re: Enabling swap on debian based system
Posted by Anonymous (73.44.xx.xx) on Sun 14 May 2017 at 14:49
I might give this a try today - it sounds really nifty. Thanks for the tip!

[ Parent ]

Re: Enabling swap on debian based system
Posted by ajt (79.77.xx.xx) on Wed 7 Jun 2017 at 18:58
[ View Weblogs ]
<p>Interesting, took a while to get the lz4 bit working - I needed to add it to the kernel module list, but I wasn't able to get z3fold to work at all, it's happy to use zbud though.</p> <p>-- <br /> "It's Not Magic, It's Work"<br /> <a href="http://www.iredale.net/" rel="nofollow">Adam</a></p>

[ Parent ]