contrib: improve nm-in-container.sh script

This commit is contained in:
Thomas Haller 2021-09-14 13:23:26 +02:00
parent 3a3613b561
commit 7fea431061
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
2 changed files with 58 additions and 27 deletions

View file

@ -7,7 +7,7 @@ die() {
exit 1
}
cleanup() {
do_cleanup() {
local IDX="$1"
pkill -F "/tmp/nm-dnsmasq-d_$IDX.pid" dnsmasq &>/dev/null || :
@ -21,7 +21,7 @@ cleanup() {
ip link del "d_$IDX" &>/dev/null || :
}
setup() {
do_setup() {
local IDX="$1"
cleanup "$IDX"
@ -62,13 +62,20 @@ EOF
&
}
do_redo() {
cleanup "$1"
setup "$1"
}
###############################################################################
IDX=1
CMD=
CMD=redo
for (( i=1 ; i<="$#" ; )) ; do
c="${@:$i:1}"
i=$((i+1))
case "$c" in
setup|cleanup)
redo|setup|cleanup)
CMD="$c"
;;
--idx|-i)
@ -82,6 +89,4 @@ for (( i=1 ; i<="$#" ; )) ; do
esac
done
test "$CMD" != "" || die "missing command (setup|cleanup)"
$CMD "$IDX"
do_$CMD "$IDX"

View file

@ -2,6 +2,23 @@
set -e
###############################################################################
# Script to create a podman container for testing NetworkManager.
#
# Commands:
# - build: build a new image, named "$CONTAINER_NAME_REPOSITORY:$CONTAINER_NAME_TAG" ("nm:nm")
# - run: start the container and tag it "$CONTAINER_NAME_NAME" ("nm")
# - exec: run bash inside the container
# - stop: stop the container
# - clean: delete the container and the image.
#
# Options:
# --no-cleanup: don't delete the CONTAINERFILE and other artifacts
# --stop: only has effect with "run". It will stop the container afterwards.
#
# It bind mounts the current working directory inside the container.
# You can run `make install` and run tests.
# There is a script nm-env-prepare to generate a net1 interface for testing.
###############################################################################
BASE_IMAGE="${BASE_IMAGE:-fedora:latest}"
@ -9,12 +26,23 @@ BASE_IMAGE="${BASE_IMAGE:-fedora:latest}"
BASEDIR_NM="$(readlink -f "$(dirname "$(readlink -f "$0")")/../..")"
BASEDIR="$BASEDIR_NM/contrib/scripts/nm-in-container.d"
CONTAINER_NAME_REPOSITORY=${CONTAINER_NAME_REPOSITORY:-my}
CONTAINER_NAME_REPOSITORY=${CONTAINER_NAME_REPOSITORY:-nm}
CONTAINER_NAME_TAG=${CONTAINER_NAME_TAG:-nm}
CONTAINER_NAME_NAME=${CONTAINER_NAME_NAME:-nm}
###############################################################################
usage() {
cat <<EOF
$0: build|run|exec|stop|clean [--no-cleanup] [--stop]
EOF
echo
awk '/^####*$/{ if(on) exit; on=1} { if (on) { if (on==2) print(substr($0,3)); on=2; } }' "$BASH_SOURCE"
echo
}
###############################################################################
die() {
printf "%s\n" "$*" >&2
exit 1
@ -152,8 +180,8 @@ RUN sed 's/.*RateLimitBurst=.*/RateLimitBurst=0/' /etc/systemd/journald.conf -i
RUN echo -e '[logging]\nlevel=TRACE\ndomains=ALL,VPN_PLUGIN:TRACE\n' >> /etc/NetworkManager/conf.d/90-my.conf
RUN echo -e '[main]\nno-auto-default=*\ndebug=RLIMIT_CORE,fatal-warnings\n' >> /etc/NetworkManager/conf.d/90-my.conf
RUN echo -e '[device-veths-1]\nmatch-device=interface-name:d_*\nmanaged=0\n' >> /etc/NetworkManager/conf.d/90-my.conf
RUN echo -e '[device-veths-2]\nmatch-device=interface-name:net*\nmanaged=1\n' >> /etc/NetworkManager/conf.d/90-my.conf
RUN echo -e '[device-managed-0]\nmatch-device=interface-name:d_*,interface-name:tap*\nmanaged=0\n' >> /etc/NetworkManager/conf.d/90-my.conf
RUN echo -e '[device-managed-1]\nmatch-device=interface-name:net*\nmanaged=1\n' >> /etc/NetworkManager/conf.d/90-my.conf
RUN rm -rf /etc/NetworkManager/system-connections/*
@ -181,14 +209,6 @@ EOF
###############################################################################
usage() {
cat <<EOF
$0: build|run|exec|clean [--no-cleanup]
EOF
}
###############################################################################
container_image_exists() {
podman image exists my:nm || return 1
}
@ -241,10 +261,20 @@ do_run() {
do_exec() {
do_run
podman exec --workdir "$BASEDIR_NM" -it "$CONTAINER_NAME_NAME" bash
if [ "$DO_STOP" = 1 ]; then
do_stop
fi
}
do_stop() {
container_is_running "$CONTAINER_NAME_NAME" || return 0
podman stop "$CONTAINER_NAME_NAME"
}
###############################################################################
DO_STOP=0
CMD=exec
for (( i=1 ; i<="$#" ; )) ; do
c="${@:$i:1}"
@ -253,7 +283,10 @@ for (( i=1 ; i<="$#" ; )) ; do
--no-cleanup)
DO_CLEANUP=0
;;
build|run|exec|clean)
--stop)
DO_STOP=1
;;
build|run|exec|stop|clean)
CMD=$c
;;
-h|--help)
@ -272,11 +305,4 @@ test "$UID" != 0 || die "cannot run as root"
###############################################################################
case "$CMD" in
clean|build|run|exec)
do_$CMD
;;
*)
die "missing command, one of build|run|exec|clean"
;;
esac
do_$CMD