-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitrd.nix
More file actions
140 lines (121 loc) · 3.68 KB
/
initrd.nix
File metadata and controls
140 lines (121 loc) · 3.68 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
{
lib,
stdenv,
writeScript,
makeInitrd,
cacert,
pkgsStatic,
buildEnv,
busybox,
}:
let
rootfs = buildEnv {
name = "rootfs-env";
paths = map lib.getBin [
busybox
];
pathsToLink = [
"/bin"
"/sbin"
];
};
defaultReadonlyPartition = if stdenv.hostPlatform.isSparc then "/dev/sda" else "/dev/vda";
defaultStoragePartition = if stdenv.hostPlatform.isSparc then "/dev/sdb" else "/dev/vdb";
init = writeScript "init" ''
#! /bin/ash -e
export PATH=/bin:/sbin
mkdir -p /proc /sys /dev
mount -t proc none /proc
mount -t sysfs none /sys
mount -t debugfs none /sys/kernel/debug
mount -t devtmpfs devtmpfs /dev
ln -s /proc/self/fd /dev/fd
ln -s /proc/self/fd/0 /dev/stdin
ln -s /proc/self/fd/1 /dev/stdout
ln -s /proc/self/fd/2 /dev/stderr
echo 1 > /proc/sys/vm/panic_on_oom
mkdir -p /etc
echo -n > /etc/fstab
mkdir -p /dev/pts /dev/shm /tmp /run /var
mount -t cgroup2 none /sys/fs/cgroup
mount -t bpf bpf /sys/fs/bpf
mount -t devpts none /dev/pts
mount -t tmpfs -o "mode=1777" none /dev/shm
mount -t tmpfs -o "mode=1777" none /var
mount -t tmpfs -o "mode=1777" none /tmp
mount -t tmpfs -o "mode=755" none /run
ln -sfn /run /var/run
ln -sf /proc/mounts /etc/mtab
echo "127.0.0.1 localhost" > /etc/hosts
echo "nameserver 127.0.0.1" > /etc/resolv.conf
echo "root:x:0:0::/root:/bin/sh" > /etc/passwd
mkdir -p /etc/ssl/certs
ln -s ${cacert.out}/etc/ssl/certs/ca-bundle.crt /etc/ssl/certs/ca-bundle.crt
# shared dir
mkdir /mnt
mount -t 9p -o trans=virtio shared /mnt || true
# mount -t virtiofs shared /mnt
ifconfig lo up
udhcpc
if [ -e ${defaultReadonlyPartition} ]; then
exec setsid cttyhack /init2
else
if [ -e /bin/bash ]; then
exec setsid cttyhack /bin/bash
else
exec setsid cttyhack /bin/sh
fi
fi
'';
init2 = writeScript "init2" ''
#! /bin/ash -e
export PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin
mkdir -p /new_root
mount -t tmpfs tmpfs /new_root
mkdir -p /new_root/lower /new_root/upper/upper /new_root/upper/work /new_root/merged
mount -t erofs ${defaultReadonlyPartition} /new_root/lower
if [ -e ${defaultStoragePartition} ]; then
mount -t ext4 ${defaultStoragePartition} /new_root/upper
mkdir -p /new_root/upper/upper /new_root/upper/work
fi
mount -t overlay overlay -o lowerdir=/new_root/lower,upperdir=/new_root/upper/upper,workdir=/new_root/upper/work /new_root/merged
mkdir -p /new_root/merged/proc /new_root/merged/dev /new_root/merged/sys /new_root/merged/run /new_root/merged/tmp /new_root/merged/mnt
mount --move /proc /new_root/merged/proc
mount --move /dev /new_root/merged/dev
mount --move /sys /new_root/merged/sys
mount --move /run /new_root/merged/run
mount --move /tmp /new_root/merged/tmp
mount --move /mnt /new_root/merged/mnt || true
rm -f /new_root/merged/etc/resolv.conf
cp -f /etc/resolv.conf /new_root/merged/etc/resolv.conf
cp ${./resize_term} /new_root/merged/bin/resize_term
if [ -e /new_root/merged/bin/bash ]; then
exec switch_root /new_root/merged /bin/bash
else
exec switch_root /new_root/merged /bin/sh
fi
'';
initrd = makeInitrd {
makeUInitrd = false;
compressor = "zstd";
contents = [
{
object = init;
symlink = "/init";
}
{
object = init2;
symlink = "/init2";
}
{
object = rootfs + "/bin";
symlink = "/bin";
}
{
object = rootfs + "/sbin";
symlink = "/sbin";
}
];
};
in
initrd