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, subcmd, opts, *args):
68 """${cmd_name}: create qcow image
71 ${name} ${cmd_name} <ksfile> [OPTS]
76 raise errors.Usage("Extra arguments given")
78 creatoropts = configmgr.create
81 if creatoropts['runtime'] == "bootstrap":
82 configmgr._ksconf = ksconf
83 rt_util.bootstrap_mic()
84 elif not rt_util.inbootstrap():
86 fs_related.find_binary_path('mic-native')
87 except errors.CreatorError:
88 if not msger.ask("Subpackage \"mic-native\" has not been "
89 "installed in your host system, still "
90 "continue with \"native\" running mode?",
92 raise errors.Abort("Abort because subpackage 'mic-native' "
93 "has not been installed")
96 if len(creatoropts['record_pkgs']) > 0:
97 recording_pkgs = creatoropts['record_pkgs']
99 if creatoropts['release'] is not None:
100 if 'name' not in recording_pkgs:
101 recording_pkgs.append('name')
102 if 'vcs' not in recording_pkgs:
103 recording_pkgs.append('vcs')
105 configmgr._ksconf = ksconf
107 # try to find the pkgmgr
109 backends = pluginmgr.get_plugins('backend')
110 if 'auto' == creatoropts['pkgmgr']:
111 for key in configmgr.prefer_backends:
113 pkgmgr = backends[key]
116 for key in backends.keys():
117 if key == creatoropts['pkgmgr']:
118 pkgmgr = backends[key]
122 raise errors.CreatorError("Can't find backend: %s, "
123 "available choices: %s" %
124 (creatoropts['pkgmgr'],
125 ','.join(backends.keys())))
127 creator = QcowImageCreator(creatoropts,
130 if len(recording_pkgs) > 0:
131 creator._recording_pkgs = recording_pkgs
133 image_names = [creator.name + ".img"]
134 image_names.extend(creator.get_image_names())
135 cls.check_image_exists(creator.destdir,
138 creatoropts['release'])
141 creator.check_depend_tools()
142 creator.mount(None, creatoropts["cachedir"])
144 creator.configure(creatoropts["repomd"])
145 creator.copy_kernel()
147 creator.package(creatoropts["destdir"])
149 if creatoropts['release'] is not None:
150 creator.release_output(ksconf,
151 creatoropts['destdir'],
152 creatoropts['release'])
153 creator.print_outimage_info()
155 except errors.CreatorError:
160 msger.info("Finished.")
164 def do_chroot(cls, target, cmd=[]):
168 def do_unpack(cls, srcimg):