RSSAmplifier

aryan's blog · Jul 20, 2025

Reboot?

0
Sign in to vote or save

aryan's blog · aryan's blog

Little bash function to know if a reboot is required on Debian 12

This bash function checks the current running version against the newest installed kernel to see if a reboot is required (as Debian doesn't consistently use the /var/run/reboot-required file.

It's been mostly created for Proxmox but should work on any #Debian installation. I've tested compatibility with bash and zsh. Handles zfs and ext4 boot partitions.

This can be put into .bashrc or .zshrc and then when you issue the command MY_REBOOT it will tell you if a reboot is required or not.

MY_REBOOT() {
    # Colour support
    RED="\033[1;31m"; GREEN="\033[1;32m"; YELLOW="\033[1;33m"; NC="\033[0m"
    # Resolve boot path with ZFS/UEFI support
    boot_path=$(readlink -f /boot 2>/dev/null || echo '/boot')
    # Kernel discovery
    latest_kernel_file=$(find "$boot_path" -maxdepth 1 -name 'vmlinuz-*' -print0 2>/dev/null |
        sort -zVr | head -zn1 | tr -d '\0')
    # Get and validate pinned kernel
    pinned_kernel=$(proxmox-boot-tool status 2>/dev/null |
        awk '/^Next boot:/ {sub(/.*pve-kernel-/,""); print $1}')
    # Check pinned kernel existence
    if [ -n "$pinned_kernel" ] && [ ! -f "$boot_path/vmlinuz-$pinned_kernel" ]; then
        echo -e "${YELLOW}Warning: Pinned kernel $pinned_kernel not found${NC}" >&2
        pinned_kernel=""
    fi
    # Determine target kernel (pinned > latest)
    if [ -n "$pinned_kernel" ]; then
        target="$pinned_kernel"
    elif [ -n "$latest_kernel_file" ]; then
        target="${latest_kernel_file##*/vmlinuz-}"
    else
        echo -e "${RED}Error: No valid kernels detected${NC}" >&2
        return 3
    fi
    current=$(uname -r)
    # Version comparison
    clean_current=$(echo "$current" | sed 's/-pve//')
    clean_target=$(echo "$target" | sed 's/-pve//')
    if [ "$clean_current" != "$clean_target" ]; then
        echo -e "${RED}Reboot required${NC} (Current: ${current} → Target: ${target})"
        # Show pinned/latest mismatch warning
        if [ -n "$pinned_kernel" ] && [ -n "$latest_kernel_file" ] &&
           [ "$pinned_kernel" != "${latest_kernel_file##*/vmlinuz-}" ]; then
            echo -e "${YELLOW}Notice: Pinned kernel differs from latest available${NC}" >&2
        fi
        return 1
    else
        echo -e "${GREEN}No reboot required${NC} (Current: ${current} → Target: ${target})"
        return 0
    fi
}

#homelab #shell #proxmox #debian #bookworm

Read the original on blog.ameri.coffee

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.