Posted On January 18, 2021

Enable Hibernation Option on Linux Mint 20

kimconnect 0 comments
blog.KimConnect.com >> Linux >> Enable Hibernation Option on Linux Mint 20

At first glance, one would be tempted to simply expand an existing a /swapfile as generated by Linux Mint installation defaults. Although that may be helpful in allocating more storage as augments for physical RAM, such /swapfile cannot be used toward hibernation resumes. A device or partition is required for hibernation to work correctly. Hence, these are the 2 options as prerequisites to enabling hibernation:

  1. Add a new physical disk as dedicated toward hibernation. When dealing with virtual machines, this would be the obvious choice.
  2. Resize an existing partition to reallocate storage toward a new partition. This is a risky move that could render the original partition as broken in worst-case scenarios.

Given the explanations above, we shall proceed with demonstrating this configuration.

Part 1: Enable hibernation menus and setting system to bypass memory checking

# Create the option to hibernate
sudo tee /etc/polkit-1/localauthority/90-mandatory.d/enable-hibernate.pkla <<'EOF'
[Enable hibernate]
Identity=unix-user:*
Action=org.freedesktop.login1.hibernate;org.freedesktop.login1.handle-hibernate-key;org.freedesktop.login1;org.freedesktop.login1.hibernate-multiple-sessions
ResultActive=yes
EOF

# Obtain path of systemd-logind.service
kim@kim-linux:~$ systemctl show -p FragmentPath systemd-logind.service
FragmentPath=/lib/systemd/system/systemd-logind.service

# Insert Line after [Service] to bypass memory checking
sudo sed -i '/^\[Service\].*/a Environment=SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK=1' /lib/systemd/system/systemd-logind.service

# Restart systemd-logind
sudo systemctl restart systemd-logind
sudo systemctl daemon-reload

kim@kim-linux:~$ systemctl restart systemd-logind
Warning: The unit file, source configuration file or drop-ins of systemd-logind.service changed on disk. Run 'systemctl daemon-reload' to reload units.

Part 2: Add a swap partition

# Discover physical disks on the system
root@kim-linux:/home/kim# lsblk -ido KNAME,TYPE,SIZE,MODEL
KNAME TYPE   SIZE MODEL
loop0 loop  97.9M 
sda   disk  14.9G KINGSTON_SNS4151S316GD
sdb   disk 232.9G Samsung_SSD_850_EVO_250GB

# List the partitions
root@kim-linux:/home/kim# parted -l
Error: /dev/sda: unrecognised disk label
Model: ATA KINGSTON SNS4151 (scsi)                                        
Disk /dev/sda: 16.0GB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags: 

Model: ATA Samsung SSD 850 (scsi)
Disk /dev/sdb: 250GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End    Size   File system  Name                  Flags
 1      1049kB  538MB  537MB  fat32        EFI System Partition  boot, esp
 2      538MB   250GB  250GB  ext4

# Initiating partitioning process
root@kim-linux:/home/kim# sudo parted /dev/sda
GNU Parted 3.3
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.

# Create partitioning table as 'gpt'
(parted) mklabel gpt

# Create primary partition using the entire disk
(parted) mkpart primary swap 0% 100% # Correct command would be mkpart primary linux-swap 0% 100%
parted: invalid token: linux-swap
# Check result
(parted) print                                                            
Model: ATA KINGSTON SNS4151 (scsi)
Disk /dev/sda: 16.0GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 
Number  Start   End     Size    File system  Name     Flags
 1      1049kB  16.0GB  16.0GB               primary

# Exit partitioning program
(parted) quit
Information: You may need to update /etc/fstab.

# Alternative command to perform the steps above as 1-liner, assuming gpt has been set on a disk without any partitions
parted -a optimal /dev/sda mkpart primary '0%' '100%'

# Check disk partitions
root@kim-linux:/home/kim# lsblk                                           
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
loop0    7:0    0  97.9M  1 loop /snap/core/10583
sda      8:0    0  14.9G  0 disk 
└─sda1   8:1    0  14.9G  0 part 
sdb      8:16   0 232.9G  0 disk 
├─sdb1   8:17   0   512M  0 part /boot/efi
└─sdb2   8:18   0 232.4G  0 part /

# Set swapfile variable - be certain that a valid value is given here
# In this example: /dev/sda1 has been formatted as swap prior to this
swapPartition=/dev/sda1
 
# Zero out first 16MB of disk to effectively clear its partitioning table
sudo dd if=/dev/zero of=$swapPartition count=4096 bs=4096
 
# Set appropriate permissions to secure the directory
chmod 600 $swapPartition 
 
# Create swapfile in the swapPartition directory
mkswap $swapPartition
 
#configure system to use $swapfile
swapon $swapPartition 
 
# Check swapPartition settings
swapon -s

root@kim-linux:/home/kim# swapon -s
Filename				Type		Size	Used	Priority
/swapfile                              	file    	16777212	0	-2
/dev/sda1                              	partition	15637500	0	-3
 
# Enable swapPartition to persist reboots
sudo echo "$swapPartition swap swap sw 0 0" >> /etc/fstab
 
# Check fstab
cat /etc/fstab

Part 3: replace swap file with swap partition

# Check for existence of swap files and partitions
root@kim-linux:/home/kim# swapon -s
Filename				Type		Size	Used	Priority
/swapfile                              	file    	16777212	0	-2
/dev/sda1                              	partition	15637500	0	-3

# Comment out /swapfile usage as hibernation cannot work with it
sed '/^#/! {/swapfile/ s/^/#/}' -i /etc/fstab

# Remove the /swapfile
rm /swapfile

# Restart swapping engine
swapPartition=/dev/sda1 # Specify swap partition
swapoff -a # turn off swapping
swapon $swapPartition # direct engine to use given swap partition

Part 4: enable startup resuming process to retrieve saved session and fix grub os_prober bug

# Unique identifier (UUID) is a 128 bit number of 32 hexadecimal digits, a constant value used to name partitions in Linux operating systems. Since this value is a property of the partition, it can be transferred to different systems without loosing data. Target systems often automatically create hard links as mount points (e.g. /dev/sda1) toward the partition's UUID. It's recommended that the UUID value is being used on system resuming, instead of partitioning mount points.

# Discover the swap partition's UUID by correlating discovered disks to mounted IDs
root@kim-linux:/home/kim# ls -l /dev/disk/by-uuid
total 0
lrwxrwxrwx 1 root root 10 Jan 17 16:53 2e561ffb-fc38-4a2f-a7e3-f72659fff171 -> ../../sda1
lrwxrwxrwx 1 root root 10 Jan 17 16:09 4249-C724 -> ../../sdb1
lrwxrwxrwx 1 root root 10 Jan 17 16:09 de2a0dd7-1aea-4db2-a62d-0bc739646c8e -> ../../sdb2

# Set resume to retrieve saved sessions from the swap partition
sudo su
swapUuid=/dev/disk/by-uuid/2e561ffb-fc38-4a2f-a7e3-f72659fff171
sed -i '/^GRUB_CMDLINE_LINUX_DEFAULT=*/ c\GRUB_CMDLINE_LINUX_DEFAULT=\"quiet splash resume='"$swapUuid"'\"' /etc/default/grub

# Fix grub boot delays
# Comment out last matching line of 30_os-prober (a bug)
file=/etc/grub.d/30_os-prober
lastMatchLineNumber=$(grep -n 'adjust_timeout' "$file" |tail -1|cut -f1 -d':')
sed -e "$lastMatchLineNumber s/^#*/#/" -i $file

# Edit grub
sudo vim /etc/default/grub
##### Set content similar to this #####
GRUB_DEFAULT=0
GRUB_HIDDEN_TIMEOUT=0
GRUB_TIMEOUT_STYLE=hidden
GRUB_HIDDEN_TIMEOUT_QUIET=true
GRUB_TIMEOUT=0
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=/dev/disk/by-uuid/2e561ffb-fc38-4a2f-a7e3-f72659fff171"
GRUB_CMDLINE_LINUX=""

# Update grub
sudo update-grub
sudo update-grub2
sudo update-initramfs -u

# Test Hibernation
sudo systemctl hibernate

The following steps deal with swap files, which is NOT helpful with hibernation. These are included here as reminder.

# Attempt to trigger hibernation
kim@kim-linux:~$ systemctl hibernate
Failed to hibernate system via logind: Not enough swap space for hibernation

# Check memory & swap
kim@kim-linux:~$ free -m
              total        used        free      shared  buff/cache   available
Mem:          32016        4179       24991        1204        2845       26243
Swap:          2047           0        2047

# Increase swap to 16GB
swapoff -a # turn off swapping
sudo dd if=/dev/zero of=/swapfile bs=1M count=16384 # copying zeros to swap with data size of 16GiB
sudo chmod 600 /swapfile # set correct permissions
sudo mkswap /swapfile # create swapfile
swapon -a # turn on swapping

# Check swapfile location
root@kim-linux:/home/kim# cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda2 during installation
UUID=de2a0dd7-1aea-4db2-a62d-0bc739646c8e /               ext4    errors=remount-ro 0       1
# /boot/efi was on /dev/sda1 during installation
UUID=4249-C724  /boot/efi       vfat    umask=0077      0       1
/swapfile                                 none            swap    sw              0       0

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

Linux Mint How To Remove Repository

Following is an exercise in removing erroneous repo config that would cause apt update to…

Uhhuh. NMI received for unknown reason 3d on CPU 4

Problem: Message from syslogd@linux03 at Aug 31 04:28:21 ... kernel:[273033.123489] Uhhuh. NMI received for unknown…

HAProxy on CentOS 7

# Install HAProxy 1.8 using SCL repo yum install centos-release-scl yum install rh-haproxy18-haproxy rh-haproxy18-haproxy-syspaths  …
Other project: DragonCoin.com