Linux

Create and enable new swap space on a Linux server

Jack Wallen instructs you on how to get a new, larger swap partition created for your Linux server or desktop.

Over the weekend I discovered something interesting with my 64-bit version Ubuntu 12.04 desktop installation. I do a lot of graphics work using larger images. After opening a couple of these images, my machine would start to bog down; this hadn't happened when I was using the 32-bit version of Mint.

I knew my machine wasn't a powerhouse and only had 2 GB of RAM, but the machine shouldn't have become nearly unusable. I started digging around, and I discovered that the default installation added only a 2 GB swap space. Normally that would be okay, but with all of the factors combined, it became clear I needed more swap space. I had no desire to re-install the OS and realized this could become an issue with Linux-based servers that have become bogged down under heavy loads.

So, how can one create and enable a new swap space on a working Linux server without losing data or your mind? It's actually quite easy. Here's what you'll need:

  • Copy of the GParted Live distribution burned onto a disk (or USB device).
  • Room on your server hard drive to make space for the new space.

I also highly recommend you do an image backup of the server (using a tool like Clonezilla) in case something goes catastrophically wrong. Although this task is fairly reliable and easy, you never know when lightning may strike. With your backup image securely stored on an external drive, let's move forward.

Step 1: Download the ISO for GParted Live and burn it to disk

You should use whatever tool you like that is capable of burning an ISO image to a disk. The GParted Live image isn't terribly large, so it will fit on a CD or DVD. If you prefer, you can download the ISO and then, using a tool like UNetbootin, install it on a USB drive. Once you have GParted Live on a disk or USB, reboot your machine and boot from the GParted Live disk.

Step 2: Fire up GParted and resize your primary partition

From the GParted Live desktop, you should see an icon for GParted. Double-click that icon to start the application. From the main GParted window, right-click the primary partition (most like /dev/sda) and select Resize from the menu (Figure A). Figure A

You see the small swap space on a Ubuntu machine. (Click the image to enlarge.)
When the new window pops up, resize the partition from the right side. Grab the right-pointing arrow with your cursor (Figure B) and drag the right edge to where you want the resizing to take place. Figure B

I am resizing a partition that is not my primary partition because I am working within that partition and cannot unmount it to resize. (Click the image to enlarge.)

Once you're satisfied with the new size, click the Resize/Move button and then click the Apply button (the check mark) on the main window. Depending on how much you are resizing, this task could take a while. Once it's complete, you are ready to move on.

Step 3: Create a new swap space

This is where it gets the slightest bit tricky. If your swap space is within an extended partition (you'll know because it will be a brown rectangle within a light blue rectangle), you can delete the swap partition and the extended partition. Once you delete them, click the Apply button to apply the changes. You now have space to create a swap partition that meets your needs.

To create the new partition, right-click the empty space and click New. In the resulting window, leave everything as default but select linux-swap as the file system (Figure C). Click the Add button to continue. Figure C

I'm working on a drive other than the one housing the OS, but the steps are identical. (Click the image to enlarge.)

Click the Apply button to complete the process. The swap partition is created, and now you have to make the OS aware it's there and that it should be used.

Reboot the machine without GParted Live. You will probably receive some errors -- that's fine, just continue.

Step 4: Edit /etc/fstab

If you open the /etc/fstab file (in your favorite editor), you will find an entry that looks similar to this:

UUID=76301bca-499e-4f6f-8094-c1de524d7608 none            swap    sw              0       0

The UUID, which is the ID of the block system for the old swap space, is what's important here. To find out the new UUID, issue the command sudo blkid. This command will list the UUIDs of all listed block devices; you should see the new swap partition listed there. You should change the UUID (or add a new entry and comment out the old entry) to the new UUID of the new swap space. Before you reboot your system again, you need to turn that swap space on with the command swapon -a. This will enable the swap partition. Now reboot the machine, and you should be good to go.

Note: Any time you are dealing with the partition structure of a hard disk, you should use caution and always be prepared with a bare metal backup image.

About

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website getjackd.net.

2 comments
pgit
pgit

I've never had much luck with clonezilla. I've found it failed to make the copy bootable on a number of occasions. dd on the other hand has been 100%, even when creating or restoring the image over the network.(the mondo rescue disk uses dd and has networking and an ftp client) Some distros will automatically detect blank disk space and prompt you to set it up as swap. Mandriva did this for me once a while back. All I had to do was remove the old swap from fstab and boot.

lehnerus2000
lehnerus2000

I don't think that "Figure C" matches the text.