Swap

From ArchWiki
Revision as of 13:39, 21 September 2011 by NTia89 (Talk | contribs) (add swap over usb section)

Jump to: navigation, search

Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary wiki Template:Article summary wiki Template:Article summary end

From All about Linux swap space:

Linux divides its physical RAM (random access memory) into chucks of memory called pages. Swapping is the process whereby a page of memory is copied to the preconfigured space on the hard disk, called swap space, to free up that page of memory. The combined sizes of the physical memory and the swap space is the amount of virtual memory available.

Swap space will usually be a disk partition but can also be a file. Users may create a swap space during installation of Arch Linux or at any later time should it become necessary. Swap space is generally recommended for users with less than 1 GB of RAM, but becomes more a matter of personal preference on systems with gratuitous amounts of physical RAM (though it is required for suspend-to-disk support).

Swap partition

A swap partition can be created with most GNU/Linux partitioning tools (e.g. Template:Codeline, Template:Codeline). Swap partitions are designated as type 82.

To set up a Linux swap area, the Template:Codeline command is used. For example:

# mkswap /dev/sda2
Warning: All data on the specified partition will be lost.

To enable the device for paging:

# swapon /dev/sda2

To enable this swap partition on boot, add an entry to fstab:

/dev/sda2 swap swap defaults 0 0

Swap file

As an alternative to creating an entire partition, a swap file offers the ability to vary its size on-the-fly, and is more easily removed altogether. This may be especially desirable if disk space is at a premium (e.g. a modestly-sized SSD).

Swap file creation

As root, use Template:Codeline to create a file the size of your choosing. For example, creating a 512 MB swap file:

# dd if=/dev/zero of=/swapfile bs=1M count=512

Set the right permissions (a world-readable swap file is a huge local vulnerability)

# chmod 600 /swapfile

After creating the correctly-sized file, format it to swap:

# mkswap /swapfile

Activate the swapfile:

# swapon /swapfile

Edit Template:Filename and add an entry for the swap file:

/swapfile swap swap defaults 0 0

Ensure proper swap file size and activation with Template:Codeline:

$ free -m

Swap file removal

To remove a swap file, the current swap file must be turned off.

As root:

# swapoff -a

Remove swapfile:

# rm -rf /swapfile

Resume from Swap file

Resuming the system from a swap file after hibernation requires an addition parameter to the kernel compared to resuming from a swap partition. The additional parameter is resume_offset=<Swap File Offset>.

The value of <Swap FIle Offset> can be obtained from the output of filefrag -v; The output is in a table format; the required value is located in the 'physical' column from the first row. Eg:

# filefrag -v /swapfile
Filesystem type is: ef53
File size of /swapfile is 4290772992 (1047552 blocks, blocksize 4096)
ext logical  physical  expected  length flags
  0       0     7546880                6144 
  1    6144  7557120  7553023   2048 
  2    8192  7567360  7559167   2048 
...

From the example <Swap FIle Offset> is 7546880.

Swap over USB

Thanks to modularity offer by Linux, we can use it for use multiple swap partition over different device. These are step to follow to activate it:

First, take a USB flash and partition it with a swap partition. To do this you can use graphical tools such as Gparted or console tools like fdisk.

Then we should edit the Template:Codeline

# nano /etc/fstab

Now we add a new entry, just under the swap entry, which take the swap partition over the USB

UUID=... swap swap defaults,pri=10 0 0

where UUID is taken from the output of the command

ls -l /dev/disk/by-uuid/ | grep XXX

where, again, XXX is the swap partition in the USB memory, e.g. Template:Codeline

We use UUID because with other devices attached into computer, which could modify the device order, fstab mount the right partition

Last, add

pri=0

in the original swap entry for teaching fstab to use HD swap only when USB is full

ADVANTAGES

  • access time is slower than traditional HD
  • avoid power on HD when is in stand-by
  • USB are cheaper than HD

DISADVANTES

  • flash memories have limited write cycles
  • another device attached into the computer

For checking if the tweak works, just

swapon -s

and see: if swap in USB partition isn't empty then the tweak works.

Naturally this guide is working for other memory such as SD cards

Swappiness

The swappiness parameter represents the kernel's preference (or avoidance) of swap space. Swappiness can have a value between 0 and 100. Setting this parameter to a low value will reduce swapping from RAM, and is known to improve responsiveness on many systems.

Template:File