Create and enable new swap space on a Linux server

By Jack Wallen

Takeaway: 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.

   64位的ubuntu12.04  出問題了吧。哈哈

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.(2G swap????) 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:(增加新的swap空間

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章