Modify the order of generating image by cpio.
[platform/upstream/mic.git] / plugins / imager / qcow_plugin.py
1 #
2 # Copyright (c) 2014 Intel, Inc.
3 #
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
7 #
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
11 # for more details.
12 #
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.
16
17 import os
18 import shutil
19
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
26
27 class QcowImageCreator(LoopImageCreator):
28     img_format = 'qcow'
29
30     def __init__(self, creatoropts=None, pkgmgr=None):
31         LoopImageCreator.__init__(self, creatoropts, pkgmgr) 
32         self.cmd_qemuimg = 'qemu-img'
33
34     def _stage_final_image(self):
35         try:
36             self.cmd_qemuimg = fs_related.find_binary_path('qemu-img')
37         except errors.CreatorError:
38             return LoopImageCreator._stage_final_image(self)
39
40         self._resparse()
41
42         imgfile = None
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'
47                                 % imgfile)
48                 self.image_files.setdefault('partitions', {}).update(
49                          {item['mountpoint']: item['label']})
50                 imgfile = os.path.join(self._imgdir, item['name'])
51
52         if imgfile:
53             qemuimage = imgfile + ".x86"
54             runner.show("%s convert -O qcow2 %s %s"
55                         % (self.cmd_qemuimg, imgfile, qemuimage))
56             os.unlink(imgfile)
57             os.rename(qemuimage, imgfile)
58
59         for item in os.listdir(self._imgdir):
60             shutil.move(os.path.join(self._imgdir, item),
61                         os.path.join(self._outdir, item))
62
63 class QcowPlugin(ImagerPlugin):
64     name = 'qcow'
65
66     @classmethod
67     def do_create(cls, args):
68         """${cmd_name}: create qcow image
69
70         Usage:
71             ${name} ${cmd_name} <ksfile> [OPTS]
72
73         ${cmd_option_list}
74         """
75         if args is None:
76             raise errors.Usage("Invalid arguments")
77
78         creatoropts = configmgr.create
79         ksconf = args.ksfile
80
81         if creatoropts['runtime'] == "bootstrap":
82             configmgr._ksconf = ksconf
83             rt_util.bootstrap_mic()
84
85         recording_pkgs = []
86         if len(creatoropts['record_pkgs']) > 0:
87             recording_pkgs = creatoropts['record_pkgs']
88
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')
94
95         configmgr._ksconf = ksconf
96
97         # try to find the pkgmgr
98         pkgmgr = None
99         backends = pluginmgr.get_plugins('backend')
100         if 'auto' == creatoropts['pkgmgr']:
101             for key in configmgr.prefer_backends:
102                 if key in backends:
103                     pkgmgr = backends[key]
104                     break
105         else:
106             for key in backends.keys():
107                 if key == creatoropts['pkgmgr']:
108                     pkgmgr = backends[key]
109                     break
110
111         if not pkgmgr:
112             raise errors.CreatorError("Can't find backend: %s, "
113                                        "available choices: %s" %
114                                       (creatoropts['pkgmgr'],
115                                        ','.join(backends.keys())))
116
117         creator = QcowImageCreator(creatoropts,
118                                    pkgmgr)
119
120         if len(recording_pkgs) > 0:
121             creator._recording_pkgs = recording_pkgs
122
123         image_names = [creator.name + ".img"]
124         image_names.extend(creator.get_image_names())
125         cls.check_image_exists(creator.destdir,
126                                 creator.pack_to,
127                                 image_names,
128                                 creatoropts['release'])
129
130         try:
131             creator.check_depend_tools()
132             creator.mount(None, creatoropts["cachedir"])
133             creator.install()
134             creator.configure(creatoropts["repomd"])
135             creator.copy_kernel()
136             creator.create_cpio_image()
137             creator.unmount()
138             creator.copy_cpio_image()
139             creator.package(creatoropts["destdir"])
140             creator.create_manifest()
141
142             if creatoropts['release'] is not None:
143                 creator.release_output(ksconf,
144                                        creatoropts['destdir'],
145                                        creatoropts['release'])
146             creator.print_outimage_info()
147
148         except errors.CreatorError:
149             raise
150         finally:
151             creator.cleanup()
152
153         msger.info("Finished.")
154         return 0
155
156     @classmethod
157     def do_chroot(cls, target, cmd=[]):
158         pass
159
160     @classmethod
161     def do_unpack(cls, srcimg):
162         pass