8dc7285f31d555060b1a089c355fbe6c10819f61
[platform/upstream/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 shutil
20 import tempfile
21
22 from mic import configmgr, pluginmgr, chroot, msger
23 from mic.utils import misc, fs_related, errors
24 import mic.imager.loop as loop
25
26 from mic.pluginbase import ImagerPlugin
27 class LoopPlugin(ImagerPlugin):
28     name = 'loop'
29
30     @classmethod
31     def do_create(self, subcmd, opts, *args):
32         """${cmd_name}: create loop image
33
34         ${cmd_usage}
35         ${cmd_option_list}
36         """
37
38         if not args:
39             raise errors.Usage("More arguments needed")
40
41         if len(args) != 1:
42             raise errors.Usage("Extra arguments given")
43
44         cfgmgr = configmgr.getConfigMgr()
45         creatoropts = cfgmgr.create
46         ksconf = args[0]
47
48         recording_pkgs = None
49         if creatoropts['release'] is not None:
50             recording_pkgs = "name"
51             ksconf = misc.save_ksconf_file(ksconf, creatoropts['release'])
52             name = os.path.splitext(os.path.basename(ksconf))[0]
53             creatoropts['outdir'] = "%s/%s-%s/" % (creatoropts['outdir'], name, creatoropts['release'])
54         cfgmgr._ksconf = ksconf
55
56         # try to find the pkgmgr
57         pkgmgr = None
58         for (key, pcls) in pluginmgr.PluginMgr().get_plugins('backend').iteritems():
59             if key == creatoropts['pkgmgr']:
60                 pkgmgr = pcls
61                 break
62
63         if not pkgmgr:
64             pkgmgrs = pluginmgr.PluginMgr().get_plugins('backend').keys()
65             raise errors.CreatorError("Can't find package manager: %s (availables: %s)" % (creatoropts['pkgmgr'], ', '.join(pkgmgrs)))
66
67         creator = loop.LoopImageCreator(creatoropts, pkgmgr)
68
69         if recording_pkgs is not None:
70             creator._recording_pkgs = recording_pkgs
71
72         try:
73             creator.check_depend_tools()
74             creator.mount(None, creatoropts["cachedir"])
75             creator.install()
76             creator.configure(creatoropts["repomd"])
77             creator.unmount()
78             creator.package(creatoropts["outdir"])
79             outimage = creator.outimage
80             if creatoropts['release'] is not None:
81                 creator.release_output(ksconf, creatoropts['outdir'], creatoropts['name'], creatoropts['release'])
82             creator.print_outimage_info()
83
84         except errors.CreatorError:
85             raise
86         finally:
87             creator.cleanup()
88
89         msger.info("Finished.")
90         return 0
91
92     @classmethod
93     def do_chroot(cls, target):#chroot.py parse opts&args
94         img = target
95         imgsize = misc.get_file_size(img)
96         extmnt = misc.mkdtemp()
97         extloop = fs_related.ExtDiskMount(fs_related.SparseLoopbackDisk(img, imgsize),
98                                                          extmnt,
99                                                          "ext3",
100                                                          4096,
101                                                          "ext3 label")
102         try:
103             extloop.mount()
104
105         except errors.MountError:
106             extloop.cleanup()
107             shutil.rmtree(extmnt, ignore_errors = True)
108             raise
109
110         try:
111             chroot.chroot(extmnt, None,  "/bin/env HOME=/root /bin/bash")
112         except:
113             raise errors.CreatorError("Failed to chroot to %s." %img)
114         finally:
115             chroot.cleanup_after_chroot("img", extloop, None, extmnt)
116
117     @classmethod
118     def do_unpack(cls, srcimg):
119         image = os.path.join(tempfile.mkdtemp(dir = "/var/tmp", prefix = "tmp"), "target.img")
120         msger.info("Copying file system ...")
121         shutil.copyfile(srcimg, image)
122         return image