From a3461db9a8062decbf6b907a6cbf167f8b6e3e6b Mon Sep 17 00:00:00 2001 From: Kroese Date: Fri, 14 Aug 2026 11:07:46 +0200 Subject: [PATCH] feat: Centralize image and boot state checks (#2137) --- src/image.sh | 12 ++++++------ src/install.sh | 53 ++++++++++++++++++++++++++++++++++---------------- src/mido.sh | 2 +- src/power.sh | 10 +++++----- src/samba.sh | 2 +- 5 files changed, 49 insertions(+), 30 deletions(-) diff --git a/src/image.sh b/src/image.sh index 2baa19e7..79a6582d 100644 --- a/src/image.sh +++ b/src/image.sh @@ -400,9 +400,9 @@ extractImage() { local archive="${dir}.archive" local file size required archiveSize rc - if [ -z "$CUSTOM" ]; then + if ! isCustomImage; then desc="downloaded ISO" - if [[ "$version" != "http"* ]]; then + if ! isURL "$version"; then desc=$(printVariant "$version" "$desc") fi fi @@ -1652,7 +1652,7 @@ extractESD() { index="" - if [[ "${version,,}" == "http"* ]]; then + if isURL "$version"; then # Direct ESD URLs have no catalog identity. Restrict automatic detection # to installable images because indexes 1-3 contain setup components. @@ -1758,7 +1758,7 @@ extractESD() { return 1 fi - if [[ "${version,,}" != "http"* ]] && + if ! isURL "$version" && [[ "${resultEdition,,}" != "${edition,,}" ]]; then error "Prepared install.esd does not contain only '$edition' at index 1!" return 1 @@ -1925,7 +1925,7 @@ buildImage() { checkFreeSpace "$TMP" "$size" || return 1 - if [[ "${BOOT_MODE,,}" == "windows_legacy" ]] && [ -z "${BOOT_LOAD_SIZE:-}" ]; then + if isLegacyBoot && [ -z "${BOOT_LOAD_SIZE:-}" ]; then if [[ "${DETECTED,,}" != "win9"* && "${DETECTED,,}" != "winnt4" ]]; then error "Failed to determine the boot image load size!" return 1 @@ -1951,7 +1951,7 @@ buildImage() { # Use separate layouts for modern hybrid media, NT 5.x legacy media, Win9x, # and other legacy releases because their El Torito requirements differ. - if [[ "${BOOT_MODE,,}" != "windows_legacy" ]]; then + if ! isLegacyBoot; then args+=( -no-emul-boot diff --git a/src/install.sh b/src/install.sh index e9f99cc7..5f091cfa 100644 --- a/src/install.sh +++ b/src/install.sh @@ -151,7 +151,7 @@ configureMachine() { if ! checkMemory "$DETECTED"; then - if [ -z "$CUSTOM" ]; then + if ! isCustomImage; then useOriginalImage "$iso" || { error "Failed to preserve the original installation image!" return 79 @@ -295,12 +295,12 @@ startInstall() { html "Starting $APP..." - if [ -z "$CUSTOM" ]; then + if ! isCustomImage; then local file="${VERSION//\//}.iso" local boot="$file" - if [[ "${VERSION,,}" == "http"* ]]; then + if isURL "$VERSION"; then file=$(basename "${VERSION%%[\?#]*}") printf -v file '%b' "${file//%/\\x}" @@ -385,7 +385,7 @@ startInstall() { return 50 fi - if [ -z "$CUSTOM" ]; then + if ! isCustomImage; then if [ -s "$BOOT" ]; then ISO="$TMP/$(basename "$BOOT")" @@ -397,7 +397,7 @@ startInstall() { # Keep existing media at its persistent path until all storage cleanup has # completed successfully, so a later failure cannot strand it under $TMP. - if [ -n "$CUSTOM" ] || [ ! -s "$BOOT" ]; then + if isCustomImage || [ ! -s "$BOOT" ]; then if ! rm -f -- "$BOOT"; then error "Failed to remove obsolete ISO file \"$BOOT\" !" return 50 @@ -419,14 +419,14 @@ startInstall() { return 50 fi - if [ -z "$CUSTOM" ] && [[ "${VERSION,,}" != "http"* ]]; then + if ! isCustomImage && ! isURL "$VERSION"; then checkMemory "$VERSION" || return 67 setDiskMinimum "$VERSION" || return 67 fi # Work from the temporary directory so the persistent source path can # later contain either the preserved ISO or the rebuilt installation image. - if [ -z "$CUSTOM" ] && [ -f "$BOOT" ] && [ -s "$BOOT" ]; then + if ! isCustomImage && [ -f "$BOOT" ] && [ -s "$BOOT" ]; then if ! mv -f -- "$BOOT" "$ISO"; then error "Failed to move ISO file from \"$BOOT\" to \"$ISO\" !" return 50 @@ -603,7 +603,7 @@ finishInstall() { fi if [[ "$boot" == "$STORAGE/"* ]]; then - if [[ "$aborted" != [Yy1]* ]] || [ -z "$CUSTOM" ]; then + if [[ "$aborted" != [Yy1]* ]] || ! isCustomImage; then base=$(basename "$boot") writeState "base" "$base" || { @@ -615,7 +615,7 @@ finishInstall() { fi if [[ "${PLATFORM,,}" == "x64" ]]; then - if [[ "${BOOT_MODE,,}" == "windows_legacy" ]]; then + if isLegacyBoot; then writeState "mode" "$BOOT_MODE" || { error "Failed to save the Windows boot mode!" @@ -707,7 +707,7 @@ findFile() { file=$(find "$STORAGE" -maxdepth 1 -type f -iname "$fname" -print -quit) || return 1 fi - if [ ! -s "$file" ] && [[ "${VERSION,,}" != "http"* ]]; then + if [ ! -s "$file" ] && ! isURL "$VERSION"; then base=$(basename "$VERSION") file="$STORAGE/$base" fi @@ -736,7 +736,7 @@ findFile() { normalizeDetected() { # Known catalog versions already provide the required image metadata. - if [ -z "$DETECTED" ] && [ -z "$CUSTOM" ] && [[ "${VERSION,,}" != "http"* ]]; then + if [ -z "$DETECTED" ] && ! isCustomImage && ! isURL "$VERSION"; then DETECTED="$VERSION" fi @@ -752,14 +752,14 @@ detectCustom() { findFile "custom.iso" || return 1 - if [ -n "$CUSTOM" ]; then + if isCustomImage; then DETECTED="" return 0 fi findFile "boot.iso" || return 1 - if [ -n "$CUSTOM" ]; then + if isCustomImage; then DETECTED="" return 0 fi @@ -767,6 +767,25 @@ detectCustom() { return 0 } +isCustomImage() { + + [ -n "${CUSTOM:-}" ] +} + +isURL() { + + local value="$1" + + [[ "${value,,}" == "http"* ]] +} + +isLegacyBoot() { + + local mode="${BOOT_MODE:-}" + + [[ "${mode,,}" == "windows_legacy" ]] +} + hasImage() { local iso="$1" @@ -837,7 +856,7 @@ useOriginalImage() { local iso="$1" - if [ -n "$CUSTOM" ]; then + if isCustomImage; then BOOT="$iso" elif [[ "$iso" != "$BOOT" ]]; then if ! mv -f -- "$iso" "$BOOT"; then @@ -858,7 +877,7 @@ removeImage() { local iso="$1" - [ -n "$CUSTOM" ] && return 0 + isCustomImage && return 0 if ! rm -f -- "$iso" 2>/dev/null; then warn "failed to remove image \"$iso\"!" @@ -953,7 +972,7 @@ prepareImage() { ETFS="${ETFS:-boot/etfsboot.com}" # Legacy rebuilt media must retain the source ISO's El Torito boot-load size. - if [[ "${BOOT_MODE,,}" == "windows_legacy" ]]; then + if isLegacyBoot; then getBootLoadSize "$iso" "$dir" "$desc" || return 1 @@ -961,7 +980,7 @@ prepareImage() { supportsXML "$DETECTED" || return 0 - if [[ "${BOOT_MODE,,}" == "windows_legacy" ]]; then + if isLegacyBoot; then extractBootImage "$iso" "$dir" "$desc" && return 0 diff --git a/src/mido.sh b/src/mido.sh index cc3237fa..db0f5cb4 100644 --- a/src/mido.sh +++ b/src/mido.sh @@ -1277,7 +1277,7 @@ downloadImage() { local tried="n" success="n" seconds="5" local i url sum size base language desc web_desc rc - if [[ "${version,,}" == "http"* ]]; then + if isURL "$version"; then base=$(basename "$iso") desc=$(fromFile "$base") diff --git a/src/power.sh b/src/power.sh index 3f4df5e4..9a4e0fbb 100644 --- a/src/power.sh +++ b/src/power.sh @@ -28,7 +28,7 @@ bootStatus() { [ ! -s "$QEMU_PTY" ] && return 1 - if [[ "${BOOT_MODE,,}" == "windows_legacy" ]]; then + if isLegacyBoot; then local line last recent # Only inspect output produced after the most recent BIOS boot attempt so @@ -130,7 +130,7 @@ waitForBoot() { elif bootKeyReady; then (( keyWait += 1 )) - if [[ "${BOOT_MODE,,}" == "windows_legacy" ]]; then + if isLegacyBoot; then # Keep the legacy fallback at about one second after the DVD marker. if (( keyWait >= 5 )); then if sendKey ret 0 100 6 0.25; then @@ -265,7 +265,7 @@ ready() { [ ! -s "$QEMU_PTY" ] && return 1 - if [[ "${BOOT_MODE,,}" == "windows_legacy" ]]; then + if isLegacyBoot; then legacyBootReady && return 0 return 1 fi @@ -358,7 +358,7 @@ bootKeyReady() { [ ! -s "$QEMU_PTY" ] && return 1 - if [[ "${BOOT_MODE,,}" == "windows_legacy" ]]; then + if isLegacyBoot; then grep -Fq "Booting from DVD/CD" "$QEMU_PTY" return $? fi @@ -372,7 +372,7 @@ bootKeyReady() { getBootMarker() { - if [[ "${BOOT_MODE,,}" == "windows_legacy" ]]; then + if isLegacyBoot; then grep -nE "$LEGACY_BOOT_PATTERN" "$QEMU_PTY" | tail -1 return 0 fi diff --git a/src/samba.sh b/src/samba.sh index 5a392bbe..c2ce2aa6 100644 --- a/src/samba.sh +++ b/src/samba.sh @@ -388,7 +388,7 @@ isUserMode && return 0 # Older Windows versions discover shares through NetBIOS, while modern Windows # uses Web Services Discovery. -if [[ "${BOOT_MODE:-}" == "windows_legacy" ]]; then +if isLegacyBoot; then startNetbios || : else startWsddn || :