diff --git a/plinth/modules/names/__init__.py b/plinth/modules/names/__init__.py index 6236f78f7..c46ca09ad 100644 --- a/plinth/modules/names/__init__.py +++ b/plinth/modules/names/__init__.py @@ -102,22 +102,31 @@ class NamesApp(app_module.App): """Install and configure the app.""" super().setup(old_version) - # Fresh install or upgrading to version 2 - if old_version < 2: - privileged.set_resolved_configuration(dns_fallback=True) + if not is_resolved_installed(): + try: + # Requires internet connectivity and could fail + privileged.install_resolved() + privileged.set_resolved_configuration(dns_fallback=True) + except Exception: + pass - # Load the configuration files for systemd-resolved provided by - # FreedomBox. - service_privileged.restart('systemd-resolved') + if is_resolved_installed(): + # Fresh install or upgrading to version 2 + if old_version < 2: + privileged.set_resolved_configuration(dns_fallback=True) - # After systemd-resolved is freshly installed, /etc/resolve.conf - # becomes a symlink to configuration pointing to systemd-resovled stub - # resolver. However, the old contents are not fed from network-manager - # (if it was present earlier and wrote to /etc/resolve.conf). Ask - # network-manager to feed the DNS servers from the connections it has - # established to systemd-resolved so that using fallback DNS servers is - # not necessary. - network.refeed_dns() + # Load the configuration files for systemd-resolved provided by + # FreedomBox. + service_privileged.restart('systemd-resolved') + + # After systemd-resolved is freshly installed, /etc/resolve.conf + # becomes a symlink to configuration pointing to systemd-resovled + # stub resolver. However, the old contents are not fed from + # network-manager (if it was present earlier and wrote to + # /etc/resolve.conf). Ask network-manager to feed the DNS servers + # from the connections it has established to systemd-resolved so + # that using fallback DNS servers is not necessary. + network.refeed_dns() self.enable() diff --git a/plinth/modules/names/privileged.py b/plinth/modules/names/privileged.py index 8d492085e..d113f11b5 100644 --- a/plinth/modules/names/privileged.py +++ b/plinth/modules/names/privileged.py @@ -52,6 +52,20 @@ def set_domain_name(domain_name: str | None = None): hosts_path.write_text(''.join(new_lines), encoding='utf-8') +@privileged +def install_resolved(): + """Install systemd-resolved related packages.""" + packages = ['systemd-resolved', 'libnss-resolve'] + subprocess.run(['dpkg', '--configure', '-a'], check=False) + with action_utils.apt_hold_freedombox(): + action_utils.run_apt_command(['--fix-broken', 'install']) + returncode = action_utils.run_apt_command(['install'] + packages) + + if returncode: + raise RuntimeError( + f'Apt command failed with return code: {returncode}') + + @privileged def set_resolved_configuration(dns_fallback: bool | None = None, dns_over_tls: str | None = None,