From 1b4ce2c5c07c6e4665a0ea879eb09dc4dcd3de4c Mon Sep 17 00:00:00 2001 From: Alvar Penning Date: Sat, 12 Feb 2022 14:07:48 +0100 Subject: [PATCH] resolv_conf: make /etc/resolv.conf selectable By refactoring the previous --host-resolv flag into a more generic --resolv flag which accepts auto, guest, and host as its parameter, one can select the /etc/resolv.conf to be used. By default, the auto behavior is chosen and prefers the guest's resolv.conf over an overlay from the host. Related to #57. --- CHANGELOG.md | 2 +- README.md | 13 ++++++++++--- modules/resolv_conf.sh | 30 +++++++++++++++++++++++------- pimod.sh | 29 ++++++++++++++++++++++------- 4 files changed, 56 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bc2bf0..3ee599c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added -- New --host-resolv flag to enforce using the host's /etc/resolv.conf. +- New -r/--resolv flag to select which /etc/resolv.conf to use. ### Fixed - Apply `ENV` variables also to `HOST` commands. diff --git a/README.md b/README.md index de2251f..5fdbdef 100644 --- a/README.md +++ b/README.md @@ -38,13 +38,20 @@ For detailed information [read our paper](https://jonashoechst.de/assets/papers/ pimod pimod.sh -h Usage: pimod.sh [Options] Pifile +Options: Options: -c --cache DEST Define cache location. -d --debug Debug on failure; run an interactive shell before tear down. -h --help Print this help message. - --host-resolv Always uses the host's /etc/resolv.conf file. - Be aware, that when run within Docker this might be Docker's - resolv.conf file. + -r --resolv TYPE Specify which /etc/resolv.conf file to use for networking. + By default, TYPE "auto" is used, which prefers an already + existing resolv.conf, only to be replaced by the host's if + missing. + TYPE "guest" never mounts the host's file within the guest, + even when such a file is absent within the image. + TYPE "host" always uses the host's file within the guest. + Be aware that when run within Docker, the host's file might + be Docker's resolv.conf file. -t --trace Trace each executed command for debugging. ``` diff --git a/modules/resolv_conf.sh b/modules/resolv_conf.sh index bfa0464..8b5d474 100644 --- a/modules/resolv_conf.sh +++ b/modules/resolv_conf.sh @@ -1,5 +1,5 @@ if [ -z "${PIMOD_HOST_RESOLV+x}" ]; then - PIMOD_HOST_RESOLV=0 + PIMOD_HOST_RESOLV_TYPE="auto" fi # resolv_conf_setup checks the /etc/resolv.conf file within an image and remaps @@ -7,9 +7,25 @@ fi resolv_conf_setup() { local resolv_conf="${CHROOT_MOUNT}/etc/resolv.conf" - if [[ -f "${resolv_conf}" ]] || (RUN test -e "/etc/resolv.conf"); then - [[ "${PIMOD_HOST_RESOLV}" -eq "0" ]] && return - fi + case "${PIMOD_HOST_RESOLV_TYPE}" in + auto) + # Do not mount the host's file when a /etc/resolv.conf already exists. + ((test -f "${resolv_conf}") || (RUN test -e "/etc/resolv.conf")) && return + ;; + + guest) + # Never mount the host's file. + return + ;; + + host) + # Always mount the host's file as an overlay. + ;; + + *) + echo -e "\033[0;31m### Error: unknown resolv type ${PIMOD_HOST_RESOLV_TYPE} \033[0m" + return 1 + esac if [[ -L "${resolv_conf}" ]]; then RESOLV_CONF_BACKUP=$(mktemp -u) @@ -17,8 +33,8 @@ resolv_conf_setup() { fi if ! touch "${resolv_conf}"; then - echo -e "\033[0;33m### Warning: Mounting ${resolv_conf} failed.\033[0m" - return + echo -e "\033[0;31m### Error: Mounting ${resolv_conf} failed.\033[0m" + return 1 fi mount -o ro,bind /etc/resolv.conf "${resolv_conf}" @@ -27,7 +43,7 @@ resolv_conf_setup() { # resolv_conf_teardown resets the actions done by resolv_conf_setup. resolv_conf_teardown() { - [[ -z ${RESOLVE_MOUNT+x} ]] && return 0 + [[ -z ${RESOLVE_MOUNT+x} ]] && return local resolv_conf="${CHROOT_MOUNT}/etc/resolv.conf" diff --git a/pimod.sh b/pimod.sh index 1860a9d..570c7b6 100755 --- a/pimod.sh +++ b/pimod.sh @@ -26,14 +26,19 @@ Options: -c --cache DEST Define cache location. -d --debug Debug on failure; run an interactive shell before tear down. -h --help Print this help message. - --host-resolv Always uses the host's /etc/resolv.conf file. - Be aware, that when run within Docker this might be Docker's - resolv.conf file. + -r --resolv TYPE Specify which /etc/resolv.conf file to use for networking. + By default, TYPE "auto" is used, which prefers an already + existing resolv.conf, only to be replaced by the host's if + missing. + TYPE "guest" never mounts the host's file within the guest, + even when such a file is absent within the image. + TYPE "host" always uses the host's file within the guest. + Be aware that when run within Docker, the host's file might + be Docker's resolv.conf file. -t --trace Trace each executed command for debugging. EOF } - main() { local pifile @@ -56,9 +61,19 @@ main() { exit 0 ;; - --host-resolv) - # PIMOD_HOST_RESOLV is defined in modules/resolv_conf.sh - PIMOD_HOST_RESOLV=1 + -r|--resolv) + [[ "$#" -le "2" ]] && (echo "Usage: $0 --resolv KIND"; exit 1) + case "$2" in + auto|guest|host) + # PIMOD_HOST_RESOLV_TYPE is defined in modules/resolv_conf.sh + PIMOD_HOST_RESOLV_TYPE="$2" + ;; + + *) + echo "Usage: $0 --resolv KIND" + exit 1 + esac + shift ;; -t|--trace)