Skip to content

Commit 597a900

Browse files
committed
add fallback if a user has no home
this is useful for testing and more robust
1 parent 2fc9f9e commit 597a900

File tree

2 files changed

+86
-3
lines changed

2 files changed

+86
-3
lines changed

src/nixos-anywhere.sh

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,14 @@ runVmTest() {
465465

466466
uploadSshKey() {
467467
# ssh-copy-id requires this directory
468-
mkdir -p "$HOME/.ssh/"
468+
local sshCopyHome="$HOME"
469+
if ! mkdir -p "$HOME/.ssh/" 2>/dev/null; then
470+
# Fallback: create a temporary home directory for ssh-copy-id in sshKeyDir
471+
sshCopyHome="$sshKeyDir/ssh-home"
472+
mkdir -p "$sshCopyHome/.ssh"
473+
echo "Warning: Could not create $HOME/.ssh, using temporary directory: $sshCopyHome"
474+
fi
475+
469476
if [[ -n ${sshPrivateKeyFile} ]]; then
470477
cp "$sshPrivateKeyFile" "$sshKeyDir/nixos-anywhere"
471478
ssh-keygen -y -f "$sshKeyDir/nixos-anywhere" >"$sshKeyDir/nixos-anywhere.pub"
@@ -477,7 +484,7 @@ uploadSshKey() {
477484
step Uploading install SSH keys
478485
until
479486
if [[ ${envPassword} == y ]]; then
480-
sshpass -e \
487+
HOME="$sshCopyHome" sshpass -e \
481488
ssh-copy-id \
482489
-o ConnectTimeout=10 \
483490
"${sshArgs[@]}" \
@@ -486,7 +493,7 @@ uploadSshKey() {
486493
# To override `IdentitiesOnly=yes` set in `sshArgs` we need to set
487494
# `IdentitiesOnly=no` first as the first time an SSH option is
488495
# specified on the command line takes precedence
489-
ssh-copy-id \
496+
HOME="$sshCopyHome" ssh-copy-id \
490497
-o IdentitiesOnly=no \
491498
-o ConnectTimeout=10 \
492499
"${sshArgs[@]}" \

tests/ubuntu-kexec-test.nix

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{ pkgs, nixos-anywhere, kexec-installer, nix-vm-test, ... }:
2+
3+
let
4+
# Create dummy store paths for testing
5+
testPaths = pkgs.runCommand "test-store-paths" {} ''
6+
mkdir -p $out
7+
echo "echo 'Dummy disko script'" > $out/disko
8+
echo "echo 'Dummy system closure'" > $out/system
9+
chmod +x $out/disko $out/system
10+
'';
11+
in
12+
nix-vm-test.ubuntu."22_04" {
13+
memorySize = 2048;
14+
15+
# Forward SSH port to allow connection from the host
16+
virtualisation.forwardPorts = [
17+
{ from = "host"; host.port = 2222; guest.port = 22; }
18+
];
19+
20+
# Make the SSH keys available in the VM
21+
sharedDirs = {
22+
sshKeys = {
23+
source = "${nixos-anywhere}/tests/modules/ssh-keys";
24+
target = "/tmp/ssh-keys";
25+
};
26+
};
27+
28+
# The test script
29+
testScript = ''
30+
# Wait for the system to be fully booted
31+
vm.wait_for_unit("multi-user.target")
32+
33+
# Unmask SSH service (which is masked by default in the test VM)
34+
vm.succeed("systemctl unmask ssh.service")
35+
vm.succeed("systemctl unmask ssh.socket")
36+
vm.succeed("systemctl start ssh")
37+
38+
# Setup SSH with the existing keys
39+
vm.succeed("mkdir -p /root/.ssh")
40+
vm.succeed("cp /tmp/ssh-keys/ssh.pub /root/.ssh/authorized_keys")
41+
vm.succeed("chmod 644 /root/.ssh/authorized_keys")
42+
43+
# Setup SSH for connection from host
44+
vm.succeed("sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config")
45+
vm.succeed("systemctl restart ssh")
46+
47+
# Wait for SSH to be available
48+
vm.wait_for_open_port(22)
49+
print("SSH server is ready")
50+
51+
# Run nixos-anywhere from the host against the VM
52+
print("Starting kexec test phase...")
53+
54+
# Use Python's subprocess to run nixos-anywhere from the host
55+
import subprocess
56+
57+
cmd = f"""${nixos-anywhere}/bin/nixos-anywhere \\
58+
-i ${nixos-anywhere}/tests/modules/ssh-keys/ssh \\
59+
--ssh-port 2222 \\
60+
--phases kexec \\
61+
--kexec ${kexec-installer} \\
62+
--store-paths ${testPaths}/disko ${testPaths}/system \\
63+
--debug \\
64+
root@localhost
65+
"""
66+
67+
print(f"Running command: {cmd}")
68+
result = subprocess.run(cmd, shell=True, check=False)
69+
70+
if result.returncode == 0:
71+
print("kexec phase completed successfully")
72+
else:
73+
print(f"nixos-anywhere failed with exit code {result.returncode}")
74+
raise Exception("nixos-anywhere command failed")
75+
'';
76+
}

0 commit comments

Comments
 (0)