Create and activate swap spaces.
After completing this section, students should be able to:
Create and format a partition for swap space.
Activate the swap space.
A swap space is an area of a disk which can be used with the Linux kernel memory management subsystem. Swap spaces are used to supplement the system RAM by holding inactive pages of memory. The combined system RAM plus swap spaces is called virtual memory.
When the memory usage on a system exceeds a defined limit, the kernel will comb through RAM looking for idle memory pages assigned to processes. The kernel writes the idle page to the swap area, and will reassign the RAM page to be used by another process. If a program requires access to a page that has been written to disk, the kernel will locate another idle page of memory, write it to disk, then recall the needed page from the swap area.
Since swap areas reside on disk, swap is incredibly slow when compared with RAM. While it is used to augment system RAM, usage of swap spaces should be kept to a minimum whenever possible.
To create a swap space, an administrator needs to do three things:
Create a partition.
Set the type of the partition as 82 Linux Swap.
Format a swap signature on the device.
Create a partition
Use a tool, such as fdisk, to create a partition of the desired size. In the following example, a 256 MiB partition will be created.
[root@serverX ~]#fdisk /dev/vdbWelcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0x34e4e6d7. Command (m for help):nPartition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p):pPartition number (1-4, default 1): 1 First sector (2048-20971519, default 2048):EnterUsing default value 2048 Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519):+256MPartition 1 of type Linux and of size 256 MiB is set Command (m for help):pDisk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x34e4e6d7 Device Boot Start End Blocks Id System /dev/vdb1 2048 526335 262144 83 Linux
Assign the partition type
After the swap partition has been created, it is recommended practice to
change the partition's type, or system ID, to 82 Linux Swap.
In the past, tools looked at the partition type to determine if the
device should be activated; however, that is no longer the case.
Even though the partition type is not used by utilities any longer, having
the type set allows administrators to quickly determine the partition's
purpose. The following example continues from within
fdisk.
Command (m for help):tSelected partition 1 Hex code (type L to list all codes):82Changed type of partition 'Linux' to 'Linux swap / Solaris' Command (m for help):pDisk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x34e4e6d7 Device Boot Start End Blocks Id System /dev/vdb1 2048 526335 262144 82 Linux swap / Solaris
Format the device
The mkswap command applies a swap signature to the device. Unlike other formatting utilities, mkswap writes a single block of data at the beginning of the device, leaving the rest of the device unformatted so it can be used for storing memory pages.
[root@serverX ~]#mkswap /dev/vdb1Setting up swapspace version 1, size = 262140 KiB no label, UUID=fbd7fa60-b781-44a8-961b-37ac3ef572bf
An administrator can use the swapon command to activate
a formatted swap space. swapon can be called on the
device, or swapon -a will activate all swap spaces
listed in the /etc/fstab file.
[root@serverX ~]#freetotal used free shared buffers cached Mem: 1885252 791812 1093440 17092 688 292024 -/+ buffers/cache: 499100 1386152 Swap: 0 0 0[root@serverX ~]#swapon /dev/vdb1[root@serverX ~]#freetotal used free shared buffers cached Mem: 1885252 792116 1093136 17092 692 292096 -/+ buffers/cache: 499328 1385924Swap:2621400262140
Persistently activate swap space
It is likely that a swap space will be required to automatically
activate every time the machine boots. In order for the machine to
activate the swap space at every boot, it must be configured in the
/etc/fstab file.
If needed, an administrator can deactivate a swap space using the swapoff command. A swapoff will only be successful if any swapped data can be written to other active swap spaces or back into memory. If data cannot be written to other places, the swapoff will fail, with an error, and the swap space will stay active.
The following is an example line in /etc/fstab adding a
previously created swap space.
UUID=fbd7fa60-b781-44a8-961b-37ac3ef572bf swap swap defaults 0 0
The example uses the UUID as the first field. The
UUID is stored in the swap signature stored on the device,
and was part of the output of mkswap. If the output of
mkswap has been lost, the blkid
command can be used to scan the system and report on all attached block
devices. If the administrator does not wish to use the UUID,
the raw device name can also be used in the first field.
The second field is typically reserved for the mount point.
However, for swap devices, which are not accessible through the directory
structure, this field is the placeholder value swap.
The third field is the file system type. The file system type for a swap
space is swap.
The fourth field is for options. In the example, the option
defaults is used. defaults includes the mount
option auto, which is what causes the swap space to be
automatically activated at boot.
The final two fields are the dump flag and fsck order. Swap spaces require neither backing up nor file system checking.
By default, swap spaces are used in series, meaning that the first
activated swap space will be used until it is full, then the kernel will
start using the second swap space. Swap space priorities are displayed
with swapon -s, and can be set with the
pri= mount option. If swap spaces have the same priority,
the kernel will write to them round-robin instead of writing to a single
swap space until it is at capacity.
mkswap(8), swapon(8), swapoff(8), mount(8), fdisk(8) man pages