Skip to content

Commit

Permalink
Use cp instead of rsync + systemd-tmpfiles for cleaning
Browse files Browse the repository at this point in the history
This use cp with hardlinks + a bind mount for keeping the removed kernel
modules accessible.
Cleaning up the backup is done by systemd-tmpfiles on next boot.
Cleaning up the mount is either done on shutdown implicitly, or when we
reinstall the running kernel by the pre-hook script.

No systemd service enabling is needed.
  • Loading branch information
VannTen committed Feb 21, 2023
1 parent ded83bc commit c554030
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 56 deletions.
19 changes: 8 additions & 11 deletions .SRCINFO
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@ pkgbase = kernel-modules-hook
pkgver = 0.1.7
pkgrel = 1
url = https://github.com/saber-nyan/kernel-modules-hook
install = kernel-modules-hook.install
arch = any
license = UNLICENSE
depends = rsync
license = Unlicense
source = linux-modules-cleanup.conf
source = linux-modules-cleanup.service
source = 10-linux-modules-post.hook
source = 10-linux-modules-pre.hook
source = UNLICENSE
sha256sums = 4169b44c297ddb7aad2220c6eba7c7942e3396f92528c59617955ab5560cb4cf
sha256sums = 5d947290ef8c94b33c79c531e5615f4c9bea38e7649092d34af3bf0af5b1ca24
sha256sums = 900502d030e925fca6188b9448fbaf6562d6e23cd5c50938cdf00522825f76c2
sha256sums = f7ea2947c3fbe1510b3ea5cc5793b8197f0718dcb12daea3da9b27b3cf1c4116
sha256sums = 7e12e5df4bae12cb21581ba157ced20e1986a0508dd10d0e8a4ab9a4cf94e85c
source = linux-modules-restore
source = linux-modules-save
sha256sums = 950f851eba08dac4d0b93ff62b3fb16ddacd4f8ebb98a2435f80bf05f2ea5a29
sha256sums = 0492722850f90066d33baf7896eb4625fd812890bdb03f025184c8b4bf0ef94f
sha256sums = 2b86a037a9a394f57279408745bf5b19542f26b0ad862fd514470cd145108c10
sha256sums = 7cf080b0d0d5a07ccb414ffb793525e554ca95447201f22b917f0be04ff48d2a
sha256sums = 3168ea6c2740dbde4229d718e44f6262f2d10aef529fb446cdb4f1778b8b6424

pkgname = kernel-modules-hook

8 changes: 3 additions & 5 deletions 10-linux-modules-post.hook
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
[Trigger]
Operation = Install
Operation = Upgrade
Operation = Remove
Type = Path
Target = usr/lib/modules/*/vmlinuz

[Action]
Description = Restoring Linux kernel modules...
When = PostTransaction
Depends = coreutils
Depends = rsync
Exec = /bin/sh -xc 'KVER="${KVER:-$(uname -r)}"; if test -e "/usr/lib/modules/backup/${KVER}"; then rsync -AHXal --ignore-existing "/usr/lib/modules/backup/${KVER}" /usr/lib/modules/; fi; rm -rf /usr/lib/modules/backup'
Exec = /usr/share/libalpm/scripts/linux-modules-restore
NeedsTarget
8 changes: 4 additions & 4 deletions 10-linux-modules-pre.hook
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[Trigger]
Operation = Install
Operation = Upgrade
Operation = Remove
Type = Path
Target = usr/lib/modules/*/vmlinuz

