Help File Library: Building a Swap Partition
Written By:
grrlfox
A lot of the documentation available about Linux, especially
the older writings, stress the importance of a SWAP file
or partition in computers with limited memory. The examples
often cited are computers with 8 or 16 megabytes of RAM,
a reasonable enough amount a couple of years ago. These
documents seem to indicate that if you have much more RAM
than that, you won't need a swap file/partition. For instance,
I recently installed Linux on a machine with 96 megs of
RAM, and didn't worry about swapping at all, until I stopped
to monitor my memory usage for a couple of days. I realized
that I'd allowed myself to be mislead, in spite of years
of experience with multi-tasking operating systems, and
that I desparately needed some swap space. This is how I
created it.
Linux will allow you to use a file for your swap space,
but this is by far an inferior solution to setting aside
a partition on one of your hard disk drives. Files are more
fragile, and are subject to fragmentation (a frequent cause
of slow swapping in Windows operating systems). Setting
aside a partition is preferable; there will be nothing on
that partition save for the swapped information, eliminating
the possibility of fragments.
First I needed an empty partition to use, since swap partitions
don't use a standard file system. That meant finding the
hard drive with the most unused space on it, and shrinking
the partition to free a chunk for my swap space. There are
a number of tools around that can be used for this. DiskDruid
comes with the Red Hat and Mandrake distros, while fips,
a DOS program, is freely available at a number of FTP sites.
I'm lucky though, in that I have a copy of Partition Magic,
from PowerQuest, which recognizes all flavors
of FAT (File Allocation Table, the DOS/Windows file systems)
partitions, as well as NTFS (NT file system, for Windows
NT), HPFS (High Performance file system, for OS/2 users)
and, of course, the Linux native ext2fs.
With Partition Magic (as I would have been with fips or
DiskDruid), I was able to resize one of my extant NTFS partitions,
and set aside about 150 megs for use as my swap partition.
I wanted at least as much swap space as I had physical memory,
and I tend to be conservative and overestimate what I actually
will need. 100 megs would probably have been enough for
my system, as that's about equal to the physical memory.
I prefer to overcommit swap space though, preferring 1 1/2
to 2 times as much swap space as physical RAM. I left it
unformatted, as there's a file system type that's designed
for such use (all file systems have a hexadecimal number
assigned to them - ext2fs is 83, and the Linux swap file
system number is 82) and I knew that I'd be setting that
from within the Linux version of the fdisk utility.
My next step was to boot into Linux from my bootdisk. If
you don't have one of these, then MAKE ONE NOW!!!! It's
easily done - type
"mke2fs -c /dev/fd0 at the
command line then type cp /vmlinuz
without quotation marksof course. ohand do be sure to have
a floppy disk in drive first.
Once Linux was running on my system, I ran fdisk. That
was the easy part. I knew which disk drive it was on - in
my case, /dev/hdc, which DOS/Windows
would report as drive 2 (starting from 0). So, I typed "fdisk
/dev/hdc". Fdisk opened, and I was able to look at
the partitions on the disk, and chose the correct one, hdc6.
(Partition naming is a separate topic altogether.)
Fdisk tells you that pressing 'm'
will bring up help for the application, but it really gives
you a menu of choices. I chose 'l', to list the different
file system types, so that I'd be precisely sure of my choice,
then 't' to change the type on the partition i was working
with. Fdisk told me what to do to make the desired change.
Once I was satisfied that I'd made the change I wanted to
the correct partition, I typed a 'w', which wrote the changes
to disk, and exited fdisk.
Then it was time to start reading man pages. I read about
a dozen, and found the actual steps to be simple. First
I did "mkswap -c -v1 /dev/hdc6"
(remember that the actual device name will almost certainly
be different on your system). The -c option told the computer
to check for any bad spots on the disk, and to tell me how
many it found, if any. Had any been found, I'd have had
to have rethought my plan, as I'd not wish to use a partition
that had physical flaws on it. Happily, there were none.
The -v1 switch told it to use the updated version of the
swap filesystem. I read several pages of documentation to
come to the conclusion that NOT using that switch would
have been extremely dumb of me. The computer happily formatted
the partition to the correct type for me. Next, it was time
to turn on the new partition, with the /sbin/swapon
command. I used it with the '-a' switch, which makes all
devices listed as 'swap' in the file /etc/fstab
available. As I'd not edited that file, the switch wasn't
needed; I could have simply typed "swapon
/dev/hdc6".
But it didn't work. The device was "busy", because it takes
a reboot to complete the changes to the partition table
that I'd made. So I added a line to the file /etc/rc.d/rc.local
(THIS FILE IS DISTRO SPECIFIC. In Mandrake and Red Hat distros,
the correct file is /etc/rc.d/rc.sysinit.
that read "swapon -a /dev/hdc6".)
The file name that I edited is in the Slackware distro;
you may have to look through the files in your /etc/rc.d
directory to choose the one you need.
One thing I did NOT do was to add a line to my /etc/fstab
file reading "dev/hdc6 swap
swap default 1 0". This would have been automated had I
installed a swap partition at the same time that I installed
Linux.Then the line in my /etc/rc.d/rc.local
file could have read simply "swapon -a".
In any event, after a reboot, this time from the hard drive
rather than the floppy, my new swap partition corrrectly
loaded. The benefits are that Linux uses much less of the
physical RAM in the machine, so that there's more available
for caching information. It's most noticeable when I'm switching
applications in X windows, or switching between the different
virtual desktops I've set up.
PowerQuest,
which recognizes all flavors of FAT (File Allocation Table,
the DOS/Windows file systems) partitions, as well as NTFS
(NT file system, for Windows NT), HPFS (High Performance
file system, for OS/2 users) and, of course, the Linux native
ext2fs.
With Partition Magic (as I would have been with fips or
DiskDruid), I was able to resize one of my extant NTFS partitions,
and set aside about 150 megs for use as my swap partition.
I wanted at least as much swap space as I had physical memory,
and I tend to be conservative and overestimate what I actually
will need. 100 megs would probably have been enough for
my system, as that's about equal to the physical memory.
I prefer to overcommit swap space though, preferring 1 1/2
to 2 times as much swap space as physical RAM. I left it
unformatted, as there's a file system type that's designed
for such use (all file systems have a hexadecimal number
assigned to them - ext2fs is 83, and the Linux swap file
system number is 82) and I knew that I'd be setting that
from within the Linux version of the fdisk utility.
My next step was to boot into Linux from my bootdisk. If
you don't have one of these, then MAKE ONE NOW!!!! It's
easily done - type "mke2fs -c /dev/fd0at the command linethen
type cp /vmlinuzwithout quotation marksof course. ohand
do be sure to have a floppy disk in drive first.