[Fedora-livecd-list] [RFE PATCH] Add a tool for exposing the ImageCreator class

David Michael fedora.dm0 at gmail.com
Thu Oct 24 21:16:37 UTC 2013


---
Hi,

I've been looking into systemd-nspawn recently.  Rather than relying
on "yum --installroot" and manual post-install tweaking, I thought
kickstart files would be a better way to create these environments.
The following is a proof-of-concept patch that allows building a root
directory for a namespace container with livecd-creator.

Most of the functionality is already found in imgcreate.  I've made
two changes to creator.py.  First:  The fstype definition is moved up
the inheritance chain from LoopImageCreator to ImageCreator, since
ImageCreator requires it to write /etc/fstab.  Second:  A hacky
function __destroy_minimal_dev is added to prevent Python from copying
block device nodes as regular files during the final package() step.

In addition to the imgcreate changes, this adds the "nsroot" image
type to livecd-creator.  It simply instantiates ImageCreator instead
of LiveImageCreator or LoopImageCreator.

I'd appreciate if ImageCreator functionality could be exposed through
such a command.

Comments?  Suggestions for improvements or alternatives?

Thanks.

David


 Makefile             |  1 +
 imgcreate/creator.py | 14 ++++++++++++--
 tools/livecd-creator | 13 +++++++++++--
 3 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/Makefile b/Makefile
index 16fe3f1..ff77e0c 100644
--- a/Makefile
+++ b/Makefile
@@ -23,6 +23,7 @@ man:
 install: man
     $(INSTALL_PROGRAM) -D tools/livecd-creator
$(DESTDIR)/usr/bin/livecd-creator
     ln -s ./livecd-creator $(DESTDIR)/usr/bin/image-creator
+    ln -s ./livecd-creator $(DESTDIR)/usr/bin/nsroot-creator
     $(INSTALL_PROGRAM) -D tools/liveimage-mount
$(DESTDIR)/usr/bin/liveimage-mount
     $(INSTALL_PROGRAM) -D tools/livecd-iso-to-disk.sh
$(DESTDIR)/usr/bin/livecd-iso-to-disk
     $(INSTALL_PROGRAM) -D tools/livecd-iso-to-pxeboot.sh
$(DESTDIR)/usr/bin/livecd-iso-to-pxeboot
diff --git a/imgcreate/creator.py b/imgcreate/creator.py
index 42faf6f..3d5a016 100644
--- a/imgcreate/creator.py
+++ b/imgcreate/creator.py
@@ -99,6 +99,8 @@ class ImageCreator(object):
                     self.__selinux_mountpoint = fields[4]
                     break

+        self.__fstype = kickstart.get_image_fstype(self.ks, "ext3")
+
     def __del__(self):
         self.cleanup()

@@ -232,7 +234,7 @@ class ImageCreator(object):
         A sensible default implementation is provided.

         """
-        s =  "/dev/root  /         %s    defaults,noatime 0 0\n"
%(self._fstype)
+        s =  "/dev/root  /         %s    defaults,noatime 0 0\n"
%(self.__fstype)
         s += self._get_fstab_special()
         return s

@@ -451,6 +453,13 @@ class ImageCreator(object):
                 os.symlink(src, self._instroot + dest)
         os.umask(origumask)

+    def __destroy_minimal_dev(self):
+        """Destroy the minimal /dev so that Python doesn't copy /dev/zero as a
+        regular file.  On the running system, /dev is mounted tmpfs anyway."""
+        for f in ['null', 'urandom', 'random', 'full', 'ptmx', 'tty',
'zero', 'fd', 'stdin', 'stdout', 'stderr']:
+            if os.path.exists(self._instroot + "/dev/" + f):
+                os.unlink(self._instroot + "/dev/" + f)
+
     def __create_selinuxfs(self):
         if not os.path.exists(self.__selinux_mountpoint):
             return
@@ -538,6 +547,8 @@ class ImageCreator(object):
         from the install root.

         """
+        self.__destroy_minimal_dev()
+
         self.__destroy_selinuxfs()

         self._undo_bindmounts()
@@ -802,7 +813,6 @@ class LoopImageCreator(ImageCreator):

         self.__minsize_KB = 0
         self.__blocksize = 4096
-        self.__fstype = kickstart.get_image_fstype(self.ks, "ext3")

         self.__instloop = None
         self.__imgdir = None
diff --git a/tools/livecd-creator b/tools/livecd-creator
index a39e43f..2236885 100755
--- a/tools/livecd-creator
+++ b/tools/livecd-creator
@@ -103,10 +103,12 @@ def parse_options(args):
     if not options.image_type:
         if sys.argv[0].endswith('image-creator'):
             options.image_type = 'image'
+        elif sys.argv[0].endswith('nsroot-creator'):
+            options.image_type = 'nsroot'
         else:
             options.image_type = 'livecd'
-    if options.image_type not in ('livecd', 'image'):
-        raise Usage("'%s' is a recognized image type" % options.image_type)
+    if options.image_type not in ('livecd', 'image', 'nsroot'):
+        raise Usage("'%s' is not a recognized image type" % options.image_type)

     # image-create compatibility: Last argument is kickstart file
     if len(args) == 1:
@@ -200,6 +202,13 @@ def main():
                                         tmpdir=os.path.abspath(options.tmpdir),
                                         cacheonly=options.cacheonly,
                                         docleanup=not options.nocleanup)
+    elif options.image_type == 'nsroot':
+        creator = imgcreate.ImageCreator(ks, name,
+                                        releasever=options.releasever,
+                                        useplugins=options.plugins,
+                                        tmpdir=os.path.abspath(options.tmpdir),
+                                        cacheonly=options.cacheonly,
+                                        docleanup=not options.nocleanup)
     else:
         # Cannot happen, we validate this when parsing options.
         logging.error(u"'%s' is not a valid image type" % options.image_type)
-- 
1.8.3.1


More information about the livecd mailing list