names: Try to install systemd-resolved during app setup

- If installing systemd-resolved for the first time, set fallback DNS setting to
True irrespective of the app version.

Tests:

- Ensure that systemd-resolved is not installed. On a fresh systemd without
first setup done, run service.

- Names app setup is run and systemd-resolved is installed if internet
connection is available. Setup succeeds. Fallback DNS setting is true in privacy
app. systemd-resolved has been restarted and current DNS known to Network
Manager has been populated in it. Name resolution works.

- If Internet connection is not available, setup still succeeds but
systemd-resolved package is not installed.

- Rerun setup without internet connectivity. Setup succeeds without installing
systemd-resolved.

- Rerun setup with internet connectivity. Setup succeeds and installs
systemd-resolved. Fallback DNS setting is true in privacy app. systemd-resolved
has been restarted and current DNS known to Network Manager has been populated
in it. Name resolution works.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Veiko Aasa <veiko17@disroot.org>
This commit is contained in:
Sunil Mohan Adapa 2024-10-01 19:26:51 -07:00 committed by Veiko Aasa
parent 70c37f309e
commit 7e8819d7d5
No known key found for this signature in database
GPG Key ID: 478539CAE680674E
2 changed files with 37 additions and 14 deletions

View File

@ -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()

View File

@ -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,