-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsystem.nix
More file actions
320 lines (268 loc) · 8.41 KB
/
system.nix
File metadata and controls
320 lines (268 loc) · 8.41 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
{
config,
pkgs,
modulesPath,
inputs,
...
}:
let
python3 = pkgs.python3.withPackages (
ps: with ps; [
tinyturing
tinygrad
numpy
numba
pillow
pyserial
psutil
]
);
in
{
imports = [
"${modulesPath}/installer/netboot/netboot.nix"
];
# *** netboot config
system.build.netboot = pkgs.runCommandLocal "netboot" { } ''
mkdir -p $out
ln -s ${config.system.build.kernel}/bzImage $out/bzImage
ln -s ${config.system.build.initialRamdisk}/initrd $out/initrd
'';
# *** kernel config
boot.kernelPackages = pkgs.linuxPackages_latest;
# *** initrd config
boot.initrd.systemd.enable = true;
boot.initrd.systemd.emergencyAccess = true;
boot.initrd.systemd.network.enable = true;
boot.initrd.systemd.network.networks.ethernet-default-dhcp = {
matchConfig = {
Name = [
"en*"
"eth*"
];
};
networkConfig = {
DHCP = "yes";
};
};
# mount tmpfs at sysroot
boot.initrd.systemd.mounts = [
{
where = "/";
what = "tmpfs";
type = "tmpfs";
options = "mode=0755";
}
];
# stop at initrd
boot.initrd.systemd.services.initrd-parse-etc.enable = false;
boot.initrd.systemd.services.initrd-find-nixos-closure.enable = false;
# *** kernel config
boot.initrd.includeDefaultModules = true;
boot.initrd.availableKernelModules = [
"igb"
"bnxt_en"
"mlx5_core"
"usbserial"
"cdc_ether"
"cdc_acm"
"usb_storage"
"uas"
# ipmi
"ipmi_msghandler"
"ipmi_devintf"
"ipmi_si"
"acpi_ipmi"
"ipmi_ssif"
];
boot.blacklistedKernelModules = [
"nouveau"
"amdgpu"
];
# *** system config
boot.initrd.systemd.extraBin = {
python3 = "${python3}/bin/python3";
ip = "${pkgs.iproute2}/bin/ip";
lsusb = "${pkgs.usbutils}/bin/lsusb";
usbreset = "${pkgs.usb-reset}/bin/usb-reset";
lspci = "${pkgs.pciutils}/bin/lspci";
nc = "${pkgs.netcat-openbsd}/bin/nc";
wget = "${pkgs.wget}/bin/wget";
sgdisk = "${pkgs.gptfdisk}/bin/sgdisk";
efibootmgr = "${pkgs.efibootmgr}/bin/efibootmgr";
ipmitool = "${pkgs.ipmitool}/bin/ipmitool";
grep = "${pkgs.gnugrep}/bin/grep";
mdadm = "${pkgs.mdadm}/bin/mdadm";
# busybox
busybox = "${pkgs.busybox}/bin/busybox";
pgrep = "${pkgs.busybox}/bin/busybox";
watch = "${pkgs.busybox}/bin/busybox";
awk = "${pkgs.busybox}/bin/busybox";
pkill = "${pkgs.busybox}/bin/busybox";
hostname = "${pkgs.busybox}/bin/busybox";
};
boot.initrd.systemd.storePaths = [
pkgs.busybox
pkgs.pciutils
python3
pkgs.python3
inputs.tinyos
];
boot.initrd.systemd.contents = {
"/opt/tinybox/service/display/docs_qr.png".source = "${inputs.tinyos}/service/display/docs_qr.png";
"/opt/tinybox/service/display/logo.png".source = "${inputs.tinyos}/service/display/logo.png";
"/opt/tinybox/service/display/service.py".source = "${inputs.tinyos}/service/display/service.py";
"/opt/tinybox/service/display/stats.py".source = "${inputs.tinyos}/service/display/stats.py";
};
boot.initrd.systemd.services.tinybox-display = {
description = "tinybox-display";
wantedBy = [ "initrd.target" ];
unitConfig.DefaultDependencies = "no";
unitConfig.StartLimitIntervalSec = 0;
serviceConfig = {
Type = "simple";
Restart = "always";
RestartSec = 1;
KillSignal = "SIGKILL";
TimeoutStopSec = "1s";
};
script = ''
python3 /opt/tinybox/service/display/service.py
'';
};
boot.initrd.systemd.services.takeover = {
description = "takeover";
wantedBy = [ "initrd.target" ];
after = [ "tinybox-display.service" ];
unitConfig.DefaultDependencies = "no";
serviceConfig = {
Type = "oneshot";
StandardOutput = "journal+console";
};
script = ''
set -x
set +u
source ${inputs.tinyos}/service/display/api.sh
wait_for_display 5
# wait for network to be up
while ! ip addr show | grep -q "scope global"; do
sleep 1
done
# find first /dev/sd{a-z} that is not mounted
drive=""
for i in a b c d e f g h i j k l m n o p q r s t u v w x y z; do
if ! mount | grep -q "/dev/sd$i" && [ -b "/dev/sd$i" ]; then
drive="/dev/sd$i"
break
fi
done
if [ -z "$drive" ]; then
# if no drive found, try /dev/nvme0n1
if [ -b "/dev/nvme0n1" ]; then
drive="/dev/nvme0n1"
fi
fi
if [ -z "$drive" ]; then
display_text "no drive found"
exit 1
fi
# see if the drive is used for a mdraid
if mdadm --examine "$drive" | grep -q "Raid"; then
display_text "drive is part of a raid"
exit 1
fi
display_text "using drive, $drive"
sleep 1
mkdir -p /tmp/tmp
mount -t tmpfs -o size=112G tmpfs /tmp/tmp
IMG_HOSTS="http://192.168.52.180:11001/tinyos"
# determine which image we are downloading and flashing
image_name="tinyos.core.img"
# see if any pci devices have NVIDIA
if lspci | grep -q "NVIDIA"; then
image_name="tinyos.green.img"
elif lspci | grep -qe "VGA.*AMD/ATI"; then
image_name="tinyos.red.img"
fi
display_text "downloading,$image_name"
sleep 1
# we can't write tinyos green image to nvme drives
if [[ "$image_name" == "tinyos.green.img" && "$drive" == /dev/nvme* ]]; then
display_text "can't flash to nvme,reboot box"
exit 1
fi
# download the os image
# first find a host that is accessible and that has both images
selected_host=""
for host in $IMG_HOSTS; do
if wget -q --spider "$host/$image_name"; then
selected_host="$host"
break
fi
done
if [ -z "$selected_host" ]; then
display_text "no download host found"
exit 1
else
display_text "using host,$selected_host"
fi
wget -b -o /tmp/log -O /tmp/tmp/tinyos.img "$selected_host/$image_name"
# wait until the image is downloaded
while true; do
sleep 1
# extract the downloaded percentage from the log file
percentage=$(grep -oP '\d+%' /tmp/log | tail -n1)
# extract the estimated time left from the log file
time_left=$(grep -oP '(\d+m)?\d+s' /tmp/log | tail -n1)
display_text "downloading,$image_name,$percentage - $time_left"
if ! pgrep -f "wget -b -o /tmp/log -O /tmp/tmp/tinyos.img" > /dev/null; then
break
fi
done
# see if the image was downloaded successfully by seeing if there is a 100% in the log file
if ! grep -q "100%" /tmp/log; then
display_text "download failed"
exit 1
fi
display_text "flashing,$image_name,$drive"
sleep 1
file_size=$(stat -c %s /tmp/tmp/tinyos.img)
watch -t -n1 pkill -USR1 dd > /dev/null &
# write the image to the drive
dd if=/tmp/tmp/tinyos.img of="$drive" bs=16M oflag=direct 2>&1 | while read -r line; do
case $line in
*"bytes"*)
# extract the written bytes from the line
written=$(echo "$line" | grep -oP '\d+' | head -n1)
# calculate the percentage of the written bytes
percentage=$(awk "BEGIN {print int(($written/$file_size)*100)}")
# extract the speed
speed=$(echo "$line" | grep -oP '(\d+.\d+MB/s)|(\d+ MB/s)|(\d+B/s)' || echo "0 MB/s")
display_text "flashing,$image_name,$drive,$percentage% - $speed"
;;
esac
done
pkill watch || true
display_text "flashed,$image_name,$drive"
# fix the backup gpt header
sgdisk -e "$drive"
# delete previous tinyos uefi boot entries no bash
entries="$(efibootmgr | grep -i "tinyos" | grep -oP 'Boot\d+' | grep -oP '\d+' || echo "")"
for entry in $entries; do
efibootmgr -b "$entry" -B
done
# tell uefi to boot from the internal drive
efibootmgr --create --disk "$drive" --part 1 --label "tinyos" --loader '\EFI\BOOT\BOOTX64.EFI'
bootnum="$(efibootmgr | grep -i "tinyos" | grep -oP 'Boot\d+' | grep -oP '\d+' | head -n1)"
efibootmgr -n "$bootnum"
display_text "Rebooting,$bootnum"
display_text "Rebooting..."
sleep 1
systemctl kill tinybox-display
reboot
'';
};
networking.hostName = "takeover";
# stateless so just set to latest
system.stateVersion = config.system.nixos.release;
}