[Action]
Description = Saving Linux kernel modules...
Description = Saving Linux kernel modules directory (hardlinks)...
When = PreTransaction
Depends = rsync
Exec = /bin/sh -c 'KVER="${KVER:-$(uname -r)}"; if test -e "/usr/lib/modules/${KVER}"; then rsync -AHXal --delete-after "/usr/lib/modules/${KVER}" /usr/lib/modules/backup/; fi'
Exec = /usr/share/libalpm/scripts/linux-modules-save
NeedsTarget
21 changes: 11 additions & 10 deletions PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ pkgrel=1
pkgdesc="Keeps your system fully functional after a kernel upgrade"
arch=('any')
url="https://github.com/saber-nyan/kernel-modules-hook"
depends=('rsync')
install="${pkgname}.install"
license=('Unlicense')
source=("linux-modules-cleanup.conf"
"linux-modules-cleanup.service"
"10-linux-modules-post.hook"
"10-linux-modules-pre.hook")
sha256sums=('4169b44c297ddb7aad2220c6eba7c7942e3396f92528c59617955ab5560cb4cf'
'5d947290ef8c94b33c79c531e5615f4c9bea38e7649092d34af3bf0af5b1ca24'
'900502d030e925fca6188b9448fbaf6562d6e23cd5c50938cdf00522825f76c2'
'f7ea2947c3fbe1510b3ea5cc5793b8197f0718dcb12daea3da9b27b3cf1c4116')
"10-linux-modules-post.hook"
"10-linux-modules-pre.hook"
"linux-modules-restore"
"linux-modules-save"
)
sha256sums=('950f851eba08dac4d0b93ff62b3fb16ddacd4f8ebb98a2435f80bf05f2ea5a29'
'0492722850f90066d33baf7896eb4625fd812890bdb03f025184c8b4bf0ef94f'
'2b86a037a9a394f57279408745bf5b19542f26b0ad862fd514470cd145108c10'
'7cf080b0d0d5a07ccb414ffb793525e554ca95447201f22b917f0be04ff48d2a'
'3168ea6c2740dbde4229d718e44f6262f2d10aef529fb446cdb4f1778b8b6424')

package() {
install -Dm644 'linux-modules-cleanup.conf' "${pkgdir}/usr/lib/tmpfiles.d/linux-modules-cleanup.conf"
install -Dm644 'linux-modules-cleanup.service' "${pkgdir}/usr/lib/systemd/system/linux-modules-cleanup.service"
install -Dm644 10-linux-modules-{pre,post}.hook -t "${pkgdir}/usr/share/libalpm/hooks/"
install -Dm755 linux-modules-{save,restore} -t "${pkgdir}/usr/share/libalpm/scripts/"
}
16 changes: 0 additions & 16 deletions kernel-modules-hook.install

This file was deleted.

3 changes: 2 additions & 1 deletion linux-modules-cleanup.conf
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
R! /usr/lib/modules/.old/* - - - 4w
R! /usr/lib/modules/running-kernel
r /usr/lib/modules/*
9 changes: 0 additions & 9 deletions linux-modules-cleanup.service

This file was deleted.

11 changes: 11 additions & 0 deletions linux-modules-restore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash -e

kver = $(uname -r)
while read -r line; do
# We only care about the running kernel
if [[ "$line" == usr/lib/modules/$kver/vmlinuz && -d "${line/$kver/running-kernel}" ]];then
mount --mkdir --bind --options ro /usr/lib/modules/{running-kernel,$kver}
# Mounting read-only since the only modification here should be unmounting
# when rebooting or reinstalling the running kernel
fi
done
24 changes: 24 additions & 0 deletions linux-modules-save
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash -e

kver = $(uname -r)
while read -r line; do
# We only care about the running kernel
if [[ "$line" == "usr/lib/modules/$kver/vmlinuz" ]];then
if [[ mountpoint --nofollow --quiet ${line%vmlinuz} ]];then
# Mount point is already present
# This means we already ran that hook during 'remove case'
#
# Remove the mount so we can reinstall the kernel
umount ${line%vmlinuz}
elif [[ -f "$line" && ! -d "/usr/lib/modules/running-kernel/" ]];then
# Kernel install is present and we do not have a copy
#
# This is the removal case, so we save the kernel
mkdir /usr/lib/modules/running-kernel
cp --archive --link /usr/lib/modules/{$kver}/{kernel,modules*} \
/usr/lib/modules/running-kernel/
fi
# If we are re-removing the running kernel, (after removing + reinstalling),
# we already have a backup and this hook is a no-op
fi
done

0 comments on commit c554030

Please sign in to comment.