-->
Previous | Table of Contents | Next |
Youre better off using a swap partition instead of a swap file. All access to a swap file is performed through the normal Linux file system. The disk blocks that make up the swap file are probably not contiguous and, therefore, performance isnt as good as it is with a swap partition. I/O to swap partitions is performed directly to the device, and disk blocks on a swap partition are always contiguous. Also, by keeping the swap space off a normal file system, you reduce the risk of corrupting your regular file system if something bizarre happens to your swap file.
To create a swap partition, you must have created a disk partition by using fdisk and tagged it as type 82, Linux swap. After you create the swap partition, you have two additional steps to follow to make the swap partition active.
First, you must prepare the partition in a manner similar to creating a file system. Instead of mkfs, the command for preparing the partition is mkswap. The syntax of the mkswap command is as follows:
mkswap [-c] device size-in-blocks
where device is the name of the swap partition, such as /dev/hda2, and size-in-blocks is the size of the target file system in blocks. You can get the size in blocks by running fdisk and looking at the partition table. In the example in the section Making Sure the Sizes Are Correct, the size of /dev/hda2 was 19,159 blocks. Linux requires that swap partitions be between 9 and 65,537 blocks in size. The -c argument tells mkswap to check the file system for bad blocks when creating the swap space, which is a good idea.
Following the example in Making Sure the Sizes Are Correct, the command for setting up a swap partition on /dev/hda2 is this:
mkswap -c /dev/hda2 19159
After you run mkswap to prepare the partition, you must make it active so that the Linux kernel can use it. The command to make the swap partition active is swapon. The syntax for the swapon command is as follows:
swapon filesys
where filesys is the file system that you want to make available as swap space. Linux makes a call to swapon -a during boot, which mounts all available swap partitions listed in the /etc/fstab file.
NOTE: Remember to put an entry for any swap partitions or swap files that you create into the /etc/fstab file so that Linux can automatically access them at boot time.
Swap files can be useful if you need to expand your swap space and cant allocate disk space to create a dedicated swap partition. Setting up a swap file is almost identical to creating a swap partition. The main difference is that you have to create the file before you can run mkswap and swapon.
To create a swap file, you use the dd command, which is used for copying large chunks of data. For a full description of this command, see the man page for dd. The main things that you have to know before creating the file are the name of the swap file you want to create and its size in blocks. A block under Linux is 1,024 bytes. For example, to create a 10MB swap file named /swap, enter
# dd if=/dev/zero of=/swap bs=1024 count=10240
of=/swap specifies that the file to be created is named /swap, and count=10240 sets the size of the output file to be 10,240 blocks, or 10MB. You then use mkswap to prepare the file as a swap space:
# mkswap /swap 10240
Remember that you have to tell mkswap how big the file is. Before you run swapon, you need to make sure that the file is completely written to disk by using the /etc/sync command.
Now youre ready to make the swap file active. Like with the swap partition, you use the swapon command to make the file active; for example,
# swapon /swap
If you need to get rid of a swap file, you must make sure that its not active. Use the swapoff command to deactivate the swap file, as in
# swapoff /swap
You can then safely delete the swap file.
In this chapter, youve looked at many different aspects of the Linux file system, from a tour of the basic directory structure to mounting and unmounting file systems. Youve explored accessing remote file systems with NFS and looked in detail at how to create file systems and prepare them for use. Finally, this chapter discussed the creation of swap partitions and swap files.
You can find more information about systems administration in the following chapters:
Previous | Table of Contents | Next |