only add the user to groups that exist in the target system

for https://bugs.launchpad.net/subiquity/+bug/1775228
("user is in debian-tor group on fresh install")
This commit is contained in:
Michael Hudson-Doyle 2018-06-14 15:27:06 +12:00
parent 2f184ce9a6
commit ad17c530b0
2 changed files with 18 additions and 2 deletions

View File

@ -20,6 +20,7 @@ import yaml
from subiquitycore.models.identity import IdentityModel
from subiquitycore.models.network import NetworkModel
from subiquitycore.utils import run_command
from .filesystem import FilesystemModel
from .installpath import InstallpathModel
@ -41,12 +42,24 @@ def setup_yaml():
setup_yaml()
def get_all_groups(dry_run):
command = ['chroot', '/target', 'getent', 'group']
if dry_run:
del command[:2]
cp = run_command(command, check=True)
groups = set()
for line in cp.stdout.splitlines():
groups.add(line.split(':')[0])
return groups
class SubiquityModel:
"""The overall model for subiquity."""
def __init__(self, common):
root = '/'
if common['opts'].dry_run:
self.opts = common['opts']
if self.opts.dry_run:
root = os.path.abspath(".subiquity")
self.locale = LocaleModel(common['signal'])
self.keyboard = KeyboardModel(root)
@ -61,13 +74,15 @@ class SubiquityModel:
def _cloud_init_config(self):
user = self.identity.user
users_and_groups_path = (
os.path.join(os.environ.get("SNAP", "/does-not-exist"),
os.path.join(os.environ.get("SNAP", "."),
"users-and-groups"))
if os.path.exists(users_and_groups_path):
groups = open(users_and_groups_path).read().split()
else:
groups = ['admin']
groups.append('sudo')
all_groups = get_all_groups(self.opts.dry_run)
groups = [group for group in groups if group in all_groups]
user_info = {
'name': user.username,
'gecos': user.realname,

1
users-and-groups Normal file
View File

@ -0,0 +1 @@
adm cdrom dip lpadmin plugdev sambashare debian-tor libvirtd lxd