dannyman.toldme.com


Linux, Sundry, Technical

HOWTO: Add a Swapfile at Boot

A while back I outfitted my personal workstation with 4GB of RAM. That’s plenty of memory and since disk space was tight I didn’t want to waste any on a swap partition, and I lived happily without swap for a very long time until I began using more virtualization. (I freed up space on my physical desk top by migrating my work environment from a laptop to a virtual machine on my personal workstation.)

I wrote a script to add a “temporary” swap file on demand but what I wanted was a swap file at boot. That actually turns out to be pretty simple. In this case, I just prepare the swap file:

FILE=/mnt/swapfile
SIZE=8388608 # 8 GB

dd if=/dev/zero of=$FILE bs=1024 count=$SIZE
mkswap $FILE $SIZE
swapon $FILE
swapon -s

Then, to make it stick, add this line to /etc/fstab just as you would for a swap partition:

/mnt/swapfile none swap sw 0 0

This is a win for the Unix everything-is-a-file philosophy.

See Also: Ubuntu Community Swap FAQ

Read More

Next:
Previous:
Categories: Linux, Sundry, Technical

Responses

May 29th, 2010

Mike

One possible downside — I think that hibernation requires a real swap partition.

June 1st, 2010

Andrew Kaczorek

Another downside: I’m pretty sure diskdump won’t work if if you’re into that sort of thing. It tends to prefer its swap like Gerardo prefers his women, raw like sushi. Even LVM don’t cut it. But then again, you did say this was your personal workstation, not a production server. I should really put my pocket protector away.

I enjoy keeping up on Dannyman’s worldwide exploits. I’m a longtime lurker, first time poster.

June 1st, 2010

dannyman

It’s the swap files that bring it out . . .

Comment

Leave a comment . . .

Tiny Print:

  1. For private messages, e-mail me: dannyman@toldme.com.
  2. You must provide an e-mail address.
  3. You can use a bogus e-mail address, but I like to know who you are.
  4. I will not spam you. I will not publish or share your e-mail address.
  5. First-time commenters will be held for review.
  6. You can use these HTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>