Skip to content

Commit 3f514df

Browse files
authored
fix regression when installing on systems with dash (#591)
2 parents 22ce6cb + 086921a commit 3f514df

File tree

1 file changed

+46
-79
lines changed

1 file changed

+46
-79
lines changed

src/nixos-anywhere.sh

Lines changed: 46 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -726,53 +726,45 @@ runKexec() {
726726
abort "Could not create a temporary log file for $sshUser"
727727
fi
728728
729-
# Unified kexec error handling function
730-
handleKexecResult() {
731-
local exitCode=$1
732-
local operation=$2
729+
# Handle kexec operation failures
730+
handleKexecFailure() {
731+
local operation=$1
733732
734-
if [[ $exitCode -eq 0 ]]; then
735-
echo "$operation completed successfully" >&2
736-
else
737-
# If operation failed, try to fetch the log file
738-
local logContent=""
739-
if logContent=$(
740-
set +x
741-
runSsh "cat \"$remoteLogFile\" 2>/dev/null" 2>/dev/null
742-
); then
743-
echo "Remote output log:" >&2
744-
echo "$logContent" >&2
745-
fi
746-
echo "$operation failed" >&2
747-
exit 1
733+
# Try to fetch the log file
734+
local logContent=""
735+
if logContent=$(
736+
set +x
737+
runSsh "cat \"$remoteLogFile\" 2>/dev/null" 2>/dev/null
738+
); then
739+
echo "Remote output log:" >&2
740+
echo "$logContent" >&2
748741
fi
742+
echo "$operation failed" >&2
743+
exit 1
749744
}
750745
746+
# Extract directly to the user's home directory
747+
if [[ -z $remoteHomeDir ]]; then
748+
abort "Could not determine home directory for user $sshUser"
749+
fi
750+
751751
# Define common remote commands template
752752
local remoteCommandTemplate
753753
remoteCommandTemplate="
754-
${enableDebug:+set -x}
755-
# Create a script that we can run with sudo
756-
kexec_script_tmp=\$(mktemp /tmp/kexec-script.XXXXXX.sh)
757-
trap 'rm -f \"\$kexec_script_tmp\"' EXIT
758-
cat > \"\$kexec_script_tmp\" << 'KEXEC_SCRIPT'
759-
#!/usr/bin/env bash
760-
set -eu ${enableDebug}
761-
rm -rf /root/kexec
762-
mkdir -p /root/kexec
763-
cd /root/kexec
764-
echo 'Downloading kexec tarball (this may take a moment)...'
765-
# Execute tar command
766-
%TAR_COMMAND% && TMPDIR=/root/kexec setsid --wait /root/kexec/kexec/run --kexec-extra-flags $(printf '%q ' "$kexecExtraFlags")
767-
KEXEC_SCRIPT
768-
769-
# Run the script and let output flow naturally
770-
${maybeSudo} bash \"\$kexec_script_tmp\" 2>&1 | tee \"$remoteLogFile\" || true
754+
# Run kexec commands with sudo if needed
755+
{
756+
set -eu ${enableDebug}
757+
${maybeSudo} rm -rf \"$remoteHomeDir/kexec\"
758+
mkdir -p \"$remoteHomeDir/kexec\"
759+
cd \"$remoteHomeDir/kexec\"
760+
echo Downloading kexec tarball, this may take a moment...
761+
# Execute tar command
762+
%TAR_COMMAND%
763+
TMPDIR=\"$remoteHomeDir/kexec\" ${maybeSudo} setsid --wait \"$remoteHomeDir/kexec/kexec/run\" --kexec-extra-flags $(printf '%q' "$kexecExtraFlags")
764+
} 2>&1 | tee \"$remoteLogFile\" || true
765+
771766
# The script will likely disconnect us, so we consider it successful if we see the kexec message
772-
if grep -q 'machine will boot into nixos' \"$remoteLogFile\"; then
773-
echo 'Kexec initiated successfully'
774-
exit 0
775-
else
767+
if ! grep -q 'machine will boot into nixos' \"$remoteLogFile\"; then
776768
echo 'Kexec may have failed - check output above'
777769
exit 1
778770
fi
@@ -805,51 +797,26 @@ fi
805797
localUploadCommand=(curl --fail -Ss -L "${kexecUrl}")
806798
fi
807799
808-
# If no local upload command is defined, we use the remote command to download and execute
800+
# Determine the tar command based on upload method
801+
local tarCommand
809802
if [[ ${#localUploadCommand[@]} -eq 0 ]]; then
810-
# Use remote command for download and execution
811-
local tarCommand
803+
# Use remote command for download
812804
tarCommand="$(printf '%q ' "${remoteUploadCommand[@]}") | tar -xv ${tarDecomp}"
813-
local remoteCommands
814-
remoteCommands=${remoteCommandTemplate//'%TAR_COMMAND%'/$tarCommand}
815-
816-
# Run the SSH command - for kexec with sudo, we expect it might disconnect
817-
local sshExitCode
818-
(
819-
set +x
820-
runSsh sh -c "$(printf '%q' "$remoteCommands")"
821-
)
822-
sshExitCode=$?
823-
824-
handleKexecResult $sshExitCode "Kexec"
825805
else
826-
# Why do we need $remoteHomeDir?
827-
# In the case where the ssh user is not root, we need to upload the kexec tarball
828-
# to a location where the user has write permissions. We then use sudo to run
829-
# kexec from that location.
830-
if [[ -z $remoteHomeDir ]]; then
831-
abort "Could not determine home directory for user $sshUser"
832-
fi
833-
834-
(
835-
set +x
836-
"${localUploadCommand[@]}" | runSsh "cat > \"$remoteHomeDir\"/kexec-tarball.tar.gz"
837-
)
806+
# Upload the kexec tarball first
807+
"${localUploadCommand[@]}" | runSsh "cat > \"$remoteHomeDir\"/kexec-tarball.tar.gz"
808+
# Use local file for extraction
809+
tarCommand="cat \"$remoteHomeDir\"/kexec-tarball.tar.gz | tar -xv ${tarDecomp}"
810+
fi
838811
839-
# Use local command with pipe to remote
840-
local tarCommand="cat \"$remoteHomeDir\"/kexec-tarball.tar.gz | tar -xv ${tarDecomp}"
841-
local remoteCommands=${remoteCommandTemplate//'%TAR_COMMAND%'/$tarCommand}
812+
local remoteCommands
813+
remoteCommands=${remoteCommandTemplate//'%TAR_COMMAND%'/$tarCommand}
842814
843-
# Execute the local upload command and check for success
844-
local uploadExitCode
845-
(
846-
set +x
847-
runSsh sh -c "$(printf '%q' "$remoteCommands")"
848-
)
849-
uploadExitCode=$?
850-
851-
handleKexecResult $uploadExitCode "Upload"
852-
fi
815+
# Create and execute the script on the remote system
816+
runSsh "mkdir -p \"$remoteHomeDir/kexec\" && cat > \"$remoteHomeDir/kexec/unpack.sh\"" <<EOF
817+
$remoteCommands
818+
EOF
819+
runSsh "bash $remoteHomeDir/kexec/unpack.sh" || handleKexecFailure "Kexec"
853820
854821
# use the default SSH port to connect at this point
855822
local i

0 commit comments

Comments
 (0)