NVIDIA suspend-then-hibernate

These notes are for an optimus laptop with an Intel Iris card and a GeForce RTX 3050. YMMV.

I found a systemd issue for this after typing it up, lol:
https://github.com/systemd/systemd/issues/27559

After installing X windows and configuring it to use the proprietary nvidia drivers, the system was hanging on suspend and hibernate. It turns out that NVIDIA requires a couple services be enabled that prepare the graphics card to sleep, nvidia-suspend and nvidia-hibernate. There is also an nvidia-resume service, but this apparently has been superceded by a system-sleep script (/usr/lib/systemd/system-sleep/nvidia), which is run during every sleep state change. Alert readers may note that there’s no suspend-then-hibernate service. As one might expect, the suspend-then-hibernate routine still hangs.

Here is the basic flow:
– the nvidia power management services use the Before= setting to ensure they run the /usr/bin/nvidia-sleep.sh helper script to prepare the GPU before the main systemd pm service is run
– the systemd power management services call the systemd tool /usr/lib/systemd/systemd-sleep
– systemd-sleep calls all scripts in the system-sleep directory /usr/lib/systemd/system-sleep for each state change

The nvidia-sleep script on sleep does:
– saves the current virtual terminal #
– switches to an unused vt (63)
– echoes either suspend or hibernate to /proc/driver/nvidia/suspend

The nvidia-sleep script is also called on resume, and in that case:
– echoes resume to proc file
– switches vt to saved vt #

The strategy I used to get it to work is based on https://forums.developer.nvidia.com/t/systemds-suspend-then-hibernate-not-working-in-nvidia-optimus-laptop/213690

This modifies the nvidia system-sleep script, which currently only handles the resume case, and has it also handle the suspend and hibernate cases. This duplicates the functionality of the nvidia-* service scripts, but is also run for the sustemd-then-hibernate case, so the nvidia-* scripts can all be disabled.

The problem with this script out of the box, at least with systemd 254.6, is that systemd-sleep prevents the virtual terminal from being switched in the system-sleep context for suspend-then-hibernate. There is a call to freeze_thaw_user_slice before suspend is executed that is the most obvious difference in the s2h case vs the suspend only case.

https://github.com/systemd/systemd/blob/main/src/sleep/sleep.c

In order to work around this, I had to make the following changes:
– pass along the command given by system-sleep as arg $2 to the system-sleep scripts
– modify the nvidia-sleep command to ignore resumes for the s2h command
– modify the systemd-suspend-then-hibernate.service file to run the nvidia-sleep script before and after systemd-sleep to control the suspend and resume outside the systemd-sleep lock

There are some issues / areas of improvement with this, namely that modifying systemd-suspend-then-hibernate.service directly is prone to breakage, as is modifying the nvidia scripts. Additionally, the GPU is not notified of the hibernate transition, only the initial suspend.

A couple systemd service scripts could probably be created to run before and after the systemd-suspend-then-hibernate service, so that would not need to be run. /usr/lib/systemd/system-sleep/nvidia would still need to be modified to avoid running the nvidia-sleep resume script inside the system-sleep context during a suspend-then-hibernate action.

Edit: Here’s a script to do that: /etc/systemd/system/systemd-suspend-then-hibernate.service.d/nvidia.conf

[Service]
ExecStartPre=/usr/bin/nvidia-sleep.sh suspend
ExecStartPost=/usr/bin/nvidia-sleep.sh resume

Influx setup

install influx repo and rpms
> influx setup # creates a bucket, user/pass, and operator token
> influx auth ls # shows the “operator token”
> influx auth create –all-access … # creates an all access token which is safer to use than operator, though it probably doesnt matter if you just have one database…

If you are using a v1 api application, you need to create a username / password for it (it can be the same as the v2 username)
> influx v1 authorization create –username <username> –password <pass> –read-bucket <bucket id> –write-bucket <bucket id>

bucket is the same as database name

Combining audio, uploading to youtube

Combining audio:
ffmpeg -f concat -safe 0 -i <(echo "file '$fn1'"; echo "file '$fn2'") -c copy $fn_out fn1 and fn2 can't be relative paths. Can use readlink -f to resolve any relative paths. Creating videos from images and audio:
for x in p*; do
out=$x/video.mp4
mp3=$(ls $x/*mp3)
cover=$x/cover.jpg

echo ffmpeg -loop 1 -framerate 2 -i $cover -i “$mp3” -c:v libx264 -preset medium -tune stillimage -crf 18 -c:a aac -b:a 192k -shortest -pix_fmt yuv420p -movflags +faststart $out
done

Upload
https://github.com/tokland/youtube-upload

Problems with no_std, rust, staticlib and compiler_builtins

Core requires that certain memory functions exist, which are normally provided by libc. These can be provided by compiler_builtins. For some reason compiler_builtins doesn’t always build the memory functions when using the -Z build_std. In order to get it work, you need to add -Z build-std-features=compiler-builtins-mem

Otherwise you’ll either get errors like:

multiple rlib candidates for `compiler_builtins` found

or that memcpy, strlen, etc are not defined.

There used to be (is?) code to automatically enable that feature on no_std targets, but for some reason it wasn’t working for me.

Disabling Framebuffer on Arch Linux Install under QEMU curses

You can add these to the kernel parameters:

vga=normal nofb nomodeset video=vesafb:off

The kernel parameters can be accessed by switching to the serial console (alt-2) and hitting TAB on the 1st install option.

Adding console=ttyS0 didn’t work for my set up. It does look like Arch has a login set up on ttyS1 so it may be possible to map the QEMU serial console to that port.

Google Keep Clone

I’d like to get a modified google keep clone going… I set up a wiki to try some ideas, but I think a WordPress admin redo might actually be the best? Basically I’d like a blog like stream where it’d be really easy to add new links and images, as well as longer posts, both chronological and topic oriented. Sort of a bookmark manager, meme keeper, journal, and wiki all in one.

Some interesting WordPress plugins that could help:
– https://wordpress.org/plugins/visual-link-preview/

Could have different post types. I think the new WP themes have different post types already, and different ways of displaying them:
– link
– image
– journal
– robust tag page?

Writing plugins is pretty easy as well.

https://github.com/Automattic/wp-calypso

Actually I wonder if any social networks have similar features… Obv. they are mostly externally facing, but I wonder if you could use it like a blog. Perhaps the tagging and hyperlinking might be hard.