saber-nyan / kernel-modules-hook

Keeps your Arch Linux fully functional after a kernel upgrade
The Unlicense
344 stars 25 forks source link

What does this part of code mean? #1

Closed wheatdog closed 5 years ago

wheatdog commented 6 years ago

https://github.com/saber-nyan/kernel-modules-hook/blob/a43cd6bca2bbe65440f544e5f08d53b96c870903/linux-modules-cleanup.service#L6

Sorry, I am bash newbie. I am confused about the if statement and $$ in the script above. Can you try to explain more of it?

xuiqzy commented 5 years ago

I also don't get the pattern matching parts of it and found it difficult to find this information online and apply it to this code in a reasonable time! Could you / someone please explain it? :)

craftyguy commented 5 years ago

Here's my attempt to explain what is going on (tl;dr: most of the weirdness is systemd and bash, but mostly systemd):

$$ is how you escape $ in systemd service files

A##*/ is a super awkward way of basically saying "give me the files or directories that match A" (reference: man bash)

%v is a systemd unit specifier that expands to "current running kernel version" (e.g. uname -r)

So, putting it all together...

List out directories in /usr/lib/modules that start with 0-9: for i in /usr/lib/modules/[0-9]*; do

Does $i exist in /usr/lib/modules? : if [[ $${i##*/} = \'%v\' ]]

Or is $i owned by an installed package (your installed and at this point, presumably running kernel)? (Note: the 'u' parameter seems like a bug, since it means 'upgrade'.. -Qo should be sufficient.. @saber-nyan ?) : || pacman -Qou "$${i}";

If any of the previous are true, then continue, else move the old kernel modules directory to 'old kernel version'.old. (rsync + rm -rf = move): then continue; fi; rsync -AHXal "$${i}" /usr/lib/modules/.old/; rm -rf "$${i}"; done

saber-nyan commented 5 years ago

I'll check this out later, thanks.

saber-nyan commented 5 years ago

@craftyguy

Note: the 'u' parameter seems like a bug, since it means 'upgrade'

According the pacman man-page:

   -u, --upgrades
       Restrict or filter output to packages that are out-of-date on the local system. Only
       package versions are used to find outdated packages; replacements are not checked here.
       This option works best if the sync database is refreshed using -Sy.
craftyguy commented 5 years ago

@saber-nyan

Yea I saw that in the man page, but I get this error when I run it manually:

$ pacman -Qou /lib/modules/4.20.5-arch1-1-ARCH
error: invalid option: '--owns' and '--upgrade' may not be used together

So I'm a bit confused how your script works in this case..

saber-nyan commented 5 years ago

@craftyguy Seems like bug, thanks!

And I probably broke hook with last commit... shit.

saber-nyan commented 5 years ago

@craftyguy Maybe I do not quite get it, but in the file linux-modules-cleanup.service there is no || pacman -Qou "$${i}";, there is only || pacman -Qo "$${i}";...

craftyguy commented 5 years ago

@saber-nyan hmm, you're right. I don't know how that 'u' got in there in my copy/paste.. You can probably close this :) (and sorry for the confusion!)

saber-nyan commented 5 years ago

You're welcome~