From af59619e52a5d61719fe5297ce804946b2ec72b4 Mon Sep 17 00:00:00 2001 From: Slack Coder Date: Fri, 26 Apr 2024 15:18:58 -0500 Subject: Be safer and use correct signals - Avoid misusing SIGQUIT. - Handle situation where kernel files were modified during install process. --- efi-sync | 67 +++++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/efi-sync b/efi-sync index 1129a16..02ab587 100644 --- a/efi-sync +++ b/efi-sync @@ -1,6 +1,6 @@ #!/bin/bash -trap graceful_exit QUIT TERM +trap graceful_exit INT TERM # Inofity events which signal something changed. MODIFICATION_EVENTS="CREATE,CLOSE_WRITE,MOVED_TO,MOVED_FROM,MOVE" @@ -12,10 +12,10 @@ graceful_exit () { STOP=true if [[ ! -z "$KPID" ]]; then - kill -TERM $KPID + kill -TERM $KPID >/dev/null 2>/dev/null fi if [[ ! -z "$MPID" ]]; then - kill -TERM $MPID + kill -TERM $MPID >/dev/null 2>/dev/null fi } @@ -56,12 +56,12 @@ efi_install () { KERNEL_VERSION="$(basename $(realpath "$KERNEL") | sed -E 's/vmlinuz-(.*)-(.*)/\2/')" - log "Installing $(realpath "$KERNEL")." cp -H "$KERNEL" "${EFI_KERNEL}"|| log "Failed to copy '${KERNEL}' into your EFI at '${EFI_KERNEL}'." + log "Installed $(realpath "$KERNEL")." if [[ -f /etc/mkinitrd.conf ]]; then - log "Installing the initial RAM disk for kernel version '${KERNEL_VERSION}'." mkinitrd -F -c -k "${KERNEL_VERSION}" >/dev/null 2>&1 || log "mkinitrd failed to build and install your initial ramdisk." + log "Installed the initial RAM disk for kernel version '${KERNEL_VERSION}'." fi } @@ -73,43 +73,58 @@ efi_watch () { while ! $STOP; do log "Watching ${KERNEL} for updates." - inotifywait "$(dirname "$KERNEL")" --include "./$(basename $KERNEL)$" \ + >/dev/null inotifywait "$(dirname "$KERNEL")" --include "./$(basename $KERNEL)$" \ --event "$MODIFICATION_EVENTS" \ - --quiet \ - >/dev/null & + --quiet & KPID="$!" # Watching for directory updates is enough and helps avoid the system's watch limit. - find /lib/firmware /lib/modules -type d \ - | xargs inotifywait \ + readarray -d '' INITRD_FILES < <(find /lib/firmware /lib/modules -type d -print0) + >/dev/null inotifywait \ --event "$MODIFICATION_EVENTS" \ --quiet \ --recursive \ - >/dev/null & + "${INITRD_FILES[@]}" & MPID="$!" - if ! wait -n "$KPID" "$MPID"; then - kill -QUIT $KPID $MPID >/dev/null 2>&1 + if ! wait -n "$KPID" "$MPID" || $STOP; then + kill -TERM $KPID $MPID >/dev/null 2>/dev/null continue fi - kill -QUIT $KPID $MPID >/dev/null 2>&1 - unset KPID - unset MPID + kill -TERM $KPID $MPID >/dev/null 2>/dev/null + unset KPID MPID + + # Be safe and consider changes to the kernel system which happen during + # installation. + # + # Use file creation time to check if re-installation is needed. + TIMESTAMP_FILE="$(mktemp -t "efi-sync.XXXXXXX")" + + while true; do + touch "$TIMESTAMP_FILE" + + if [[ -f /etc/mkinitrd.conf ]]; then + local count=0 - if [[ -f /etc/mkinitrd.conf ]]; then - local count=0 + while [[ $count -lt ${WAIT_TIME} ]]; do + while ps -C "${INSTALL_PROGRAMS}" >/dev/null; do + let "count = 0" + sleep 1 + done - while [[ $count -lt ${WAIT_TIME} ]]; do - while ps -C "${INSTALL_PROGRAMS}" >/dev/null; do - let "count = 0" - sleep 1 + let "count = $count + 1" done + fi - let "count = $count + 1" - done - fi + efi_install - efi_install + NEW_FILES="$(find "$KERNEL" /lib/firmware /lib/modules -newer "$TIMESTAMP_FILE")" + if [[ -z "$NEW_FILES" ]]; then + break + fi + done + + rm "$TIMESTAMP_FILE" done log "Stopped watching ${KERNEL} for updates." -- cgit v1.2.3