Skip to content

Commit

Permalink
resolv_conf: make /etc/resolv.conf selectable
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
oxzi committed Feb 12, 2022
1 parent 1386db6 commit 1b4ce2c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
```

Expand Down
30 changes: 23 additions & 7 deletions modules/resolv_conf.sh
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
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
# it, if necessary.
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)
mv "${resolv_conf}" "${RESOLV_CONF_BACKUP}"
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}"

Expand All @@ -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"

Expand Down
29 changes: 22 additions & 7 deletions pimod.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down

0 comments on commit 1b4ce2c

Please sign in to comment.