Skip to content

Commit 152666c

Browse files
committed
pass env as initrd addition
1 parent 824fb1e commit 152666c

1 file changed

Lines changed: 36 additions & 12 deletions

File tree

os/installer/kexec.nix

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@
1212
(import ./config.nix args)
1313
];
1414

15-
boot.initrd.compressor = "xz";
15+
boot.initrd.compressor = "cat";
1616

1717
# https://github.com/nix-community/nixos-images/blob/main/nix/kexec-installer/module.nix#L50
1818
system.build.kexecInstallerTarball = pkgs.runCommand "kexec-tarball" { } ''
1919
mkdir xnodeos $out
20-
cp "${config.system.build.netbootRamdisk}/initrd" xnodeos/initrd
20+
cp "${config.system.build.netbootRamdisk}/initrd" xnodeos/initrd-base
2121
cp "${config.system.build.kernel}/${config.system.boot.loader.kernelFile}" xnodeos/bzImage
2222
cp "${config.system.build.kexecScript}" xnodeos/install
2323
cp "${pkgs.pkgsStatic.kexec-tools}/bin/kexec" xnodeos/kexec
24+
cp "${pkgs.pkgsStatic.coreutils}/bin/cp" xnodeos/cp
25+
cp "${pkgs.pkgsStatic.coreutils}/bin/mkdir" xnodeos/mkdir
26+
cp "${pkgs.pkgsStatic.findutils}/bin/find" xnodeos/find
2427
cp "${pkgs.pkgsStatic.iproute2.override { iptables = null; }}/bin/ip" xnodeos/ip
28+
cp "${pkgs.pkgsStatic.cpio}/bin/cpio" xnodeos/cpio
2529
tar -czvf $out/xnodeos-kexec-installer-${pkgs.stdenv.hostPlatform.system}.tar.gz xnodeos
2630
'';
2731

@@ -31,22 +35,43 @@
3135
pkgs.writeScript "kexec-boot" ''
3236
#!/usr/bin/env bash
3337
SCRIPT_DIR=$( cd -- "$( dirname -- "''${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
34-
NETWORK_CONFIG="{ \"address\": $(''${SCRIPT_DIR}/ip -j address show), \"route\": $(''${SCRIPT_DIR}/ip -j route show) }"
35-
''${SCRIPT_DIR}/kexec --load ''${SCRIPT_DIR}/bzImage \
36-
--initrd=''${SCRIPT_DIR}/initrd \
37-
--command-line "init=${config.system.build.toplevel}/init ${toString config.boot.kernelParams} && $(cat << EOF
38+
cd ''${SCRIPT_DIR}
3839
40+
./mkdir -p ./xnode-config
41+
42+
NETWORK_CONFIG=$(echo "{ \"address\": $(./ip --json address show), \"route\": $(./ip --json route show) }" | sed 's/"/\\"/g')
43+
cat << EOF > ./xnode-config/env
3944
export XNODE_OWNER="''${XNODE_OWNER}" && export DOMAIN="''${DOMAIN}" && export ACME_EMAIL="''${ACME_EMAIL}" && export USER_PASSWD="''${USER_PASSWD}" && export ENCRYPTED="''${ENCRYPTED}" && export NETWORK_CONFIG="''${NETWORK_CONFIG}" && export INITIAL_CONFIG="''${INITIAL_CONFIG}"
4045
EOF
41-
)"
42-
''${SCRIPT_DIR}/kexec -e
46+
47+
cp ./initrd-base ./initrd
48+
./find ./xnode-config | ./cpio --format newc --create >> ./initrd
49+
50+
./kexec --load ./bzImage \
51+
--initrd=./initrd \
52+
--command-line "init=${config.system.build.toplevel}/init ${toString config.boot.kernelParams}"
53+
./kexec -e
4354
''
4455
);
4556

57+
boot.initrd.systemd.enable = true;
58+
boot.initrd.systemd.services.restore-config-from-initrd = {
59+
unitConfig = {
60+
DefaultDependencies = false;
61+
RequiresMountsFor = "/sysroot /dev";
62+
};
63+
wantedBy = [ "initrd.target" ];
64+
requiredBy = [ "rw-etc.service" ];
65+
before = [ "rw-etc.service" ];
66+
serviceConfig.Type = "oneshot";
67+
script = ''
68+
cp -r xnode-config /sysroot
69+
'';
70+
};
71+
4672
systemd.services.install-xnodeos.script = lib.mkBefore ''
4773
# Extract environmental variables
48-
sed '2q;d' /proc/cmdline > /tmp/xnode-env
49-
source /tmp/xnode-env
74+
source /xnode-config/env
5075
'';
5176

5277
systemd.services.apply-network-config = {
@@ -66,8 +91,7 @@
6691
];
6792
script = ''
6893
# Extract environmental variables
69-
sed '2q;d' /proc/cmdline > /tmp/xnode-env
70-
source /tmp/xnode-env
94+
source /xnode-config/env
7195
7296
if [[ $NETWORK_CONFIG ]]; then
7397
interfaces=$(echo "$NETWORK_CONFIG" | jq -c '.address.[]')

0 commit comments

Comments
 (0)