2014-08-11 16:30:01 +02:00
|
|
|
#!/usr/bin/env python3
|
2015-02-18 15:06:10 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2020-08-25 16:05:56 +02:00
|
|
|
# === This file is part of Calamares - <https://calamares.io> ===
|
2014-08-11 16:30:01 +02:00
|
|
|
#
|
2020-08-24 16:36:47 +02:00
|
|
|
# SPDX-FileCopyrightText: 2014 Philip Müller <philm@manjaro.org>
|
|
|
|
# SPDX-FileCopyrightText: 2014 Teo Mrnjavac <teo@kde.org>
|
|
|
|
# SPDX-FileCopyrightText: 2017 Alf Gaida <agaida@siduction.org>
|
|
|
|
# SPDX-FileCopyrightText: 2019 Adriaan de Groot <groot@kde.org>
|
2021-08-16 14:06:13 +02:00
|
|
|
# SPDX-FileCopyrightText: 2021 Anke boersma <demm@koasx.us>
|
2020-08-24 16:36:47 +02:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2014-08-11 16:30:01 +02:00
|
|
|
#
|
2020-08-25 16:05:56 +02:00
|
|
|
# Calamares is Free Software: see the License-Identifier above.
|
2014-08-11 16:30:01 +02:00
|
|
|
#
|
|
|
|
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
import libcalamares
|
|
|
|
|
2019-04-19 17:02:03 +02:00
|
|
|
import gettext
|
|
|
|
_ = gettext.translation("calamares-python",
|
|
|
|
localedir=libcalamares.utils.gettext_path(),
|
|
|
|
languages=libcalamares.utils.gettext_languages(),
|
|
|
|
fallback=True).gettext
|
|
|
|
|
|
|
|
|
|
|
|
def pretty_name():
|
|
|
|
return _("Saving network configuration.")
|
|
|
|
|
2014-08-11 16:30:01 +02:00
|
|
|
|
2021-09-24 12:25:33 +02:00
|
|
|
def get_live_user():
|
2021-09-24 12:15:16 +02:00
|
|
|
"""
|
|
|
|
Gets the "live user" login. This might be "live", or "nitrux",
|
|
|
|
or something similar: it is the login name used *right now*,
|
|
|
|
and network configurations saved for that user, should be applied
|
|
|
|
also for the installed user (which probably has a different name).
|
|
|
|
"""
|
|
|
|
# getlogin() is a thin-wrapper, and depends on getlogin(3),
|
|
|
|
# which reads utmp -- and utmp isn't always set up right.
|
|
|
|
try:
|
|
|
|
return os.getlogin()
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
# getpass will return the **current** user, which is generally root.
|
|
|
|
# That isn't very useful, because the network settings have been
|
|
|
|
# made outside of Calamares-running-as-root, as a different user.
|
|
|
|
#
|
|
|
|
# If Calamares is running as non-root, though, this is fine.
|
|
|
|
import getpass
|
|
|
|
name = getpass.getuser()
|
|
|
|
if name != "root":
|
|
|
|
return name
|
|
|
|
|
|
|
|
# TODO: other mechanisms, e.g. guessing that "live" is the name
|
|
|
|
# TODO: support a what-is-the-live-user setting
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2021-09-24 12:25:33 +02:00
|
|
|
def replace_username(nm_config_filename, live_user, target_user):
|
|
|
|
"""
|
|
|
|
If @p live_user isn't None, then go through the given
|
|
|
|
file and replace @p live_user by the @p target_user.
|
|
|
|
|
|
|
|
Reads the file, then (re-)writes it with new permissions lives.
|
|
|
|
"""
|
|
|
|
# FIXME: Perhaps if live_user is None, we should just replace **all**
|
|
|
|
# permissions lines? After all, this is supposed to be a live
|
|
|
|
# system so **whatever** NM networks are configured, should be
|
|
|
|
# available to the new user.
|
|
|
|
if live_user is None:
|
|
|
|
return
|
|
|
|
if not os.path.exists(nm_config_filename):
|
|
|
|
return
|
|
|
|
|
|
|
|
with open(target_network, "r") as network_conf:
|
|
|
|
text = network_conf.readlines()
|
|
|
|
|
|
|
|
live_permissions = 'permissions=user:{}:;'.format(live_user)
|
|
|
|
target_permissions = 'permissions=user:{}:;\n'.format(user)
|
|
|
|
with open(target_network, "w") as network_conf:
|
|
|
|
for line in text:
|
|
|
|
if live_permissions in line:
|
|
|
|
line = target_permissions
|
|
|
|
network_conf.write(line)
|
|
|
|
|
|
|
|
|
2014-08-11 16:30:01 +02:00
|
|
|
def run():
|
2017-03-29 16:49:03 +02:00
|
|
|
"""
|
|
|
|
Setup network configuration
|
|
|
|
"""
|
2014-08-11 16:30:01 +02:00
|
|
|
root_mount_point = libcalamares.globalstorage.value("rootMountPoint")
|
2021-08-16 14:06:13 +02:00
|
|
|
user = libcalamares.globalstorage.value("username")
|
2021-09-24 12:25:33 +02:00
|
|
|
live_user = get_live_user()
|
2019-04-28 20:43:39 +02:00
|
|
|
|
|
|
|
if root_mount_point is None:
|
|
|
|
libcalamares.utils.warning("rootMountPoint is empty, {!s}".format(root_mount_point))
|
|
|
|
return (_("Configuration Error"),
|
|
|
|
_("No root mount point is given for <pre>{!s}</pre> to use." ).format("networkcfg"))
|
|
|
|
|
2014-08-11 16:30:01 +02:00
|
|
|
source_nm = "/etc/NetworkManager/system-connections/"
|
2017-03-29 16:49:03 +02:00
|
|
|
target_nm = os.path.join(
|
|
|
|
root_mount_point, "etc/NetworkManager/system-connections/"
|
|
|
|
)
|
2014-08-11 16:30:01 +02:00
|
|
|
|
|
|
|
# Sanity checks. We don't want to do anything if a network
|
|
|
|
# configuration already exists on the target
|
|
|
|
if os.path.exists(source_nm) and os.path.exists(target_nm):
|
|
|
|
for network in os.listdir(source_nm):
|
|
|
|
# Skip LTSP live
|
|
|
|
if network == "LTSP":
|
|
|
|
continue
|
|
|
|
|
|
|
|
source_network = os.path.join(source_nm, network)
|
|
|
|
target_network = os.path.join(target_nm, network)
|
|
|
|
|
|
|
|
if os.path.exists(target_network):
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
2021-05-12 16:20:13 +02:00
|
|
|
shutil.copy(source_network, target_network, follow_symlinks=False)
|
2021-09-24 12:25:33 +02:00
|
|
|
replace_username(target_network, live_user, user)
|
2014-08-11 16:30:01 +02:00
|
|
|
except FileNotFoundError:
|
2017-03-29 16:49:03 +02:00
|
|
|
libcalamares.utils.debug(
|
|
|
|
"Can't copy network configuration files in "
|
|
|
|
+ "{}".format(source_network)
|
|
|
|
)
|
2014-08-11 16:30:01 +02:00
|
|
|
except FileExistsError:
|
|
|
|
pass
|
|
|
|
|
2016-07-10 16:29:49 +02:00
|
|
|
# We need to overwrite the default resolv.conf in the chroot.
|
|
|
|
source_resolv = "/etc/resolv.conf"
|
|
|
|
target_resolv = os.path.join(root_mount_point, "etc/resolv.conf")
|
2016-08-02 13:25:00 +02:00
|
|
|
if source_resolv != target_resolv and os.path.exists(source_resolv):
|
2016-09-22 15:43:41 +02:00
|
|
|
try:
|
|
|
|
os.remove(target_resolv)
|
2017-01-24 15:29:55 +01:00
|
|
|
except Exception as err:
|
2017-03-29 16:49:03 +02:00
|
|
|
libcalamares.utils.debug(
|
|
|
|
"Couldn't remove {}: {}".format(target_resolv, err)
|
|
|
|
)
|
2017-01-15 18:11:16 +01:00
|
|
|
|
2016-07-10 16:29:49 +02:00
|
|
|
try:
|
2021-05-12 16:20:13 +02:00
|
|
|
shutil.copy(source_resolv, target_resolv, follow_symlinks=False)
|
2017-01-24 15:29:55 +01:00
|
|
|
except Exception as err:
|
2017-03-29 16:49:03 +02:00
|
|
|
libcalamares.utils.debug(
|
|
|
|
"Can't copy resolv.conf from {}: {}".format(source_resolv, err)
|
|
|
|
)
|
2016-07-10 16:29:49 +02:00
|
|
|
|
2014-08-11 16:30:01 +02:00
|
|
|
return None
|