From 2ce0d14dd4a06d4e487bf1d2505de9e94f70c994 Mon Sep 17 00:00:00 2001 From: "xiaojuan.mao" Date: Fri, 7 Apr 2017 15:08:40 +0800 Subject: [PATCH] Use cpio gzip in mic. Change-Id: Ifce99807698d3a35fd3dddcdf8f9d60f4c2bb22c --- mic/cmd_create.py | 2 ++ mic/conf.py | 1 + mic/imager/baseimager.py | 24 ++++++++++++++++++++++++ mic/kickstart/__init__.py | 10 +++++----- plugins/imager/fs_plugin.py | 2 ++ plugins/imager/loop_plugin.py | 2 ++ plugins/imager/qcow_plugin.py | 2 ++ plugins/imager/raw_plugin.py | 2 ++ tools/mic | 11 +++++++---- 9 files changed, 47 insertions(+), 9 deletions(-) mode change 100644 => 100755 mic/conf.py mode change 100644 => 100755 plugins/imager/fs_plugin.py mode change 100644 => 100755 plugins/imager/loop_plugin.py mode change 100644 => 100755 plugins/imager/qcow_plugin.py diff --git a/mic/cmd_create.py b/mic/cmd_create.py index 54fdbde..729100b 100755 --- a/mic/cmd_create.py +++ b/mic/cmd_create.py @@ -146,6 +146,8 @@ def main(parser, args, argv): if args.pack_to is not None: configmgr.create['pack_to'] = args.pack_to + if args.cpio: + configmgr.create['cpio'] = args.cpio if args.copy_kernel: configmgr.create['copy_kernel'] = args.copy_kernel diff --git a/mic/conf.py b/mic/conf.py old mode 100644 new mode 100755 index d844c65..45ea0c3 --- a/mic/conf.py +++ b/mic/conf.py @@ -75,6 +75,7 @@ class ConfigMgr(object): "extrarepos": {}, "ignore_ksrepo": False, "strict_mode": False, + "cpio":False, }, 'chroot': { "saveto": None, diff --git a/mic/imager/baseimager.py b/mic/imager/baseimager.py index 5982dcc..a1e80fb 100755 --- a/mic/imager/baseimager.py +++ b/mic/imager/baseimager.py @@ -87,6 +87,7 @@ class BaseImageCreator(object): self.pack_to = None self.repourl = {} self.multiple_partitions = False + self.cpio = False # If the kernel is save to the destdir when copy_kernel cmd is called. self._need_copy_kernel = False @@ -1250,6 +1251,29 @@ class BaseImageCreator(object): pass self._instloops.remove(item) + def create_cpio_image(self): + if self.cpio: + cpiomountdir = self._instroot + '/mnt/initrd' + if os.path.exists(cpiomountdir): + msger.info("Create image by cpio.") + imgfile = os.path.join(self._imgdir, 'ramdisk.img') + if imgfile: + os.remove(imgfile) + try: + cpiocmd = fs.find_binary_path('cpio') + if cpiocmd: + oldoutdir = os.getcwd() + os.chdir(cpiomountdir) + # find . | cpio --create --'format=newc' | gzip > ../ramdisk.img + runner.show('find . | cpio -o -H newc | gzip > %s' % imgfile) + shutil.rmtree(cpiomountdir, ignore_errors=True) + fs.makedirs(cpiomountdir) + os.chdir(oldoutdir) + except OSError, (errno, msg): + raise errors.KsError("Create image by cpio error: %s" % msg) + else: + msger.warning("Do not create image by cpio. There is no directory '/mnt/initrd'.") + def package(self, destdir = "."): """Prepares the created image for final delivery. diff --git a/mic/kickstart/__init__.py b/mic/kickstart/__init__.py index fb0e4d8..8ec653c 100755 --- a/mic/kickstart/__init__.py +++ b/mic/kickstart/__init__.py @@ -203,12 +203,12 @@ class TimezoneConfig(KickstartConfig): tz_source = "/usr/share/zoneinfo/%s" % (tz) tz_dest = "/etc/localtime" try: - cpcmd = fs.find_binary_inchroot('ln', self.instroot) - if cpcmd: - self.call([cpcmd, "-s", tz_source, tz_dest]) + lncmd = fs.find_binary_inchroot('ln', self.instroot) + if lncmd: + self.call([lncmd, "-s", tz_source, tz_dest]) else: - cpcmd = fs.find_binary_path('ln') - subprocess.call([cpcmd, "-s", + lncmd = fs.find_binary_path('ln') + subprocess.call([lncmd, "-s", self.path(tz_source), self.path(tz_dest)]) except (IOError, OSError), (errno, msg): diff --git a/plugins/imager/fs_plugin.py b/plugins/imager/fs_plugin.py old mode 100644 new mode 100755 index d74530f..b573127 --- a/plugins/imager/fs_plugin.py +++ b/plugins/imager/fs_plugin.py @@ -103,6 +103,8 @@ class FsPlugin(ImagerPlugin): creator.configure(creatoropts["repomd"]) creator.copy_kernel() + if creatoropts['cpio']: + creator.create_cpio_image() creator.unmount() creator.package(creatoropts["destdir"]) creator.create_manifest() diff --git a/plugins/imager/loop_plugin.py b/plugins/imager/loop_plugin.py old mode 100644 new mode 100755 index 1830230..50aab52 --- a/plugins/imager/loop_plugin.py +++ b/plugins/imager/loop_plugin.py @@ -102,6 +102,8 @@ class LoopPlugin(ImagerPlugin): creator.install() creator.configure(creatoropts["repomd"]) creator.copy_kernel() + if creatoropts['cpio']: + creator.create_cpio_image() creator.unmount() creator.package(creatoropts["destdir"]) creator.create_manifest() diff --git a/plugins/imager/qcow_plugin.py b/plugins/imager/qcow_plugin.py old mode 100644 new mode 100755 index 8acf572..cfbbd66 --- a/plugins/imager/qcow_plugin.py +++ b/plugins/imager/qcow_plugin.py @@ -133,6 +133,8 @@ class QcowPlugin(ImagerPlugin): creator.install() creator.configure(creatoropts["repomd"]) creator.copy_kernel() + if creatoropts['cpio']: + creator.create_cpio_image() creator.unmount() creator.package(creatoropts["destdir"]) creator.create_manifest() diff --git a/plugins/imager/raw_plugin.py b/plugins/imager/raw_plugin.py index 09a9714..0df535c 100755 --- a/plugins/imager/raw_plugin.py +++ b/plugins/imager/raw_plugin.py @@ -100,6 +100,8 @@ class RawPlugin(ImagerPlugin): creator.install() creator.configure(creatoropts["repomd"]) creator.copy_kernel() + if creatoropts['cpio']: + creator.create_cpio_image() creator.unmount() creator.generate_bmap() creator.package(creatoropts["destdir"]) diff --git a/tools/mic b/tools/mic index 26df153..44df61e 100755 --- a/tools/mic +++ b/tools/mic @@ -54,7 +54,7 @@ def chroot_parser(parser): help = "command which will be executed in chroot environment") parser.set_defaults(alias="ch") return parser - + @subparser def create_parser(parser): """create an image @@ -64,15 +64,18 @@ def create_parser(parser): """ parent_parser = ArgumentParser(add_help=False) - parent_parser.add_argument('ksfile', help='Path of ksfile'); + parent_parser.add_argument('--cpio', action='store_true', + dest='cpio', default=False, + help='Use cpio to generate image') + parent_parser.add_argument('ksfile', help='Path of ksfile') parent_parser.add_argument('--logfile', dest='logfile', default=None, help='Path of logfile') parent_parser.add_argument('-c', '--config', dest='config', default=None, - help='Specify config file for mic') + help='Specify config file for mic') parent_parser.add_argument('-k', '--cachedir', action='store', dest='cachedir', default=None, help='Cache directory to store the downloaded') - parent_parser.add_argument('-o', '--outdir', action='store', dest='outdir', + parent_parser.add_argument('-o', '--outdir', action='store', dest='outdir', default=None, help='Output directory') parent_parser.add_argument('-A', '--arch', dest='arch', default=None, help='Specify repo architecture') -- 2.7.4