You are not logged in.

#1 2021-02-10 16:00:06

n8henrie
Member
From: Shiprock, NM
Registered: 2014-03-30
Posts: 45
Website

[SOLVED] Is NeedsTargets in pacman mkinitcipo hooks unnecessary?

I've seen a pattern a couple times in the Wiki for running mkinitcpio after upgrading the kernel and/or a kernel-relevant package. I'm wondering whether the `NeedsTarget` and `while read trg` business is accomplishing its stated goal.

The justification is:

> Note: The complication in the Exec line above is in order to avoid running mkinitcpio multiple times if both nvidia and linux get updated. In case this doesn't bother you, the Target=linux and NeedsTargets lines may be dropped, and the Exec line may be reduced to simply Exec=/usr/bin/mkinitcpio -P.

So it seems like the idea is "no matter if one target or multiple targets is upgraded / installed / removed in this transaction, just run the Exec once."

However, in my testing it seems like the Exec is only called once regardless.

The documentation for NeedsTargets: https://man.archlinux.org/man/alpm-hooks.5

> Causes the list of matched trigger targets to be passed to the running hook on stdin.

Here are the examples where I see this pattern:

https://wiki.archlinux.org/index.php/NVIDIA

[Trigger]
Operation=Install
Operation=Upgrade
Operation=Remove
Type=Package
Target=nvidia
Target=linux
# Change the linux part above and in the Exec line if a different kernel is used

[Action]
Description=Update Nvidia module in initcpio
Depends=mkinitcpio
When=PostTransaction
NeedsTargets
Exec=/bin/sh -c 'while read -r trg; do case $trg in linux) exit 0; esac; done; /usr/bin/mkinitcpio -P'

https://wiki.archlinux.org/index.php/Dy … le_Support

[Trigger]
Operation=Install
Operation=Upgrade
Operation=Remove
Type=Package
Target=zfs-dkms
Target=linux

[Action]
Description=Update dkms modules in Linux initcpio
Depends=mkinitcpio
When=PostTransaction
NeedsTargets
Exec=/bin/sh -c 'while read -r trg; do case $trg in linux) exit 0; esac; done; /usr/bin/mkinitcpio -p linux'

Here is my example hook:

[Trigger]
Operation = Upgrade
Operation = Install
Operation = Remove
Type = Package
Target = linux
Target = linux-lts
Target = skim
Target = ripgrep

[Action]
Description = Testing
When = PostTransaction
Exec = /bin/echo "I'm running"

After which, I run:

$ sudo pacman -S skim --noconfirm |& grep running
I'm running
$ sudo pacman -S ripgrep --noconfirm |& grep running
I'm running
$ sudo pacman -S skim ripgrep --noconfirm |& grep running
I'm running

It's only if I add NeedsTargets and the "while read" loop that it runs multiple times:

EDIT: Add example

[Trigger]
Operation = Upgrade
Operation = Install
Operation = Remove
Type = Package
Target = linux
Target = linux-lts
Target = skim
Target = ripgrep

[Action]
Description = Cleaning pacman cache...
When = PostTransaction
NeedsTargets
Exec = /bin/sh -c 'while read trg; do echo "running for $trg"; done'
$ sudo pacman -S skim ripgrep --noconfirm |& grep running
running for ripgrep
running for skim

/EDIT

So why are the example hooks using NeedsTargets? Wouldn't it also accomplish the goal of only running once if they got rid of NeedsTargets, got rid of the loop, and used "Exec = /usr/bin/mkinitcpio -P"?

Last edited by n8henrie (2021-02-10 16:46:40)

Offline

#2 2021-02-10 16:36:26

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 25,346

Re: [SOLVED] Is NeedsTargets in pacman mkinitcipo hooks unnecessary?

Your tests do not check for the target that this is intended to guard against. The "problem" is that an update of the linux package will inherently trigger it's own build of mkinitcpio and if you happen to have an update of nvidia or whatever at the same time it would lead to mkinitcpio running twice.

Your pacman install  should include the linux package and the hook should not lead to a run of  the intended command, if the linux package is present in that same transaction.

You could, instead, disable the package provided hooks and make your new hook the "single main hook" that runs mkinitcpio as necessary in which case the proposal would work, but you'd need to explicitly create overrides for the package provided hooks in this case.

Last edited by V1del (2021-02-10 16:44:57)

Offline

#3 2021-02-10 16:42:58

n8henrie
Member
From: Shiprock, NM
Registered: 2014-03-30
Posts: 45
Website

Re: [SOLVED] Is NeedsTargets in pacman mkinitcipo hooks unnecessary?

Ok, I think I understand, and thank you.

My mistaken thinking was that the intent was for this hook to run mkinitcpio only once (instead of twice).

However, the point is actually for this hook to not run mkinitcpio at all if linux is being upgraded, since mkinitcpio will already be run through a separate mechanism.

In other words, this hook only runs mkinitcpio if e.g. nvidia is being updated and linux is *not* also being upgraded.

Does that sound correct?

Last edited by n8henrie (2021-02-10 16:43:55)

Offline

#4 2021-02-10 16:45:32

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 25,346

Re: [SOLVED] Is NeedsTargets in pacman mkinitcipo hooks unnecessary?

Exactly.

Offline

#5 2021-02-10 16:46:59

n8henrie
Member
From: Shiprock, NM
Registered: 2014-03-30
Posts: 45
Website

Re: [SOLVED] Is NeedsTargets in pacman mkinitcipo hooks unnecessary?

Great. Thanks again.

Offline

#6 2021-02-15 11:59:17

Xwang
Member
From: EU
Registered: 2012-05-14
Posts: 414

Re: [SOLVED] Is NeedsTargets in pacman mkinitcipo hooks unnecessary?

Since I'm using nvidia-drms and have linux, linux.lts and linux-zen, should I modify the /etc/pacman.d/hooks/nvidia.hook in the following way?

[Trigger]
Operation=Install
Operation=Upgrade
Operation=Remove
Type=Package
Target=nvidia-dkms
Target=linux
Target=linux-lts
Target=linux-zen
# Change the linux part above and in the Exec line if a different kernel is used
[Action]
Description=Update Nvidia module in initcpio
Depends=mkinitcpio
When=PostTransaction
NeedsTargets
Exec=/bin/sh -c 'while read -r trg; do case $trg in [ linux | linux-lts | linux-zen ] ) exit 0; esac; done; /usr/bin/mkinitcpio -P'

Offline

Board footer

Powered by FluxBB