Skip to content

Commit bc77bd1

Browse files
Mic92mergify[bot]
authored andcommitted
use early return in runKexec
1 parent ef708ac commit bc77bd1

File tree

1 file changed

+46
-44
lines changed

1 file changed

+46
-44
lines changed

src/nixos-anywhere.sh

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -375,65 +375,67 @@ importFacts() {
375375
}
376376

377377
runKexec() {
378-
if [[ ${isKexec-n} == "n" ]] && [[ ${isInstaller-n} == "n" ]]; then
379-
if [[ ${isContainer-none} != "none" ]]; then
380-
echo "WARNING: This script does not support running from a '${isContainer}' container. kexec will likely not work" >&2
381-
fi
378+
if [[ ${isKexec-n} == "y" ]] || [[ ${isInstaller-n} == "y" ]]; then
379+
return
380+
fi
382381

383-
if [[ $kexecUrl == "" ]]; then
384-
case "${isArch-unknown}" in
385-
x86_64 | aarch64)
386-
kexecUrl="https://github.com/nix-community/nixos-images/releases/download/nixos-24.05/nixos-kexec-installer-noninteractive-${isArch}-linux.tar.gz"
387-
;;
388-
*)
389-
abort "Unsupported architecture: ${isArch}. Our default kexec images only support x86_64 and aarch64 cpus. Checkout https://github.com/nix-community/nixos-anywhere/#using-your-own-kexec-image for more information."
390-
;;
391-
esac
392-
fi
382+
if [[ ${isContainer-none} != "none" ]]; then
383+
echo "WARNING: This script does not support running from a '${isContainer}' container. kexec will likely not work" >&2
384+
fi
385+
386+
if [[ $kexecUrl == "" ]]; then
387+
case "${isArch-unknown}" in
388+
x86_64 | aarch64)
389+
kexecUrl="https://github.com/nix-community/nixos-images/releases/download/nixos-24.05/nixos-kexec-installer-noninteractive-${isArch}-linux.tar.gz"
390+
;;
391+
*)
392+
abort "Unsupported architecture: ${isArch}. Our default kexec images only support x86_64 and aarch64 cpus. Checkout https://github.com/nix-community/nixos-anywhere/#using-your-own-kexec-image for more information."
393+
;;
394+
esac
395+
fi
393396

394-
step Switching system into kexec
395-
runSsh sh <<SSH
397+
step Switching system into kexec
398+
runSsh sh <<SSH
396399
set -efu ${enableDebug}
397400
$maybeSudo rm -rf /root/kexec
398401
$maybeSudo mkdir -p /root/kexec
399402
SSH
400403

401-
# no way to reach global ipv4 destinations, use gh-v6.com automatically if github url
402-
if [[ ${hasIpv6_only-n} == "y" ]] && [[ $kexecUrl == "https://github.com/"* ]]; then
403-
kexecUrl=${kexecUrl/"github.com"/"gh-v6.com"}
404-
fi
404+
# no way to reach global ipv4 destinations, use gh-v6.com automatically if github url
405+
if [[ ${hasIpv6_only-n} == "y" ]] && [[ $kexecUrl == "https://github.com/"* ]]; then
406+
kexecUrl=${kexecUrl/"github.com"/"gh-v6.com"}
407+
fi
405408

406-
if [[ -f $kexecUrl ]]; then
407-
runSsh "${maybeSudo} tar -C /root/kexec -xvzf-" <"$kexecUrl"
408-
elif [[ ${hasCurl-n} == "y" ]]; then
409-
runSsh "curl --fail -Ss -L '${kexecUrl}' | ${maybeSudo} tar -C /root/kexec -xvzf-"
410-
elif [[ ${hasWget-n} == "y" ]]; then
411-
runSsh "wget '${kexecUrl}' -O- | ${maybeSudo} tar -C /root/kexec -xvzf-"
412-
else
413-
curl --fail -Ss -L "${kexecUrl}" | runSsh "${maybeSudo} tar -C /root/kexec -xvzf-"
414-
fi
409+
if [[ -f $kexecUrl ]]; then
410+
runSsh "${maybeSudo} tar -C /root/kexec -xvzf-" <"$kexecUrl"
411+
elif [[ ${hasCurl-n} == "y" ]]; then
412+
runSsh "curl --fail -Ss -L '${kexecUrl}' | ${maybeSudo} tar -C /root/kexec -xvzf-"
413+
elif [[ ${hasWget-n} == "y" ]]; then
414+
runSsh "wget '${kexecUrl}' -O- | ${maybeSudo} tar -C /root/kexec -xvzf-"
415+
else
416+
curl --fail -Ss -L "${kexecUrl}" | runSsh "${maybeSudo} tar -C /root/kexec -xvzf-"
417+
fi
415418

416-
runSsh <<SSH
419+
runSsh <<SSH
417420
TMPDIR=/root/kexec setsid ${maybeSudo} /root/kexec/kexec/run --kexec-extra-flags "${kexecExtraFlags}"
418421
SSH
419422

420-
# use the default SSH port to connect at this point
421-
for i in "${!sshArgs[@]}"; do
422-
if [[ ${sshArgs[i]} == "-p" ]]; then
423-
sshArgs[i + 1]=$postKexecSshPort
424-
break
425-
fi
426-
done
423+
# use the default SSH port to connect at this point
424+
for i in "${!sshArgs[@]}"; do
425+
if [[ ${sshArgs[i]} == "-p" ]]; then
426+
sshArgs[i + 1]=$postKexecSshPort
427+
break
428+
fi
429+
done
427430

428-
# wait for machine to become unreachable.
429-
while runSshTimeout -- exit 0; do sleep 1; done
431+
# wait for machine to become unreachable.
432+
while runSshTimeout -- exit 0; do sleep 1; done
430433

431-
# After kexec we explicitly set the user to root@
432-
sshConnection="root@${sshHost}"
434+
# After kexec we explicitly set the user to root@
435+
sshConnection="root@${sshHost}"
433436

434-
# waiting for machine to become available again
435-
until runSsh -o ConnectTimeout=10 -- exit 0; do sleep 5; done
436-
fi
437+
# waiting for machine to become available again
438+
until runSsh -o ConnectTimeout=10 -- exit 0; do sleep 5; done
437439
}
438440

439441
runDisko() {

0 commit comments

Comments
 (0)