Skip to content

Commit 20d3cea

Browse files
committed
Mount /opt/runcvm in-container to /.runcvm/guest
- Facilitates installing RunCVM in a RunCVM VM - Reduces number of top-level in-container mountpoints to just one - Introduces $RUNCVM_GUEST variable (="/.runcvm/guest") to be used where possible, pursuant to possible future functionality allowing this to be configured - Fixes kernel-selection logic to match docs and comments
1 parent 2a8b493 commit 20d3cea

19 files changed

+217
-117
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,10 @@ RUN apk add --allow-untrusted /tmp/dropbear/dropbear-ssh*.apk /tmp/dropbear/drop
109109

110110
# Patch the binaries and set up symlinks
111111
COPY build-utils/elf-patcher.sh /usr/local/bin/elf-patcher.sh
112-
ENV BINARIES="busybox bash jq ip nc mke2fs blkid findmnt dnsmasq xtables-legacy-multi nft xtables-nft-multi nft mount s6-applyuidgid qemu-system-x86_64 qemu-ga /usr/lib/qemu/virtiofsd tput stdbuf coreutils getent dropbear dbclient dropbearkey"
112+
ENV BINARIES="busybox bash jq ip nc mke2fs blkid findmnt dnsmasq xtables-legacy-multi nft xtables-nft-multi nft mount s6-applyuidgid qemu-system-x86_64 qemu-ga /usr/lib/qemu/virtiofsd tput coreutils getent dropbear dbclient dropbearkey"
113113
ENV EXTRA_LIBS="/usr/lib/xtables /usr/libexec/coreutils /tmp/dropbear/libepka_file.so /usr/lib/qemu/*.so"
114114
ENV CODE_PATH="/opt/runcvm"
115+
ENV EXEC_PATH="/.runcvm/guest"
115116
RUN /usr/local/bin/elf-patcher.sh && \
116117
cd $CODE_PATH/bin && \
117118
for cmd in \

