You are not logged in.

#1 2019-06-10 22:52:19

CarbonChauvinist
Member
Registered: 2012-06-16
Posts: 413
Website

[Solved] Dracut - Rebuild Initramfs on Linux Update

I'm playing with using dracut and I've created a new initramfs-linux-dracut.img so I can test and still use the standard mkinitcpo to automatically generate the default initramfs-linux.img

I've been using the following, which is working well for my needs:

# dracut -H --lz4 -f /boot/initramfs-linux-dracut.img 

I'm trying to figure out how to regenerate the dracut initramfs when the kernel has just been updated without rebooting first.

For instance I just updated linux from 5.1.7.arch-1 to 5.1.8.arch1-1 and tried to regenerate dracut initramfs.

$ sudo pacman -Syu
...
:: Starting full system upgrade...
resolving dependencies...
looking for conflicting packages...

Package (8)             Old Version    New Version    Net Change  Download Size

core/glib2              2.60.3-1       2.60.3-2        -0.06 MiB       2.39 MiB
extra/libevdev          1.6.0-1        1.7.0-1          0.00 MiB       0.05 MiB
extra/libmm-glib        1.10.0-1       1.10.2-1         0.79 MiB       0.38 MiB
core/linux              5.1.7.arch1-1  5.1.8.arch1-1    0.00 MiB      71.73 MiB
core/linux-lts          4.19.48-1      4.19.49-1        0.00 MiB      74.95 MiB
core/linux-lts-docs     4.19.48-1      4.19.49-1        0.00 MiB       6.19 MiB
core/linux-lts-headers  4.19.48-1      4.19.49-1        0.00 MiB       8.74 MiB
extra/modemmanager      1.10.0-1       1.10.2-1         0.09 MiB       1.06 MiB

Total Download Size:   165.51 MiB
Total Installed Size:  321.82 MiB
...

# dracut -H --lz4 -f /boot/initramfs-linux-dracut.img 5.1.8.arch1-1
dracut: Cannot find module directory /lib/modules/5.1.8.arch1-1/
dracut: and --no-kernel was not specified

# dracut -H --lz4 -f /boot/initramfs-linux-dracut.img 5.1.8.arch1-1-ARCH
dracut: Cannot find module directory /lib/modules/5.1.8.arch1-1-ARCH/
dracut: and --no-kernel was not specified

# dracut -H --lz4 -f /boot/initramfs-linux-dracut.img --kver 5.1.8.arch1-1
dracut: Cannot find module directory /lib/modules/5.1.8.arch1-1/
dracut: and --no-kernel was not specified

# dracut -H --lz4 -f /boot/initramfs-linux-dracut.img --kver 5.1.8.arch1-1-ARCH
dracut: Cannot find module directory /lib/modules/5.1.8.arch1-1-ARCH/
dracut: and --no-kernel was not specified

Do I have to reboot in order to do this? The module directory it complains about is there:

$ ls -al /lib/modules
total 264
drwxr-xr-x   6 root root   4096 Jun 10 18:05 .
drwxr-xr-x 201 root root 245760 Jun 10 18:05 ..
drwxr-xr-x   4 root root   4096 Jun 10 18:05 4.19.49-1-lts
drwxr-xr-x   3 root root   4096 Jun 10 18:05 5.1.8-arch1-1-ARCH
drwxr-xr-x   2 root root   4096 Jun 10 18:05 extramodules-4.19-lts
drwxr-xr-x   2 root root   4096 Jun 10 18:05 extramodules-ARCH

I understand that I'm still ?technically on the old kernel until reboot?:

$ uname -a
Linux 5510 5.1.7-arch1-1-ARCH #1 SMP PREEMPT Tue Jun 4 15:47:45 UTC 2019 x86_64 GNU/Linux

but feel like this should be possible, as obviously mkinitcpio is doing this, and I'm just missing something obvious.

Last edited by CarbonChauvinist (2019-06-20 21:22:37)


"the wind-blown way, wanna win? don't play"

Offline

#2 2019-06-10 23:09:54

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [Solved] Dracut - Rebuild Initramfs on Linux Update

I use a pacman hook:

[trigger]
operation = upgrade
type = package
target = linux-jwr

