#!/bin/bash # ----------------------------------------------------------------------------------------------- # --------------------------------- Server Reboot Script --------------------------------------- # ----------------------------------------------------------------------------------------------- # Gracefully reboots the unRAID server with a configurable user warning delay. # Stops Docker and VM Manager cleanly before issuing reboot. # Reboot delay is configured in Master.conf under REBOOT_SLEEP. # Supports --dry-run to walk through the sequence without actually rebooting. # ----------------------------------------------------------------------------------------------- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$SCRIPT_DIR/../Master.conf" source "$SCRIPT_DIR/../common.sh" parse_args "$@" # ----------------------------------------------------------------------------------------------- # ━━━ $ICON_GEAR Setup ━━━ # ----------------------------------------------------------------------------------------------- echo "" echo "━━━ $ICON_GEAR Setup ━━━" # ROOT CHECK if [[ "$EUID" -ne 0 ]]; then error "Must be run as root" exit 1 fi success "Running as root" # VALIDATION validate_int REBOOT_SLEEP "$REBOOT_SLEEP" # ----------------------------------------------------------------------------------------------- # ━━━ $ICON_SUMMARY Status ━━━ # ----------------------------------------------------------------------------------------------- if [[ "$SHOW_STATUS" == true ]]; then echo "" echo "━━━━━ $ICON_SUMMARY STATUS ━━━━━" echo "$ICON_REBOOT Delay: ${REBOOT_SLEEP}s" echo "$ICON_GEAR Dry Run: $DRY_RUN" echo "━━━━━━━━━━━━━━━━━━━━━━━" exit 0 fi # ----------------------------------------------------------------------------------------------- # FUNCTIONS # ----------------------------------------------------------------------------------------------- # Broadcasts a wall message warning all logged in users of the upcoming reboot. notify_users() { warn "Notifying users — reboot in ${REBOOT_SLEEP}s" wall "$ICON_WARN unRAID server will reboot in ${REBOOT_SLEEP} second(s). Save your work." } # Stops the Docker service cleanly. # Warns but continues if Docker is already stopped or fails — shutdown must proceed. stop_docker() { if [[ "$DRY_RUN" == true ]]; then warn "DRY RUN — would stop Docker service" return fi info "Stopping Docker service..." if /etc/rc.d/rc.docker stop; then success "Docker stopped" else warn "Docker stop failed or already stopped — continuing" fi } # Stops the VM Manager (libvirt) cleanly. # Warns but continues if libvirt is already stopped or fails — shutdown must proceed. stop_vm_manager() { if [[ "$DRY_RUN" == true ]]; then warn "DRY RUN — would stop VM Manager (libvirt)" return fi info "Stopping VM Manager..." if /etc/rc.d/rc.libvirt stop; then success "VM Manager stopped" else warn "VM Manager stop failed or already stopped — continuing" fi } # Flushes filesystem buffers to disk before reboot. sync_disks() { if [[ "$DRY_RUN" == true ]]; then warn "DRY RUN — would sync filesystem buffers" return fi info "Syncing disks..." if sync; then success "Disk sync complete" else warn "Sync returned an error — continuing" fi } # Issues the system reboot command. # System will not return from this call unless dry-run is active. reboot_system() { if [[ "$DRY_RUN" == true ]]; then warn "DRY RUN — would reboot system now" return fi echo "" echo "$ICON_REBOOT Rebooting system NOW..." /sbin/reboot } # ----------------------------------------------------------------------------------------------- # ━━━ $ICON_REBOOT Reboot Sequence ━━━ # ----------------------------------------------------------------------------------------------- echo "" echo "━━━ $ICON_REBOOT Reboot Sequence ━━━" echo "$ICON_REBOOT Delay: ${REBOOT_SLEEP}s" echo "$ICON_GEAR Dry Run: $DRY_RUN" echo "" [[ "$DRY_RUN" == true ]] && warn "DRY RUN — no changes will be made" START=$(date +%s) if [[ "$REBOOT_SLEEP" -gt 0 ]]; then notify_users info "Waiting ${REBOOT_SLEEP}s before shutdown sequence..." sleep "$REBOOT_SLEEP" fi stop_docker stop_vm_manager sync_disks reboot_system # NOTE: system will not reach here unless --dry-run is active END=$(date +%s) # ----------------------------------------------------------------------------------------------- # ━━━ $ICON_SUMMARY Summary ━━━ # ----------------------------------------------------------------------------------------------- echo "" echo "━━━━━ $ICON_SUMMARY REBOOT SUMMARY ━━━━━" echo "$ICON_REBOOT Delay: ${REBOOT_SLEEP}s" echo "$ICON_TIME Duration: $(format_duration $((END - START)))" if [[ "$DRY_RUN" == true ]]; then echo "$ICON_WARN Status: DRY RUN — no reboot executed" else echo "$ICON_REBOOT Status: $ICON_WARN SYSTEM SHOULD BE REBOOTING" fi echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"