Another method of install tpk.
[tools/mic.git] / plugins / imager / loop_plugin.py
1 #!/usr/bin/python -tt
2 #
3 # Copyright (c) 2011 Intel, Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation; version 2 of the License
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 # for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc., 59
16 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18 import os
19 import subprocess
20 import shutil
21 import tempfile
22
23 from mic import chroot, msger, rt_util
24 from mic.utils import misc, fs_related, errors, runner
25 from mic.conf import configmgr
26 from mic.plugin import pluginmgr
27 from mic.imager.loop import LoopImageCreator, load_mountpoints
28
29 from mic.pluginbase import ImagerPlugin
30 class LoopPlugin(ImagerPlugin):
31     name = 'loop'
32
33     @classmethod
34     def do_create(self, args):
35         """${cmd_name}: create loop image
36
37         Usage:
38             ${name} ${cmd_name} <ksfile> [OPTS]
39
40         ${cmd_option_list}
41         """
42
43         if args is None:
44             raise errors.Usage("Invalid arguments")
45
46         creatoropts = configmgr.create
47         ksconf = args.ksfile
48
49         if creatoropts['runtime'] == "bootstrap":
50             configmgr._ksconf = ksconf
51             rt_util.bootstrap_mic()
52
53         recording_pkgs = []
54         if len(creatoropts['record_pkgs']) > 0:
55             recording_pkgs = creatoropts['record_pkgs']
56
57         if creatoropts['release'] is not None:
58             if 'name' not in recording_pkgs:
59                 recording_pkgs.append('name')
60             if 'vcs' not in recording_pkgs:
61                 recording_pkgs.append('vcs')
62
63         configmgr._ksconf = ksconf
64
65         # try to find the pkgmgr
66         pkgmgr = None
67         backends = pluginmgr.get_plugins('backend')
68         if 'auto' == creatoropts['pkgmgr']:
69             for key in configmgr.prefer_backends:
70                 if key in backends:
71                     pkgmgr = backends[key]
72                     break
73         else:
74             for key in backends.keys():
75                 if key == creatoropts['pkgmgr']:
76                     pkgmgr = backends[key]
77                     break
78
79         if not pkgmgr:
80             raise errors.CreatorError("Can't find backend: %s, "
81                                       "available choices: %s" %
82                                       (creatoropts['pkgmgr'],
83                                        ','.join(backends.keys())))
84
85         creator = LoopImageCreator(creatoropts,
86                                    pkgmgr,
87                                    args.compress_image,
88                                    args.shrink)
89
90         if len(recording_pkgs) > 0:
91             creator._recording_pkgs = recording_pkgs
92
93         image_names = [creator.name + ".img"]
94         image_names.extend(creator.get_image_names())
95         self.check_image_exists(creator.destdir,
96                                 creator.pack_to,
97                                 image_names,
98                                 creatoropts['release'])
99
100         try:
101             creator.check_depend_tools()
102             creator.mount(None, creatoropts["cachedir"])
103             creator.install()
104             creator.tpkinstall()
105             creator.configure(creatoropts["repomd"])
106             creator.copy_kernel()
107             creator.create_cpio_image()
108             creator.unmount()
109             creator.copy_cpio_image()
110             creator.package(creatoropts["destdir"])
111             creator.create_manifest()
112
113             if creatoropts['release'] is not None:
114                 creator.release_output(ksconf,
115                                        creatoropts['destdir'],
116                                        creatoropts['release'])
117             creator.print_outimage_info()
118
119         except errors.CreatorError:
120             raise
121         finally:
122             creator.cleanup()
123
124         #Run script of --run_script after image created
125         if creatoropts['run_script']:
126             cmd = creatoropts['run_script']
127             try:
128                 runner.show(cmd)
129             except OSError,err:
130                 msger.warning(str(err))
131
132         msger.info("Finished.")
133         return 0
134
135     @classmethod
136     def _do_chroot_tar(cls, target, cmd=[]):
137         mountfp_xml = os.path.splitext(target)[0] + '.xml'
138         if not os.path.exists(mountfp_xml):
139             raise errors.CreatorError("No mount point file found for this tar "
140                                       "image, please check %s" % mountfp_xml)
141
142         import tarfile
143         tar = tarfile.open(target, 'r')
144         tmpdir = misc.mkdtemp()
145         tar.extractall(path=tmpdir)
146         tar.close()
147
148         mntdir = misc.mkdtemp()
149
150         loops = []
151         for (mp, label, name, size, fstype) in load_mountpoints(mountfp_xml):
152             if fstype in ("ext2", "ext3", "ext4"):
153                 myDiskMount = fs_related.ExtDiskMount
154             elif fstype == "btrfs":
155                 myDiskMount = fs_related.BtrfsDiskMount
156             elif fstype in ("vfat", "msdos"):
157                 myDiskMount = fs_related.VfatDiskMount
158             else:
159                 raise errors.CreatorError("Cannot support fstype: %s" % fstype)
160
161             name = os.path.join(tmpdir, name)
162             size = size * 1024L * 1024L
163             loop = myDiskMount(fs_related.SparseLoopbackDisk(name, size),
164                                os.path.join(mntdir, mp.lstrip('/')),
165                                fstype, size, label)
166
167             try:
168                 msger.verbose("Mount %s to %s" % (mp, mntdir + mp))
169                 fs_related.makedirs(os.path.join(mntdir, mp.lstrip('/')))
170                 loop.mount()
171
172             except:
173                 loop.cleanup()
174                 for lp in reversed(loops):
175                     chroot.cleanup_after_chroot("img", lp, None, mntdir)
176
177                 shutil.rmtree(tmpdir, ignore_errors=True)
178                 raise
179
180             loops.append(loop)
181
182         try:
183             if len(cmd) != 0:
184                 cmdline = "/usr/bin/env HOME=/root " + ' '.join(cmd)
185             else:
186                 cmdline = "/usr/bin/env HOME=/root /bin/bash"
187             chroot.chroot(mntdir, None, cmdline)
188         except:
189             raise errors.CreatorError("Failed to chroot to %s." % target)
190         finally:
191             for loop in reversed(loops):
192                 chroot.cleanup_after_chroot("img", loop, None, mntdir)
193
194             shutil.rmtree(tmpdir, ignore_errors=True)
195
196     @classmethod
197     def do_chroot(cls, target, cmd=[]):
198         if target.endswith('.tar'):
199             import tarfile
200             if tarfile.is_tarfile(target):
201                 LoopPlugin._do_chroot_tar(target, cmd)
202                 return
203             else:
204                 raise errors.CreatorError("damaged tarball for loop images")
205
206         img = target
207         imgsize = misc.get_file_size(img) * 1024L * 1024L
208         imgtype = misc.get_image_type(img)
209         if imgtype == "btrfsimg":
210             fstype = "btrfs"
211             myDiskMount = fs_related.BtrfsDiskMount
212         elif imgtype in ("ext3fsimg", "ext4fsimg"):
213             fstype = imgtype[:4]
214             myDiskMount = fs_related.ExtDiskMount
215         else:
216             raise errors.CreatorError("Unsupported filesystem type: %s" \
217                                       % imgtype)
218
219         extmnt = misc.mkdtemp()
220         extloop = myDiskMount(fs_related.SparseLoopbackDisk(img, imgsize),
221                                                          extmnt,
222                                                          fstype,
223                                                          4096,
224                                                          "%s label" % fstype)
225         try:
226             extloop.mount()
227
228         except errors.MountError:
229             extloop.cleanup()
230             shutil.rmtree(extmnt, ignore_errors=True)
231             raise
232
233         try:
234             if cmd is not None:
235                 cmdline = cmd
236             else:
237                 cmdline = "/bin/bash"
238             envcmd = fs_related.find_binary_inchroot("env", extmnt)
239             if envcmd:
240                 cmdline = "%s HOME=/root %s" % (envcmd, cmdline)
241             chroot.chroot(extmnt, None, cmdline)
242         except:
243             raise errors.CreatorError("Failed to chroot to %s." % img)
244         finally:
245             chroot.cleanup_after_chroot("img", extloop, None, extmnt)
246
247     @classmethod
248     def do_unpack(cls, srcimg):
249         image = os.path.join(tempfile.mkdtemp(dir="/var/tmp", prefix="tmp"),
250                              "target.img")
251         msger.info("Copying file system ...")
252         shutil.copyfile(srcimg, image)
253         return image