Archive

Archive for September, 2019

Enable Hibernate in Ubuntu 18.04

September 12th, 2019 No comments

Ref: https://askubuntu.com/questions/6769/hibernate-and-resume-from-a-swap-file/1132154#1132154

All credits goes to https://askubuntu.com/users/29219/anthony-o !

make it work with Ubuntu 18.04.

Make your /swapfile have at least the size of your RAM

sudo swapoff /swapfile
sudo dd if=/dev/zero of=/swapfile bs=$(cat /proc/meminfo | grep MemTotal | grep -oh '[0-9]*') count=1024 conv=notrun
sudo mkswap /swapfile
sudo swapon /swapfile

Note the UUID of the partition containing your /swapfile:

$ sudo findmnt -no SOURCE,UUID -T /swapfile
/dev/nvme0n1p5 20562a02-cfa6-42e0-bb9f-5e936ea763d0

Reconfigure the package uswsusp in order to correctly use the swapfile:

sudo dpkg-reconfigure -pmedium uswsusp
# Answer "Yes" to continue without swap space
# Select "/dev/disk/by-uuid/20562a02-cfa6-42e0-bb9f-5e936ea763d0" replace the UUID with the result from the previous findmnt command
# Encrypt: "No"

Edit the SystemD hibernate service using sudo systemctl edit systemd-hibernate.service and fill it with the following content:

[Service]
ExecStart=
ExecStartPre=-/bin/run-parts -v -a pre /lib/systemd/system-sleep
ExecStart=/usr/sbin/s2disk
ExecStartPost=-/bin/run-parts -v --reverse -a post /lib/systemd/system-sleep

Note the resume offset of your /swapfile:

$ sudo swap-offset /swapfile
resume offset = 34818

Configure Grub to resume from the swapfile by editing /etc/default/grub and modify the following line:

GRUB_CMDLINE_LINUX_DEFAULT="resume=UUID=20562a02-cfa6-42e0-bb9f-5e936ea763d0 resume_offset=34818 quiet splash"

Update Grub:

sudo update-grub

Update initramfs: sudo update-initramfs -u -k all

Create the following /etc/initramfs-tools/conf.d/resume:

RESUME=UUID=20562a02-cfa6-42e0-bb9e-5e936ea763d0 resume_offset=34816
# Resume from /swapfile

Update initramfs:

sudo update-initramfs -u -k all

Now you can hibernate with sudo systemctl hibernate.

One can also create those scripts:

    sudo tee /usr/local/bin/gotosleep <<EOF
    dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock
    sleep 2
    sudo /usr/sbin/s2both
    EOF
    sudo chmod +x /usr/local/bin/gotosleep
    sudo tee /usr/local/bin/gotohibernation <<EOF
    dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock
    sleep 2
    sudo systemctl hibernate
    EOF
    sudo chmod +x /usr/local/bin/gotohibernation

So you can sleep with gotosleep or hibernate with gotohibernation.

You must be able to execute sudo s2both, sudo s2ram and sudo systemctl hibernatewithout having to enter your password for the previous scripts to work.

You could do that for example by creating a powerdev group, add your current user to it, and configure the following sudoers config (edit it with sudo visudo -f /etc/sudoers.d/powerdev):

     %powerdev ALL=NOPASSWD: /usr/sbin/s2both, /usr/sbin/s2ram, /bin/systemctl hibernate

Documentation used:

Add ‘Hibernate’ Option in Power Menu in Ubuntu 18.04

Ref: http://ubuntuhandbook.org/index.php/2018/05/add-hibernate-option-ubuntu-18-04/

sudo gedit /etc/polkit-1/localauthority/50-local.d/com.ubuntu.enable-hibernate.pkla

When the file opens, paste following content and save it.

[Re-enable hibernate by default in upower]
Identity=unix-user:*
Action=org.freedesktop.upower.hibernate
ResultActive=yes

[Re-enable hibernate by default in logind]
Identity=unix-user:*
Action=org.freedesktop.login1.hibernate;org.freedesktop.login1.handle-hibernate-key;org.freedesktop.login1;org.freedesktop.login1.hibernate-multiple-sessions;org.freedesktop.login1.hibernate-ignore-inhibit
ResultActive=yes

Install the Gnome connector:

sudo apt-get install chrome-gnome-shell

Open Firefox, press CTRL+SHIFT+a, and install the “GNOME Shell integration” Add-On. Link to the add-on: https://addons.mozilla.org/en-US/firefox/addon/gnome-shell-integration/?src=search

Go to https://extensions.gnome.org/ and search for “Hibernate Status Button“, or click this link: https://extensions.gnome.org/extension/755/hibernate-status-button/. Turn “ON” the extension in the upper right corner.

Restart your computer and enjoy the new hibernate-button in the shut-down dialog!

For those who never installed a gnome extension, see this how to tutorial for details.

Categories: Bash, Linux, Ubuntu Tags:

How to Change Lid Close Action in Ubuntu 18.04 LTS

September 5th, 2019 No comments

Open terminal by pressing Ctrl+Alt+T or searching for “Terminal” from start menu. When it opens, run command:

sudo gedit /etc/systemd/logind.conf

When the files opens, uncomment the line #HandleLidSwitch=suspend by removing # in the beginning, and change the value to:

  • HandleLidSwitch=poweroff, shutdown / power off when lid is closed.
  • HandleLidSwitch=hibernate, hibernate when lid is closed (need to test if hibernate works).
  • HandleLidSwitch=ignore, do nothing.
  • HandleLidSwitch=suspend, suspend laptop when lid is closed.

Save the file and finally restart the Systemd service to apply changes via command:

systemctl restart systemd-logind.service
Categories: Bash, Linux, Ubuntu Tags: