-
Notifications
You must be signed in to change notification settings - Fork 22
/
vm_autorun.env
292 lines (243 loc) · 7.46 KB
/
vm_autorun.env
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# SPDX-License-Identifier: (LGPL-2.1 OR LGPL-3.0)
# Copyright (C) SUSE LLC 2016-2022, all rights reserved.
. /rapido.conf
alias shutdown='echo o > /proc/sysrq-trigger'
alias reboot='echo b > /proc/sysrq-trigger'
alias vi='vim'
alias view='vim -R'
alias l='ls -la'
_ini_parse() {
local ini_file=$1
typeset -n vals_ref=$2
local ini_section=$3
local ini_key=$4
local vals=()
mapfile "vals" < \
<(sed -e 's/[[:space:]]*\=[[:space:]]*/=/' \
-e 's/;.*$//' \
-e 's/[[:space:]]*$//' \
-e 's/^[[:space:]]*//' \
-e "s/^\(.*\)=\([^\"']*\)$/\1=\2/" \
-e ':b; s/^\([^=]*\)* /\1_/; tb;' \
-n -e "/^\[$ini_section\]/,/^\s*\[/ { \
s/^$ini_key\=\(.*\)/\1/p;
}" \
< "$ini_file")
vals_ref+=(${vals[@]})
}
function _fatal() {
[[ -z $1 ]] || echo "FATAL: $1"
shutdown
sleep 2
}
# safety check to confirm that autorun scripts run from a rapido VM
function _vm_ar_env_check {
[ -f /rapido.conf ]
}
# create /etc/hosts file with the essential IPv4 and IPv6 lines
function _vm_ar_hosts_create
{
local hostname_fqn="`cat /proc/sys/kernel/hostname`" \
|| _fatal "hostname unavailable"
# we don't put a fqdn in proc hostname, but maybe systemd?
local hostname_short="${hostname_fqn%%.*}"
local vm_domain="$(cat /proc/sys/kernel/domainname)"
if [ "$hostname_fqn" == "$hostname_short" ] && \
[ -n "$vm_domain" ] && [ "$vm_domain" != "(none)" ]; then
hostname_fqn="${hostname_short}.${vm_domain}"
fi
# need hosts file for hostname -s
cat > /etc/hosts <<EOF
127.0.0.1 localhost
127.0.1.1 $hostname_fqn $hostname_short
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
EOF
}
# set a kcli_$param variable based on the presence of $param[=$value] in
# /proc/cmdline. Dots '.' in $param will be replaced in the variable with '_'.
# If $param is present but doesn't have an "=$value" component, then
# kcli_$param will be set to an empty string, otherwise it'll be set to
# $value.
function _vm_kcli_param_get()
{
local param=$1
[ -n "$param" ] || _fatal "invalid kcli param"
local variable="kcli_${param//./_}"
eval unset $variable
for i in $(cat /proc/cmdline); do
case "$i" in
"${param}="*)
val="${i#${param}=}"
eval ${variable}=${val}
return
;;
"${param}")
eval ${variable}=""
return
;;
esac
done
# $param not found, variable unset
}
_vm_ar_hostname_set() {
local vm_num="$1"
local hostname domain
_vm_kcli_param_get "rapido.hostname"
if [ -z "$kcli_rapido_hostname" ]; then
hostname="rapido${vm_num}"
else
hostname="${kcli_rapido_hostname%%.*}"
[ "$hostname" == "$kcli_rapido_hostname" ] \
|| domain="${kcli_rapido_hostname#*.}"
fi
echo "$hostname" > /proc/sys/kernel/hostname \
|| _fatal "failed to set hostname"
export HOSTNAME="$hostname"
if [ -n "$domain" ]; then
echo "$domain" > /proc/sys/kernel/domainname \
|| _fatal "failed to set hostname"
fi
}
# enable dynamic debug for all DYN_DEBUG_MODULES and DYN_DEBUG_FILES specified
# in rapido.conf. This should be called *after* all kernel modules are loaded.
function _vm_ar_dyn_debug_enable
{
if [ ! -d "/sys/kernel/debug/dynamic_debug" ]; then
mount -t debugfs debugfs /sys/kernel/debug/
fi
for i in $DYN_DEBUG_MODULES; do
echo "module $i +pf" > /sys/kernel/debug/dynamic_debug/control
done
for i in $DYN_DEBUG_FILES; do
echo "file $i +pf" > /sys/kernel/debug/dynamic_debug/control
done
}
function _vm_ar_virtfs_mount
{
[ -z "$VIRTFS_SHARE_PATH" ] && return
mkdir /host || _fatal "unable to create /host"
mount -t 9p host0 /host || _fatal "unable to mount host"
}
_vm_ar_configfs_mount() {
modprobe configfs
cat /proc/mounts | grep -m1 configfs &> /dev/null
if [ $? -ne 0 ]; then
mount -t configfs configfs /sys/kernel/config/
fi
}
_vm_ar_network_setup() {
local vm_num="$1"
local f kcli re oldpath="$PATH"
[[ -d /rapido-rsc/net ]] \
|| return # networkless image
modprobe -a virtio_net af_packet
mkdir -p /etc/systemd/ /run/systemd/
ln -s "/rapido-rsc/net/vm${vm_num}" /etc/systemd/network
# add a netd match for guest MAC addr
kcli="$(cat /proc/cmdline)"
for f in $(ls /etc/systemd/network); do
[[ $f =~ ^(.*)\.network$ ]] || continue
local host_tap="${BASH_REMATCH[1]}"
re="[\^ ]rapido.mac.${host_tap}=([:[:alnum:]]*)"
[[ $kcli =~ $re ]] \
&& echo -e "[Match]\nMACAddress=${BASH_REMATCH[1]}" \
>> "/etc/systemd/network/${f}"
done
# ensure lo comes up - normally done by systemd loopback-setup.c
echo -e "[Match]\nName=lo" >> /etc/systemd/network/lo.network
_vm_kcli_param_get systemd.machine_id
[[ -v kcli_systemd_machine_id ]] \
|| _fatal "systemd.machine_id missing in kcli"
echo "$kcli_systemd_machine_id" > /etc/machine-id
PATH="/usr/lib/systemd:${PATH}"
systemd-udevd --daemon || _fatal
udevadm trigger /sys/class/net/* || _fatal "udevadm trigger failed"
echo "systemd-network:x:482:482:systemd Network Management:/:/sbin/nologin" \
>> /etc/passwd || _fatal
#export SYSTEMD_LOG_LEVEL=debug
setsid --fork systemd-networkd
echo "Waiting for network to come online..."
systemd-networkd-wait-online --timeout=20 \
|| _fatal 'wait-online failed: "rapido setup-network" needed?'
PATH="$oldpath"
}
_vm_ar_cfg_macs() {
local vm_net_dir="/rapido-rsc/net/vm${1}"
typeset -n macs_ref="$2" || _fatal
local _macs=()
local netf
[[ -d $vm_net_dir ]] || return
for netf in $(ls "$vm_net_dir") ; do
[[ $netf =~ ^(.*)\.network$ ]] || continue
[[ ${BASH_REMATCH[1]} == lo ]] && continue
_ini_parse "${vm_net_dir}/${netf}" "_macs" Link MACAddress
done
macs_ref+=(${_macs[@]})
}
# ips_ref is filled with *configured* (not necessarity connected) addresses,
# which may include a netmask suffix. XXX this won't work for DHCP.
_vm_ar_cfg_ips() {
local vm_net_dir="/rapido-rsc/net/vm${1}"
typeset -n ips_ref="$2" || _fatal
local strip_netmask="$3"
local _ips=()
local netf ip
[[ -d $vm_net_dir ]] || return
for netf in $(ls "$vm_net_dir") ; do
[[ $netf =~ ^(.*)\.network$ ]] || continue
[[ ${BASH_REMATCH[1]} == lo ]] && continue
_ini_parse "${vm_net_dir}/${netf}" "_ips" Network Address
_ini_parse "${vm_net_dir}/${netf}" "_ips" Address Address
done
if [[ -n $strip_netmask ]]; then
for ip in "${_ips[@]}"; do
ips_ref+=(${ip%/*})
done
else
ips_ref+=(${_ips[@]})
fi
}
_vm_ar_cfg_ips_nomask() {
_vm_ar_cfg_ips "$1" "$2" "nomask"
}
_vm_ar_ip_addrs() {
typeset -n ips_ref="$1" || _fatal
local l strip_sfx
[[ -n "$2" ]] && strip_sfx="/*"
local -a thisl addrs
mapfile addrs < <(ip -o addr || _fatal)
for l in "${addrs[@]}"; do
thisl=($l)
[[ ${thisl[1]} == "lo" ]] && continue
[[ ${thisl[2]} == "inet" ]] && ips_ref+=(${thisl[3]%$strip_sfx})
[[ ${thisl[2]} == "inet6" ]] && ips_ref+=(${thisl[3]%$strip_sfx})
done
}
_vm_ar_ip_addrs_nomask() {
_vm_ar_ip_addrs "$1" "nomask"
}
_vm_ar_load_kmods() {
local -a kmods
[ "$QEMU_EXTRA_ARGS" == "${QEMU_EXTRA_ARGS/virtio-rng-pci}" ] \
|| kmods+=("virtio-rng")
[ -n "$VIRTFS_SHARE_PATH" ] && kmods+=("9pnet" "9pnet_virtio" "9p")
((${#kmods[*]} > 0)) && modprobe -a "${kmods[@]}"
}
_vm_ar_load_kmods
_vm_kcli_param_get "rapido.vm_num"
[ -z "$kcli_rapido_vm_num" ] && _fatal "rapido.vm_num missing in kcli"
# Set hostname manually, DHCP may override it
_vm_ar_hostname_set "$kcli_rapido_vm_num"
_vm_ar_network_setup "$kcli_rapido_vm_num"
export TERM="linux"
export PS1="$(cat /proc/sys/kernel/hostname):\${PWD}# "
resize &> /dev/null
_vm_ar_virtfs_mount
# The boot sequence is:
# dracut -> 00-rapido-init.sh -> rapido.rc (here) -> /rapido_autorun/*
for _f in /rapido_autorun/*; do
echo "Rapido: starting $_f"
[ -f "$_f" ] && . "$_f"
done