Debian Codegraph @

tools/nm-in-container

raw symbols SCIP
1 #!/bin/bash
2
3 set -e
4
5 ###############################################################################
6 # Script to create a podman container for testing NetworkManager.
7 #
8 # Commands:
9 # - build: build a new image, named "$CONTAINER_NAME_REPOSITORY:$CONTAINER_NAME_TAG" ("nm:nm").
10 # - run: start the container and tag it "$CONTAINER_NAME_NAME" ("nm").
11 # - exec: run bash inside the container (this is the default).
12 # - journal|j: print the journal from inside the container.
13 # - stop: stop the container.
14 # - reset: stop and delete the container.
15 # - clean: stop and delete the container and the image.
16 #
17 # Options:
18 # --no-cleanup: don't delete the CONTAINERFILE and other artifacts
19 # --stop: only has effect with "run". It will stop the container afterwards.
20 # -- [EXTRA_ARGS]:
21 # - with command "exec", provide a command and arguments to run in the container.
22 # Defaults to "bash".
23 # - with command "journal", additional arguments that are passed to journalctl.
24 #
25 # It bind mounts the current working directory inside the container.
26 # You can run `meson install` and run tests.
27 # There is a script nm-env-prepare.sh to generate a net1 interface for testing.
28 #
29 # This will bind-mount the NetworkManager working tree inside the container (and symlink
30 # from /NetworkManager). Create a file ".git/nm-in-container-host" to bind mount the host's
31 # "/" to "/Host".
32 #
33 # Create a symlink ./.git/NetworkManager-ci, to also bind-mount the CI directory.
34 # Create additional symlinks ./.git/nm-guest-link-*, to bind mount additional
35 # directories.
36 #
37 # Currently NM-ci requires a working eth1.
38 # Hence call `nm-env-prepare.sh --prefix eth -i 1 && sleep 1 && nmcli device connect eth1` before
39 # running a CI test.
40 ###############################################################################
41
42 if [ -z "$BASE_IMAGE" ]; then
43 if grep -q "^ID=fedora$" /etc/os-release 2>/dev/null ; then
44 BASE_IMAGE="$(sed -n 's/^VERSION_ID=\([0-9]\+\)$/fedora:\1/p' /etc/os-release)"
45 fi
46 fi
47 if [ -z "$BASE_IMAGE" ]; then
48 BASE_IMAGE=fedora:latest
49 fi
50
51 BASEDIR_NM="$(readlink -f "$(dirname "$(readlink -f "$0")")/..")"
52 BASEDIR_DATA="$BASEDIR_NM/tools/nm-guest-data"
53
54 SYMLINK_NAME=()
55 SYMLINK_TARGET=()
56 for d in $(ls -1d "$BASEDIR_NM/.git/NetworkManager-ci" "$BASEDIR_NM/.git/nm-guest-link-"* 2>/dev/null) ; do
57 NAME="${d##*/}"
58 NAME="${NAME##nm-guest-link-}"
59 TARGET="$(readlink -f "$d")"
60 test -e "$TARGET"
61 SYMLINK_NAME+=("$NAME")
62 SYMLINK_TARGET+=("$TARGET")
63 done
64
65 CONTAINER_NAME_REPOSITORY=${CONTAINER_NAME_REPOSITORY:-nm}
66 CONTAINER_NAME_TAG=${CONTAINER_NAME_TAG:-nm}
67 CONTAINER_NAME_NAME=${CONTAINER_NAME_NAME:-nm}
68
69 EXEC_ENV=()
70
71 ###############################################################################
72
73 usage() {
74 cat <<EOF
75 $0: build|run|exec|stop|reset|reexec|clean|journal [--no-cleanup] [--stop] [-- EXTRA_ARGS]
76 EOF
77 echo
78 awk '/^####*$/{ if(on) exit; on=1} { if (on) { if (on==2) print(substr($0,3)); on=2; } }' "$BASH_SOURCE"
79 echo
80 }
81
82 ###############################################################################
83
84 die() {
85 (
86 echo -n -e "\033[31m"
87 printf "%s" "$*"
88 echo -e "\033[0m"
89 ) >&2
90 exit 1
91 }
92
93 ###############################################################################
94
95 CLEANUP_FILES=()
96 DO_CLEANUP=1
97 cleanup() {
98 test "$DO_CLEANUP" = 1 || return 0
99 for f in "${CLEANUP_FILES[@]}" ; do
100 rm -rf "$f"
101 done
102 }
103
104 trap cleanup EXIT
105
106 ###############################################################################
107
108 tmp_file() {
109 cat > "$1"
110 CLEANUP_FILES+=( "$1" )
111 test -z "$2" || chmod "$2" "$1"
112 }
113
114 gen_file() {
115 local PERMS
116 [[ $1 =~ bin-* ]] && PERMS=755 || PERMS=644
117 sed "s|{{BASEDIR_NM}}|$BASEDIR_NM|g" "$BASEDIR_DATA/$1.in" \
118 | tmp_file "$BASEDIR_DATA/data-$1" $PERMS
119 }
120
121 bind_files() {
122 VARIABLE_NAME="$1"
123
124 ARR=()
125 H=~
126
127 ARR+=( -v "$BASEDIR_NM:$BASEDIR_NM" )
128
129 if [ -e "$BASEDIR_NM/.git/nm-in-container-host" ] ; then
130 ARR+=( -v /:/Host )
131 fi
132
133 for i in $(seq 1 ${#SYMLINK_TARGET[@]}) ; do
134 j=$((i - 1))
135 ARR+=( -v "${SYMLINK_TARGET[$j]}:${SYMLINK_TARGET[$j]}" )
136 done
137
138 for f in ~/.gitconfig* ~/.vim* ; do
139 test -e "$f" || continue
140 f2="${f#$H/}"
141 [[ "$f2" = .viminf* ]] && continue
142 [[ "$f2" = *.tmp ]] && continue
143 [[ "$f2" = *~ ]] && continue
144 f2="/root/$f2"
145 ARR+=( -v "$f:$f2" )
146 done
147
148 eval "$VARIABLE_NAME=( \"\${ARR[@]}\" )"
149 }
150
151 create_dockerfile() {
152
153 local CONTAINERFILE="$1"
154 local BASE_IMAGE="$2"
155 local GEN_FILES="bin-nm-env-prepare.sh bin-nm-deploy.sh bin-_nm-in-container-setup.sh
156 etc-rc.local etc-motd-container etc-bashrc.my etc-bashrc-motd.my nm-90-my.conf
157 nm-95-user.conf home-bash_history home-gdbinit home-gdb_history home-behaverc
158 systemd-20-nm.override"
159
160 cp "$BASEDIR_NM/contrib/scripts/NM-log" "$BASEDIR_DATA/data-bin-NM-log"
161 CLEANUP_FILES+=( "$BASEDIR_DATA/data-NM-log" )
162
163 for f in $GEN_FILES; do
164 gen_file "$f"
165 done
166 chmod 755 "$BASEDIR_DATA/data-etc-rc.local"
167
168 RUN_LN_BASEDIR_NM=
169 if [ -n "$BASEDIR_NM" -a "$BASEDIR_NM" != "/NetworkManager" ] ; then
170 RUN_LN_BASEDIR_NM="RUN ln -snf \"$BASEDIR_NM\" /NetworkManager"
171 fi
172
173 RUN_LN_SYMLINK_CMDS=""
174 for i in $(seq 1 ${#SYMLINK_NAME[@]}) ; do
175 j=$((i - 1))
176 if [ -d "${SYMLINK_TARGET[$j]}" ] ; then
177 RUN_LN_SYMLINK_CMDS="$RUN_LN_SYMLINK_CMDS"$'\n'"RUN ln -snf \"${SYMLINK_TARGET[$j]}\" \"/${SYMLINK_NAME[$j]}\""
178 fi
179 done
180
181 cat <<EOF | tmp_file "$CONTAINERFILE"
182 FROM $BASE_IMAGE
183
184 ENTRYPOINT ["/sbin/init"]
185
186 RUN sed -i 's/^tsflags=.*/tsflags=/' /etc/dnf/dnf.conf
187
188 RUN dnf install -y \\
189 --skip-broken \\
190 \\
191 /usr/bin/python \\
192 audit \\
193 ModemManager-devel \\
194 ModemManager-glib-devel \\
195 NetworkManager \\
196 audit-libs-devel \\
197 bash-completion \\
198 bind-utils \\
199 black \\
200 bluez-libs-devel \\
201 ccache \\
202 clang \\
203 clang-tools-extra \\
204 cscope \\
205 dbus-devel \\
206 dbus-x11 \\
207 dhclient \\
208 dnsmasq \\
209 firewalld-filesystem \\
210 gcc-c++ \\
211 gdb \\
212 gettext-devel \\
213 git \\
214 glib2-doc \\
215 glibc-langpack-pl \\
216 gnutls-devel \\
217 gobject-introspection-devel \\
218 gtk-doc \\
219 intltool \\
220 iproute \\
221 iproute-tc \\
222 iptables \\
223 jansson-devel \\
224 libasan \\
225 libcurl-devel \\
226 libndp-devel \\
227 libnvme-devel \\
228 libpsl-devel \\
229 libselinux-devel \\
230 libubsan \\
231 libuuid-devel \\
232 meson \\
233 plocate \\
234 mobile-broadband-provider-info-devel \\
235 newt-devel \\
236 nispor \\
237 nmstate \\
238 nss-devel \\
239 polkit-devel \\
240 ppp \\
241 ppp-devel \\
242 procps \\
243 python3-behave \\
244 python3-black \\
245 python3-dbus \\
246 python3-devel \\
247 python3-gobject \\
248 python3-pexpect \\
249 python3-pip \\
250 python3-pyte \\
251 python3-pyyaml \\
252 qt-devel \\
253 radvd \\
254 readline-devel \\
255 rp-pppoe \\
256 rpm-build \\
257 strace \\
258 systemd \\
259 systemd-devel \\
260 tcpdump \\
261 teamd-devel \\
262 vala \\
263 vala-devel \\
264 valgrind \\
265 vim \\
266 which \\
267 \\
268 'dbus*' \\
269 'openvswitch*' \\
270 /usr/bin/pytest \\
271 /usr/bin/debuginfo-install \\
272 NetworkManager-openvpn \\
273 NetworkManager-pptp \\
274 NetworkManager-strongswan \\
275 NetworkManager-vpnc \\
276 NetworkManager-ovs \\
277 NetworkManager-wifi \\
278 NetworkManager-team \\
279 NetworkManager-ppp \\
280 cryptsetup \\
281 dhcp-client \\
282 dhcp-relay \\
283 dhcp-server \\
284 dnsmasq \\
285 dracut-network \\
286 ethtool \\
287 firewalld \\
288 gcc \\
289 git \\
290 hostapd \\
291 iproute-tc \\
292 iputils \\
293 iscsi-initiator-utils \\
294 iw \\
295 ldns \\
296 libreswan \\
297 libselinux-utils \\
298 libyaml-devel \\
299 logrotate \\
300 lvm2 \\
301 mdadm \\
302 net-tools \\
303 nfs-utils \\
304 nmap-ncat \\
305 nss-tools \\
306 openvpn \\
307 perl-IO-Pty-Easy \\
308 perl-IO-Tty \\
309 psmisc \\
310 python3-dbus \\
311 python3-gobject \\
312 python3-netaddr \\
313 qemu-kvm \\
314 radvd \\
315 rp-pppoe \\
316 scsi-target-utils \\
317 tcpdump \\
318 tcpreplay \\
319 tuned \\
320 wireguard-tools \\
321 wireshark-cli
322
323 RUN dnf debuginfo-install --skip-unavailable --skip-broken NetworkManager \$(ldd /usr/sbin/NetworkManager | sed -n 's/.* => \\(.*\\) (0x[0-9A-Fa-f]*)$/\1/p' | xargs -n1 readlink -f) -y
324
325 RUN dnf clean all
326
327 RUN pip3 install --user behave_html_formatter || true
328
329 RUN mkdir -p /etc/systemd/system/NetworkManager.service.d
330
331 COPY data-bin-NM-log "/usr/bin/NM-log"
332 COPY data-bin-nm-env-prepare.sh "/usr/bin/nm-env-prepare.sh"
333 COPY data-bin-nm-deploy.sh "/usr/bin/nm-deploy.sh"
334 COPY data-bin-_nm-in-container-setup.sh "/usr/bin/_nm-in-container-setup.sh"
335 COPY data-etc-rc.local "/etc/rc.d/rc.local"
336 COPY data-etc-motd-container /etc/motd
337 COPY data-etc-bashrc.my /etc/bashrc.my
338 COPY data-etc-bashrc-motd.my /etc/bashrc-motd.my
339 COPY data-nm-90-my.conf /etc/NetworkManager/conf.d/90-my.conf
340 COPY data-nm-95-user.conf /etc/NetworkManager/conf.d/95-user.conf
341 COPY data-home-bash_history /root/.bash_history
342 COPY data-home-gdbinit /root/.gdbinit
343 COPY data-home-gdb_history /root/.gdb_history
344 COPY data-home-behaverc /root/.behaverc
345 COPY data-systemd-20-nm.override /etc/systemd/system/NetworkManager.service.d/20-nm.override
346
347 RUN systemctl enable NetworkManager
348
349 # Generate a stable machine id.
350 RUN echo "10001000100010001000100010001000" > /etc/machine-id
351
352 RUN echo -e "# Default from the container image\nnameserver 8.8.8.8" > /etc/resolv.conf
353
354 # Generate a fixed (version 1) secret key.
355 RUN mkdir -p /var/lib/NetworkManager
356 RUN chmod 700 /var/lib/NetworkManager
357 RUN echo -n "nm-in-container-secret-key" > /var/lib/NetworkManager/secret_key
358 RUN chmod 600 /var/lib/NetworkManager/secret_key
359
360 RUN mkdir -p /etc/systemd/journald.conf.d/ && \
361 echo "RateLimitBurst=0" > /etc/systemd/journald.conf.d/no-rate-limit.conf
362
363 $RUN_LN_BASEDIR_NM
364 $RUN_LN_SYMLINK_CMDS
365
366 RUN rm -rf /etc/NetworkManager/system-connections/*
367
368 RUN echo -e '\n. /etc/bashrc.my\n' >> /etc/bashrc
369 RUN echo -e '\n. /etc/bashrc-motd.my\n' >> /etc/bashrc
370
371 RUN updatedb
372 EOF
373 }
374
375 ###############################################################################
376
377 container_image_exists() {
378 podman image exists "$1" || return 1
379 }
380
381 container_exists() {
382 podman container exists "$1" || return 1
383 }
384
385 container_is_running() {
386 test "$(podman ps --format "{{.ID}} {{.Names}}" | sed -n "s/ $1\$/\0/p")" != "" || return 1
387 }
388
389 ###############################################################################
390
391 do_reset() {
392 podman stop "$CONTAINER_NAME_NAME" || :
393 podman rm "$CONTAINER_NAME_NAME" || :
394 }
395
396 do_clean() {
397 do_reset
398 podman rmi "$CONTAINER_NAME_REPOSITORY:$CONTAINER_NAME_TAG" || :
399 }
400
401 do_build() {
402 container_image_exists "$CONTAINER_NAME_REPOSITORY:$CONTAINER_NAME_TAG" && return 0
403
404 CONTAINERFILE="$BASEDIR_DATA/containerfile"
405 create_dockerfile "$CONTAINERFILE" "$BASE_IMAGE"
406 podman build --squash-all --tag "$CONTAINER_NAME_REPOSITORY:$CONTAINER_NAME_TAG" -f "$CONTAINERFILE"
407 }
408
409 do_run() {
410 do_build
411
412 if container_is_running "$CONTAINER_NAME_NAME" ; then
413 return 0
414 fi
415
416 if container_exists "$CONTAINER_NAME_NAME" ; then
417 podman start "$CONTAINER_NAME_NAME"
418 return 0
419 fi
420
421 bind_files BIND_FILES
422
423 podman run --privileged \
424 --name "$CONTAINER_NAME_NAME" \
425 --dns=none \
426 --no-hosts \
427 -d \
428 "${BIND_FILES[@]}" \
429 "$CONTAINER_NAME_REPOSITORY:$CONTAINER_NAME_TAG"
430 }
431
432 do_exec() {
433 do_run
434
435 local e
436 local EXTRA_ARGS=("$@")
437 if [ "${#EXTRA_ARGS[@]}" = 0 ]; then
438 EXTRA_ARGS=('bash')
439 fi
440
441 local ENV=()
442 for e in "${EXEC_ENV[@]}" ; do
443 ENV+=(-e "$e")
444 done
445
446 podman exec "${ENV[@]}" --workdir "$BASEDIR_NM" -it "$CONTAINER_NAME_NAME" "${EXTRA_ARGS[@]}"
447
448 if [ "$DO_STOP" = 1 ]; then
449 do_stop
450 fi
451 }
452
453 do_reexec() {
454 do_reset
455 do_exec "$@"
456 }
457
458 do_journal() {
459 EXEC_ENV+=( "SYSTEMD_COLORS=0" )
460 do_exec "journalctl" --no-pager "$@"
461 }
462
463 do_stop() {
464 container_is_running "$CONTAINER_NAME_NAME" || return 0
465 podman stop "$CONTAINER_NAME_NAME"
466 }
467
468 ###############################################################################
469
470 DO_STOP=0
471 CMD=exec
472 EXTRA_ARGS=()
473 for (( i=1 ; i<="$#" ; )) ; do
474 c="${@:$i:1}"
475 i=$((i+1))
476 case "$c" in
477 --no-cleanup)
478 DO_CLEANUP=0
479 ;;
480 --stop)
481 DO_STOP=1
482 ;;
483 j)
484 CMD=journal
485 ;;
486 build|run|exec|stop|reset|reexec|clean|journal)
487 CMD=$c
488 ;;
489 --)
490 EXTRA_ARGS=( "${@:$i}" )
491 break
492 ;;
493 -h|--help)
494 usage
495 exit 0
496 ;;
497 *)
498 if [ "$CMD" = "journal" ]; then
499 EXTRA_ARGS=( "${@:$((i-1))}" )
500 break;
501 else
502 usage
503 die "invalid argument: $c"
504 fi
505 ;;
506 esac
507 done
508
509 ###############################################################################
510
511 test "$UID" != 0 || die "cannot run as root"
512
513 if test "$CMD" != exec -a "$CMD" != journal -a "$CMD" != reexec -a "${#EXTRA_ARGS[@]}" != 0 ; then
514 die "Extra arguments are only allowed with exec|journal|reexec command"
515 fi
516
517 ###############################################################################
518
519 do_$CMD "${EXTRA_ARGS[@]}"