Skip to content

Commit

Permalink
Allow users to be created with an existing GID.
Browse files Browse the repository at this point in the history
  • Loading branch information
dashea committed Sep 30, 2015
1 parent e6a7223 commit 8844620
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions pyanaconda/users.py
Expand Up @@ -181,6 +181,22 @@ def _getgrnam(self, group_name, root):

return None

def _getgrgid(self, gid, root):
"""Like grp.getgrgid, but able to use a different root.
Just returns the fields as a list of strings.
"""
# Conver the probably-int GID to a string
gid = str(gid)

with open(root + "/etc/group", "r") as f:
for line in f:
fields = line.split(":")
if fields[2] == gid:
return fields

return None

@contextmanager
def _ensureLoginDefs(self, root):
"""Runs a command after creating /etc/login.defs, if necessary.
Expand Down Expand Up @@ -269,10 +285,12 @@ def createUser(self, user_name, *args, **kwargs):

args = ["-R", root]

# If a specific gid is requested, create the user group with that GID.
# Otherwise let useradd do it automatically.
# If a specific gid is requested, and that gid does not exist,
# create the user group with that GID.
if kwargs.get("gid", None):
self.createGroup(user_name, gid=kwargs['gid'], root=root)
if not self._getgrgid(kwargs['gid'], root):
self.createGroup(user_name, gid=kwargs['gid'], root=root)

args.extend(['-g', str(kwargs['gid'])])
else:
args.append('-U')
Expand Down

0 comments on commit 8844620

Please sign in to comment.