1. Set correct 'swap' partitionHibernating writes the current session to the swap partition. So, if our swap partition is not mapped correctly, Ubuntu will obviously have trouble. To diagnose this problem, type 'free' on the command line to see how much swap space we have:
Here, the swap space is zero. One can usually enable a device for swap with the 'swapon' command, so I tried that:
$ free | grep Swap
Swap: 0 0 0
The 'swapon' command looks at the '/etc/fstab' file to determine which UUID should be used for swap. Since there was a 'No such file' error, it would seem that there is an entry in '/etc/fstab' which points to a file that doesn't exist.
$ swapon -a
swapon: cannot stat /dev/disk/by-uuid/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee: No such file or directory
Lets test this theory. First, see what is listed as the 'swap' device:
Now, check the '/dev/disk/by-uuid/' directory to see if any such UUID exists:
$ cat /etc/fstab | grep swap
UUID=aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeee none swap sw 0 0
There is no UUID corresponding to
$ ls -l /dev/disk/by-uuid/
lrwxrwxrwx 1 root root 07D2-0A18 -> ../../sda1
lrwxrwxrwx 1 root root 99d3e3b8-d10d-4ad0-a859-896a64126e4e -> ../../sda3
lrwxrwxrwx 1 root root b4bc91e8-f644-45b9-81d0-2884bb5638a2 -> ../../sda5
lrwxrwxrwx 1 root root E2A87FC6A87F97AD -> ../../sda2
aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeee, so our hypothesis is correct.
To fix this problem, we just need to update the '/etc/fstab' file with a UUID that actually exists. But which one should we use? To answer this question, use the 'fdisk' command:
It is sda5. Looking back in '/dev/disk/by-uuid/', we can see this disk has a UUID of
$ sudo fdisk -l | grep swap
/dev/sda5 4788 4865 626503+ 82 Linux swap / Solaris
Now we have all the information we need to set the swap partition correctly. First, edit the '/etc/fstab' file so that it has the correct UUID:
Now we should be able to use 'swapon' with no trouble
$ sudo gedit /etc/fstab # Edit file and save.
$ cat /etc/fstab | grep swap
UUID=b4bc91e8-f644-45b9-81d0-2884bb5638a2 none swap sw 0 0
And finally, verify that we have nonzero swap space with 'free':
$ swapon -a
Note that the entry for 'Swap' is greater than 'Mem'. This is good -- the 'hibernate' operation will have enough disk space to store all of memory. If we had less, we would have to fix this before proceeding. (This did not happen to me.)
$ free | grep -E 'Mem|Swap'
Mem: 515500 319868 195632 0 4292 118724
Swap: 626492 0 626492
Restart and verify our fix worked. But do not attempt to hibernate yet! The 'resume' process after hibernating may possibly undo all the work we just did.
2. Fix 'resume' partition UUIDWhen our system resumes from hibernate, it needs to know which partition to load the image from. Ubuntu looks for this information in the '/etc/initramfs-tools/conf.d/resume' file:
As you can see, it is looking for the old (incorrect) UUID that it thought was the swap partition, so we need to fix this here too:
$ cat /etc/initramfs-tools/conf.d/resume
Finally, we need to update the intitrd with the new UUID:
$ sudo gedit /etc/initramfs-tools/conf.d/resume # Edit file and save.
Restart your machine, and then try to hibernate and resume.
$ sudo update-initramfs -u
At this point, everything may just work. For me though, it was not sufficient. My computer would hibernate and seem to resume without any errors, but it was very slow and the screen was not refreshing properly. After poking around on the web, I decided to intsall a more reliable 'userspace' version of the software suspend program.
3. Switch to Userspace Software SuspendUserspace Software Suspend (µswsusp) is an alternate suspend program from the one shipped with the Ubuntu kernel. Let's see if it works on our system. First, download and install the program like this:
Then, test the hibernate function manually:
sudo apt-get install uswsusp
If the test is successful, we'll want to make this program the default for the 'hibernate' operation. This command will do the trick:
Restart and test 'hibernate' operations.
sudo dpkg-divert --rename --divert /usr/sbin/pmi-disabled /usr/sbin/pmi
Btw, this still may not work. If that is the case, you can type the following command to undo the Userspace Software Suspend installation:
sudo dpkg-divert --rename --remove /usr/sbin/pmi
sudo apt-get remove uswsusp
Yay! After following these steps, I have been able to 'hibernate' successfully many times.