add blog post about change kernel params with ansible

This commit is contained in:
Pim Kunis 2023-06-19 10:09:11 +02:00
parent 46c2838e49
commit aac6399fac

View file

@ -0,0 +1,75 @@
---
layout: post
title: Using Ansible to alter Kernel Parameters
date: 2023-06-19 09:31:00 Europe/Amsterdam
categories: ansible grub linux
---
For months, I've had a peculiar problem with my laptop: once in a while, seemingly without reason, my laptop screen would freeze.
This only happened on my laptop screen, and not on an external monitor.
I had kind of learned to live with it as I couldn't find a solution online.
The only remedy I had was reloading my window manager, which would often unfreeze the screen.
Yesterday I tried Googling once more and I actually found [a thread](https://bbs.archlinux.org/viewtopic.php?id=246841) about it on the Arch Linux forums!
They talk about the same laptop model, the Lenovo ThinkPad x260, having the problem.
Fortunately, they also propose [a temporary fix](https://bbs.archlinux.org/viewtopic.php?pid=1888932#p1888932).
# Trying the Fix
Apparently, a problem with the Panel Self Refresh (PSR) feature of Intel iGPUs is the culprit.
According to the [Linux source code](https://github.com/torvalds/linux/blob/45a3e24f65e90a047bef86f927ebdc4c710edaa1/drivers/gpu/drm/i915/display/intel_psr.c#L42), PSR enables the display to go into a lower standby mode when the sytem is idle but the screen is in use.
These lower standby modes can reduce power usage of your device when idling.
This all seems useful, except when it makes your screen freeze!
The proposed fix disables the PSR feature entirely.
To do this, we need to change a parameter to the Intel Graphics Linux Kernel Module (LKM).
The LKM for Intel Graphics is called `i915`.
There are [multiple ways](https://wiki.archlinux.org/title/Kernel_parameters) to change kernel parameters, but I chose to edit my Grub configuration.
First, I wanted to test whether it actually works.
When booting into my Linux partition via Grub, you can press `e` to edit the Grub definition.
Somewhere there, you can find the `linux` command which specifies to boot Linux and how to do that.
I simply appended the option `i915.enable_psr=0` to this line.
After rebooting, I noticed my screen no longer freezes!
Success!
# Persisting the Fix
To make the change permanent, we need to permanently change Grub's configuration.
One way to do this, is by changing Grub's defaults in `/etc/default/grub`.
Namely, the `GRUB_CMDLINE_LINUX_DEFAULT` option specifies what options Grub should pass to the Linux kernel by default.
For me, this is a nice solution as the problem exists for both Linux OSes I have installed.
I changed this option to:
```ini
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash i915.enable_psr=0"
```
Next, I wanted to automate this solution using Ansible.
This turned out to be quite easy, as the Grub configuration looks a bit like an ini file (maybe it is?):
```yaml
- name: Edit grub to disable Panel Self Refresh
become: true
ini_file:
path: /etc/default/grub
section: null
option: "GRUB_CMDLINE_LINUX_DEFAULT"
value: '"quiet splash i915.enable_psr=0"'
no_extra_spaces: true
notify: update grub
```
Lastly, I created the `notify` hook to update the Grub configuration:
```yaml
- name: update grub
become: true
command:
cmd: update-grub
```
# Conclusion
It turned out to be quite easy to change Linux kernel parameters using Ansible.
Maybe some kernel gurus have better ways to change parameters, but this works for me for now.
As a sidenote, I started reading a bit more about NixOS and realised that it can solve issues like these much more nicely than Ansible does.
I might replace my OS with NixOS some day, if I manage to rewrite my Ansible for it.