[virt-tools-list] [virt-bootstrap] [PATCH v3 15/24] Enable UID/GID mapping for qcow2

Radostin Stoyanov rstoyanov1 at gmail.com
Wed Aug 2 12:08:29 UTC 2017


---
 src/virtBootstrap/sources/docker_source.py |  6 +++++-
 src/virtBootstrap/sources/file_source.py   |  6 +++++-
 src/virtBootstrap/utils.py                 | 32 ++++++++++++++++++++++++++++++
 src/virtBootstrap/virt_bootstrap.py        |  2 ++
 4 files changed, 44 insertions(+), 2 deletions(-)

diff --git a/src/virtBootstrap/sources/docker_source.py b/src/virtBootstrap/sources/docker_source.py
index 9d7c187..a9fa8d5 100644
--- a/src/virtBootstrap/sources/docker_source.py
+++ b/src/virtBootstrap/sources/docker_source.py
@@ -58,6 +58,8 @@ class DockerSource(object):
         self.url = self.gen_valid_uri(kwargs['uri'])
         self.username = kwargs.get('username', None)
         self.password = kwargs.get('password', None)
+        self.uid_map = kwargs.get('uid_map', None)
+        self.gid_map = kwargs.get('gid_map', None)
         self.output_format = kwargs.get('fmt', utils.DEFAULT_OUTPUT_FORMAT)
         self.insecure = kwargs.get('not_secure', False)
         self.no_cache = kwargs.get('no_cache', False)
@@ -270,7 +272,9 @@ class DockerSource(object):
                 utils.Build_QCOW2_Image(
                     tar_files=self.tar_files,
                     dest=dest,
-                    progress=self.progress
+                    progress=self.progress,
+                    uid_map=self.uid_map,
+                    gid_map=self.gid_map
                 )
             else:
                 raise Exception("Unknown format:" + self.output_format)
diff --git a/src/virtBootstrap/sources/file_source.py b/src/virtBootstrap/sources/file_source.py
index 760e50a..19026c5 100644
--- a/src/virtBootstrap/sources/file_source.py
+++ b/src/virtBootstrap/sources/file_source.py
@@ -45,6 +45,8 @@ class FileSource(object):
         """
         self.path = kwargs['uri'].path
         self.output_format = kwargs.get('fmt', utils.DEFAULT_OUTPUT_FORMAT)
+        self.uid_map = kwargs.get('uid_map', None)
+        self.gid_map = kwargs.get('gid_map', None)
         self.progress = kwargs['progress'].update_progress
 
     def unpack(self, dest):
@@ -68,7 +70,9 @@ class FileSource(object):
             utils.Build_QCOW2_Image(
                 tar_files=[self.path],
                 dest=dest,
-                progress=self.progress
+                progress=self.progress,
+                uid_map=self.uid_map,
+                gid_map=self.gid_map
             )
         else:
             raise Exception("Unknown format:" + self.output_format)
diff --git a/src/virtBootstrap/utils.py b/src/virtBootstrap/utils.py
index d554735..1a10785 100644
--- a/src/virtBootstrap/utils.py
+++ b/src/virtBootstrap/utils.py
@@ -29,6 +29,7 @@ import json
 import os
 import subprocess
 import sys
+import tarfile
 import tempfile
 import logging
 import re
@@ -68,6 +69,8 @@ class Build_QCOW2_Image(object):
                 'tar_files must be list not %s' % type(self.tar_files)
             )
         self.progress = kwargs['progress']
+        self.uid_map = kwargs.get('uid_map', None)
+        self.gid_map = kwargs.get('gid_map', None)
         self.fmt = 'qcow2'
         self.qcow2_files = [os.path.join(kwargs['dest'], 'layer-%s.qcow2' % i)
                             for i in range(len(self.tar_files))]
@@ -103,6 +106,14 @@ class Build_QCOW2_Image(object):
         # from tar file.
         self.g.tar_in(tar_file, '/', get_compression_type(tar_file),
                       xattrs=True, selinux=True, acls=True)
+
+        # UID/GID Mapping
+        if self.uid_map or self.gid_map:
+            tar_members = tarfile.open(tar_file).getmembers()
+            balance_uid_gid_maps(self.uid_map, self.gid_map)
+            for uid, gid in zip(self.uid_map, self.gid_map):
+                self.map_id(tar_members, uid, gid)
+
         # Shutdown guestfs instance to avoid hot-plugging of devices.
         self.g.umount('/')
 
@@ -141,6 +152,27 @@ class Build_QCOW2_Image(object):
                           logger=logger)
             self.tar_in(tar_file, devices[i])
 
+    def map_id(self, tar_members, map_uid, map_gid):
+        """
+        Remapping ownership of all files inside image.
+
+        map_gid and map_uid: Contain integers in a list with format:
+            [<start>, <target>, <count>]
+        """
+        if map_uid:
+            uid_opts = get_mapping_opts(map_uid)
+        if map_gid:
+            gid_opts = get_mapping_opts(map_gid)
+
+        for member in tar_members:
+            old_uid = member.uid
+            old_gid = member.gid
+
+            new_uid = get_map_id(old_uid, uid_opts) if map_uid else -1
+            new_gid = get_map_id(old_gid, gid_opts) if map_gid else -1
+            if new_uid != -1 or new_gid != -1:
+                self.g.lchown(new_uid, new_gid, os.path.join('/', member.name))
+
 
 def get_compression_type(tar_file):
     """
diff --git a/src/virtBootstrap/virt_bootstrap.py b/src/virtBootstrap/virt_bootstrap.py
index 0bc2e2b..99aca24 100755
--- a/src/virtBootstrap/virt_bootstrap.py
+++ b/src/virtBootstrap/virt_bootstrap.py
@@ -124,6 +124,8 @@ def bootstrap(uri, dest,
            fmt=fmt,
            username=username,
            password=password,
+           uid_map=uid_map,
+           gid_map=gid_map,
            not_secure=not_secure,
            no_cache=no_cache,
            progress=prog).unpack(dest)
-- 
2.13.3




More information about the virt-tools-list mailing list