söndag 27 mars 2016

Enabling Hibernation in Fedora 23

By default, Fedora doesn't enable hibernation (suspend to disk). Basically what you need is a big enough swap partition. Then it has to be added to fstab and Grub. Initramfs also has to support resuming from hibernation.

On my laptop (MSI GE60) unfortunately the swap partition was too small. Fedora recommends that a swap partition should be half the size of RAM without hibernation and 1.5 the size of RAM with hibernation with 8 GB - 64 GB of RAM[1].

This laptop has 12 GB of RAM, but only a 6 GB swap partition. The partition has to be extended to 18 GB.

There is a 250 GB SSD with 174 GB of space in /home (ext4 on LVM). I had to reduce the home partition to 162 GB to free up space for the swap partition. Of course, I had to free up enough space in the filesystem first. It is better to have a few times more free space than the amount to be reduced.

Resizing an LVM/ext4 /home partition

I don't recommend doing this, unless you have knowledge about partitions and LVM. You could easily destroy your data! But I will describe step by step how I did it.

Boot the laptop into single mode. At the Grub screen, edit the linux kernel command line and add "single" or "rescue.target" (without quotes) to the command line. Ctrl-X to boot. At the rescue prompt, enter your root password.

At the root rescue prompt, unmount /home with:
umount /home
Check the partition:
e2fsck -f /dev/mapper/fedora_msi-home
(on another laptop, the partition will be named something else, but you will find it under /dev/mapper. "df" will also show currently mounted partitions and usually they are also listed in /etc/fstab)

Resize the file system:
resize2fs -p /dev/mapper/fedora_msi-home 162G
Resize the logical volume:
lvreduce -L 162G /dev/fedora_msi/home

Before continuing, make sure that the partition lines up with the logical volume:
e2fsck -f /dev/mapper/fedora_msi-home
If there are errors because of a too small logical volume, it can be extended with for instance "lvextend -L +1G /dev/fedora_msi/home". Continue extending and running e2fsck until e2fsck is happy.

Make sure the file system fills up the logical volume:
resize2fs -p /dev/mapper/fedora_msi-home
Check again:
e2fsck -f /dev/mapper/fedora_msi-home
Now mount the home partition again:
mount /home
Looks good:
df -h|grep home
/dev/mapper/fedora_msi-home    160G    80G    72G  53% /home

Extending the swap partition

Now that there is enough space available in the LVM volume group, we can extend the logical volume and swap space. Still at the rescue prompt, turn off swap (this should work fine also in runlevel 5/desktop):
swapoff -a
Extend the logical volume:
lvextend -L +12G /dev/fedora_msi/swap
Then just create a new swap partition on the 18GB volume:
mkswap /dev/fedora_msi/swap
Turn on swap:
swapon -a
Check with "free" that your swap space is available. If you didn't already, now you can shutdown and reboot the computer.

Enabling hibernation in Fedora

Check that your laptop at least is able to suspend:
systemctl suspend
In Gnome, you can also click the Gnome status menu, then press the Alt key and the power off icon will turn into a suspend icon. On MSI GE60, the Fn+F10 key combination also suspends.

If it suspends fine and turns on successfully with the power button to where you had left it, there is at least a better chance that also hibernation works fine.

To enable hibernation, add your swap space to fstab (if it isn't already). Note that the partition UUID changed when creating a new swap space above. To get the UUID, issue:
blkid /dev/mapper/fedora_msi-swap
(on another laptop, the partition will be named something else, but you will find it under /dev/mapper)

The output will be something like:
/dev/mapper/fedora_msi-swap: UUID="eb721352-fe3c-4097-9d3f-042de0c98aa5" TYPE="swap"
 Add it to /etc/fstab in the following format, preferably as the last row after the other partitions:
UUID=eb721352-fe3c-4097-9d3f-042de0c98aa5 swap swap defaults 0 0
Check if initramfs contains support for resuming from hibernation:
lsinitrd|more
In the "dracut modules:" section, you should find "resume".

If not, then run dracut to regenerate initramfs for the current running linux kernel:
dracut -f -v
The output should now include the "resume" module.

Add the swap partition to Grub. In my case, I added it to /etc/default/grub:
GRUB_CMDLINE_LINUX="resume=/dev/disk/by-uuid/eb721352-fe3c-4097-9d3f-042de0c98aa5 rd.lvm.lv=fedora_msi/swap rd.lvm.lv=fedora_msi/root nouveau.modeset=0 rd.driver.blacklist=nouveau rhgb quiet"
Just add the "resume=/dev/disk/by-uuid/eb721352-fe3c-4097-9d3f-042de0c98aa5" with your own uuid of your swap partition. You know the uuid from the blkid command and fstab. Then write the new Grub configuration:
grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg

To be sure that you set everything alright in fstab and Grub, reboot once:
systemctl reboot
If you rebooted successfully, login and try to hibernate:
systemctl hibernate
Another nice sleep state:
systemctl hybrid-sleep
The latter is a nice sleep state for especially stationary laptops. If there is still power when resuming, the system wakes up quickly from suspend in RAM. If the battery and/or power is lost, then it will resume from hibernation (from disk).

Unfortunately Gnome doesn't support hibernation by default. You have to use your own shortcut or hibernate from command line. There is also a Gnome extension that currently seems to work fine[2]. It will add a hibernation icon to the Gnome Status menu. Using Alt modifier, you can also do a hybrid sleep.



[1] https://docs.fedoraproject.org/en-US/Fedora/23/html/Installation_Guide/sect-installation-gui-manual-partitioning-recommended.html
[2] https://extensions.gnome.org/extension/755/hibernate-status-button/