[action]
description = regenerate initrd after kernel build
depends = dracut
when = posttransaction
exec = /usr/share/libalpm/scripts/dracut-jwr

And the script uses --kver to point to the correct modules. Your third invocation should work. I don't have access to my laptop at the moment, but I don't see any obvious flaw.

edit: the script just generates this line:

dracut -H -f --no-hostonly-cmdline --kver 5.1.7-1-JWR /boot/initramfs-dracut.img

It works without rebooting into the newly installed kernel.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2019-06-10 23:29:55

progandy
Member
Registered: 2012-05-17
Posts: 5,190

Re: [Solved] Dracut - Rebuild Initramfs on Linux Update

Look carefully, the package version and the kernel version are subtly different. There is a dash instead of a dot in addition to the appended -ARCH.

5.1.8.arch1-1
5.1.8-arch1-1-ARCH

Last edited by progandy (2019-06-10 23:31:43)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

#4 2019-06-10 23:31:17

CarbonChauvinist
Member
Registered: 2012-06-16
Posts: 413
Website

Re: [Solved] Dracut - Rebuild Initramfs on Linux Update

@jasonwryan Thanks, this is very helpful (and I'm definitely going to go the hook route once I feel comfortable with the manual process). So, just to confirm -- your script isn't hard-coded to that kernel version when rebuilding (i.e. 5.1.7-1-JWR) and instead you have some logic that parses out and uses the updated kernel's version/name?

@progandy, Wow, I knew it had to be something I wasn't seeing - thanks a lot - I'm sure this is the issue. I'll verify and update this thread on the next linux update.

Last edited by CarbonChauvinist (2019-06-10 23:39:47)


"the wind-blown way, wanna win? don't play"

Offline

#5 2019-06-10 23:51:24

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [Solved] Dracut - Rebuild Initramfs on Linux Update

CarbonChauvinist wrote:

@jasonwryan Thanks, this is very helpful (and I'm definitely going to go the hook route once I feel comfortable with the manual process). So, just to confirm -- your script isn't hard-coded to that kernel version when rebuilding (i.e. 5.1.7-1-JWR) and instead you have some logic that parses out and uses the updated kernel's version/name?

Yes, I just parse pacman -Q. There is probably a better way to do it, but it does the job tongue


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#6 2019-06-12 20:31:50

fjvinal
Member
From: Madrid
Registered: 2012-06-23
Posts: 45

Re: [Solved] Dracut - Rebuild Initramfs on Linux Update

This works for me:

dracut -H -f --no-hostonly-cmdline --kver $(ls /lib/modules| grep ARCH) /boot/initramfs-linux.img

Offline

#7 2019-06-12 20:40:51

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [Solved] Dracut - Rebuild Initramfs on Linux Update

You really should not parse `ls`. And that relies on only one kernel having ARCH in its name...


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#8 2019-06-12 21:32:01

progandy
Member
Registered: 2012-05-17
Posts: 5,190

Re: [Solved] Dracut - Rebuild Initramfs on Linux Update

I would probably use something like this for a universal hook. Converting the package version would require knowledge on which elements have to be changed.
This way, I can get away with the assumption that the kernel package creates a copy of the kernel image in /usr/lib/modules/${KERNELVERSION}/vmlinuz

pacman -Qql ${kernelpackage} |  sed -n 's#^/usr/lib/modules/\([^/]*\)/vmlinuz$#\1#p'

The official mkinitcpio hooks have the version string hardcoded.

A dynamic hook specifically for the linux package could use this:

expac -Q "%v-ARCH" linux | sed 's/\.arch/-arch/'

Edit: Improved with ideas from fjvinal and jasonwryan.

Last edited by progandy (2019-06-12 22:26:25)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

#9 2019-06-12 21:38:32

fjvinal
Member
From: Madrid
Registered: 2012-06-23
Posts: 45

Re: [Solved] Dracut - Rebuild Initramfs on Linux Update

A bit ugly, but works:

dracut -H -f /boot/initramfs-linux.img $(pacman -Ql linux|grep /usr/lib/modules|grep vmlinuz| cut -d '/' -f 5)

Offline

#10 2019-06-12 21:53:07

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [Solved] Dracut - Rebuild Initramfs on Linux Update

Only because I like complicating things...

#!/usr/bin/bash
type expac > /dev/null 2>&1 || { printf "%s\n" "No expac, bailing..."; exit 1;}
kver=$(expac -Q "%v" linux-jwr)-JWR
imgname="/boot/initramfs-dracut.img"

/usr/bin/dracut -q --kver "${kver}" "$imgname"

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#11 2019-06-12 22:09:23

fjvinal
Member
From: Madrid
Registered: 2012-06-23
Posts: 45

Re: [Solved] Dracut - Rebuild Initramfs on Linux Update

A pacman hook:

[Trigger]
Operation = Upgrade
Type = Package
Target = linux

[Action]
Description = regenerate initrd after kernel build
Depends = dracut
When = PostTransaction
Exec = /usr/bin/bash -c "dracut -H -f /boot/initramfs-linux.img $(pacman -Ql linux|grep /usr/lib/modules|grep vmlinuz| cut -d '/' -f 5)"

Last edited by fjvinal (2019-06-12 22:11:23)

Offline

#12 2019-06-13 10:48:51

lquidfire
Member
From: South Africa
Registered: 2017-07-26
Posts: 53

Re: [Solved] Dracut - Rebuild Initramfs on Linux Update

Thanks all for your input - I have been watching silently and came up with the following last night. Please give your input on the below snippets (first time to write a Bash script, use `awk` and `sed` and to write a Pacman hook) - after all, this is the Newbie Corner and I'd be happy to learn smile

#!/bin/bash
# Get the version number of the currently installed Linux kernel and store it in a variable
linuxver="$(pacman -Q linux | awk '{print $2}' | sed 's/.arch/-arch/')-ARCH"

# run dracut to generate a initramfs (source: https://bbs.archlinux.org/viewtopic.php?id=247015)
sudo dracut -H -f --no-hostonly-cmdline --kver $linuxver /boot/initramfs-dracut.img

And the Pacman hook:

[Trigger]
Operation = Install
Operation = Upgrade
Type = Package
Target = linux

[Action]
Description = Generate initrd with dracut after kernel upgrade
Depends = dracut
When = PostTransaction
Exec = /usr/share/libalpm/scripts/dracut-ejjl

Last edited by lquidfire (2019-06-13 10:58:32)

Offline

#13 2019-06-13 17:24:41

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [Solved] Dracut - Rebuild Initramfs on Linux Update

grep and awk are redundant:

pacman -Q linux | awk '{sub(/.arch/,"-arch"); print $2}'

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#14 2019-06-13 21:37:20

sepplgruber
Member
From: Germany
Registered: 2019-06-13
Posts: 1

Re: [Solved] Dracut - Rebuild Initramfs on Linux Update

lquidfire wrote:

And the Pacman hook:

[Trigger]
Operation = Install
Operation = Upgrade
Type = Package
Target = linux

[Action]
Description = Generate initrd with dracut after kernel upgrade
Depends = dracut
When = PostTransaction
Exec = /usr/share/libalpm/scripts/dracut-ejjl

With this hook, dracut will regenerate the initramfs when linux is updated. As far as I know the mkinitcpio pacman hook also runs when other packages are updated (eg systemd, lvm2, cryptsetup ...). Is this necessary in order to get a working initramfs? I think this could easily be added with more packages listed as Targets

Offline

#15 2019-06-15 13:36:05

lquidfire
Member
From: South Africa
Registered: 2017-07-26
Posts: 53

Re: [Solved] Dracut - Rebuild Initramfs on Linux Update

Thank you, Jason, I have changed that and will look more deeply into awk.

Thank you, too, sepplgruber - that is a fair point and I will look into that!

Offline

#16 2019-06-20 21:21:40

CarbonChauvinist
Member
Registered: 2012-06-16
Posts: 413
Website

Re: [Solved] Dracut - Rebuild Initramfs on Linux Update

Marking this as solved. Thanks to all for the insight and examples -- really helpful. Thanks to those, I've been able to set up some pacman hooks that work really well for my needs.


"the wind-blown way, wanna win? don't play"

Offline

#17 2020-01-05 09:54:40

Moo-Crumpus
Member
From: Hessen / Germany
Registered: 2003-12-01
Posts: 1,487

Re: [Solved] Dracut - Rebuild Initramfs on Linux Update

Do you mind sharing them with us?


Frumpus addict
[mu'.krum.pus], [frum.pus]

Offline

#18 2020-01-05 10:28:58

mxfm
Member
Registered: 2015-10-23
Posts: 163

Re: [Solved] Dracut - Rebuild Initramfs on Linux Update

sepplgruber wrote:
lquidfire wrote:

And the Pacman hook:

[Trigger]
Operation = Install
Operation = Upgrade
Type = Package
Target = linux

[Action]
Description = Generate initrd with dracut after kernel upgrade
Depends = dracut
When = PostTransaction
Exec = /usr/share/libalpm/scripts/dracut-ejjl

With this hook, dracut will regenerate the initramfs when linux is updated. As far as I know the mkinitcpio pacman hook also runs when other packages are updated (eg systemd, lvm2, cryptsetup ...). Is this necessary in order to get a working initramfs? I think this could easily be added with more packages listed as Targets

Yes. Depending on the setup intel-ucode/amd-ucode are also needed (in addition to cryptsetup/lvm).

Offline

#19 2020-01-13 14:32:33

Moo-Crumpus
Member
From: Hessen / Germany
Registered: 2003-12-01
Posts: 1,487

Re: [Solved] Dracut - Rebuild Initramfs on Linux Update

... what is the content of /usr/share/libalpm/scripts/dracut-ejjl?


Frumpus addict
[mu'.krum.pus], [frum.pus]

Offline

#20 2020-01-13 17:54:49

CarbonChauvinist
Member
Registered: 2012-06-16
Posts: 413
Website

Re: [Solved] Dracut - Rebuild Initramfs on Linux Update

@Moo-Crumpus

I can certainly share mine when I'm back home in front of my box, but mine was not perfect - there were times were the name wasn't parsed correctly and I had to manually rebuild dracut (normally when arch1-1 or arch2-1 or something came up) on a kernel upgrade.

I'd suggest looking at this page, as @krathalan's approach (quoted below) seems like it would be more robust.

~/.local/bin/90-dracut-linux.script
#!/bin/bash

args=('-H' '--no-hostonly-cmdline')

while read -r line; do
    if [[ $line = usr/lib/modules/+([^/])/pkgbase ]]; then
        mapfile -O ${#pkgbase[@]} -t pkgbase < "/$line"
        kver=${line#"usr/lib/modules/"}
        kver=${kver%"/pkgbase"}
    fi
done

dracut "${args[@]}" -f /boot/initramfs-"${pkgbase[@]}".img --kver "${kver[@]}"
dracut -f /boot/initramfs-"${pkgbase[@]}"-fallback.img --kver "${kver[@]}"
/etc/pacman.d/hooks/90-dracut-linux.hook
[Trigger]
Type = File
Operation = Install
Operation = Upgrade
Target = usr/lib/modules/*/pkgbase

[Action]
Description = Updating linux initcpios (with dracut!)...
When = PostTransaction
Exec = /home/username/.local/bin/90-dracut-linux.script
NeedsTargets
krathalan wrote:

You will also have to copy the new vmlinuz kernel file from /usr/lib/modules/[kver]/vmlinuz to /boot/vmlinuz-linux every kernel upgrade, as the linux package is no longer responsible for that -- 90-mkinitcpio-install.hook is. From the arch-general mailing list:

I've meant to incorporate this strat in my hook -- just haven't gotten around to it yet.

Last edited by CarbonChauvinist (2020-01-13 18:03:49)


"the wind-blown way, wanna win? don't play"

Offline

#21 2020-02-05 15:05:51

Moo-Crumpus
Member
From: Hessen / Germany
Registered: 2003-12-01
Posts: 1,487

Re: [Solved] Dracut - Rebuild Initramfs on Linux Update

thank you


Frumpus addict
[mu'.krum.pus], [frum.pus]

Offline

#22 2020-03-31 10:24:11

Moo-Crumpus
Member
From: Hessen / Germany
Registered: 2003-12-01
Posts: 1,487

Re: [Solved] Dracut - Rebuild Initramfs on Linux Update

Hm, those are the scripts offered at the wiki, they freeze...


Frumpus addict
[mu'.krum.pus], [frum.pus]

Offline

#23 2020-03-31 12:40:44

CarbonChauvinist
Member
Registered: 2012-06-16
Posts: 413
Website

Re: [Solved] Dracut - Rebuild Initramfs on Linux Update

@Moo-Crumpus, I'm not sure what you mean by freeze - I have no issues with the following which were all copied from the wiki (I only changed the generated initramfs file names to also include the string dracut and also added `usr/lib/dracut/*` as targets for both hooks):

$ cat /etc/pacman.d/hooks/60-dracut-remove.hook 
[Trigger]
Type = Path
Operation = Remove
Target = usr/lib/modules/*/pkgbase
Target = usr/lib/dracut/* 
[Action]
Description = Removing linux initcpios...
When = PreTransaction
Exec = /usr/local/bin/dracut-remove.sh
NeedsTargets
$ cat /etc/pacman.d/hooks/90-dracut-install.hook 
[Trigger]
Type = Path
Operation = Install
Operation = Upgrade
Target = usr/lib/modules/*/pkgbase
Target = usr/lib/dracut/*
[Action]
Description = Updating linux initcpios (with dracut!)...
When = PostTransaction
Exec = /usr/local/bin/dracut-install.sh
NeedsTargets
$ cat /usr/local/bin/dracut-install.sh
#!/usr/bin/env bash

while read -r line; do
	if [[ "$line" == 'usr/lib/modules/'+([^/])'/pkgbase' ]]; then
		read -r pkgbase < "/${line}"
		kver="${line#'usr/lib/modules/'}"
		kver="${kver%'/pkgbase'}"

		install -Dm0644 "/${line%'/pkgbase'}/vmlinuz" "/boot/vmlinuz-dracut-${pkgbase}"
		dracut  --force "/boot/initramfs-dracut-${pkgbase}.img" --kver "$kver"
		dracut --no-hostonly --force "/boot/initramfs-dracut-${pkgbase}-fallback.img" --kver "$kver"
	fi
done
$ cat /usr/local/bin/dracut-remove.sh
#!/usr/bin/env bash

while read -r line; do
	if [[ "$line" == 'usr/lib/modules/'+([^/])'/pkgbase' ]]; then
		read -r pkgbase < "/${line}"
		rm -f "/boot/vmlinuz-dracut-${pkgbase}" "/boot/initramfs-dracut-${pkgbase}.img" "/boot/initramfs-dracut-${pkgbase}-fallback.img"
	fi
done
$ cat /etc/dracut.conf.d/myflags.conf 
hostonly_cmdline="yes"
compress="lz4"
force_drivers+=" i915 "
omit_dracutmodules+=" network iscsi "
early_microcode="yes"
install_optional_items+=" /etc/environment /etc/modprobe.d/modprobe.conf /usr/bin/systemd-tty-ask-password-agent "

For my dracut flags above^^^, I'm only adding systemd-tty-ask-password-agent to get rid of a informational warning message. This work around should not be necessary when new version of dracut is released on Arch AFAICT.

I'd suggest if you're still having issues to open a new thread so you can get more focused attention.


"the wind-blown way, wanna win? don't play"

Offline

#24 2020-03-31 13:47:18

merlock
Member
Registered: 2018-10-30
Posts: 235

Re: [Solved] Dracut - Rebuild Initramfs on Linux Update

CarbonChauvinist wrote:
$ cat /etc/dracut.conf.d/myflags.conf 
hostonly_cmdline="yes"
compress="lz4"
force_drivers+=" i915 "
omit_dracutmodules+=" network iscsi "
early_microcode="yes"
install_optional_items+=" /etc/environment /etc/modprobe.d/modprobe.conf /usr/bin/systemd-tty-ask-password-agent "

For my dracut flags above^^^, I'm only adding systemd-tty-ask-password-agent to get rid of a informational warning message. This work around should not be necessary when new version of dracut is released on Arch AFAICT.

Thanks for this, @CarbonChauvinist.  I was getting tired of those warnings, too.


Eenie meenie, chili beanie, the spirits are about to speak -- Bullwinkle J. Moose
It's a big club...and you ain't in it -- George Carlin
Registered Linux user #149839
perl -e 'print$i=pack(c5,(41*2),sqrt(7056),(unpack(c,H)-2),oct(115),10); '

Offline

Board footer

Powered by FluxBB