2023-04-02 11:04:23 +08:00
|
|
|
#!/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
|
2023-05-31 23:19:32 +08:00
|
|
|
if mountpoint --nofollow --quiet /${line%vmlinuz};then
|
2023-04-02 11:04:23 +08:00
|
|
|
# Mount point is already present
|
|
|
|
# This means we already ran that hook during 'remove case'
|
|
|
|
#
|
|
|
|
# Remove the mount so we can reinstall the kernel
|
2023-05-31 14:02:04 +08:00
|
|
|
# Most of the time udevd would block the umount, so suppress the first error message
|
2023-05-31 23:19:32 +08:00
|
|
|
umount /${line%vmlinuz} 2>/dev/null || umount --lazy /${line%vmlinuz}
|
|
|
|
rmdir /${line%vmlinuz}
|
2023-04-02 11:04:23 +08:00
|
|
|
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
|
2023-04-02 11:05:50 +08:00
|
|
|
cp --archive --link --no-target-directory /usr/lib/modules/${kver}/ \
|
2023-04-02 11:04:23 +08:00
|
|
|
/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
|