3 # Copyright (c) 2011 Intel, Inc.
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
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
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.
22 from mic import configmgr, pluginmgr, chroot, msger
23 from mic.utils import misc, fs_related, errors
24 import mic.imager.loop as loop
26 from mic.pluginbase import ImagerPlugin
27 class LoopPlugin(ImagerPlugin):
31 def do_create(self, subcmd, opts, *args):
32 """${cmd_name}: create loop image
39 raise errors.Usage("More arguments needed")
42 raise errors.Usage("Extra arguments given")
44 cfgmgr = configmgr.getConfigMgr()
45 creatoropts = cfgmgr.create
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
56 # try to find the pkgmgr
58 for (key, pcls) in pluginmgr.PluginMgr().get_plugins('backend').iteritems():
59 if key == creatoropts['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)))
67 creator = loop.LoopImageCreator(creatoropts, pkgmgr)
69 if recording_pkgs is not None:
70 creator._recording_pkgs = recording_pkgs
73 creator.check_depend_tools()
74 creator.mount(None, creatoropts["cachedir"])
76 creator.configure(creatoropts["repomd"])
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()
84 except errors.CreatorError:
89 msger.info("Finished.")
93 def do_chroot(cls, target):#chroot.py parse opts&args
95 imgsize = misc.get_file_size(img)
96 extmnt = misc.mkdtemp()
97 extloop = fs_related.ExtDiskMount(fs_related.SparseLoopbackDisk(img, imgsize),
105 except errors.MountError:
107 shutil.rmtree(extmnt, ignore_errors = True)
111 chroot.chroot(extmnt, None, "/bin/env HOME=/root /bin/bash")
113 raise errors.CreatorError("Failed to chroot to %s." %img)
115 chroot.cleanup_after_chroot("img", extloop, None, extmnt)
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)