iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐧

Enabling Hibernation on ThinkPad T14 Gen3 + Xubuntu 22.10

に公開1件

Hibernation

After reading Hibernation on Ubuntu 22.04 - Journal InTime(2023-01-03), I noticed that while my ThinkPad T14 Gen3 isn't too bad, it does lose a fair amount of battery during suspend, so I decided to try setting up hibernation myself.

The hibernation setup procedure is exactly as described in the blog post above:

# swapoff -a
# grep -q swapfile/etc/fstab ||  echo "/swapfile  none  swap  sw  0  0" >> /etc/fstab
# fallocate -l 32G /swapfile
# chown 0 /swapfile
# chmod 0600 /swapfile
# mkswap /swapfile
# swapon /swapfile
# findmnt / -o UUID
UUID
90263320-8701-4639-b7b9-9705677d9c7a
# filefrag -v /swapfile |grep " 0:"| awk '{print $4}'
34816..

Edit /etc/default/grub:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=UUID=90263320-8701-4639-b7b9-9705677d9c7a resume_offset=34816"
# update-grub

Since I have 24GB of RAM, I set the swap to 32GB. It feels a bit scary to specify the physical location of a swap file on the file system—I wonder if it's really okay. I can't help but think that preparing a dedicated swap partition might be safer.

Testing hibernation:

# systemctl hibernate

If it hibernates properly and resumes with the power button, it's all good.

However, upon resume, the screen remained pitch black, and [drm] *ERROR* Fault errors on pipe A: 0x00000080 was being output continuously to the syslog.

In that state, pressing Ctrl-Alt-F1 followed by Alt-F7 displays the login screen and stops the logs, but I wanted to stop it for good because the logs accumulate at a massive rate and eat up disk space.

Apparently, this was fixed in kernel 6.0.3, so I downloaded and installed 6.0.19 from the mainline PPA, and it was fixed.

I wonder if I should actually be using 6.1, though...

Hibernating when the lid is closed

Xubuntu does not allow you to set hibernation as the behavior when the lid is closed.
Perhaps there is a way to enable it, but I couldn't find one after a quick search, so I'll manage it myself.

Since I just need to catch the LID event, I checked the input devices:

% sudo evtest
No device specified, trying to scan all of /dev/input/event*
Available devices:
/dev/input/event0:	Sleep Button
/dev/input/event1:	Lid Switch
/dev/input/event10:	sof-hda-dsp Mic
/dev/input/event11:	sof-hda-dsp Headphone
/dev/input/event12:	sof-hda-dsp HDMI/DP,pcm=3
/dev/input/event13:	sof-hda-dsp HDMI/DP,pcm=4
/dev/input/event14:	sof-hda-dsp HDMI/DP,pcm=5
/dev/input/event15:	Integrated Camera: Integrated C
/dev/input/event16:	rkremap
/dev/input/event2:	Power Button
/dev/input/event3:	AT Translated Set 2 keyboard
/dev/input/event4:	ThinkPad Extra Buttons
/dev/input/event5:	ELAN0676:00 04F3:3195 Mouse
/dev/input/event6:	TPPS/2 Elan TrackPoint
/dev/input/event7:	Video Bus
/dev/input/event8:	Intel HID events
/dev/input/event9:	ELAN0676:00 04F3:3195 Touchpad
Select the device event number [0-16]: ^C

/dev/input/event1 looks like the right one. With my custom gem rkremap installed, I was able to catch the lid-closed event and execute systemctl hibernate using something like this:

% sudo ruby -rrkremap -e 'dev = Rkremap::Evdev.new("/dev/input/event1"); dev.grab; loop{e = dev.read_event; system("systemctl hibernate") if e.ev_type == 5 && e.value == 1 }'

Since it's using grab, it doesn't trigger the regular power management's suspend or anything. I suspect there might be a simpler way, but this will do.

Suspend-then-hibernate

Hibernation takes longer to resume compared to suspend, so hibernating every time the lid is closed is not ideal. I found a feature called suspend-then-hibernate that hibernates after being in suspend for a while, so I'll try that.

% sudo ruby -rrkremap -e 'dev = Rkremap::Evdev.new("/dev/input/event1"); dev.grab; loop{e = dev.read_event; system("systemctl suspend-then-hibernate") if e.ev_type == 5 && e.value == 1 }'

The time between suspending and hibernating can be configured with HibernateDelaySec in /etc/systemd/sleep.conf. The default is apparently 120min. That's a bit long, so I changed it to 30min.

Hibernation waking up on its own

I thought I was finished! ...but sometimes, even though the lid was closed, the power would come on for some reason, drain the battery, and then it would go back into suspend.

After testing systemctl hibernate several times, I found that it would resume immediately after hibernating about once every 4 or 5 times.

I referred to the Wakeup triggers - ArchWiki and tried the following fix.

Listing the enabled wakeup triggers for S4 (hibernation):

% cat /proc/acpi/wakeup | grep 'S4.*enabled'
PEG0      S4    *enabled   pci:0000:00:06.0
TXHC      S4    *enabled   pci:0000:00:0d.0
TDM0      S4    *enabled   pci:0000:00:0d.2
TDM1      S4    *enabled   pci:0000:00:0d.3
TRP0      S4    *enabled   pci:0000:00:07.0
TRP2      S4    *enabled   pci:0000:00:07.2
AWAC      S4    *enabled   platform:ACPI000E:00
LID       S4    *enabled   platform:PNP0C0D:00

LID is the lid, so it's fine for that to be enabled. I wasn't sure about the others, so I disabled all of them using echo XXX > /proc/acpi/wakeup.

After testing in this state, it seems it no longer wakes up on its own, so I'll make this setting persistent.
It seems you can set up persistence by tweaking udev, but it looked complicated, so I created a startup script instead.

/etc/init.d/disable-wakeup:

#!/bin/sh
### BEGIN INIT INFO
# Default-Start: 2 3 4 5
### END INIT INFO
cat /proc/acpi/wakeup | grep 'S4.*enabled' | awk '{print $1}' | grep -v LID | while read x; do
  echo "$x" > /proc/acpi/wakeup
done

I created this file and ran sudo update-rc.d disable-wakeup defaults so it runs automatically when Linux starts.

I've been using it for a while and it seems fine.
Just in case, I've also configured the power settings to suspend after 15 minutes of inactivity when on battery power.

Discussion

HORIE SeiichiHORIE Seiichi

ファイルシステム上にあるスワップファイルの物理位置を指定するのってなんか怖いんだけど大丈夫なのかな。

fallocateを使うべきではない、というのが原則のようです。それを踏まえた上で、swaponコマンドはスワップファイルが不連続の場合にエラーメッセージを出して異常終了するため、「終わりよければ全てよし」と考えています。

1