-
Notifications
You must be signed in to change notification settings - Fork 39
/
stage-1.nix
212 lines (190 loc) · 6.33 KB
/
stage-1.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
{ lib, pkgs, config, ... }:
with lib;
let
modules = pkgs.makeModulesClosure {
rootModules = config.boot.initrd.availableKernelModules ++ config.boot.initrd.kernelModules;
allowMissing = true;
kernel = config.system.build.kernel;
firmware = config.hardware.firmware;
};
plymouth = (pkgs.plymouth.override {
udev = null;
gtk3 = null;
systemd = null;
}).overrideDerivation (old: {
#src = /tmp/plymouth;
src = pkgs.fetchgit {
url = "https://anongit.freedesktop.org/git/plymouth";
rev = "266d954b7a0ff5b046df6ed54c22e3322b2c80d0";
sha256 = "10k7vfbfp3q1ysw3w5nd6wnixizbng3lqbb21bgd18v997k74xb3";
};
#patches2 = [ ./fix2.patch ./udev3.patch ];
});
dhcpcd = pkgs.dhcpcd.override { udev = null; };
extraUtils = pkgs.runCommandCC "extra-utils"
{
nativeBuildInputs = [ pkgs.nukeReferences ];
allowedReferences = [ "out" ];
} ''
set +o pipefail
mkdir -p $out/bin $out/lib
ln -s $out/bin $out/sbin
copy_bin_and_libs() {
[ -f "$out/bin/$(basename $1)" ] && rm "$out/bin/$(basename $1)"
cp -pd $1 $out/bin
}
# Copy Busybox
for BIN in ${pkgs.busybox}/{s,}bin/*; do
copy_bin_and_libs $BIN
done
copy_bin_and_libs ${pkgs.dhcpcd}/bin/dhcpcd
# Copy ld manually since it isn't detected correctly
cp -pv ${pkgs.glibc.out}/lib/ld*.so.? $out/lib
# Copy all of the needed libraries
find $out/bin $out/lib -type f | while read BIN; do
echo "Copying libs for executable $BIN"
LDD="$(ldd $BIN)" || continue
LIBS="$(echo "$LDD" | awk '{print $3}' | sed '/^$/d')"
for LIB in $LIBS; do
TGT="$out/lib/$(basename $LIB)"
if [ ! -f "$TGT" ]; then
SRC="$(readlink -e $LIB)"
cp -pdv "$SRC" "$TGT"
fi
done
done
# Strip binaries further than normal.
chmod -R u+w $out
stripDirs "lib bin" "-s"
# Run patchelf to make the programs refer to the copied libraries.
find $out/bin $out/lib -type f | while read i; do
if ! test -L $i; then
nuke-refs -e $out $i
fi
done
find $out/bin -type f | while read i; do
if ! test -L $i; then
echo "patching $i..."
patchelf --set-interpreter $out/lib/ld*.so.? --set-rpath $out/lib $i || true
fi
done
# Make sure that the patchelf'ed binaries still work.
echo "testing patched programs..."
$out/bin/ash -c 'echo hello world' | grep "hello world"
export LD_LIBRARY_PATH=$out/lib
$out/bin/mount --help 2>&1 | grep -q "BusyBox"
'';
shell = "${extraUtils}/bin/ash";
enablePlymouth = false;
dhcpHook = pkgs.writeScript "dhcpHook" ''
#!${shell}
'';
bootStage1 = pkgs.writeScript "stage1" ''
#!${shell}
echo
echo "[1;32m<<< NotOS Stage 1 >>>[0m"
echo
export PATH=${extraUtils}/bin/${lib.optionalString enablePlymouth ":${plymouth}/bin/"}
mkdir -p /proc /sys /dev /etc/udev /tmp /run/ /lib/ /mnt/ /var/log /etc/plymouth /bin
mount -t devtmpfs devtmpfs /dev/
mount -t proc proc /proc
mount -t sysfs sysfs /sys
${lib.optionalString enablePlymouth ''
ln -sv ${plymouth}/lib/plymouth /etc/plymouth/plugins
ln -sv ${plymouth}/etc/plymouth/plymouthd.conf /etc/plymouth/plymouthd.conf
ln -sv ${plymouth}/share/plymouth/plymouthd.defaults /etc/plymouth//plymouthd.defaults
ln -sv ${plymouth}/share/plymouth/themes /etc/plymouth/themes
ln -sv /dev/fb0 /dev/fb
''}
ln -sv ${shell} /bin/sh
ln -s ${modules}/lib/modules /lib/modules
${lib.optionalString enablePlymouth ''
# gdb --args plymouthd --debug --mode=boot --no-daemon
sleep 1
plymouth --show-splash
''}
for x in ${lib.concatStringsSep " " config.boot.initrd.kernelModules}; do
modprobe $x
done
root=/dev/vda
realroot=tmpfs
for o in $(cat /proc/cmdline); do
case $o in
systemConfig=*)
set -- $(IFS==; echo $o)
sysconfig=$2
;;
root=*)
set -- $(IFS==; echo $o)
root=$2
;;
netroot=*)
set -- $(IFS==; echo $o)
mkdir -pv /var/run /var/db
${lib.optionalString enablePlymouth ''plymouth display-message --text="waiting for eth"''}
sleep 5
${lib.optionalString enablePlymouth ''plymouth display-message --text="dhcp query"''}
dhcpcd eth0 -c ${dhcpHook}
${lib.optionalString enablePlymouth ''plymouth display-message --text="downloading rootfs"''}
tftp -g -r "$3" "$2"
root=/root.squashfs
;;
realroot=*)
set -- $(IFS==; echo $o)
realroot=$2
;;
esac
done
${lib.optionalString enablePlymouth ''plymouth display-message --text="mounting things"''}
${config.not-os.preMount}
if [ $realroot = tmpfs ]; then
mount -t tmpfs root /mnt/ -o size=1G || exec ${shell}
else
mount $realroot /mnt || exec ${shell}
fi
chmod 755 /mnt/
mkdir -p /mnt/nix/store/
${if config.not-os.nix then ''
# make the store writeable
mkdir -p /mnt/nix/.ro-store /mnt/nix/.overlay-store /mnt/nix/store
mount $root /mnt/nix/.ro-store -t squashfs
if [ $realroot = $1 ]; then
mount tmpfs -t tmpfs /mnt/nix/.overlay-store -o size=1G
fi
mkdir -pv /mnt/nix/.overlay-store/work /mnt/nix/.overlay-store/rw
modprobe overlay
mount -t overlay overlay -o lowerdir=/mnt/nix/.ro-store,upperdir=/mnt/nix/.overlay-store/rw,workdir=/mnt/nix/.overlay-store/work /mnt/nix/store
'' else ''
# readonly store
mount $root /mnt/nix/store/ -t squashfs
''}
${lib.optionalString enablePlymouth ''
plymouth --newroot=/mnt
plymouth update-root-fs --new-root-dir=/mnt --read-write
''}
exec env -i $(type -P switch_root) /mnt/ $sysconfig/init
exec ${shell}
'';
initialRamdisk = pkgs.makeInitrd {
contents = [ { object = bootStage1; symlink = "/init"; } ];
};
in
{
options = {
not-os.preMount = mkOption {
type = types.lines;
default = "";
};
boot.initrd.enable = mkOption {
type = types.bool;
default = true;
};
};
config = {
system.build.bootStage1 = bootStage1;
system.build.initialRamdisk = initialRamdisk;
system.build.extraUtils = extraUtils;
boot.initrd.availableKernelModules = [ ];
boot.initrd.kernelModules = [ "tun" "loop" "squashfs" ] ++ (lib.optional config.not-os.nix "overlay");
};
}