Skip to content

Commit 6c4cbfd

Browse files
committed
Refactor NS resolver logic for clarity and consistency
The NS resolver munging was previously scattered across multiple locations in the Mock codebase. This duplication made the logic difficult to follow and led to bugs, such as the one addressed in rpm-software-management#1697. This change simplifies and consolidates the code. Follow-up-for: rpm-software-management#1697
1 parent eceeb52 commit 6c4cbfd

File tree

4 files changed

+36
-29
lines changed

4 files changed

+36
-29
lines changed

mock/py/mock.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,6 @@ def main():
766766
# allow bootstrap buildroot to access the network for getting packages
767767
bootstrap_buildroot_config['rpmbuild_networking'] = True
768768
bootstrap_buildroot_config['use_host_resolv'] = True
769-
util.setup_host_resolv(bootstrap_buildroot_config)
770769

771770
# disable updating bootstrap chroot
772771
bootstrap_buildroot_config['update_before_build'] = False
@@ -812,10 +811,6 @@ def main():
812811
mount_point = BindMountPoint(srcpath=key_dir, bindpath=chroot_dir)
813812
bootstrap_buildroot.mounts.add(mount_point)
814813

815-
# this changes config_opts['nspawn_args'], so do it after initializing
816-
# bootstrap chroot to not inherit the changes there
817-
util.setup_host_resolv(config_opts)
818-
819814
buildroot = Buildroot(config_opts, uidManager, state, plugins, bootstrap_buildroot)
820815

821816
for baseurl in options.repos:

mock/py/mockbuild/buildroot.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import stat
1515
import tempfile
1616
import uuid
17+
from textwrap import dedent
1718

1819
from . import file_util
1920
from . import mounts
@@ -491,9 +492,28 @@ def _copy_config(self, filename, symlink=False, warn=True):
491492

492493
@traceLog()
493494
def _setup_resolver_config(self):
495+
"""
496+
There are a few things to think about when reading/modifying this
497+
method.
498+
"""
494499
if self.config['use_host_resolv'] and self.config['rpmbuild_networking']:
495500
self._copy_config('resolv.conf')
496501
self._copy_config('hosts')
502+
else:
503+
# If we don't copy host's resolv.conf, we at least want to resolve
504+
# our own hostname. See commit 28027fc26d.
505+
if 'etc/hosts' not in self.config['files']:
506+
self.config['files']['etc/hosts'] = dedent('''\
507+
127.0.0.1 localhost localhost.localdomain
508+
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
509+
''')
510+
# We want to have empty resolv.conf to speedup name resolution
511+
# failure (see commit 3f939785bb).
512+
rconf = self.make_chroot_path("/etc/resolv.conf")
513+
file_util.unlink_if_exists(rconf)
514+
file_util.touch(rconf)
515+
516+
util.temporary_nspawn_resolver_hack(self.config)
497517

498518
@traceLog()
499519
def _setup_katello_ca(self):

mock/py/mockbuild/util.py

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import tempfile
2828
# pylint: disable=wrong-import-order
2929
import termios
30-
from textwrap import dedent
3130
import time
3231
import uuid
3332

@@ -782,9 +781,6 @@ def _prepare_nspawn_command(chrootPath, user, cmd, nspawn_args=None, env=None,
782781
if not USE_NSPAWN_SECCOMP:
783782
env['SYSTEMD_SECCOMP'] = '0'
784783

785-
if _check_nspawn_resolv_conf():
786-
nspawn_argv.append("--resolv-conf=off")
787-
788784
# The '/bin/sh -c' wrapper is explicitly requested (--shell). In this case
789785
# we shrink the list of arguments into one shell command, so the command is
790786
# completely shell-expanded.
@@ -859,30 +855,21 @@ def clean_env():
859855

860856

861857
@traceLog()
862-
def setup_host_resolv(config_opts):
863-
log = getLog()
864-
if not config_opts['use_host_resolv']:
865-
# If we don't copy host's resolv.conf, we at least want to resolve
866-
# our own hostname. See commit 28027fc26d.
867-
if 'etc/hosts' not in config_opts['files']:
868-
config_opts['files']['etc/hosts'] = dedent('''\
869-
127.0.0.1 localhost localhost.localdomain
870-
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
871-
''')
872-
858+
def temporary_nspawn_resolver_hack(config_opts):
859+
"""
860+
Remove once we stop supporting RHEL 8 hosts. RHEL 9+ has --resolv-conf=off
861+
which can be added into 'nspawn_args' unconditionally (even if nspawn is
862+
unused).
863+
"""
873864
if not USE_NSPAWN:
874-
# Not using nspawn -> don't touch /etc/resolv.conf; we already have
875-
# a valid file prepared by Buildroot._init() (if user requested).
865+
# Not using nspawn, no need for additional hacks.
876866
return
877867

878-
if config_opts['rpmbuild_networking'] and not config_opts['use_host_resolv']:
879-
# keep the default systemd-nspawn's /etc/resolv.conf
868+
if _check_nspawn_resolv_conf():
869+
# do not let nspawn override the files we create above
870+
config_opts["nspawn_args"] += ["--resolv-conf=off"]
880871
return
881872

882-
# Either we want to have empty resolv.conf to speedup name resolution
883-
# failure (rpmbuild_networking is off, see commit 3f939785bb), or we want
884-
# to copy hosts resolv.conf file.
885-
886873
resolv_path = (tempfile.mkstemp(prefix="mock-resolv."))[1]
887874
atexit.register(_nspawnTempResolvAtExit, resolv_path)
888875

@@ -893,9 +880,10 @@ def setup_host_resolv(config_opts):
893880
try:
894881
shutil.copyfile('/etc/resolv.conf', resolv_path)
895882
except FileNotFoundError:
883+
log = getLog()
896884
log.warning("Non-existing resolv.conf on host, using an empty one")
897885

898-
config_opts['nspawn_args'] += ['--bind={0}:/etc/resolv.conf'.format(resolv_path)]
886+
config_opts['nspawn_args'] += [f'--bind={resolv_path}:/etc/resolv.conf']
899887

900888

901889
def pretty_getcwd():
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The NS resolver munging was previously scattered across multiple locations in
2+
the Mock codebase. This duplication made the logic difficult to follow and led
3+
to bugs, such as the one addressed in [PR#1697][]. This change simplifies and
4+
consolidates the code.

0 commit comments

Comments
 (0)