Grub Hook to grub-install and grub-mkconfig?

Sorry to bother you again.
As was discussed in other threads, as I learned from @dalto that to be sure Grub will work properly I better use “hook” and not a script as I did (thought the script luckily worked for me), but I am always seeking the best.

After reading:

I found that I have:
/etc/pacman.d/hooks/ empty folder.
/usr/share/libalpm/hooks/ has many “.hook” files including:

eos-grub-fix-initrd-generation.hook
eos-grub-update-after-kernel.hook
zz-snap-pac-grub-post.hook

So, according to my humble understanding I created a file called it grub_update.hook that ‘supposedly’ runs grub-install and grub-mkconfig -o /boot/grub/grub.cfg if grub gets updated,…
The hook file grub_update.hook is as follows:

[Trigger]
Operation = Install
Operation = Remove
Operation = Upgrade

Type = Package
Target = grub

[Action]
Description = Re-generate Grub if Updated
When = PostTransaction
Depends = grub
Exec = /bin/sh -c "/usr/bin/grub-install"
Exec = /bin/sh -c "/usr/bin/grub-mkconfig -o /boot/grub/grub.cfg"

I am on KDE Plasma, SSD, BTRFS, Snapper, BTRFS-Assistant, … just followed defaults during online installation.

Should I just copy this file to folder /usr/share/libalpm/hooks/ then restart so I am sure whenever Grub gets updated things will be fine?

Should I put sudo after “Exec =” (last 2 lines in file)?

As usual when it is something that critical I prefer to consult the experts instead of taking the risk of having unbootable system.

I appreciate your patience and support.

Is this OK and I can proceed copying the file to folder /usr/share/libalpm/hooks/?

I never heard of two Exec in the HOOK script.
Two lines together in Exec

Exec = /bin/sh -c "/usr/bin/grub-install && /usr/bin/grub-mkconfig -o /boot/grub/grub.cfg" 
1 Like

I would put it in: /etc/pacman.d/hooks which is the default path in pacman.conf.

You would need though uncomment the line:

#HookDir = /etc/pacman.d/hooks/

in pacman.conf.

Also no need to run the commands if you remove the package, so I would delete:

Operation = Remove
1 Like

Thanks @Zesko for your prompt reply.
I just made it like this according to my understanding of KISS, to be readable.
I would change it as you suggested.

But to learn more, would it not work if done in 2 lines?

Should I proceed and just copy it to the folder?
Anything else to be done?

You mean if I removed grub? I do not think I will do it if this hook works fine.
I will remove this line
Operation = Remove

But this folder is empty!
I wonder what the other folder is doing then?
So, I should as well copy the 3 files from this folder to /etc/pacman.d/hooks?

that file in folder “/usr/bin”?

Just put your hook in /etc/pacman.d/hooks and uncomment the line

#HookDir = /etc/pacman.d/hooks/

in

/etc/pacman.conf

I would delete:


Operation = remove

There will be no grub-install to talk about if the package Grub is removed.

Operation = install

seems unnecessary as well, since the package is already installed.

The hook needs to be triggered only when the package is upgraded.

1 Like

I do not know if HOOK supports multiple Exec run in sequence. :point_down:


If the first command grub-install fails, then the second command grub-mkconfig is not executed. That ist different than multiple Exec

1 Like

Done.

Done.

Done

Now, which folder I should copy my .hook file to?
Why are the hook files in the other folder while the folder you are telling me to copy to is empty?
Does this mean I should copy the three files with grub in their names to this empty folder?

Sorry for many questions!

Pacman can run pre- and post-transaction hooks from the /usr/share/libalpm/hooks/ directory; more directories can be specified with the HookDir option in pacman.conf, which defaults to /etc/pacman.d/hooks.

Just do as @pebcak says…

2 Likes
/etc/pacman.d/hooks

The hooks in /usr/share/libalpm/hooks are put in there by the system itself.

The right place for user made modifications is /etc. In this way you can be sure your modifications won’t be overwritten.

The /etc/pacman.d/hook is empty because you haven’t put a hook in it yet.

No.

2 Likes

Done
copied the file to /etc/pacman.d/hooks/

I was not arguing of course.

I just wanted to understand why this folder is empty and the other is full.
But you clarified it for me.

Now, I suppose I am sure the 2 commands will be automatically executed if there is a grub update and will not face any issues due to a grub update.I wont then need the script I did. Right?

Thanks a lot @pebcak for explaining this to me.
Now I understand a little bit more about the system and hooks.
Thank you @pebcak , thank you @Zesko , thank you @anon13373109

A wonderful community as usual.

1 Like

I might be somewhat mistaken as to what puts those hooks in /usr/share/libalpm/hooks.

The directory seems to “belong” to pacman (you could check: pacman -Ql pacman) but for example:

$ pacman -Qo texinfo-remove.hook
error: No package owns texinfo-remove.hook
1 Like

It is OK. I found them there. So, I assume they were put during the default online installation.

I hope now my system won’t break or have issues when grub updates.
Hopefully at least for my system I am back to normal now I can just yay -Syu and everything is done automagically!

1 Like

I don’t have these hooks. These look however to have an “owner”.

For example:

eos-grub-update-after-kernel.hook is from: endeavouros/grub-tools

I don’t have the packages from which these hooks come from.

You could always run: pacman -Qo FILE to find out what packages “owns” FILE.

1 Like

Maybe they came in as a result of running the script I made to do the 2 commands in case there is a grub update at Grub Updated Seamlessly which was actually run after the latest grub update.

By the way, wasn’t it meant that grub-tools should be removed from EnOS?

I haven’t read through all more than thousand posts in different grub related threads since the “Grub Issue” so I am not sure.