runcvm-init/dumb-init.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void forward_signal(int signum) {
9999

100100
pid_t shutdown() {
101101
pid_t my_child_pid;
102-
char *shutdown_cmd[] = {"/opt/runcvm/scripts/runcvm-ctr-shutdown", NULL};
102+
char *shutdown_cmd[] = {"/.runcvm/guest/scripts/runcvm-ctr-shutdown", NULL};
103103

104104
my_child_pid = fork();
105105
if (my_child_pid < 0) {
@@ -123,7 +123,7 @@ pid_t shutdown() {
123123

124124
void quit(int exit_status) {
125125
char exit_status_string[4];
126-
char *exit_cmd[] = {"/opt/runcvm/scripts/runcvm-ctr-exit", exit_status_string, NULL};
126+
char *exit_cmd[] = {"/.runcvm/guest/scripts/runcvm-ctr-exit", exit_status_string, NULL};
127127

128128
sprintf(exit_status_string, "%d", exit_status & 0xFF);
129129

runcvm-scripts/runcvm-ctr-defaults

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

3-
RUNCVM=/opt/runcvm
4-
RUNCVM_PATH=$RUNCVM/usr/sbin:$RUNCVM/usr/bin:$RUNCVM/sbin:$RUNCVM/bin:$RUNCVM/usr/lib/qemu
3+
RUNCVM_GUEST=${RUNCVM_GUEST:-/.runcvm/guest}
4+
RUNCVM_PATH=$RUNCVM_GUEST/usr/sbin:$RUNCVM_GUEST/usr/bin:$RUNCVM_GUEST/sbin:$RUNCVM_GUEST/bin:$RUNCVM_GUEST/usr/lib/qemu
55

66
QEMU_VIRTIOFSD_SOCKET=/run/.virtiofs.sock
77
QEMU_GUEST_AGENT=/run/.qemu-guest-agent
@@ -12,6 +12,7 @@ SSHD_PORT=22222
1212
clean_env() {
1313
export -n \
1414
RUNCVM_BREAK RUNCVM_INIT \
15+
RUNCVM_GUEST \
1516
RUNCVM_RUNTIME_DEBUG RUNCVM_BIOS_DEBUG RUNCVM_KERNEL_DEBUG \
1617
RUNCVM_KERNEL RUNCVM_KERNEL_ROOT RUNCVM_KERNEL_APPEND RUNCVM_KERNEL_INITRAMFS_PATH RUNCVM_KERNEL_PATH RUNCVM_DISKS \
1718
RUNCVM_UIDGID RUNCVM_VM_MOUNTPOINT RUNCVM_TMPFS \
@@ -27,4 +28,30 @@ load_network() {
2728
[ -d /.runcvm/network/devices ] && [ -s /.runcvm/network/devices/$if ] || return 1
2829
read -r DOCKER_IF DOCKER_IF_MAC DOCKER_IF_MTU DOCKER_IF_IP DOCKER_IF_IP_NETPREFIX DOCKER_IF_IP_GW </.runcvm/network/devices/$if
2930
return 0
30-
}
31+
}
32+
33+
which() {
34+
local cmd="$1"
35+
local WHICH_PATH="${RUNCVM_PATH//:/ }" # Replace ':' with ' '
36+
for p in $WHICH_PATH; do [ -x "$p/$cmd" ] && echo "$p/$cmd" && return 0; done
37+
return 1
38+
}
39+
40+
create_aliases() {
41+
for cmd in \
42+
bash \
43+
busybox awk cat chgrp chmod cut grep head hostname init ln ls mkdir poweroff ps rm sh sysctl touch tr \
44+
ip jq \
45+
dnsmasq \
46+
blkid findmnt getent mke2fs mount nc \
47+
xtables-nft-multi xtables-legacy-multi \
48+
qemu-system-x86_64 qemu-ga \
49+
dbclient dropbear dropbearkey \
50+
s6-applyuidgid \
51+
tput
52+
do
53+
eval "$cmd() { $(which $cmd) \"\$@\"; }"
54+
done
55+
}
56+
57+
create_aliases

runcvm-scripts/runcvm-ctr-entrypoint

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/opt/runcvm/bin/bash
1+
#!/.runcvm/guest/bin/bash
22

33
# DEBUG
44
if [[ "$RUNCVM_BREAK" =~ (prenet|postnet) ]]; then set -x; fi
@@ -55,7 +55,7 @@ printf "%s\n" "${args[@]}" >/.runcvm/entrypoint
5555
# If not, then we set HOME to the requested user's default homedir in accordance with https://github.com/moby/moby/issues/2968.
5656

5757
if [ "$RUNCVM_HAS_HOME" == "0" ]; then
58-
HOME=$(/opt/runcvm/usr/bin/getent passwd "${RUNCVM_UIDGID%%:*}" | /opt/runcvm/bin/cut -d':' -f6)
58+
HOME=$($RUNCVM_GUEST/usr/bin/getent passwd "${RUNCVM_UIDGID%%:*}" | $RUNCVM_GUEST/bin/cut -d':' -f6)
5959
fi
6060

6161
# SAVE ENVIRONMENT
@@ -64,10 +64,10 @@ export -n SHLVL OLDPWD
6464
export >/.runcvm/config
6565

6666
# NOW LOAD DEFAULT ENV AND PATH
67-
. /opt/runcvm/scripts/runcvm-ctr-defaults && PATH="$RUNCVM_PATH"
67+
. $RUNCVM_GUEST/scripts/runcvm-ctr-defaults
6868

6969
# LOAD IP MANIPULATION FUNCTIONS
70-
. $RUNCVM/scripts/runcvm-ip-functions
70+
. $RUNCVM_GUEST/scripts/runcvm-ip-functions
7171

7272
# SAVE PWD
7373
busybox pwd >/.runcvm/pwd
@@ -138,18 +138,18 @@ do
138138
ip route add default via $DOCKER_GW_IF_IP dev $QEMU_BRIDGE
139139
140140
# Accept DNS requests for $RUNCVM_DNS_IP; these will be passed to dnsmasq
141-
XTABLES_LIBDIR=/opt/runcvm/lib64/usr/lib/xtables/ /opt/runcvm/sbin/xtables-nft-multi iptables -t nat -A PREROUTING -d $RUNCVM_DNS_IP/32 -p udp -m udp --dport 53 -j REDIRECT
141+
XTABLES_LIBDIR=$RUNCVM_GUEST/usr/lib/xtables xtables-nft-multi iptables -t nat -A PREROUTING -d $RUNCVM_DNS_IP/32 -p udp -m udp --dport 53 -j REDIRECT
142142
143143
# Match UDP port 53 traffic, outgoing via the QEMU bridge, from the bridge's own IP:
144144
# -> Masquerade as if from the VM's IP.
145145
# This allows outgoing DNS requests from the VM to be received by dnsmasq running in the container.
146-
XTABLES_LIBDIR=/opt/runcvm/lib64/usr/lib/xtables/ /opt/runcvm/sbin/xtables-nft-multi iptables -t nat -A POSTROUTING -o $QEMU_BRIDGE -s $QEMU_BRIDGE_IP/32 -p udp -m udp --sport 53 -j SNAT --to-source $DOCKER_IF_IP
147-
XTABLES_LIBDIR=/opt/runcvm/lib64/usr/lib/xtables/ /opt/runcvm/sbin/xtables-nft-multi iptables -t nat -A POSTROUTING -o $QEMU_BRIDGE -s $QEMU_BRIDGE_IP/32 -p udp -m udp --dport 53 -j SNAT --to-source $DOCKER_IF_IP
146+
XTABLES_LIBDIR=$RUNCVM_GUEST/usr/lib/xtables xtables-nft-multi iptables -t nat -A POSTROUTING -o $QEMU_BRIDGE -s $QEMU_BRIDGE_IP/32 -p udp -m udp --sport 53 -j SNAT --to-source $DOCKER_IF_IP
147+
XTABLES_LIBDIR=$RUNCVM_GUEST/usr/lib/xtables xtables-nft-multi iptables -t nat -A POSTROUTING -o $QEMU_BRIDGE -s $QEMU_BRIDGE_IP/32 -p udp -m udp --dport 53 -j SNAT --to-source $DOCKER_IF_IP
148148
149149
# Match traffic on TCP port $SSHD_PORT, outgoing via the QEMU bridge, from the bridge's own IP:
150150
# -> Masquerade it as if from the DNS_IP.
151151
# This is necessary to allow SSH from within the container to the VM.
152-
XTABLES_LIBDIR=/opt/runcvm/lib64/usr/lib/xtables/ /opt/runcvm/sbin/xtables-nft-multi iptables -t nat -A POSTROUTING -o $QEMU_BRIDGE -s $QEMU_BRIDGE_IP/32 -p tcp -m tcp --dport $SSHD_PORT -j SNAT --to-source $RUNCVM_DNS_IP
152+
XTABLES_LIBDIR=$RUNCVM_GUEST/usr/lib/xtables xtables-nft-multi iptables -t nat -A POSTROUTING -o $QEMU_BRIDGE -s $QEMU_BRIDGE_IP/32 -p tcp -m tcp --dport $SSHD_PORT -j SNAT --to-source $RUNCVM_DNS_IP
153153
fi
154154
155155
done
@@ -165,11 +165,11 @@ echo "$RESOLV_CONF_NEW" >/vm/etc/resolv.conf
165165
dnsmasq -u root --no-hosts
166166
167167
# LAUNCH VIRTIOFSD
168-
/opt/runcvm/scripts/runcvm-ctr-virtiofsd &
168+
$RUNCVM_GUEST/scripts/runcvm-ctr-virtiofsd &
169169
170170
# DEBUG
171171
if [[ "$RUNCVM_BREAK" =~ postnet ]]; then bash; fi
172172
173173
# LAUNCH INIT SUPERVISING QEMU
174174
# FIXME: Add -v to debug
175-
exec /opt/runcvm/sbin/runcvm-init -c /opt/runcvm/scripts/runcvm-ctr-qemu
175+
exec $RUNCVM_GUEST/sbin/runcvm-init -c $RUNCVM_GUEST/scripts/runcvm-ctr-qemu

runcvm-scripts/runcvm-ctr-exec

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
#!/opt/runcvm/bin/bash -e
1+
#!/.runcvm/guest/bin/bash -e
22

33
# See https://qemu-project.gitlab.io/qemu/interop/qemu-ga-ref.html
44

5-
. /opt/runcvm/scripts/runcvm-ctr-defaults
5+
# Load original environment
6+
. /.runcvm/config
67

7-
busybox() {
8-
$RUNCVM/bin/busybox "$@"
9-
}
8+
# Load defaults and aliases
9+
. $RUNCVM_GUEST/scripts/runcvm-ctr-defaults
1010

1111
env() {
1212
busybox env "$@"
@@ -19,14 +19,15 @@ to_bin() {
1919

2020
# Expects:
2121
# - To be run as root
22+
# - To be given env vars
2223
# - To be given arguments
2324
# $1 <uid>:<gid>:<additionalGids>
2425
# $2 <cwd>
2526
# $3 <ENV-HOME-boolean>
2627
# $4 <wantsTerminal-boolean>
2728
# $(5...) <command> <args>
2829

29-
command="$RUNCVM/scripts/runcvm-vm-exec"
30+
command="$RUNCVM_GUEST/scripts/runcvm-vm-exec"
3031
uidgid="$1"
3132
cwd="$2"
3233
hasHome="$3"
@@ -53,7 +54,7 @@ fi
5354

5455
if [ "$hasHome" != "1" ]; then
5556
# Either this script needs to look up uid's HOME or else runcvm-vm-exec does; for now, we do it here.
56-
HOME=$($RUNCVM/usr/bin/getent passwd "$uid" | $RUNCVM/bin/cut -d':' -f6)
57+
HOME=$(getent passwd "$uid" | cut -d':' -f6)
5758
fi
5859

5960
# Clean RUNCVM env vars
@@ -75,4 +76,4 @@ if ! [ -s /.runcvm/dropbear/key ] || ! load_network; then
7576
exit 1
7677
fi
7778

78-
exec $RUNCVM/usr/bin/dbclient ${opts[@]} -p $SSHD_PORT -y -y -i /.runcvm/dropbear/key root@$DOCKER_IF_IP "$command '$uidgid' '$(echo -n $cwd | to_bin)' '$args_bin' '$env_bin'"
79+
exec $RUNCVM_GUEST/usr/bin/dbclient "${opts[@]}" -p $SSHD_PORT -y -y -i /.runcvm/dropbear/key root@$DOCKER_IF_IP "$command '$uidgid' '$(echo -n $cwd | to_bin)' '$args_bin' '$env_bin'"

runcvm-scripts/runcvm-ctr-exit

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
#!/opt/runcvm/bin/sh
1+
#!/.runcvm/guest/bin/bash
22

3-
. /opt/runcvm/scripts/runcvm-ctr-defaults && PATH="$RUNCVM_PATH"
3+
# Load original environment
4+
. /.runcvm/config
5+
6+
# Load defaults and aliases
7+
. $RUNCVM_GUEST/scripts/runcvm-ctr-defaults
48

59
# runcvm-init execs this script when it exits.
610
# It:

runcvm-scripts/runcvm-ctr-qemu

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
#!/opt/runcvm/bin/bash
1+
#!/.runcvm/guest/bin/bash
22

3-
# TODO: Clean up ENV vars
3+
# Exit on errors
4+
set -o errexit -o pipefail
5+
6+
# Load original environment
47
. /.runcvm/config
58

6-
# Load defaults after exports (so that PATH is overridden)
7-
. /opt/runcvm/scripts/runcvm-ctr-defaults && PATH="$RUNCVM_PATH"
9+
# Load defaults
10+
. $RUNCVM_GUEST/scripts/runcvm-ctr-defaults && unset PATH
811

9-
QEMU_IFUP="$RUNCVM/scripts/runcvm-ctr-qemu-ifup"
10-
QEMU_IFDOWN="$RUNCVM/scripts/runcvm-ctr-qemu-ifdown"
12+
QEMU_IFUP="$RUNCVM_GUEST/scripts/runcvm-ctr-qemu-ifup"
13+
QEMU_IFDOWN="$RUNCVM_GUEST/scripts/runcvm-ctr-qemu-ifdown"
1114

12-
INIT="init=/opt/runcvm/scripts/runcvm-vm-init"
15+
INIT="init=$RUNCVM_GUEST/scripts/runcvm-vm-init"
1316

1417
error() {
1518
echo "$1" >&2
@@ -115,10 +118,10 @@ fi
115118
# - Consider using '-device pvpanic'
116119

117120
if [ "$RUNCVM_ARCH" = "arm64" ]; then
118-
CMD="qemu-system-aarch64"
121+
CMD="$(which qemu-system-aarch64)"
119122
MACHINE+=(-cpu max -machine virt,gic-version=max,usb=off)
120123
else
121-
CMD="qemu-system-x86_64"
124+
CMD="$(which qemu-system-x86_64)"
122125
MACHINE+=(-enable-kvm -cpu host,pmu=off -machine q35,accel=kvm,usb=off,sata=off -device isa-debug-exit)
123126
fi
124127

@@ -249,6 +252,6 @@ ARGS=(
249252
-append "$RUNCVM_KERNEL_ROOT $INIT rw ${APPEND[*]} $RUNCVM_KERNEL_APPEND"
250253
)
251254

252-
if [[ "$RUNCVM_BREAK" =~ preqemu ]]; then echo Preparing to run: $CMD "${ARGS[@]@Q}"; bash; fi
255+
if [[ "$RUNCVM_BREAK" =~ preqemu ]]; then echo Preparing to run: '$CMD' "${ARGS[@]@Q}"; bash; fi
253256

254-
exec $CMD "${ARGS[@]}"
257+
exec "$CMD" "${ARGS[@]}"

runcvm-scripts/runcvm-ctr-qemu-ifdown

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
#!/opt/runcvm/bin/bash
1+
#!/.runcvm/guest/bin/bash
22

3-
. /opt/runcvm/scripts/runcvm-ctr-defaults && PATH="$RUNCVM_PATH"
3+
# Load original environment
4+
. /.runcvm/config
5+
6+
# Load defaults and aliases
7+
. $RUNCVM_GUEST/scripts/runcvm-ctr-defaults
48

59
ip link set dev "$1" down || true
610
exit 0

runcvm-scripts/runcvm-ctr-qemu-ifup

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
#!/opt/runcvm/bin/bash
1+
#!/.runcvm/guest/bin/bash
22

3-
. /opt/runcvm/scripts/runcvm-ctr-defaults && PATH="$RUNCVM_PATH"
3+
# Load original environment
4+
. /.runcvm/config
5+
6+
# Load defaults and aliases
7+
. $RUNCVM_GUEST/scripts/runcvm-ctr-defaults
48

59
tap="$1"
610
if="$(busybox sed 's/tap-//' <<<$tap)"
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
#!/opt/runcvm/bin/bash
1+
#!/.runcvm/guest/bin/bash
22

3-
. /opt/runcvm/scripts/runcvm-ctr-defaults && PATH="$RUNCVM_PATH"
3+
# Load original environment
4+
. /.runcvm/config
45

5-
echo "system_powerdown" | nc -w 1 -U $QEMU_MONITOR_SOCKET
6+
# Load defaults and aliases
7+
. $RUNCVM_GUEST/scripts/runcvm-ctr-defaults
8+
9+
echo "system_powerdown" | nc -w 1 -U $QEMU_MONITOR_SOCKET

0 commit comments

Comments
 (0)