From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pve-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9])
	by lore.proxmox.com (Postfix) with ESMTPS id 4BA111FF15C
	for <inbox@lore.proxmox.com>; Wed,  8 Jan 2025 16:29:23 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id DD21F1D524;
	Wed,  8 Jan 2025 16:29:08 +0100 (CET)
From: Daniel Kral <d.kral@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Wed,  8 Jan 2025 16:28:28 +0100
Message-Id: <20250108152828.232648-1-d.kral@proxmox.com>
X-Mailer: git-send-email 2.39.5
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.009 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [unconfigured.sh, proxmox-auto-installer.rs]
Subject: [pve-devel] [PATCH installer v2] fix #5984: unconfigured: do not
 reboot if auto-installer fails by default
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: pve-devel-bounces@lists.proxmox.com
Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com>

An unintended side effect was introduced in 21ef304, which caused the
auto-installer to reboot on error, even though the answer file option
`general.reboot_on_error` has been set to `false`.

When an error signal is received inside `unconfigured.sh` (after the
trap statement `trap 'err_reboot' ERR`), it causes any failing command
to execute the `err_reboot` function and therefore dropping the user in
the debug shell. One exception, as described under `trap` in bash(1), is
for a command being part of the test in an if statement. Therefore, if
the auto-installer failed, it won't trap to `err_reboot`.

Therefore, create a flag file "/run/proxmox-reboot-on-error" if the
auto-installer should reboot on error. If it should, the auto-installer
will be rebooted on error. If not, artificially trap to `err_reboot` to
drop into the debug shell.

The shell check at `err_reboot` can now be removed since the function is
called directly.

Fixes: 21ef304 ("unconfigured.sh: run proxmox-post-hook after successful auto-install")
Signed-off-by: Daniel Kral <d.kral@proxmox.com>
---
changes since v1 (thanks @Christoph):
- add a flag file "/run/proxmox-reboot-on-error"
- fix issue where `reboot_on_error` is ignored in general
- removing the shell check since `err_reboot` is called directly

---

Testing

I've tested this by booting the installer in debug mode, setting up DHCP
and patching the changes below. Otherwise I tested 8 possible scenarios,
where I simulated a failing auto install through trying to install a ZFS
RAID10 on only one disk and simulated a failing
post-installation-webhook by stopping the web server that handles the
POST request.

The following scenarios rebooted because of successful execution:

1. `reboot_on_error = false`, successful auto install
2. `reboot_on_error = false`, successful post-installation-webhook
3. `reboot_on_error = true`, successful auto install
4. `reboot_on_error = true`, successful post-installation-webhook

The following scenarios rebooted because of an error:

5. `reboot_on_error = true`, error during auto install
6. `reboot_on_error = true`, error during post-installation-webhook

And the following scenarios dropped into the debug shell:

7. `reboot_on_error = false`, error during auto install
8. `reboot_on_error = false`, error during post-installation-webhook

 .../src/bin/proxmox-auto-installer.rs         | 23 ++++++++-----------
 unconfigured.sh                               | 14 ++++++++---
 2 files changed, 20 insertions(+), 17 deletions(-)

diff --git a/proxmox-auto-installer/src/bin/proxmox-auto-installer.rs b/proxmox-auto-installer/src/bin/proxmox-auto-installer.rs
index ece9a94..22c727b 100644
--- a/proxmox-auto-installer/src/bin/proxmox-auto-installer.rs
+++ b/proxmox-auto-installer/src/bin/proxmox-auto-installer.rs
@@ -117,25 +117,20 @@ fn main() -> ExitCode {
         }
     };
 
+    if answer.global.reboot_on_error {
+        let _ = File::create("/run/proxmox-reboot-on-error");
+    }
+
     match run_installation(&answer, &locales, &runtime_info, &udevadm_info, &setup_info) {
-        Ok(_) => info!("Installation done."),
+        Ok(_) => {
+            info!("Installation done.");
+            ExitCode::SUCCESS
+        },
         Err(err) => {
             error!("Installation failed: {err:#}");
-            return exit_failure(answer.global.reboot_on_error);
+            ExitCode::FAILURE
         }
     }
-
-    ExitCode::SUCCESS
-}
-
-/// When we exit with a failure, the installer will not automatically reboot.
-/// Default value for reboot_on_error is false
-fn exit_failure(reboot_on_error: bool) -> ExitCode {
-    if reboot_on_error {
-        ExitCode::SUCCESS
-    } else {
-        ExitCode::FAILURE
-    }
 }
 
 fn run_installation(
diff --git a/unconfigured.sh b/unconfigured.sh
index 070cf33..4a8f09f 100755
--- a/unconfigured.sh
+++ b/unconfigured.sh
@@ -104,7 +104,6 @@ real_reboot() {
 }
 
 # reachable through the ERR trap
-# shellcheck disable=SC2317
 err_reboot() {
     printf "\nInstallation aborted - unable to continue (type exit or CTRL-D to reboot)\n"
     debugsh || true
@@ -254,12 +253,21 @@ elif [ $start_auto_installer -ne 0 ]; then
     fi
     echo "Starting automatic installation"
 
+    # creates "/run/proxmox-reboot-on-error" if `global.reboot_on_error = true`
     if /usr/bin/proxmox-auto-installer </run/automatic-installer-answers; then
         if ! /usr/bin/proxmox-post-hook </run/automatic-installer-answers; then
             echo "post installation hook failed to execute."
-            echo "waiting 30s to allow gathering the error before reboot."
-            sleep 30
+
+	    # drop into debug shell if it shouldn't reboot on error
+            if [ ! -f /run/proxmox-reboot-on-error ]; then
+                err_reboot
+            else
+                echo "waiting 30s to allow gathering the error before reboot."
+                sleep 30
+            fi
         fi
+    elif [ ! -f /run/proxmox-reboot-on-error ]; then
+        err_reboot
     fi
 else
     echo "Starting the installer GUI - see tty2 (CTRL+ALT+F2) for any errors..."
-- 
2.39.5



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel