File-based Swap for Linux

You forgot to create a swap filesystem on Linux, and you don’t know or don’t want to use fdisk to repartition a disk and create swap space.

What do you do?

Fortunately, you still have a chance of saving your job.

1. Logon as root

2. Create a new directory anywhere that you prefer to use as swap fs. I prefer creating under the root directory and name it as /swapfs

mkdir /swapfs

3. We will create a data dump of a bunch of zeroes and store it in a X-bytes worth of file. The X-bytes will represent the size of swap file that you want or need. What is the ideal size? My answer has always been, it depends! If its just your PC for browsing the internet etc…, same size with your RAM will do. If its a database server, you may not want to use swap intensively, so blow up your RAM instead. I typically benchmark it from half of my RAM, to even twice my RAM. Again, it depends on what type application is running on your server or PC.

dd if=/dev/zero of=/swapfs/swap bs=1M count=4096

I am creating here a 1 megabyte swap file. It will create a file named swap under the directory /swapfs

4. I’ll now assign /swapfs/swap file as a SWAP space

mkswap /swapfs/swap

5. Of course, that is not enough, you have to enable it

swapon /swapfs/swap

If you’ll issue a df command, you’d be able to see the new swap filesystem, and the top command will also display the additional swap space.

Advertisement
Explore posts in the same categories: Linux, System Administration

Tags: ,

You can comment below, or link to this permanent URL from your own site.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.