How do I add more swap easily so that I can hibernate my laptop? [duplicate]

The best way IMHO is just to increase the size of the swap partition.

Boot your computer with a Ubuntu Live CD/USB.

The live CD will detect and use your swap, you should disable it to be able to manipulate it. Open a terminal and type the following command:

sudo swapoff -a

Afterwards, open Gparted, an resize your partition. You will need to shrink some partition that is close to it. It should be quite straightforward.

You will need to boot from some external method, because you can't resize partitions that are in use, and it's quite likely that you will need to shrink your main partition.

Now the other questions:

  1. Yes.

  2. If you use a swap file then yes, you can get rid of your swap partition. But I believe that using a swap partition is easier. It's also the standard practice in linux, most of the tutorials that you can find on the net will expect it.

  3. Messing with swap is something that no newbie should do in any OS. No need to make it easy.


I finally solved this. Of course, to answer the "Why is this all so difficult?" part of my question, I would just have to respond "This is Linux, you fool, of course everything is difficult!"

Here are the steps:

  1. Boot to Ubuntu install CD (I'm on Natty) and choose the option to run Ubuntu now
  2. Go to system -> GParted Partition Editor
  3. Delete the swap partition and the extended partition that holds it (if by some miracle you're able to resize your swap partition from here, I imagine your life will be a lot easier than mine)
  4. Decrease the size of your primary partition by the amount you want your new swap to be (I made mine 2x RAM + 500MB just to be safe). The easiest way to do this is to fill in the amount of space you want swap to be in the "free space following" field
  5. In the free space that has now been created, choose new, type linux-swap and you can name the partition "swap" if you like
  6. Hit the Apply button (should be a check mark) to write the changes to disk
  7. When done, reboot back into Ubuntu

Now you need to find what partition your swap is on and what its UUID is. UUID?! you say? Well that's the Universally Unique IDentifier for the partition so you can reference it even if it's on a different mount point from boot-to-boot due to adding disks, etc. If this is your primary disk, it's probably not a big deal.

  1. Pull up a terminal and run gksu gparted & and enter your root password. The & lets this process run while still giving you access to the command line.
  2. Right-click on your swap partition and choose Information. You should see the Path and UUID listed there. Keep this open for further reference.
  3. Run gksu gedit /etc/fstab & and look for the line that has swap in it. It should be the third column, separated by spaces or tabs. You can either use the path or the UUID to tell Linux where to find your swap partition. I recommend UUID because it'll stay constant even if you move the partition around or the disk somehow becomes sdb instead of sda or something like that. Make the appropriate edits and save the file. Your line should look something like this if you used UUID (with your UUID instead, of course):
    • UUID=41e86209-3802-424b-9a9d-d7683142dab7 none swap sw 0 0
    • or this if you used path: /dev/sda2 none swap sw 0 0
  4. Save and reboot to make sure the new swap gets activated properly at startup

Grab a glass of milk and wait for Ubuntu to come back up... The fun part comes next

  1. Pull up a Terminal again and run cat /proc/swaps and hopefully you see the path to your swap partition listed there. If not chances are something went wrong in the steps above. Here's my output:

    Filename                Type        Size    Used    Priority
    /dev/sda2               partition   2676732 6360    -1
    
  2. gksu gedit /etc/default/grub & to pull up the boot loader configuration

  3. Look for the line GRUB_CMDLINE_LINUX="" and make sure it looks like this (using your UUID of course) GRUB_CMDLINE_LINUX="resume=UUID=41e86209-3802-424b-9a9d-d7683142dab7" and save the file
  4. sudo update-grub and wait for it to finish
  5. gksu gedit /etc/initramfs-tools/conf.d/resume & and make sure its contents are resume=UUID=41e86209-3802-424b-9a9d-d7683142dab7 (with your UUID of course in place of mine). Save the file!
  6. sudo update-initramfs -u
  7. Reboot!

Now you should be able to hibernate and resume! I really hope this helps someone else. I can't believe Ubuntu is on version 11 and it still SUCKS THIS HARD to get it to hibernate your laptop. Don't lose faith though, it can be done!