[master 4/5] Allow users to be created with an existing GID.

dashea installerbot-noreply at redhat.com
Wed Sep 30 20:20:13 UTC 2015


From: David Shea <dshea at redhat.com>

---
 pyanaconda/users.py | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)

diff --git a/pyanaconda/users.py b/pyanaconda/users.py
index 49820e20..4fc38d1 100644
--- a/pyanaconda/users.py
+++ b/pyanaconda/users.py
@@ -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.
@@ -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')


-- 
To view this commit on github, visit https://github.com/rhinstaller/anaconda/commit/88446207d1d374ff73df558d6081d967248879d5


More information about the anaconda-patches mailing list