Skip to content

Commit 39a80dc

Browse files
authored
Merge pull request #580 from nix-community/push-mrpmsymxtmkz
nixos-anywhere: reimport facts after kexec and throw if kexec failed
2 parents 213ee01 + 8441ad7 commit 39a80dc

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

src/get-facts.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ isNixos=$(if test -f /etc/os-release && grep -Eq 'ID(_LIKE)?="?nixos"?' /etc/os-
77
cat <<FACTS
88
isOs=$(uname)
99
isArch=$(uname -m)
10-
isKexec=$(if test -f /etc/is_kexec; then echo "y"; else echo "n"; fi)
1110
isNixos=$isNixos
1211
isInstaller=$(if [ "$isNixos" = "y" ] && grep -Eq 'VARIANT_ID="?installer"?' /etc/os-release; then echo "y"; else echo "n"; fi)
1312
isContainer=$(if [ "$(has systemd-detect-virt)" = "y" ]; then systemd-detect-virt --container; else echo "none"; fi)
13+
isRoot=$(if [ "$(id -u)" -eq 0 ]; then echo "y"; else echo "n"; fi)
1414
hasIpv6Only=$(if [ "$(has ip)" = "n" ] || ip r g 1 >/dev/null 2>/dev/null || ! ip -6 r g :: >/dev/null 2>/dev/null; then echo "n"; else echo "y"; fi)
1515
hasTar=$(has tar)
1616
hasCpio=$(has cpio)

src/nixos-anywhere.sh

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ envPassword=n
4646
# Facts set by get-facts.sh
4747
isOs=
4848
isArch=
49-
isKexec=
5049
isInstaller=
5150
isContainer=
51+
isRoot=
5252
hasIpv6Only=
5353
hasTar=
5454
hasCpio=
@@ -520,17 +520,31 @@ importFacts() {
520520
fi
521521
filteredFacts=$(echo "$facts" | grep -E '^(has|is)[A-Za-z0-9_]+=\S+')
522522
if [[ -z $filteredFacts ]]; then
523-
abort "Retrieving host facts via ssh failed. Check with --debug for the root cause, unless you have done so already"
523+
abort "Retrieving host facts via SSH failed. Check with --debug for the root cause, unless you have done so already"
524524
fi
525525
# make facts available in script
526526
# shellcheck disable=SC2046
527527
export $(echo "$filteredFacts" | xargs)
528528

529-
for var in isOs isArch isKexec isInstaller isContainer hasIpv6Only hasTar hasCpio hasSudo hasDoas hasWget hasCurl hasSetsid; do
529+
# Necessary to prevent Bash erroring before printing out which fact had an issue
530+
set +u
531+
for var in isOs isArch isInstaller isContainer isRoot hasIpv6Only hasTar hasCpio hasSudo hasDoas hasWget hasCurl hasSetsid; do
530532
if [[ -z ${!var} ]]; then
531533
abort "Failed to retrieve fact $var from host"
532534
fi
533535
done
536+
set -u
537+
538+
if [[ ${isRoot} == "y" ]]; then
539+
maybeSudo=
540+
elif [[ ${hasSudo} == "y" ]]; then
541+
maybeSudo=sudo
542+
elif [[ ${hasDoas} == "y" ]]; then
543+
maybeSudo=doas
544+
else
545+
# shellcheck disable=SC2016
546+
abort 'Unable to find a command to use to escalate privileges: Could not find `sudo` or `doas`'
547+
fi
534548
}
535549

536550
checkBuildLocally() {
@@ -577,7 +591,6 @@ checkBuildLocally() {
577591
}
578592
579593
generateHardwareConfig() {
580-
local maybeSudo="$maybeSudo"
581594
mkdir -p "$(dirname "$hardwareConfigPath")"
582595
case "$hardwareConfigBackend" in
583596
nixos-facter)
@@ -618,7 +631,7 @@ generateHardwareConfig() {
618631
}
619632
620633
runKexec() {
621-
if [[ ${isKexec} == "y" ]] || [[ ${isInstaller} == "y" ]]; then
634+
if [[ ${isInstaller} == "y" ]]; then
622635
return
623636
fi
624637
@@ -701,11 +714,14 @@ TMPDIR=/root/kexec setsid --wait ${maybeSudo} /root/kexec/kexec/run --kexec-extr
701714
# After kexec we explicitly set the user to root@
702715
sshConnection="root@${sshHost}"
703716
704-
# TODO: remove this after we reimport facts post-kexec and set this as a fact
705-
maybeSudo=""
706-
707717
# waiting for machine to become available again
708718
until runSsh -o ConnectTimeout=10 -- exit 0; do sleep 5; done
719+
720+
importFacts
721+
722+
if [[ ${isInstaller} == "n" ]]; then
723+
abort "Failed to kexec into NixOS installer"
724+
fi
709725
}
710726
711727
runDisko() {
@@ -864,13 +880,6 @@ main() {
864880
abort "no setsid command found, but required to run the kexec script under a new session"
865881
fi
866882
867-
maybeSudo=""
868-
if [[ ${hasSudo-n} == "y" ]]; then
869-
maybeSudo="sudo"
870-
elif [[ ${hasDoas-n} == "y" ]]; then
871-
maybeSudo="doas"
872-
fi
873-
874883
if [[ ${isOs} != "Linux" ]]; then
875884
abort "This script requires Linux as the operating system, but got $isOs"
876885
fi

0 commit comments

Comments
 (0)