Linux HOWTO: Add Temporary File to Swap
Scenario: a Linux system with two drives. The system was built with resiliency as a priority, but availability is not critical. So, the filesystems were mounted atop md raid1 for resiliency, and the swap was striped across an md raid0 for performance.
One disk failed and was removed from the system. System returns to service on remaining drive until RMA is completed. Users are informed that the system is available but does not have swap space. Consensus is reached to add some temporary swap space via a swapfile.
Figure 2GB, space is available in /tmp
. Check the man pages, and make notes. First, how much is 2GB?
> bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
1024*1024*2
2097152
quit
Next, create a file of the appropriate size, mkswap, and swapon:
# dd if=/dev/zero of=/tmp/tmpswap bs=1024 count=2097152
2097152+0 records in
2097152+0 records out
2147483648 bytes (2.1 GB) copied, 21.552 seconds, 99.6 MB/s
# mkswap /tmp/tmpswap 2097152
Setting up swapspace version 1, size = 2147479 kB
# swapon /tmp/tmpswap
# swapon -s
Filename Type Size Used Priority
/tmp/tmpswap file 2097144 0 -1
Note: I like to think we don’t need to pass the size to mkswap, and it will just use the entire file. I figured I’d play it safe, like these notes.
RMA and subsequent maintenance involve a system reboot, so this swap space will go away. I figure that if we want to swapoff while the system is running, it is simply:
# swapoff /tmp/tmpswap
# rm /tmp/tmpswap