2 # Copyright (c) 2014 Intel, Inc.
4 # This program is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License as published by the Free
6 # Software Foundation; version 2 of the License
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc., 59
15 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 from mic import msger, rt_util
22 from mic.conf import configmgr
23 from mic.plugin import pluginmgr
24 from mic.pluginbase import ImagerPlugin
25 from mic.imager.loop import LoopImageCreator
26 from mic.utils import errors, fs_related, runner
28 class QcowImageCreator(LoopImageCreator):
31 def __init__(self, creatoropts=None, pkgmgr=None):
32 LoopImageCreator.__init__(self, creatoropts, pkgmgr)
33 self.cmd_qemuimg = 'qemu-img'
35 def _stage_final_image(self):
37 self.cmd_qemuimg = fs_related.find_binary_path('qemu-img')
38 except errors.CreatorError:
39 return LoopImageCreator._stage_final_image(self)
44 for item in self._instloops:
45 if item['mountpoint'] == '/':
46 if item['fstype'] == "ext4":
47 runner.show('/sbin/tune2fs -O ^huge_file,extents,uninit_bg %s'
49 self.image_files.setdefault('partitions', {}).update(
50 {item['mountpoint']: item['label']})
51 imgfile = os.path.join(self._imgdir, item['name'])
54 qemuimage = imgfile + ".x86"
55 runner.show("%s convert -O qcow2 %s %s"
56 % (self.cmd_qemuimg, imgfile, qemuimage))
58 os.rename(qemuimage, imgfile)
60 for item in os.listdir(self._imgdir):
61 shutil.move(os.path.join(self._imgdir, item),
62 os.path.join(self._outdir, item))
64 class QcowPlugin(ImagerPlugin):
68 def do_create(cls, args):
69 """${cmd_name}: create qcow image
72 ${name} ${cmd_name} <ksfile> [OPTS]
77 raise errors.Usage("Invalid arguments")
79 creatoropts = configmgr.create
82 if creatoropts['runtime'] == "bootstrap":
83 configmgr._ksconf = ksconf
84 rt_util.bootstrap_mic()
87 if len(creatoropts['record_pkgs']) > 0:
88 recording_pkgs = creatoropts['record_pkgs']
90 if creatoropts['release'] is not None:
91 if 'name' not in recording_pkgs:
92 recording_pkgs.append('name')
93 if 'vcs' not in recording_pkgs:
94 recording_pkgs.append('vcs')
96 configmgr._ksconf = ksconf
98 # try to find the pkgmgr
100 backends = pluginmgr.get_plugins('backend')
101 if 'auto' == creatoropts['pkgmgr']:
102 for key in configmgr.prefer_backends:
104 pkgmgr = backends[key]
107 for key in backends.keys():
108 if key == creatoropts['pkgmgr']:
109 pkgmgr = backends[key]
113 raise errors.CreatorError("Can't find backend: %s, "
114 "available choices: %s" %
115 (creatoropts['pkgmgr'],
116 ','.join(backends.keys())))
118 creator = QcowImageCreator(creatoropts,
121 if len(recording_pkgs) > 0:
122 creator._recording_pkgs = recording_pkgs
124 image_names = [creator.name + ".img"]
125 image_names.extend(creator.get_image_names())
126 cls.check_image_exists(creator.destdir,
129 creatoropts['release'])
132 creator.check_depend_tools()
133 creator.mount(None, creatoropts["cachedir"])
135 creator.configure(creatoropts["repomd"])
136 creator.copy_kernel()
137 creator.create_cpio_image()
139 creator.copy_cpio_image()
140 creator.package(creatoropts["destdir"])
141 creator.create_manifest()
143 if creatoropts['release'] is not None:
144 creator.release_output(ksconf,
145 creatoropts['destdir'],
146 creatoropts['release'])
147 creator.print_outimage_info()
149 except errors.CreatorError:
154 #Run script of --run_script after image created
155 if creatoropts['run_script']:
156 cmd = creatoropts['run_script']
157 msger.info("Running command in parameter run_script: "+"".join(cmd))
159 p = subprocess.Popen(cmd, stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
162 msger.warning(str(err))
165 msger.info("Finished.")
169 def do_chroot(cls, target, cmd=[]):
173 def do_unpack(cls, srcimg):