Skip to content

Commit a848cb4

Browse files
authored
Merge pull request #153 from numtide/ssh-port
Add in ssh port option
2 parents abdec7b + 024bad7 commit a848cb4

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

src/nixos-anywhere.sh

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ Options:
1111
set the flake to install the system from.
1212
* -i <identity_file>
1313
selects which SSH private key file to use.
14+
* -p, --ssh-port <ssh_port>
15+
set the ssh port to connect with
16+
* --ssh-option <ssh_option>
17+
set an ssh option
1418
* -L, --print-build-logs
1519
print full build logs
1620
* -s, --store-paths <disko-script> <nixos-system>
@@ -23,6 +27,8 @@ Options:
2327
do not reboot after installation, allowing further customization of the target installation.
2428
* --kexec <url>
2529
use another kexec tarball to bootstrap NixOS
30+
* --post-kexec-ssh-port <ssh_port>
31+
after kexec is executed, use a custom ssh port to connect. Defaults to 22
2632
* --stop-after-disko
2733
exit after disko formating, you can then proceed to install manually or some other way
2834
* --extra-files <file...>
@@ -63,10 +69,12 @@ nix_options=(
6369
substitute_on_destination=y
6470
ssh_private_key_file=
6571
ssh_tty_param="-T"
72+
post_kexec_ssh_port=22
6673

6774
declare -A disk_encryption_keys
6875
declare -a nix_copy_options
6976
declare -a ssh_copy_id_args
77+
declare -a ssh_args
7078

7179
while [[ $# -gt 0 ]]; do
7280
case "$1" in
@@ -78,6 +86,14 @@ while [[ $# -gt 0 ]]; do
7886
ssh_private_key_file=$2
7987
shift
8088
;;
89+
-p | --ssh-port)
90+
ssh_args+=("-p" "$2")
91+
shift
92+
;;
93+
--ssh-option)
94+
ssh_args+=("-o" "$2")
95+
shift
96+
;;
8197
-L | --print-build-logs)
8298
print_build_logs=y
8399
;;
@@ -98,6 +114,10 @@ while [[ $# -gt 0 ]]; do
98114
kexec_url=$2
99115
shift
100116
;;
117+
--post-kexec-ssh-port)
118+
post_kexec_ssh_port=$2
119+
shift
120+
;;
101121
--debug)
102122
enable_debug="-x"
103123
print_build_logs=y
@@ -158,10 +178,10 @@ fi
158178

159179
# ssh wrapper
160180
timeout_ssh_() {
161-
timeout 10 ssh -i "$ssh_key_dir"/nixos-anywhere -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "$ssh_connection" "$@"
181+
timeout 10 ssh -i "$ssh_key_dir"/nixos-anywhere -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "${ssh_args[@]}" "$ssh_connection" "$@"
162182
}
163183
ssh_() {
164-
ssh "$ssh_tty_param" -i "$ssh_key_dir"/nixos-anywhere -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "$ssh_connection" "$@"
184+
ssh "$ssh_tty_param" -i "$ssh_key_dir"/nixos-anywhere -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "${ssh_args[@]}" "$ssh_connection" "$@"
165185
}
166186

167187
nix_copy() {
@@ -242,6 +262,7 @@ until
242262
-o UserKnownHostsFile=/dev/null \
243263
-o StrictHostKeyChecking=no \
244264
"${ssh_copy_id_args[@]}" \
265+
"${ssh_args[@]}" \
245266
"$ssh_connection"
246267
do
247268
sleep 3
@@ -327,13 +348,21 @@ SSH
327348
TMPDIR=/root/kexec setsid ${maybe_sudo} /root/kexec/kexec/run
328349
SSH
329350

330-
# wait for machine to become unreachable
351+
# use the default SSH port to connect at this point
352+
for i in "${!ssh_args[@]}"; do
353+
if [[ ${ssh_args[i]} == "-p" ]]; then
354+
ssh_args[i + 1]=$post_kexec_ssh_port
355+
break
356+
fi
357+
done
358+
359+
# wait for machine to become unreachable.
331360
while timeout_ssh_ -- exit 0; do sleep 1; done
332361

333362
# After kexec we explicitly set the user to root@
334363
ssh_connection="root@${ssh_host}"
335364

336-
# watiting for machine to become available again
365+
# waiting for machine to become available again
337366
until ssh_ -o ConnectTimeout=10 -- exit 0; do sleep 5; done
338367
fi
339368
for path in "${!disk_encryption_keys[@]}"; do
@@ -400,7 +429,7 @@ nohup bash -c '${maybe_reboot}' >/dev/null &
400429
SSH
401430

402431
if [[ -n ${maybe_reboot} ]]; then
403-
step Waiting for the maching to become reachable again
432+
step Waiting for the machine to become reachable again
404433
while timeout_ssh_ -- exit 0; do sleep 1; done
405434
fi
406435

0 commit comments

Comments
 (0)