Repository navigation
Expand file tree
/
Copy pathinit
More file actions
executable file
·205 lines (178 loc) · 5.12 KB
/
Copy pathinit
File metadata and controls
executable file
·205 lines (178 loc) · 5.12 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
#!/bin/sh
set -eu
CONSOLE="/dev/console"
if [ ! -c "${CONSOLE}" ]; then
if [ -c /dev/ttyAMA0 ]; then
CONSOLE="/dev/ttyAMA0"
elif [ -c /dev/ttyS0 ]; then
CONSOLE="/dev/ttyS0"
else
CONSOLE=""
fi
fi
log() {
if [ -n "${CONSOLE}" ]; then
printf "%s\n" "$*" > "${CONSOLE}" 2>/dev/null || printf "%s\n" "$*"
else
printf "%s\n" "$*"
fi
}
log_cmd() {
if [ -n "${CONSOLE}" ]; then
"$@" > "${CONSOLE}" 2>&1 || "$@" || true
else
"$@" || true
fi
}
mount -t proc proc /proc || log "[init] mount proc failed"
mount -t sysfs sysfs /sys || log "[init] mount sysfs failed"
mount -t devtmpfs devtmpfs /dev || log "[init] mount devtmpfs failed"
mkdir -p /dev/pts /dev/shm /run
mount -t devpts devpts /dev/pts || log "[init] mount devpts failed"
mount -t tmpfs tmpfs /run || log "[init] mount tmpfs failed"
export PATH=/usr/sbin:/usr/bin:/sbin:/bin
mkdir -p /tmp /var/tmp /var/cache /var/log /root /home
mount -t tmpfs tmpfs /tmp || log "[init] mount tmpfs /tmp failed"
mount -t tmpfs tmpfs /root || log "[init] mount tmpfs /root failed"
chmod 700 /root || true
mount -t tmpfs tmpfs /var/tmp || log "[init] mount tmpfs /var/tmp failed"
mount -t tmpfs tmpfs /var/cache || log "[init] mount tmpfs /var/cache failed"
mount -t tmpfs tmpfs /var/log || log "[init] mount tmpfs /var/log failed"
mkdir -p /tmp/.cache /tmp/.config /tmp/.local/share
export HOME=/root
export TMPDIR=/tmp
export XDG_CACHE_HOME=/tmp/.cache
export XDG_CONFIG_HOME=/tmp/.config
export XDG_DATA_HOME=/tmp/.local/share
export UV_CACHE_DIR=/tmp/.cache/uv
export SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt
export UV_NATIVE_TLS=true
log "[init] /dev entries:"
log_cmd ls -l /dev
if [ -d /dev/virtio-ports ]; then
log "[init] /dev/virtio-ports:"
log_cmd ls -l /dev/virtio-ports
else
log "[init] /dev/virtio-ports missing"
fi
if [ -d /sys/class/virtio-ports ]; then
log "[init] /sys/class/virtio-ports:"
log_cmd ls -l /sys/class/virtio-ports
else
log "[init] /sys/class/virtio-ports missing"
fi
if modprobe virtio_console > /dev/null 2>&1; then
log "[init] loaded virtio_console"
fi
if modprobe virtio_rng > /dev/null 2>&1; then
log "[init] loaded virtio_rng"
fi
if [ -e /dev/hwrng ]; then
log "[init] starting rngd"
rngd -r /dev/hwrng -o /dev/random > /dev/null 2>&1 &
else
log "[init] /dev/hwrng missing"
fi
if modprobe virtio_net > /dev/null 2>&1; then
log "[init] loaded virtio_net"
fi
if command -v ip > /dev/null 2>&1; then
ip link set lo up || true
ip link set eth0 up || true
else
ifconfig lo up || true
ifconfig eth0 up || true
fi
if command -v udhcpc > /dev/null 2>&1; then
UDHCPC_SCRIPT="/usr/share/udhcpc/default.script"
if [ ! -x "${UDHCPC_SCRIPT}" ]; then
UDHCPC_SCRIPT="/sbin/udhcpc.script"
fi
if [ -x "${UDHCPC_SCRIPT}" ]; then
udhcpc -i eth0 -q -n -s "${UDHCPC_SCRIPT}" || log "[init] udhcpc failed"
else
udhcpc -i eth0 -q -n || log "[init] udhcpc failed"
fi
fi
if modprobe fuse > /dev/null 2>&1; then
log "[init] loaded fuse"
fi
sandboxfs_mount="/data"
sandboxfs_binds=""
if [ -r /proc/cmdline ]; then
for arg in $(cat /proc/cmdline); do
case "${arg}" in
sandboxfs.mount=*)
sandboxfs_mount="${arg#sandboxfs.mount=}"
;;
sandboxfs.bind=*)
sandboxfs_binds="${arg#sandboxfs.bind=}"
;;
esac
done
fi
wait_for_sandboxfs() {
for i in $(seq 1 300); do
if grep -q " ${sandboxfs_mount} fuse.sandboxfs " /proc/mounts; then
return 0
fi
sleep 0.1
done
return 1
}
mkdir -p "${sandboxfs_mount}"
sandboxfs_ready=0
sandboxfs_error="sandboxfs mount not ready"
if [ -x /usr/bin/sandboxfs ]; then
log "[init] starting sandboxfs at ${sandboxfs_mount}"
SANDBOXFS_LOG="${CONSOLE:-/dev/null}"
if [ -z "${SANDBOXFS_LOG}" ]; then
SANDBOXFS_LOG="/dev/null"
fi
/usr/bin/sandboxfs --mount "${sandboxfs_mount}" --rpc-path /dev/virtio-ports/virtio-fs > "${SANDBOXFS_LOG}" 2>&1 &
if wait_for_sandboxfs; then
sandboxfs_ready=1
if [ -n "${sandboxfs_binds}" ]; then
OLD_IFS="${IFS}"
IFS=","
for bind in ${sandboxfs_binds}; do
if [ -z "${bind}" ]; then
continue
fi
mkdir -p "${bind}"
if [ "${sandboxfs_mount}" = "/" ]; then
bind_source="${bind}"
else
bind_source="${sandboxfs_mount}${bind}"
fi
log "[init] binding sandboxfs ${bind_source} -> ${bind}"
log_cmd mount --bind "${bind_source}" "${bind}"
done
IFS="${OLD_IFS}"
fi
else
log "[init] sandboxfs mount not ready"
fi
else
log "[init] /usr/bin/sandboxfs missing"
sandboxfs_error="sandboxfs binary missing"
fi
if [ "${sandboxfs_ready}" -eq 1 ]; then
printf "ok\n" > /run/sandboxfs.ready
else
printf "%s\n" "${sandboxfs_error}" > /run/sandboxfs.failed
fi
if [ -x /usr/bin/sandboxssh ]; then
log "[init] starting sandboxssh"
/usr/bin/sandboxssh > "${CONSOLE:-/dev/null}" 2>&1 &
else
log "[init] /usr/bin/sandboxssh missing"
fi
if [ -x /usr/bin/sandboxingress ]; then
log "[init] starting sandboxingress"
/usr/bin/sandboxingress > "${CONSOLE:-/dev/null}" 2>&1 &
else
log "[init] /usr/bin/sandboxingress missing"
fi
log "[init] starting sandboxd"
exec /usr/bin/sandboxd