diff --git a/misc/alpine-install.func b/misc/alpine-install.func index 906d5b14b..6feacd324 100644 --- a/misc/alpine-install.func +++ b/misc/alpine-install.func @@ -21,19 +21,41 @@ verb_ip6() { fi } -# This function catches errors and handles them with the error handler function -catch_errors() { - set -Eeuo pipefail - trap 'error_handler $LINENO "$BASH_COMMAND"' ERR +set -Eeuo pipefail +trap 'error_handler $? $LINENO "$BASH_COMMAND"' ERR +trap on_exit EXIT +trap on_interrupt INT +trap on_terminate TERM + +error_handler() { + local exit_code="$1" + local line_number="$2" + local command="$3" + + # Exitcode 0 = kein Fehler → ignorieren + if [[ "$exit_code" -eq 0 ]]; then + return 0 + fi + + printf "\e[?25h" + echo -e "\n${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}\n" + exit "$exit_code" } -# This function handles errors -error_handler() { +on_exit() { local exit_code="$?" - local line_number="$1" - local command="$2" - local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}" - echo -e "\n$error_message\n" + [[ -n "${lockfile:-}" && -e "$lockfile" ]] && rm -f "$lockfile" + exit "$exit_code" +} + +on_interrupt() { + echo -e "\n${RD}Interrupted by user (SIGINT)${CL}" + exit 130 +} + +on_terminate() { + echo -e "\n${RD}Terminated by signal (SIGTERM)${CL}" + exit 143 } # This function sets up the Container OS by generating the locale, setting the timezone, and checking the network connection diff --git a/misc/build.func b/misc/build.func index 621a2d378..f401d566a 100644 --- a/misc/build.func +++ b/misc/build.func @@ -28,34 +28,41 @@ elif command -v wget >/dev/null 2>&1; then #echo "(build.func) Loaded core.func via wget" fi -# This function enables error handling in the script by setting options and defining a trap for the ERR signal. -catch_errors() { - set -Eeo pipefail - # if [ -n "$BASH_VERSION" ] && command -v shopt >/dev/null 2>&1; then - # shopt -s errtrace - # fi - trap 'error_handler $LINENO "$BASH_COMMAND"' ERR +set -Eeuo pipefail +trap 'error_handler $? $LINENO "$BASH_COMMAND"' ERR +trap on_exit EXIT +trap on_interrupt INT +trap on_terminate TERM + +error_handler() { + local exit_code="$1" + local line_number="$2" + local command="$3" + + # Exitcode 0 = kein Fehler → ignorieren + if [[ "$exit_code" -eq 0 ]]; then + return 0 + fi + + printf "\e[?25h" + echo -e "\n${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}\n" + exit "$exit_code" } -# This function is called when an error occurs. It receives the exit code, line number, and command that caused the error, and displays an error message. -error_handler() { +on_exit() { local exit_code="$?" - local line_number="$1" - local command="$2" - printf "\e[?25h" - local error_message="[ERROR] in line $line_number: exit code $exit_code: while executing command $command" - post_update_to_api "failed" "$command" - echo -e "\n$error_message\n" + [[ -n "${lockfile:-}" && -e "$lockfile" ]] && rm -f "$lockfile" exit "$exit_code" +} - if [[ -n "$CT_ID" ]]; then - read -p "Remove this Container? " prompt - if [[ "${prompt,,}" =~ ^(y|yes)$ ]]; then - pct stop "$CT_ID" &>/dev/null - pct destroy "$CT_ID" &>/dev/null - msg_ok "Removed this Container" - fi - fi +on_interrupt() { + echo -e "\n${RD}Interrupted by user (SIGINT)${CL}" + exit 130 +} + +on_terminate() { + echo -e "\n${RD}Terminated by signal (SIGTERM)${CL}" + exit 143 } # Check if the shell is using bash diff --git a/misc/create_lxc.sh b/misc/create_lxc.sh index 4426ff07e..1c4fb07f4 100644 --- a/misc/create_lxc.sh +++ b/misc/create_lxc.sh @@ -25,34 +25,42 @@ fi # Strict error handling # ------------------------------------------------------------------------------ set -Eeuo pipefail -trap 'error_handler $LINENO "$BASH_COMMAND"' ERR +trap 'error_handler $? $LINENO "$BASH_COMMAND"' ERR trap on_exit EXIT trap on_interrupt INT trap on_terminate TERM +error_handler() { + local exit_code="$1" + local line_number="$2" + local command="$3" + + # Exitcode 0 = kein Fehler → ignorieren + if [[ "$exit_code" -eq 0 ]]; then + return 0 + fi + + printf "\e[?25h" + echo -e "\n${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}\n" + exit "$exit_code" +} + on_exit() { local exit_code="$?" [[ -n "${lockfile:-}" && -e "$lockfile" ]] && rm -f "$lockfile" exit "$exit_code" } -error_handler() { - local exit_code="$?" - local line_number="$1" - local command="$2" - printf "\e[?25h" - echo -e "\n${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}\n" - exit "$exit_code" -} - on_interrupt() { echo -e "\n${RD}Interrupted by user (SIGINT)${CL}" exit 130 } + on_terminate() { echo -e "\n${RD}Terminated by signal (SIGTERM)${CL}" exit 143 } + exit_script() { clear printf "\e[?25h" diff --git a/vm/debian-13-vm.sh b/vm/debian-13-vm.sh index 6169ba28e..e1efe022a 100644 --- a/vm/debian-13-vm.sh +++ b/vm/debian-13-vm.sh @@ -576,5 +576,29 @@ if [ "$START_VM" == "yes" ]; then msg_ok "Started Debian 13 VM" fi +msg_info "Installing resize tools in VM" +qm set "$VMID" --sshkeys ~/.ssh/id_rsa.pub >/dev/null 2>&1 || true +qm exec "$VMID" -- bash -c "apt-get update && apt-get install -y cloud-guest-utils e2fsprogs xfsprogs" +msg_ok "Installed resize tools in VM" + +msg_info "Injecting auto-resize script into VM" +cat <<'EOF' >/var/lib/vz/snippets/resize-root.sh +#!/bin/bash +set -e +DEVICE=$(lsblk -no pkname $(findmnt -n -o SOURCE /)) +PART=$(findmnt -n -o SOURCE / | sed 's|/dev/||') +growpart /dev/${DEVICE} ${PART##*[a-z]} +FSTYPE=$(findmnt -n -o FSTYPE /) +if [ "$FSTYPE" = "ext4" ]; then + resize2fs /dev/$PART +elif [ "$FSTYPE" = "xfs" ]; then + xfs_growfs / +fi +EOF +chmod +x /var/lib/vz/snippets/resize-root.sh + +qm set "$VMID" --hookscript local:snippets/resize-root.sh >/dev/null +msg_ok "Injected auto-resize script" + msg_ok "Completed Successfully!\n" echo "More Info at https://github.com/community-scripts/ProxmoxVE/discussions/836"