-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkexec.nix
63 lines (56 loc) · 1.91 KB
/
kexec.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
{ config, pkgs, ... }: {
system.build = rec {
image =
pkgs.runCommand "image" { buildInputs = [ pkgs.nukeReferences ]; } ''
mkdir "$out"
cp "${config.system.build.kernel}/bzImage" "$out/kernel"
cp "${config.system.build.netbootRamdisk}/initrd" "$out/initrd"
echo "init=${
builtins.unsafeDiscardStringContext config.system.build.toplevel
}/init ${toString config.boot.kernelParams}" > "$out/cmdline"
nuke-refs "$out/kernel"
'';
kexecScript = pkgs.writeScript "kexec-nixos" ''
#!${pkgs.stdenv.shell}
export PATH="${pkgs.kexectools}/bin:${pkgs.cpio}/bin:$PATH"
cd $(mktemp -d)
mkdir initrd
pushd initrd
for i in /etc/ssh/ssh_host_*; do
cat "$i" > "$(basename "$i")"
done
mv /tmp/ip-script .
${./ssh-keys} > ssh-keys 2>&1
chmod 755 ip-script
find -type f | cpio -o -H newc | gzip -9 > ../extra.gz
popd
cat "${image}/initrd" extra.gz > final.gz
kexec -l "${image}/kernel" --initrd=final.gz --append="init=${
builtins.unsafeDiscardStringContext config.system.build.toplevel
}/init ${toString config.boot.kernelParams}"
systemd-run --on-active=2 --timer-property=AccuracySec=100ms $(which kexec) -e
'';
};
boot.initrd.postMountCommands = ''
mkdir -p /mnt-root/etc/ssh /mnt-root/root/.ssh
umask 077
for i in /ssh_host_*; do
cat "$i" > /mnt-root/etc/ssh/"$i"
done
cat /ssh-keys > /mnt-root/root/.ssh/authorized_keys
cat /ip-script > /mnt-root/kexec-ips
chmod +x /mnt-root/kexec-ips
'';
networking.localCommands = ''
export PATH="${pkgs.iproute}/bin:$PATH"
/kexec-ips
'';
system.build.kexec_tarball =
pkgs.callPackage <nixpkgs/nixos/lib/make-system-tarball.nix> {
storeContents = [{
object = config.system.build.kexecScript;
symlink = "/kexec";
}];
contents = [ ];
};
}