Jump to content

Create additional swap space on running system


ianw1974
 Share

Recommended Posts

What do you do if you need additional swapspace?

Or you forgot to create a swap partition before you allocated it all to other Linux partitions when installing?

Don't want to repartition to add the swap space?

Or just have a real need for additional swap space temporarily or permanently?

 

Well, here is how you can easily create additional swap space either temporarily or permanently without having to screw around resizing partitions or reinstalling your system:

 

First, we need to create a file to use for the swap space:

 

dd if=/dev/zero of=/mnt/swapfile bs=1024 count=1024000

 

this will create a file called /mnt/swapfile with block size of 1024 and 1GB in size for 1GB of swap. Now, after this has been created, you need to activate it:

 

mkswap /mnt/swapfile
swapon /mnt/swapfile

 

to check it's working:

 

free

 

will show the swap space is now available on your system, either in addition to the swap space you had originally or will just show what you just created.

 

To make sure it is mounted each time you boot, add this line to /etc/fstab:

 

/mnt/swapfile swap swap defaults 0 0

 

and you're done! Whilst the ideal method might be to repartition and create a specific partition for swap, this could work well in some respects. Unsure if there is a performance hit for using a file instead of true swap space, but still a relatively good and easy method to do it quickly and simply without changing too much stuff.

Link to comment
Share on other sites

I thought I would list 1GB as an example, purely so you could then work out other sizes, such as 512MB or anything after that. Purely for ease, as you noticed :P

 

Of course, once you've done the dd command, you can easily check to make sure you got your count value correct, as this is what sets the size of the file. A quick:

 

ls -l /mnt

 

would let you see if it makes 512MB or 1GB or whatever you happened to enter here.

 

Red Hat always recommend double your ram, regardless, or at least in their exams as I've recently found when studying. However, of course, we all know that depending on your requirements/configuration, etc, you wouldn't necessarily need to follow this rule always.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...