blob: e25ba9d095415583c5a744c6cdc845a97703f850 (
plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
{
config,
lib,
...
}:
with lib; let
cfg = config.local.boot.pxe;
in {
options.local.boot.pxe = {
enable = mkEnableOption "PXE netboot";
setupMode = mkOption {
type = types.bool;
default = false;
};
initrdInterface = mkOption {
type = types.str;
};
linkLocal6 = mkOption {
type = types.str;
};
networkDriver = mkOption {
type = types.str;
};
mac = mkOption {
type = types.str;
};
panicTimeout = mkOption {
type = types.nullOr types.ints.u16;
default = 30;
};
};
# Based on nixpkgs/nixos/modules/installer/netboot/netboot.nix
config = mkIf cfg.enable {
boot = {
initrd = {
kernelModules = [cfg.networkDriver];
network = {
enable = true;
flushBeforeStage2 = true;
ssh = {
enable = true;
ignoreEmptyHostKeys = true;
};
};
preFailCommands = mkIf (!cfg.setupMode) ''
echo "init stage1 failed in unattended mode, rebooting"
reboot -f
'';
postResumeCommands = ''
store_img_dir="/nix/.ro-store-img"
store_img="$store_img_dir/nix-store.squashfs"
mkdir -p "$store_img_dir"
mount -t tmpfs -o mode=0700,nr_inodes=2,huge=within_size,nodev,nosuid tmpfs "$store_img_dir"
ip link set up ${cfg.initrdInterface}
ip addr add ${cfg.linkLocal6}/64 dev ${cfg.initrdInterface}
recv_init_timeout=120
echo -n "Waiting up to ''${recv_init_timeout}s for $store_img"
t="$recv_init_timeout"
while true; do
echo -n . | nc -u -w1 -s ${cfg.linkLocal6}%${cfg.initrdInterface} fe80::b007:b007%${cfg.initrdInterface} 1792 >/dev/null 2>&1 &
if [ -e "$store_img.tmp" ]; then
kill -x nc
break
elif [ "$t" -le 0 ]; then
echo "timed out"
fail
fi
# sleep builtin de ash retorna cuando nc termina (lo cual puede pasar muchas veces debido a IPv6 DAD)
/bin/sleep 1
echo -n .
t=$((t - 1))
done
recv_poll=60
recv_timeout=1800
t=0
last_mtime=""
poll_time="$recv_poll"
while true; do
if [ -e "$store_img" ]; then
break
elif [ "$t" -ge "$recv_timeout" ]; then
echo "Store image upload timed out"
fail
fi
sleep 1
t=$((t + 1))
poll_time=$((poll_time - 1))
if [ "$poll_time" -le 0 ]; then
current_size=$(stat -c '%s' "$store_img.tmp" || echo -)
current_mtime=$(stat -c '%y' "$store_img.tmp" || echo final)
echo "Store upload: $current_mtime: $current_size bytes"
if [ "$current_mtime" = "$last_mtime" ]; then
echo "Store image upload stalled, image not modified in ''${recv_poll}s"
fail
fi
last_mtime="$current_mtime"
poll_time="$recv_poll"
fi
done
sleep 1
kill -x sshd
chmod 0500 "$store_img_dir"
chmod 0400 "$store_img"
chattr +i "$store_img"
mount -o remount,ro "$store_img_dir"
store_size=$(stat -c '%s' "$store_img")
echo "Store image upload complete, final size is $store_size bytes"
ln -sf "$store_img" /dev/nix-store
'';
postMountCommands = ''
mkdir -p "/mnt-root$store_img_dir"
mount --move "$store_img_dir" "/mnt-root$store_img_dir"
'';
};
kernelParams =
optional (cfg.panicTimeout != null) "panic=${toString cfg.panicTimeout}"
++ optional cfg.setupMode "boot.shell_on_fail";
};
fileSystems = {
"/" = {
fsType = "tmpfs";
neededForBoot = true;
options = ["mode=0755"];
};
"/nix/store" = {
fsType = "overlay";
neededForBoot = true;
overlay = {
lowerdir = ["/nix/.ro-store"];
upperdir = "/nix/.rw-store/store";
workdir = "/nix/.rw-store/work";
};
};
"/nix/.ro-store" = {
fsType = "squashfs";
neededForBoot = true;
device = "/dev/nix-store";
options = ["loop" "threads=multi"];
};
"/nix/.rw-store" = {
fsType = "tmpfs";
neededForBoot = true;
options = ["mode=0755"];
};
};
local = {
boot.loader = "out-of-band";
};
};
}
|