aboutsummaryrefslogtreecommitdiffstats
path: root/virtme/guest/virtme-init
blob: b1159d064289a28d86ae515eb9499049c888bf4a (plain)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/bin/bash
# virtme-init: virtme's basic init (PID 1) process
# Copyright © 2014 Andy Lutomirski
# Licensed under the GPLv2, which is available in the virtme distribution
# as a file called LICENSE with SHA-256 hash:
# 8177f97513213526df2cf6184d8ff986c675afb514d4e68a404010521b880643

export PATH=/bin:/sbin:/usr/bin:/usr/sbin

log() {
    if [[ -e /dev/kmsg ]]; then
	echo "<6>virtme-init: $*" >/dev/kmsg
    else
	echo "virtme-init: $*"
    fi
}

mount -t sysfs -o nosuid,noexec,nodev sys /sys/

declare -A mount_tags
for i in /sys/bus/virtio/drivers/9pnet_virtio/virtio*/mount_tag; do
    # mount_tag is terminated with a NUL byte, which leads to a
    # "command substitution: ignored null byte in input" warning from
    # bash; use sed instead of a bare 'cat' here to strip it off.
    mount_tags["`sed '$s/\x00$//;' "$i"`"]=1
done

kver="`uname -r`"

if [[ -n "${mount_tags[virtme.moddir]}" ]]; then
    mount -t tmpfs none /lib/modules
    mkdir /lib/modules/"$kver"
    mount -n -t 9p -o ro,version=9p2000.L,trans=virtio,access=any virtme.moddir /lib/modules/"$kver"
elif [[ -d "/lib/modules/$kver" ]]; then
    # We may have mismatched modules.  Mask them off.
    mount -n -t tmpfs -o ro,mode=0000 disallow_modules "/lib/modules/$kver"
fi

mount -t tmpfs tmpfs /tmp/
[[ -w /var/log ]] || mount -t tmpfs tmpfs /var/log/

# Fix up /etc a little bit
touch /tmp/fstab
mount --bind /tmp/fstab /etc/fstab
rm /tmp/fstab

# Find udevd
if [[ -x /usr/lib/systemd/systemd-udevd ]]; then
    udevd=/usr/lib/systemd/systemd-udevd
elif [[ -x /lib/systemd/systemd-udevd ]]; then
    udevd=/lib/systemd/systemd-udevd
else
    udevd=`which udevd`
fi

# Mount proc (needed for stat, sadly)
mount -t proc -o nosuid,noexec,nodev proc /proc/

# devtmpfs might be automounted; if not, mount it.
if [[ "`stat --format=%m /dev`" != "/dev" ]]; then
    # Ideally we'll use devtmpfs (but don't rely on /dev/null existing).
    if [[ -c /dev/null ]]; then
	mount -n -t devtmpfs -o mode=0755,nosuid,noexec devtmpfs /dev \
	    &>/dev/null
    else
	mount -n -t devtmpfs -o mode=0755,nosuid,noexec devtmpfs /dev
    fi

    if (( $? != 0 )); then
	# The running kernel doesn't have devtmpfs.  Use regular tmpfs.
	mount -t tmpfs -o mode=0755,nosuid,noexec none /dev

	# Make some basic devices first, and let udev handle the rest
	mknod -m 0666 /dev/null c 1 3
	mknod -m 0660 /dev/kmsg c 1 11
	mknod -m 0600 /dev/console c 5 1
    fi
fi

for tag in "${!virtme_initmount@}"; do
    mount -t 9p -o version=9p2000.L,trans=virtio,access=any "virtme.initmount${tag:16}" "${!tag}"
done

if [[ -n "virtme_chdir" ]]; then
    cd -- "${virtme_chdir}"
fi

log "basic initialization done"

######## The remainder of this script is a very simple init (PID 1) ########

# Does the system use systemd-tmpfiles?
tmpfiles=`which systemd-tmpfiles 2>/dev/null` && {
    log "running systemd-tmpfiles"
    systemd-tmpfiles --create --boot --exclude-prefix="/dev"
}

# Make dbus work (if tmpfiles wasn't there or didn't create the directory).
install -d /run/dbus

# Try to get udevd to coldplug everything.
# We could use virtme-loadmods as a lightweight alternative.
if [[ -n "$udevd" ]]; then
    if [[ -e '/sys/kernel/uevent_helper' ]]; then
       # This kills boot performance.
       log "you have CONFIG_UEVENT_HELPER on; turn it off"
       echo '' >/sys/kernel/uevent_helper
    fi
    log "starting udevd"
    "$udevd" --daemon --resolve-names=never
    log "triggering udev coldplug"
    udevadm trigger --type=subsystems --action=add  >/dev/null 2>&1
    udevadm trigger --type=devices --action=add  >/dev/null 2>&1
    log "waiting for udev to settle"
    udevadm settle
    log "udev is done"
