Please note that in kernel "resume" parameter you have to type path to partition (e.g. resume=/dev/sda1) not to swapfile explicitly! Parameter resume_offset is to inform system where swapfile starts on hard disk.

== Swap with USB device ==

== Swap with USB device ==

Revision as of 10:00, 21 January 2012

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.

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 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 fallocate to create a swap file the size of your choosing (M = Megabytes, G = Gigabytes) (dd can also be used but will take longer). For example, creating a 512 MB swap file:

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 /etc/fstab and add an entry for the swap file:

/swapfile none swap defaults 0 0

Remove swap file

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

As root:

# swapoff -a

Remove swapfile:

# rm -rf /swapfile

Swap file resuming

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:

Please note that in kernel "resume" parameter you have to type path to partition (e.g. resume=/dev/sda1) not to swapfile explicitly! Parameter resume_offset is to inform system where swapfile starts on hard disk.

Swap with USB device

The factual accuracy of this article or section is disputed.

Reason:please use the first argument of the template to provide a brief explanation. (Discuss in Talk:Swap#)

Thanks to modularity offered by Linux, we can have multiple swap partitions spread over different devices. To add a a USB device to SWAP:

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. Make sure to label the partition as SWAP before writing the partition table.

Make sure you are writing the partition to the correct disk!

Next edit the fstab

# nano /etc/fstab

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

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

where UUID is taken from the output of the command

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

Just replace sdc1 with your new USB swap partition. sdb1

We use UUID because when you attach other devices to the computer it could modify the device order

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 lower than traditional HD

avoid power on HD when is in stand-by

USB pendrives are cheaper than HD (meaning: you can't get a $5 HDD, price per gigabyte is obviously much higher for flash drives)

DISADVANTAGES

flash memories have limited write cycles

another device attached to the computer

This guide will work for other memory such as SD cards, etc.

Swap test

Ensure that swap is activated with swapon of free:

$ swapon -s
$ free -m

Performance Tuning

Swap values can be adjusted to help performance.

Swappiness

The swappinesssysctl 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.

/etc/sysctl.conf

vm.swappiness=1
vm.vfs_cache_pressure=50

Priority

If you have more than one swap file or swap partition you should consider assigning a priority value (0 to 32767) for each swap area. The system will use swap areas of higher priority before using swap areas of lower priority. For example, if you have a faster disk (/dev/sda) and a slower disk (/dev/sdb), assign a higher priority to the swap area located on the faster device. Priorities can be assigned in fstab via the pri parameter: