07 January 2011

Adding Swap Space in Linux

We've discussed FreeBSD and Solaris, now it's time to have a look at
adding swap space in Linux.  As previously handled, the setting up
of swap on a free disk partition or else a swap file is discussed.
The following details our example host:
        HOST:           tux
        PROMPT:         tux [0]
        OS:             CentOS 5.4 Linux
PARTITION:  If you already have a free partition that isn't currently
in use, adding it as swap space is as simple as:
        tux [0] /sbin/mkswap -v1 /dev/sdc1
        Setting up swapspace version 1, size = 536862 kB
        tux [0] /sbin/swapon -v /dev/sdc1
        swapon on /dev/sdc1
Since I don't currently have a free partition, I need to create one on
an available disk.  The following 'fdisk' command gets us a listing of
our available disks:
        tux [0] /sbin/fdisk -l | /bin/grep ^Disk
        Disk /dev/sda: 11.0 GB, 11004805120 bytes
        Disk /dev/sdb: 11.0 GB, 11005853696 bytes
        Disk /dev/sdc: 536 MB, 536870912 bytes
        Disk /dev/sdd: 536 MB, 536870912 bytes
I'm not using 'sdc' for anything so we can use it to create a new
partition for our new swap space.  We can use 'sfdisk' to destroy any
previous partition tables and create a new one with a single partition
spanning the full disk.  (Alternatively, 'fdisk' or 'parted' could also
be used if you are more familiar with either of them.)  The first line
of parameters below tells 'sfdisk' to create partition 1, starting at
sector 0, ending at the last available sector, and set the partition
type to Linux swap (82).  The next three lines inform 'sfdisk' that
partitions 2 - 4 are not configured.  The rest of the details are the
output of 'sfdisk' after the second 'EOF':
        tux [0] /sbin/sfdisk /dev/sdc << EOF
        > 0,,82
        > ;
        > ;
        > ;
        > EOF
        Checking that no-one is using this disk right now ...
        OK

        Disk /dev/sdc: 512 cylinders, 64 heads, 32 sectors/track
        Old situation:
        Units = cylinders of 1048576 bytes, blocks of 1024 bytes, counting from 0

           Device Boot Start     End   #cyls    #blocks   Id  System
        /dev/sdc1          0+    511     512-    524287+  83  Linux
        /dev/sdc2          0       -       0          0    0  Empty
        /dev/sdc3          0       -       0          0    0  Empty
        /dev/sdc4          0       -       0          0    0  Empty
        New situation:
        Units = cylinders of 1048576 bytes, blocks of 1024 bytes, counting from 0

           Device Boot Start     End   #cyls    #blocks   Id  System
        /dev/sdc1          0+    511     512-    524287+  82  Linux swap / Solaris
        /dev/sdc2          0       -       0          0    0  Empty
        /dev/sdc3          0       -       0          0    0  Empty
        /dev/sdc4          0       -       0          0    0  Empty
        Warning: no primary partition is marked bootable (active)
        This does not matter for LILO, but the DOS MBR will not boot this disk.
        Successfully wrote the new partition table

        Re-reading the partition table ...

        If you created or changed a DOS partition, /dev/foo7, say, then use dd(1)
        to zero the first 512 bytes:  dd if=/dev/zero of=/dev/foo7 bs=512 count=1
        (See fdisk(8).)
Another 'sfdisk' invocation verifies the new partition table layout:
        tux [0] /sbin/sfdisk -l /dev/sdc

        Disk /dev/sdc: 512 cylinders, 64 heads, 32 sectors/track
        Units = cylinders of 1048576 bytes, blocks of 1024 bytes, counting from 0

           Device Boot Start     End   #cyls    #blocks   Id  System
        /dev/sdc1          0+    511     512-    524287+  82  Linux swap / Solaris
        /dev/sdc2          0       -       0          0    0  Empty
        /dev/sdc3          0       -       0          0    0  Empty
        /dev/sdc4          0       -       0          0    0  Empty
With the new partition created, we need to use 'mkswap' to tell Linux to
set up a swap area on 'sdc1' and use 'swapon' to add the swap space on
'sdc1'.  Finally, verify that the new swap space is available to Linux:
        tux [0] /sbin/mkswap -v1 /dev/sdc1
        Setting up swapspace version 1, size = 536862 kB
        tux [0] /sbin/swapon -v /dev/sdc1
        swapon on /dev/sdc1

    before adding:

        tux [0] /sbin/swapon -s
        Filename                                Type            Size    Used    Priority
        /dev/sda3                               partition       1052248 0       -1
        tux [0] /bin/cat /proc/swaps
        Filename                                Type            Size    Used    Priority
        /dev/sda3                               partition       1052248 0       -1

    after adding:

        tux [0] /sbin/swapon -s
        Filename                                Type            Size    Used    Priority
        /dev/sda3                               partition       1052248 0       -1
        /dev/sdc1                               partition       524276  0       -2
To retain the usage of the swap device through a system reboot, update
/etc/fstab as seen below:
        tux [0] /bin/grep swap /etc/fstab
        LABEL=SWAP-sda3         swap                    swap    defaults        0 0
        /dev/sdc1               swap                    swap    defaults        0 0
SWAPFILE:  For the second option, locate an out of the way place on
a filesystem with adequate space for the swap file to reside.  For our
purposes, we'll use '/opt/swapfile' for a new 512 MB swap file.  To start,
verify that the directory structure exists and the swap file doesn't:
        tux [0] [ ! -d /opt ] && /bin/mkdir /opt
        tux [1] /bin/ls /opt/swapfile
        /bin/ls: /opt/swapfile: No such file or directory  
To create '/opt/swapfile' at 512 MB, use 'dd':
        tux [2] /bin/dd if=/dev/zero of=/opt/swapfile bs=1k count=512k
        524288+0 records in
        524288+0 records out
        536870912 bytes (537 MB) copied, 5.78956 seconds, 92.7 MB/s
        tux [0] /bin/chown root:root /opt/swapfile 
        tux [0] /bin/chmod 0600 /opt/swapfile
        tux [0] /usr/bin/du -sh /opt/swapfile
        513M    /opt/swapfile
        tux [0] /bin/ls -l /opt/swapfile 
        -rw------- 1 root root 536870912 Jan  6 17:03 /opt/swapfile
After file creation, we've set up appropriate ownership and permissions
on 'swapfile', then verified our work.  As with our swap partition, use
'mkswap' to create the swap area in 'swapfile' and add it using 'swapon'.
Finally, verify it is availble via another 'swapon' command:
        tux [0] /sbin/mkswap -v1 /opt/swapfile   
        Setting up swapspace version 1, size = 536866 kB   
        tux [0] /sbin/swapon -v /opt/swapfile
        swapon on /opt/swapfile
        tux [0] /sbin/swapon -s
        Filename                                Type            Size    Used    Priority
        /dev/sda3                               partition       1052248 0       -1
        /dev/sdc1                               partition       524276  0       -2
        /opt/swapfile                           file            524280  0       -3
To retain the usage of the swap file through a system reboot, update
/etc/fstab as seen below:
        tux [0] /bin/grep swap /etc/fstab
        LABEL=SWAP-sda3         swap                    swap    defaults        0 0
        /dev/sdc1               swap                    swap    defaults        0 0
        /opt/swapfile           swap                    swap    defaults        0 0
Should you need to remove the swap space provided by a disk partition
or swap file, 'swapoff' is used:
        tux [0] /sbin/swapoff /dev/sdc1
        tux [0] /sbin/swapoff /opt/swapfile
        tux [0] /sbin/swapon -s
        Filename                                Type            Size    Used    Priority
        /dev/sda3                               partition       1052248 0       -1
Of note, only swap space that isn't in use can be removed.

see also:
    Adding Swap Space in FreeBSD
    Adding Swap Space in Solaris