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.
20 from mic import msger, rt_util
21 from mic.conf import configmgr
22 from mic.plugin import pluginmgr
23 from mic.pluginbase import ImagerPlugin
24 from mic.imager.loop import LoopImageCreator
25 from mic.utils import errors, fs_related, runner
27 class QcowImageCreator(LoopImageCreator):
30 def __init__(self, creatoropts=None, pkgmgr=None):
31 LoopImageCreator.__init__(self, creatoropts, pkgmgr)
32 self.cmd_qemuimg = 'qemu-img'
34 def _stage_final_image(self):
36 self.cmd_qemuimg = fs_related.find_binary_path('qemu-img')
37 except errors.CreatorError:
38 return LoopImageCreator._stage_final_image(self)
43 for item in self._instloops:
44 if item['mountpoint'] == '/':
45 if item['fstype'] == "ext4":
46 runner.show('/sbin/tune2fs -O ^huge_file,extents,uninit_bg %s'
48 self.image_files.setdefault('partitions', {}).update(
49 {item['mountpoint']: item['label']})
50 imgfile = os.path.join(self._imgdir, item['name'])
53 qemuimage = imgfile + ".x86"
54 runner.show("%s convert -O qcow2 %s %s"
55 % (self.cmd_qemuimg, imgfile, qemuimage))
57 os.rename(qemuimage, imgfile)
59 for item in os.listdir(self._imgdir):
60 shutil.move(os.path.join(self._imgdir, item),
61 os.path.join(self._outdir, item))
63 class QcowPlugin(ImagerPlugin):
67 def do_create(cls, args):
68 """${cmd_name}: create qcow image
71 ${name} ${cmd_name} <ksfile> [OPTS]
76 raise errors.Usage("Invalid arguments")
78 creatoropts = configmgr.create
81 if creatoropts['runtime'] == "bootstrap":
82 configmgr._ksconf = ksconf
83 rt_util.bootstrap_mic()
86 if len(creatoropts['record_pkgs']) > 0:
87 recording_pkgs = creatoropts['record_pkgs']
89 if creatoropts['release'] is not None:
90 if 'name' not in recording_pkgs:
91 recording_pkgs.append('name')
92 if 'vcs' not in recording_pkgs:
93 recording_pkgs.append('vcs')
95 configmgr._ksconf = ksconf
97 # try to find the pkgmgr
99 backends = pluginmgr.get_plugins('backend')
100 if 'auto' == creatoropts['pkgmgr']:
101 for key in configmgr.prefer_backends:
103 pkgmgr = backends[key]
106 for key in backends.keys():
107 if key == creatoropts['pkgmgr']:
108 pkgmgr = backends[key]
112 raise errors.CreatorError("Can't find backend: %s, "
113 "available choices: %s" %
114 (creatoropts['pkgmgr'],
115 ','.join(backends.keys())))
117 creator = QcowImageCreator(creatoropts,
120 if len(recording_pkgs) > 0:
121 creator._recording_pkgs = recording_pkgs
123 image_names = [creator.name + ".img"]
124 image_names.extend(creator.get_image_names())
125 cls.check_image_exists(creator.destdir,
128 creatoropts['release'])
131 creator.check_depend_tools()
132 creator.mount(None, creatoropts["cachedir"])
134 creator.configure(creatoropts["repomd"])
135 creator.copy_kernel()
136 creator.copy_cpio_resource()
138 creator.create_cpio_image()
139 creator.package(creatoropts["destdir"])
140 creator.create_manifest()
142 if creatoropts['release'] is not None:
143 creator.release_output(ksconf,
144 creatoropts['destdir'],
145 creatoropts['release'])
146 creator.print_outimage_info()
148 except errors.CreatorError:
153 msger.info("Finished.")
157 def do_chroot(cls, target, cmd=[]):
161 def do_unpack(cls, srcimg):