Hi folks,
I am attaching changes that implements class ChrootCreator, allowing you
to install into a target chroot. chroot-creator is the frontend script
that uses this new class. ChrootCreator is similar to existing
uses of ImageCreator except the installed chroot is not destroyed after
it is done.
Any comments to improve this?
Warren Togami
[EMAIL PROTECTED]
diff --git a/imgcreate/__init__.py b/imgcreate/__init__.py
index e535014..3731912 100644
--- a/imgcreate/__init__.py
+++ b/imgcreate/__init__.py
@@ -25,7 +25,8 @@ from imgcreate.fs import *
"""A set of classes for building Fedora system images.
The following image creators are available:
- - ImageCreator - installs to a directory
+ - ImageCreator - installs to a temporary directory
+ - ChrootCreator - installs to a targeted chroot directory
- LoopImageCreator - installs to an ext3 image
- LiveImageCreator - installs to a bootable ISO
@@ -58,6 +59,7 @@ future.
__all__ = (
'CreatorError',
'ImageCreator',
+ 'ChrootCreator',
'LiveImageCreator',
'LoopImageCreator',
'FSLABEL_MAXLEN',
diff --git a/imgcreate/creator.py b/imgcreate/creator.py
index c2ed770..84e9c42 100644
--- a/imgcreate/creator.py
+++ b/imgcreate/creator.py
@@ -672,6 +672,45 @@ class ImageCreator(object):
self.unmount()
self.package()
+class ChrootCreator(ImageCreator):
+ """Installs a system to a target chroot directory.
+ """
+
+ def __init__(self, ks, name, target = None):
+ """Initialize a ChrootCreator instance.
+ This method takes the same arguments as ImageCreator.__init__() with
+ the addition of:
+
+ target -- The directory where the chroot is to be installed.
+ """
+ ImageCreator.__init__(self, ks, name)
+
+ # Target path must be defined
+ if target is None:
+ raise CreatorError("--target chroot path must be defined.")
+ # and it must not already exist
+ if os.path.exists(target):
+ raise CreatorError("--target directory %s already exists" % target)
+
+ self.target = target
+ self.__bindroot = None
+ self._fstype = "auto"
+
+ # --bind mount target chroot upon temp directory
+ def _mount_instroot(self, base_on = None):
+ self.__imgdir = self._mkdtemp()
+
+ makedirs(self.target)
+ # Cannot add to self.__bindmounts because this must be mounted
+ # before everything is created in _instroot
+ self.__bindroot = BindChrootMount(self.target, self._instroot, "/")
+ print("Mounting %s for chroot installation" % self.__bindroot.src)
+ self.__bindroot.mount()
+
+ def _unmount_instroot(self):
+ if not self.__bindroot is None:
+ self.__bindroot.unmount()
+
class LoopImageCreator(ImageCreator):
"""Installs a system into a loopback-mountable filesystem image.
diff --git a/tools/chroot-creator b/tools/chroot-creator
new file mode 100755
index 0000000..4f7b5a4
--- /dev/null
+++ b/tools/chroot-creator
@@ -0,0 +1,81 @@
+#!/usr/bin/python -tt
+#
+# chroot-creator: Install system defined by kickstart file into target chroot
+#
+# Copyright 2008, Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Library General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+import os
+import sys
+import shutil
+import optparse
+
+import imgcreate
+
+def parse_options(args):
+ parser = optparse.OptionParser(usage = "%prog [--name=<name>] --target=<target> <kickstart>")
+
+ parser.add_option("-n", "--name", type="string", dest="name",
+ help="Image name and filesystem label")
+ parser.add_option("-t", "--target", type="string", dest="target",
+ help="Target directory for chroot")
+
+ (options, args) = parser.parse_args()
+
+ if len(args) != 1:
+ parser.print_usage()
+ sys.exit(1)
+
+ return (args[0], options)
+
+def main():
+ (kscfg, options) = parse_options(sys.argv[1:])
+
+ if os.geteuid () != 0:
+ print >> sys.stderr, "You must run chroot-creator as root"
+ return 1
+
+ try:
+ ks = imgcreate.read_kickstart(kscfg)
+ except imgcreate.CreatorError, e:
+ print >> sys.stderr, "Error loading kickstart file '%s' : %s" % (kscfg, e)
+ return 1
+
+ if options.name:
+ name = options.name
+ else:
+ name = imgcreate.build_name(kscfg)
+
+ if options.target:
+ target = options.target
+
+ creator = imgcreate.ChrootCreator(ks, name, target)
+
+ try:
+ creator.mount()
+ creator.install()
+ creator.configure()
+ creator.unmount()
+
+ except imgcreate.CreatorError, e:
+ print >> sys.stderr, "Error creating image : %s" % e
+ return 1
+ finally:
+ creator.cleanup()
+
+ return 0
+
+if __name__ == "__main__":
+ sys.exit(main())
--
Fedora-livecd-list mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/fedora-livecd-list