Swap

From Hackepedia
Jump to navigationJump to search

Swap is a secondary storage for memory that is swapped or paged out, usually residing on your hard disk. Hard drives are a lot slower to read and write to than RAM, so ideally you will want to increase your RAM if financially possible if you see that your swap is being used, assuming speed as priority.

It is also used for saving the kernel corefile when the system panics. Some systems compress their corefile to swap and others don't (such as BSD), this is why it's a good idea to have 1.5 to 2 times as much swap as physical memory in a machine (as a rule of thumb), so that you can get a good corefile.

Don't forget to ensure there is always enough room in your swap if your RAM is being used to its maximum capacity:

# swapinfo
Device          1K-blocks     Used    Avail Capacity
/dev/ad0s1b        105288    68596    36692    65%

otherwise you'll start getting errors to syslog, running applications will be killed, and it will create a general mess of things:

Oct 27 09:51:38 mail swap_pager_getswapspace(9): failed
Oct 27 09:51:38 mail swap_pager_getswapspace(5): failed
Oct 27 09:51:38 mail swap_pager_getswapspace(16): failed
Oct 27 09:51:40 mail pid 95400 (perl5.8.7), uid 125, was killed: out of swap space

If you are running out of swapspace frequently, or just want to increase the capacity, you can add a swapfile very easily on FreeBSD.



Encrypting swap in FreeBSD has existed since version 5.3



Encrypting your swap in GNU/Linux

This was done on Ubuntu feisty fawn. If you're doing this on your live system, first deactivate your current swap:

# swapoff -a

I added this to /etc/crypttab:

swap /dev/sda2  /dev/urandom  cipher=aes-cbc-essiv:sha256,hash=sha256,size=256,swap

and then added the following to /etc/fstab:

/dev/mapper/swap       none    swap     0    0

finally, I ran:

$ sudo  /etc/init.d/cryptdisks start
$ swapon -a

to see if it's working:

$ swapon -s 

or

$ cat /proc/swaps


Encrypting swap in MacOSX

System Preferences -> Security pane and choose "Use secure virtual memory"


Creating a swap file on Ubuntu 10.10

This creates a 2G file called /2gswap. Change the name, location and size as you wish:

$ sudo dd if=/dev/zero of=/2gswap bs=1M count=2048
[sudo] password for franks: 
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB) copied, 10.7058 s, 201 MB/s
$ sudo mkswap /2gswap 
mkswap: /2gswap: warning: don't erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=1a9544ea-f994-4950-bald-5d321c5455c9

Now let's see the current swap:

$ swapon -s

Let's use this badboy now:

$ sudo swapon /2gswap

You can now $ cat /proc/meminfo or:

$ swapon -s

and you should see your file. If you want to make this permanent (for it to take place after a reboot) add the following to /etc/fstab:

/2gswap none  swap  sw  0 0

Done! Check out your memory situation, including your new swap:

$ free