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:

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

Categories: Linux, Sundry, Technical

  • Mike

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

  • 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.

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