else
    log "udevd not found"
fi

# Set up useful things in /sys, assuming our kernel supports it.
mount -t configfs configfs /sys/kernel/config &>/dev/null
mount -t debugfs debugfs /sys/kernel/debug &>/dev/null

# Set up cgroup mount points (mount cgroupv2 hierarchy by default)
if mount -t tmpfs tmpfs /sys/fs/cgroup &>/dev/null; then
    mkdir /sys/fs/cgroup/unified
    mount -t cgroup2 cgroup2 /sys/fs/cgroup/unified
fi

# Set up filesystems that live in /dev
mkdir -p -m 0755 /dev/shm /dev/pts
mount -t devpts -o gid=tty,mode=620,noexec,nosuid devpts /dev/pts
mount -t tmpfs -o mode=1777,nosuid,nodev tmpfs /dev/shm

# Install /proc/self/fd symlinks into /dev if not already present
declare -r -A fdlinks=(["/dev/fd"]="/proc/self/fd"
                       ["/dev/stdin"]="/proc/self/fd/0"
                       ["/dev/stdout"]="/proc/self/fd/1"
                       ["/dev/stderr"]="/proc/self/fd/2")

for p in "${!fdlinks[@]}"; do
    [[ -e "$p" ]] || ln -s "${fdlinks[$p]}" "$p"
done

if [[ -n "$virtme_hostname" ]]; then
    log "Setting hostname to $virtme_hostname..."
    hostname "$virtme_hostname"
fi

# Bring up networking
ip link set dev lo up

if cat /proc/cmdline |grep -q -E '(^| )virtme.dhcp($| )'; then
    if [[ -f /etc/resolv.conf ]]; then
	tmpfile="`mktemp --tmpdir=/tmp`"
	chmod 644 "$tmpfile"
	mount --bind "$tmpfile" /etc/resolv.conf
	rm "$tmpfile"
    fi

    # udev is liable to rename the interface out from under us.
    virtme_net=`ls "$(ls -d /sys/bus/virtio/drivers/virtio_net/virtio* |sort -g |head -n1)"/net`
    busybox udhcpc -i "$virtme_net" -t 1 -n -q -f -s "$(dirname $0)/virtme-udhcpc-script"
fi

if [[ -x /run/virtme/data/script ]]; then
    if [[ ! -e "/dev/virtio-ports/virtme.scriptio" ]]; then
	echo "virtme-init: cannot find script I/O port; make sure virtio-serial is available"
	poweroff -f
	exit 1
    fi

    log 'starting script'
    setsid /run/virtme/data/script <>/dev/virtio-ports/virtme.scriptio 1>&0 2>&0
    log "script returned $?"

    # Hmm.  We should expose the return value somehow.
    sync
    poweroff -f
    exit 1
fi

# Figure out what the main console is
consdev="`grep ' ... (.C' /proc/consoles  |cut -d' ' -f1`"
if [[ -z "$consdev" ]]; then
    log "can't deduce console device"
    exec bash --login  # At least try to be helpful
fi

deallocvt

if [[ "$consdev" == "tty0" ]]; then
    # Create some VTs
    openvt -c 2 -- /bin/bash
    openvt -c 3 -- /bin/bash
    openvt -c 4 -- /bin/bash

    consdev=tty1  # sigh
fi

if [[ ! -e "/dev/$consdev" ]]; then
    log "/dev/$consdev doesn't exist."
    exec bash --login
fi

# Parameters that start with virtme_ shouldn't pollute the environment
for p in "${!virtme_@}"; do export -n "$p"; done

echo "virtme-init: console is $consdev"

# Set up a basic environment
install -d -m 0755 /tmp/roothome
export HOME=/tmp/roothome

# Bring up a functioning shell on the console.  This is a bit magical:
# We have no controlling terminal because we're attached to a fake
# console device (probably something like /dev/console), which can't
# be a controlling terminal.  We are also not a member of a session.
# Init apparently can't setsid (whether that's a limitation of the
# setsid binary or the system call, I don't know).
while true; do
    # Program the console sensibly
    if [[ -n "${virtme_stty_con}" ]]; then
	stty ${virtme_stty_con} <"/dev/$consdev"
    fi

    setsid bash 0<>"/dev/$consdev" 1>&0 2>&0
    echo "Shell died.  Will respawn."
    sleep 0.5
done