The FreeBSD Diary

The FreeBSD Diary (TM)

Providing practical examples since 1998

If you buy from Amazon USA, please support us by using this link.
[ HOME | TOPICS | INDEX | WEB RESOURCES | BOOKS | CONTRIBUTE | SEARCH | FEEDBACK | FAQ | FORUMS ]
swap files - how to get more swap space 1 November 1998
Need more help on this topic? Click here
This article has no comments
Show me similar articles
NOTE: After I wrote this diary entry, the Handbook section on swap files has been brought to my attention.  Perhaps that resource will provide you with better information than this entry.

If you've already added one swapfile, and want to add more, see also Adding more swapfiles.

What are swap files?
FreeBSD, like many operating systems, make use of a concept called virtual memory.  This allows the computer to access more memory than physically exists.  Disk is used to store those pages of memory which don't fit into physical memory.  These areas of disk are known as swap files.
How much do I have?
To see what swap files exist, use either of the following two commands.  I've seen no difference in the output, but that doesn't mean much.
su-2.02# swapinfo
Device      1K-blocks     Used    Avail Capacity  Type
/dev/wd0s1b    256000     9568   246368     4%    Interleaved
su-2.02# pstat -s
Device      1K-blocks     Used    Avail Capacity  Type
/dev/wd0s1b    256000     9568   246368     4%    Interleaved
The problem 22 June 2000
You need the vn driver in your kernel.  That can be added by following the instructions at the FAQ section on swap files.  After writing this article over 19 months ago, it's time I used it.   I've been getting the following messages on http://www.freshports.org/ quite often:
/kernel: swap_pager_getswapspace: failed

And I've been getting these messages for even longer:

/kernel: pid 35990 (apache), uid 99: exited on signal 11

Here was my swapinfo:

$ swapinfo
Device       1K-blocks     Used    Avail Capacity  Type
/dev/ad0s1b      70776    53232    17544    75%    Interleaved
Creating a new swapfile
This section creates the swapfile.  So my first command was:
# dd if=/dev/zero of=/usr4/swapfile bs=15m count=10
10+0 records in
10+0 records out
157286400 bytes transferred in 40.594 secs (3874578 bytes/sec)

Note that the above should have give you a 150Mb file (bs=15m, count=10 or 15Mx10 = 150Mb).  Enjoy.

The config file
I added this to /etc/vntab:
/dev/vn0c /usr4/swapfile swap
Starting vnconfig
Then I issued the command:
vnconfig -ae

If you get this error:

vnconfig: open: Device not configured

then you didn't add the vn device to your kernel.  You should have read the FAQ section on swap files first.

The results
Now I have this as swap space:
# swapinfo
Device       1K-blocks     Used    Avail Capacity  Type
/dev/ad0s1b      70776    63408     7368    90%    Interleaved
/dev/rvn0c      153472        0   153472     0%    Interleaved
Total           224248    63408   160840    28%

That looks much better.  I'll leave it for a week or so to see how it goes.

Making sure it works every time...
Ahhh, and before I forget, I added that command to /usr/local/etc/rc.d/vnconfig.sh so it runs at startup.  The file contains this:
#!/bin/sh
/usr/sbin/vnconfig -ae

And I did this to make sure the file is executable:

chmod u+x /usr/local/etc/rc.d/vnconfig.sh

That's it.  Should be fine.  But just to be sure the file would run, I did this:

# /usr/local/etc/rc.d/vnconfig.sh
vnconfig: VNIOCATTACH: Device busy

Which is to be expected because vnconfig is already running.

Things to watch out for
Samuel Blinn wrote in with this little tidbit:

Well, I made a stupid mistake and perhaps you could add my experience to the page so that this won't happen to others.

I created one swapfile and it was great.  But it wasn't enough.  so I created a second swapfile.  Unfortunately instead of pointing the second vntab entry to /usr/swap1, I pointed it to /usr/swap0 just like the first vntab entry.

Wow!  Every single program started seg faulting and core dumping. sendmail, inittab, and every once in a while, the kernel.  I couldn't figure out wtf was going on.  Until like the fourth time I studied vntab I figured out the error of my stupid ways.

I guess that's what I get trying to compile things on a 486 laptop.

-sam

The moral of the story: If you create more than one swap file using this method, make sure your entries in /etc/vntab are unique.  Thanks Sam.


Need more help on this topic? Click here
This article has no comments
Show me similar articles