forked from cleverca22/not-os
-
Notifications
You must be signed in to change notification settings - Fork 2
/
stage-1.nix
202 lines (182 loc) · 5.82 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
{ lib, pkgs, config, ... }:
with lib;
let
rootModules =
config.boot.initrd.availableKernelModules ++
config.boot.initrd.kernelModules;
modules = pkgs.makeModulesClosure {
rootModules = rootModules;
kernel = config.system.build.kernel;
firmware = config.hardware.firmware;
allowMissing = true;
};
dhcpcd = pkgs.dhcpcd.override { udev = null; };
extraUtils = pkgs.runCommandCC "extra-utils"
{
buildInputs = [ 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";
dhcpHook = pkgs.writeScript "dhcpHook" ''
#!${shell}
'';
bootStage1Text = ''
#!${shell}
echo "[37;40mEntering stage-1...[0m"
echo @stage-1@
export PATH=${extraUtils}/bin/
echo Creating base file systems...
mkdir -p /proc /sys /dev /etc/udev /tmp /run/ /lib/ /mnt/ /var/log /bin
mount -t devtmpfs devtmpfs /dev/
mount -t proc proc /proc
mount -t sysfs sysfs /sys
ln -s ${shell} /bin/sh
ln -s ${modules}/lib/modules /lib/modules
root=/dev/vda
realroot=tmpfs
for o in $(cat /proc/cmdline); do
case $o in
root=*)
set -- $(IFS==; echo $o)
root=$2
;;
netroot=*)
set -- $(IFS==; echo $o)
mkdir -pv /var/run /var/db
sleep 5
dhcpcd eth0 -c ${dhcpHook}
tftp -g -r "$3" "$2"
root=/root.squashfs
;;
realroot=*)
set -- $(IFS==; echo $o)
realroot=$2
;;
esac
done
echo Using ${extraUtils}...
echo Using ${modules}...
for x in ${lib.concatStringsSep " " config.boot.initrd.kernelModules}; do
echo Loading kernel module $x...
modprobe $x
done
${if config.not-os.live then ''
echo Creating writable Nix store...
mount -t tmpfs root /mnt/ -o size=1G || exec ${shell}
chmod 755 /mnt/
mkdir -p /mnt/nix/.ro-store /mnt/nix/.overlay-store /mnt/nix/store
mount $root /mnt/nix/.ro-store -t squashfs
mount tmpfs -t tmpfs /mnt/nix/.overlay-store -o size=1G
mkdir -p /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 ''
echo Creating readonly Nix store...
# maybe libcrc32c is not necessary
modprobe jbd2
modprobe fscrypto
modprobe mbcache
modprobe crc16
modprobe libcrc32c
modprobe crc32c_generic
modprobe ext4
modprobe af_packet
modprobe isofs
mount -t ext4 -o rw,exec $root /mnt
''}
echo Switching root filesystem...
exec env -i $(type -P switch_root) /mnt/ \
${builtins.unsafeDiscardStringContext (toString config.system.build.bootStage2)}
exec ${shell}
'';
bootStage1 = pkgs.runCommand "stage-1" {
text = bootStage1Text;
passAsFile = [ "text" ];
preferLocalBuild = true;
allowSubstitutes = false;
} ''
mv "$textPath" "$out"
substituteInPlace $out --subst-var-by stage-1 $out
chmod +x "$out"
'';
makeInitrd = { contents, compressor ? "gzip -9n", prepend ? [ ], keepStorePath }:
pkgs.callPackage build-support/kernel/make-initrd.nix {
inherit contents compressor prepend keepStorePath;
};
initialRamdisk = pkgs.makeInitrd {
contents = [ { object = bootStage1; symlink = "/init"; } ];
};
in
{
options = {
};
config = {
system.build.bootStage1 = bootStage1;
system.build.initialRamdisk = initialRamdisk;
system.build.extraUtils = extraUtils;
system.build.shrunk = modules;
system.build.rootModules = rootModules;
boot.initrd.availableKernelModules = [
# TODO Use config settings, maye be remove one of the crc thing,
# maybe some should be in kernelModules below.
# For the FAT16 boot partition.
"jbd2" "fscrypto" "mbcache" "crc16" "libcrc32c" "crc32c_generic"
# For the EXT4 partition, when stage-1 is run from the boot partition.
"ext4"
# For the config-2 (file system type iso9660) cloud config.
"isofs"
# For dhcpcd.
"af_packet" ];
boot.initrd.kernelModules =
[ "tun" "loop" "squashfs" ] ++
(lib.optional config.not-os.live "overlay");
};
}