-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfat-initramfs.nix
196 lines (153 loc) · 6.07 KB
/
fat-initramfs.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
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
{
pkgs ? (import <nixpkgs> {})
}:
pkgs.lib.makeExtensible (self: with self; {
maybeCall = f: arg: if builtins.isFunction f then f arg else f;
mountScript = "/bin/sh -i;";
kernelPackages = pkgs: pkgs.linuxPackagesFor pkgs.linux_latest;
udevPackages = pkgs: [];
kernelModulePackages = kp: [];
firmwarePackages = pkgs: [pkgs.firmwareLinuxNonfree];
tools = pkgs: [];
modprobeConfig = "";
blacklistUdevRules = [];
qemu = pkgs: pkgs.kvm;
qemuArgs = "--enable-kvm -m 2G -bios ${pkgs.OVMF.fd}/FV/OVMF.fd";
kernel = (maybeCall kernelPackages pkgs).kernel;
bzImage = kernel + "/bzImage";
firmwareSet = pkgs.buildEnv {
name = "firmware-set";
paths = maybeCall firmwarePackages pkgs;
pathsToLink = ["/lib/firmware"];
};
_kernelPackages = maybeCall kernelPackages pkgs;
_kernelModulePackages = [_kernelPackages.kernel] ++
maybeCall kernelModulePackages _kernelPackages;
kernelModules = pkgs.aggregateModules _kernelModulePackages;
_udevPackages = maybeCall udevPackages pkgs ++ [
pkgs.eudev pkgs.lvm2 pkgs.libinput
];
udevRules = pkgs.runCommand "udev-rules" {} ''
mkdir -p "$out"/etc/udev/{rules.d,hwdb.d}
for i in ${toString _udevPackages}; do
test -d "$i"/etc/udev/rules.d/ && ln -sf "$i"/etc/udev/rules.d/* "$out"/etc/udev/rules.d
test -d "$i"/var/lib/udev/rules.d/ && ln -sf "$i"/var/lib/udev/rules.d/* "$out"/etc/udev/rules.d
test -d "$i"/lib/udev/rules.d/ && ln -sf "$i"/lib/udev/rules.d/* "$out"/etc/udev/rules.d
test -d "$i"/etc/udev/hwdb.d/ && ln -sf "$i"/etc/udev/hwdb.d/* "$out"/etc/udev/hwdb.d
test -d "$i"/var/lib/udev/hwdb.d/ && ln -sf "$i"/var/lib/udev/hwdb.d/* "$out"/etc/udev/hwdb.d
test -d "$i"/lib/udev/hwdb.d/ && ln -sf "$i"/lib/udev/hwdb.d/* "$out"/etc/udev/hwdb.d
done
for i in ${toString blacklistUdevRules}; do
rm "$out/etc/udev/rules.d/$i" || true
done
${pkgs.eudev}/bin/udevadm hwdb -u -r "$out"
'';
modprobeConf = pkgs.writeText "modprobe.conf" (''
blacklist evbug
'' + modprobeConfig);
initTools = pkgs.buildEnv {
name = "init-tools";
paths =
(with pkgs; [
(lowPrio busybox)
kmod bashInteractive lvm2 cryptsetup coreutils
gnugrep gnused eudev strace util-linux e2fsprogs
])
++ (maybeCall tools pkgs);
ignoreCollisions = true;
pathsToLink = ["/"];
extraOutputsToInstall = ["bin" "out"];
};
initScript = ''
set -x
export PATH="/init-tools/bin"
{
mkdir -p /new-root /proc /sys /dev /run /tmp /etc
mount proc -t proc /proc
mount sysfs -t sysfs /sys
mount run-tmpfs -t tmpfs /run
mount dev-tmpfs -t devtmpfs /dev
mkdir -p /dev/shm /dev/pts /run/user /run/lock /run/nix
mount shm -t tmpfs /dev/shm
mount /devpts -t devpts /dev/pts
chmod a+rwxt /dev/shm
echo -n /init-tools/bin/modprobe > /proc/sys/kernel/modprobe
echo -n /firmware > /sys/module/firmware_class/parameters/path
ln -sfT /proc/mounts /etc/mtab
touch /etc/fstab
echo "Ready to start udev"
modprobe dm-mod
udevd &> /dev/null &
udevadm trigger --action=add
udevadm settle
vgchange -ay
udevd &> /dev/null &
udevadm trigger --action=add
udevadm settle
for i in /dev/sd?; do hdparm -B 255 $i; done
mount efivars -t pstore /sys/firmware/efi/efivars/
mkdir /new-root
} 2>&1 | tee /boot-log
echo "$targetSystem ## $targetInit" >> /boot-log
test -z "$targetSystem" && export targetSystem="$(cat /proc/cmdline | tr ' ' '\n' | grep '^targetSystem=' | sed -e 's/^targetSystem=//')"
test -z "$targetInit" && export targetInit="$(cat /proc/cmdline | tr ' ' '\n' | grep '^init=' | sed -e 's/^init=//')"
test -z "$targetInit" && targetInit="$targetSystem/bin/init"
echo "$targetSystem ## $targetInit" >> /boot-log
cat /proc/cmdline | tr ' ' '\n' | grep 'debug_premount=1' && sh -i
{
${mountScript}
} 2>&1 | tee -a /boot-log
cat /proc/cmdline | tr ' ' '\n' | grep 'debug_postmount=1' && sh -i
cd /new-root
export PATH="$PATH:$targetSystem/sw/bin"
chroot . test -e "$targetInit" || chroot . test -L "$targetInit" || {
echo "Oops, target init is not found"
sh -i
}
{
udevadm control -e
mkdir -p ./{proc,sys,dev,run}
mount --move /proc ./proc
mount --move /sys ./sys
mount --move /dev ./dev
mount --move /run ./run
ls -ld /new-root/nix/store
mount -o bind,ro ./nix/store ./nix/store
ls -ld /new-root/nix/store
mount -o bind,remount,ro ./nix/store ./nix/store
ls -ld /new-root/nix/store
} 2>&1 | tee -a /boot-log
mkdir -p ./var/log/boot/early
cp /boot-log ./var/log/boot/early/$(date +%Y%m%d-%H%M%S).log
echo "Ready for switch_root"
killall -s TERM
sleep 0.1
killall -s QUIT
sleep 0.1
killall -s KILL
exec switch_root . ./"$targetInit"
'';
init = pkgs.writeScript "init" ("#!/bin/sh\n" + initScript);
initrd = (pkgs.makeInitrd {
contents = [
{ object = init; symlink = "/init";}
{ object = kernelModules + "/lib/modules"; symlink = "/lib/modules"; }
{ object = initTools; symlink = "/init-tools"; }
{ object = pkgs.busybox; symlink = "/busybox"; }
{ object = initTools + "/bin/bash"; symlink = "/bin/sh"; }
{ object = initTools + "/bin/env"; symlink = "/usr/bin/env"; }
{ object = firmwareSet + "/lib/firmware"; symlink = "/firmware"; }
{ object = modprobeConf; symlink = "/etc/modprobe.d/modprobe.conf"; }
{ object = udevRules + "/etc/udev"; symlink = "/etc/udev"; }
];
}).overrideAttrs (x: { name = "${x.name}-${kernel.name}"; });
qemuScript = pkgs.writeScript "qemu-script" ''#! /bin/sh
${maybeCall qemu pkgs}/bin/qemu-system-$(echo "${builtins.currentSystem}" | sed -e "s/-.*//") \
${qemuArgs} -kernel ${bzImage} -initrd ${initrd}/initrd "$@"
'';
qemuScriptTextMode = pkgs.writeScript "qemu-script" ''#! /bin/sh
${maybeCall qemu pkgs}/bin/qemu-system-$(echo "${builtins.currentSystem}" | sed -e "s/-.*//") \
${qemuArgs} -kernel ${bzImage} -initrd ${initrd}/initrd \
-nographic -append "console=ttyS0" "$@"
'';
})