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.
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
29 from mic.pluginbase import ImagerPlugin
30 class LoopPlugin(ImagerPlugin):
34 def do_create(self, args):
35 """${cmd_name}: create loop image
38 ${name} ${cmd_name} <ksfile> [OPTS]
44 raise errors.Usage("Invalid arguments")
46 creatoropts = configmgr.create
49 if creatoropts['runtime'] == "bootstrap":
50 configmgr._ksconf = ksconf
51 rt_util.bootstrap_mic()
54 if len(creatoropts['record_pkgs']) > 0:
55 recording_pkgs = creatoropts['record_pkgs']
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')
63 configmgr._ksconf = ksconf
65 # try to find the pkgmgr
67 backends = pluginmgr.get_plugins('backend')
68 if 'auto' == creatoropts['pkgmgr']:
69 for key in configmgr.prefer_backends:
71 pkgmgr = backends[key]
74 for key in backends.keys():
75 if key == creatoropts['pkgmgr']:
76 pkgmgr = backends[key]
80 raise errors.CreatorError("Can't find backend: %s, "
81 "available choices: %s" %
82 (creatoropts['pkgmgr'],
83 ','.join(backends.keys())))
85 creator = LoopImageCreator(creatoropts,
90 if len(recording_pkgs) > 0:
91 creator._recording_pkgs = recording_pkgs
93 image_names = [creator.name + ".img"]
94 image_names.extend(creator.get_image_names())
95 self.check_image_exists(creator.destdir,
98 creatoropts['release'])
101 creator.check_depend_tools()
102 creator.mount(None, creatoropts["cachedir"])
105 creator.configure(creatoropts["repomd"])
106 creator.copy_kernel()
107 creator.create_cpio_image()
109 creator.copy_cpio_image()
110 creator.package(creatoropts["destdir"])
111 creator.create_manifest()
113 if creatoropts['release'] is not None:
114 creator.release_output(ksconf,
115 creatoropts['destdir'],
116 creatoropts['release'])
117 creator.print_outimage_info()
119 except errors.CreatorError:
124 #Run script of --run_script after image created
125 if creatoropts['run_script']:
126 cmd = creatoropts['run_script']
130 msger.warning(str(err))
132 msger.info("Finished.")
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)
143 tar = tarfile.open(target, 'r')
144 tmpdir = misc.mkdtemp()
145 tar.extractall(path=tmpdir)
148 mntdir = misc.mkdtemp()
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
159 raise errors.CreatorError("Cannot support fstype: %s" % fstype)
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('/')),
168 msger.verbose("Mount %s to %s" % (mp, mntdir + mp))
169 fs_related.makedirs(os.path.join(mntdir, mp.lstrip('/')))
174 for lp in reversed(loops):
175 chroot.cleanup_after_chroot("img", lp, None, mntdir)
177 shutil.rmtree(tmpdir, ignore_errors=True)
184 cmdline = "/usr/bin/env HOME=/root " + ' '.join(cmd)
186 cmdline = "/usr/bin/env HOME=/root /bin/bash"
187 chroot.chroot(mntdir, None, cmdline)
189 raise errors.CreatorError("Failed to chroot to %s." % target)
191 for loop in reversed(loops):
192 chroot.cleanup_after_chroot("img", loop, None, mntdir)
194 shutil.rmtree(tmpdir, ignore_errors=True)
197 def do_chroot(cls, target, cmd=[]):
198 if target.endswith('.tar'):
200 if tarfile.is_tarfile(target):
201 LoopPlugin._do_chroot_tar(target, cmd)
204 raise errors.CreatorError("damaged tarball for loop images")
207 imgsize = misc.get_file_size(img) * 1024L * 1024L
208 imgtype = misc.get_image_type(img)
209 if imgtype == "btrfsimg":
211 myDiskMount = fs_related.BtrfsDiskMount
212 elif imgtype in ("ext3fsimg", "ext4fsimg"):
214 myDiskMount = fs_related.ExtDiskMount
216 raise errors.CreatorError("Unsupported filesystem type: %s" \
219 extmnt = misc.mkdtemp()
220 extloop = myDiskMount(fs_related.SparseLoopbackDisk(img, imgsize),
228 except errors.MountError:
230 shutil.rmtree(extmnt, ignore_errors=True)
237 cmdline = "/bin/bash"
238 envcmd = fs_related.find_binary_inchroot("env", extmnt)
240 cmdline = "%s HOME=/root %s" % (envcmd, cmdline)
241 chroot.chroot(extmnt, None, cmdline)
243 raise errors.CreatorError("Failed to chroot to %s." % img)
245 chroot.cleanup_after_chroot("img", extloop, None, extmnt)
248 def do_unpack(cls, srcimg):
249 image = os.path.join(tempfile.mkdtemp(dir="/var/tmp", prefix="tmp"),
251 msger.info("Copying file system ...")
252 shutil.copyfile(srcimg, image)