Hibernation allows the system to suspend whatever that was in the RAM into the disk. This allows the computer to completely shutdown while keeping whatever that was on your computer before shutting down. Think of it as putting your computer to sleep but The whole computer is not using any power. More information about hibernation can be found in the Arch Wiki.
The article will go through much that was already on the Arch Wiki. It is highly recommended to go through the wiki first since it is a good skill to have to be able to read the wiki. Come back here if there are some part that wasn't clear. There might be something here that could be helpful.
The swap file is where the RAM is stored into when the computer hibernates. Therefore it is necessary to have it if you are using the hibernate feature. (A swap partition can also be used).Setting up the swap file is simple, just type the following commands:
The following command will create a swap file at /, named 'swapfile', with a size of 512Mb. Change these parameters to however you want, but make sure you will have enough swap size.
# dd if=/dev/zero of=/swapfile bs=1M count=512 status=progress
The following commands will set the permission of the swapfile, format the swapfile, and the activate the swap file.
# chmod 600 /swapfile # mkswap /swapfile # swapon /swapfile
Now we will configure your fstab (located at /etc/fstab so that the swap file will be mounted whenever your start your computer. Add in the following line.
# /swapfile none swap defaults 0 0
If you are only using the swap for hibernation it might be good to reduce the swappiness. To set the swappiness permanently do:
# echo "vm.swappiness=10" > /etc/sysctl.c/99-swappiness.conf
Now we need to fill in the kernel parameters in the bootloader so that the computer knows where to get to your swap file to get everything backinto your RAM.
The kernel parameter is set in your bootloader entries and it can be found in /boot/loader/entries/*.conf. It should look something like this:
# title Arch Linux # linux /vmlinuz-linux # initrd /intel-upre.img # initrd /initramfs-linux.img # options root=PARTUUID=2c51feff-c150-4b36-a6ee-95f04ca51773 rw intel_pstate=no_hwp
We will be adding these two lines.
# options resume=UUID=resume_device # options resume_offset=swap_file_offset
resume_device, and swap_file_offset can be determine using these following commands, respectively.
# findmnt -no UUID -T /swapfile
# filefrag -v /swapfile | awk '{ if($1=="0:"){print substr($4, 1, length($4)-2)} }'
Adding the resume_device, and swap_file_offset, your bootloader entries config should look like this:
# title Arch Linux # linux /vmlinuz-linux # initrd /intel-upre.img # initrd /initramfs-linux.img # options root=PARTUUID=2c51feff-c150-4b36-a6ee-95f04ca51773 rw intel_pstate=no_hwp # options resume=UUID=03d0b18c-15e7-4c54-a2df-7332a878949a # options resume_offset=77189120
Now you just have to add the resume hook in your /etc/mkinitcpio.conf file and regenerate the initramf. Add the resume hook in the mkinitcpio.conf file like so:
# HOOKS=(base udev autodetect keyboard modconf block filesystems resume fsck)
# mkinitcpio -p linux
That should be it! You should now be able to hibernate.