Copy resolv.conf handling from diskimage-builder

The copy in finalise_base runs too late for us, leaving a stale
resolv.conf around (usually copied from the build environment).

Change-Id: I4b89119b8e2eded72973fa12879b1419542ba984
This commit is contained in:
Dmitry Tantsur 2021-03-19 15:43:58 +01:00
parent 0523f992bc
commit 661158b42d
1 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,42 @@
#!/bin/bash
# dib-lint: disable=safe_sudo
if [ "${DIB_DEBUG_TRACE:-0}" -gt 0 ]; then
set -x
fi
set -eu
set -o pipefail
# NOTE(dtantsur): this a copy of code from img-functions. We need it because it
# runs too late when the image is already built.
# Finalise resolv.conf
#
# NOTE(ianw): the /etc/resolv.conf.ORIG file is an
# external interface; elements might put a resolv.conf they
# want in the final image into this file.
#
# In create_base() we replaced/created the initial resolv.conf
# inside the image with a copy of the "outside" version so that
# resolving during the build will work.
#
# If that file has been replace with a symlink (resolvconf package
# can do this), or marked immutable, then don't restore the
# original version, just leave it alone.
if [ -L $TMP_MOUNT_PATH/etc/resolv.conf ] || \
lsattr $TMP_MOUNT_PATH/etc/resolv.conf | grep '^....i' >/dev/null ; then
# We're keeping the contents of resolv.conf set in the elements,
# so remove the old saved file
sudo rm -f $TMP_MOUNT_PATH/etc/resolv.conf.ORIG
else
# Remove the resolv.conf we created and put the original (or
# perhaps modified) version back.
sudo rm -f $TMP_MOUNT_PATH/etc/resolv.conf
# Note that we use -L and -f to test here as test (and bash [[)
# return false with -e if the link target does not exist.
if [ -L $TMP_MOUNT_PATH/etc/resolv.conf.ORIG ] || [ -f $TMP_MOUNT_PATH/etc/resolv.conf.ORIG ] ; then
sudo mv $TMP_MOUNT_PATH/etc/resolv.conf.ORIG $TMP_MOUNT_PATH/etc/resolv.conf
fi
fi