Showing posts with label swapfile. Show all posts
Showing posts with label swapfile. Show all posts

28 August 2013

504. Swap file and hibernation on debian

I've got an SSD and 4 gb ram on my laptop, and have no desire to use to actual swap during normal use. However, I'd like to be able to let my laptop go into hibernation -- even suspend seems to be draining my battery pretty fast (at maybe a quarter of the rate of keeping the laptop on).

Does it make sense hibernating a laptop with SSD i.e. one which boots in ten seconds flat? Probably not. But we humans are greedy by nature.

So I need to set up a swap file, disable swapping, and see if I can use it for hibernation.

As usual, the best source of information is the archlinux wiki.

Anyway, having made it work and tried it out I would say this is really not worth the hassle IF you have an SSD. 

1. The swap file
This is my file system layout:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        39G   20G   18G  53% /
udev             10M     0   10M   0% /dev
tmpfs           380M  816K  380M   1% /run
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           760M  1.1M  759M   1% /run/shm
/dev/sda2       109G   71G   33G  69% /home

In my case I'd say that / has enough space to handle a swap file. My RAM is '4 gb' (really 3.71 Gb) -- according to this and this my swap should equal my RAM, which makes intuitive sense.

Since 4,000,000,000 byte is 3.76 gb, and 4000*1024*1024 (i.e. 4000M) is 3.91 Gb, I think 4000M should be ok:

su -
fallocate -l 4000M /swapfile
chmod 600 /swapfile
mkswap /swapfile
Setting up swapspace version 1, size = 4095996 KiB no label, UUID=2a8de3d1-14f6-473f-b40f-31618fd81169
echo 'vm.swappiness=1' >> /etc/sysctl.d/50-local.conf echo '/swapfile none swap defaults 0' >> /etc/fstab

2. "PM: Swap header not found"

To try it out without rebooting:
sudo sysctl -w vm.swappiness=1
sudo swapon /swapfile
pm-is-supported --hibernate 
echo $?
If you got 0, then you're good to go.
sudo pm-hibernate

Trying it out the first time I got "PM: swap header not found" and some weird behaviour. This has been mentioned e.g. here. A step-by-step guide is here:

Get the UUID of the partition on which the swapfile is located:
mount | grep " / "
/dev/disk/by-uuid/8adf424c-c375-4035-8d5d-181489b4461b on / type ext4 (rw,noatime,nodiratime,discard,errors=remount-ro,data=ordered)
sudo filefrag -v /swapfile | grep "First block:"

The latter command gave nothing, so I then did:
sudo filefrag -v /swapfile|less
Filesystem type is: ef53 File size of /swapfile is 4194304000 (1024000 blocks of 4096 bytes) ext: logical_offset: physical_offset: length: expected: flags: 0: 0.. 0: 7182336.. 7182336: 1: 1: 1.. 6143: 7182337.. 7188479: 6143: unwritten 2: 6144.. 8191: 7190528.. 7192575: 2048: 7188480: unwritten
So now we have the UUID (8adf424c-c375-4035-8d5d-181489b4461b) and the offset (7182336).
su -
echo "resume=UUID=8adf424c-c375-4035-8d5d-181489b4461b resume_offset=7182336" | sudo tee /etc/initramfs-tools/conf.d/resume

Edit /etc/default/grub and add the same line to GRUB_CMDLINE_LINUX_DEFAULT:
GRUB_CMDLINE_LINUX_DEFAULT="quiet resume=UUID=8adf424c-c375-4035-8d5d-181489b4461b resume_offset=7182336"
sudo update-grub
sudo update-initramfs -u


3. Hibernating.
sudo pm-hibernate

On my lenovo sl410 what I see is the screen go blank save for a blinking "-", and the little crescent light at the front of the laptop starting to blink (next to the battery light).

After 5-10 seconds the laptop turns off.

Hitting the power button starts up the laptop -- you get the bios screen, then the grub menu, and at this point you're thinking that the whole exercise has failed -- but it ends up booting into the same situation as you had when you did pm-hibernate i.e. it worked. I mean, this is how hibernation works -- but I had honestly never used anything other than suspend before, so...well..there's a first time for everything.

Overall, on a laptop with an SSD, resuming from hibernation is about as slow as a cold start -- with entering hibernation taking longer than a shutdown. On a laptop with a spinning disk this could presumably be worth it.