Skip to content

Commit 4fd4c67

Browse files
committed
runKexec: reduce number of ssh command needed for kexec
if the user has a sudo password set, we were requiring multiple times to enter the sudo password. This now runs everything in one command to reduce the number of sudo invocation.
1 parent 1e2882c commit 4fd4c67

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

src/nixos-anywhere.sh

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -626,32 +626,55 @@ runKexec() {
626626
fi
627627
628628
step Switching system into kexec
629-
runSsh sh <<SSH
630-
set -efu ${enableDebug}
631-
$maybeSudo rm -rf /root/kexec
632-
$maybeSudo mkdir -p /root/kexec
633-
SSH
634629
635630
# no way to reach global ipv4 destinations, use gh-v6.com automatically if github url
636631
if [[ ${hasIpv6Only} == "y" ]] && [[ $kexecUrl == "https://github.com/"* ]]; then
637632
kexecUrl=${kexecUrl/"github.com"/"gh-v6.com"}
638633
fi
639634
635+
# Define common remote commands template
636+
local remoteCommandTemplate="
637+
set -eu ${enableDebug}
638+
${maybeSudo} rm -rf /root/kexec
639+
${maybeSudo} mkdir -p /root/kexec
640+
%TAR_COMMAND%
641+
TMPDIR=/root/kexec setsid sudo /root/kexec/kexec/run --kexec-extra-flags \"$kexecExtraFlags\"
642+
"
643+
644+
# Define upload commands
645+
local localUploadCommand=()
646+
local remoteUploadCommand=()
647+
640648
if [[ -f $kexecUrl ]]; then
641-
runSsh "${maybeSudo} tar -C /root/kexec -xvzf-" <"$kexecUrl"
642-
elif [[ ${hasCurl} == "y" ]]; then
643-
runSsh "curl --fail -Ss -L '${kexecUrl}' | ${maybeSudo} tar -C /root/kexec -xvzf-"
644-
elif [[ ${hasWget} == "y" ]]; then
645-
runSsh "wget '${kexecUrl}' -O- | ${maybeSudo} tar -C /root/kexec -xvzf-"
649+
localUploadCommand=(cat "$kexecUrl")
650+
elif [[ $hasWget == "y" ]]; then
651+
remoteUploadCommand=(wget "$kexecUrl" -O-)
652+
elif [[ $hasCurl == "y" ]]; then
653+
remoteUploadCommand=(curl --fail -Ss -L "$kexecUrl")
646654
else
647-
curl --fail -Ss -L "${kexecUrl}" | runSsh "${maybeSudo} tar -C /root/kexec -xvzf-"
655+
# Fallback to local curl
656+
localUploadCommand=(curl --fail -Ss -L "${kexecUrl}")
648657
fi
649658
650-
runSsh <<SSH
651-
TMPDIR=/root/kexec setsid ${maybeSudo} /root/kexec/kexec/run --kexec-extra-flags "${kexecExtraFlags}"
652-
SSH
659+
local tarCommand
660+
local remoteCommands
661+
if [[ ${#localUploadCommand[@]} -eq 0 ]]; then
662+
# Use remote command for download and execution
663+
tarCommand="$(printf '%q ' "${remoteUploadCommand[@]}") | ${maybeSudo} tar -C /root/kexec -xvzf-"
664+
665+
remoteCommands=${remoteCommandTemplate//'%TAR_COMMAND%'/$tarCommand}
666+
667+
runSsh sh -c "$remoteCommands"
668+
else
669+
# Use local command with pipe to remote
670+
tarCommand="${maybeSudo} tar -C /root/kexec -xvzf-"
671+
remoteCommands=${remoteCommandTemplate//'%TAR_COMMAND%'/$tarCommand}
672+
673+
"${localUploadCommand[@]}" | runSsh sh -c "$remoteCommands"
674+
fi
653675
654676
# use the default SSH port to connect at this point
677+
local i
655678
for i in "${!sshArgs[@]}"; do
656679
if [[ ${sshArgs[i]} == "-p" ]]; then
657680
sshArgs[i + 1]=$postKexecSshPort

0 commit comments

Comments
 (0)