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.plugin import pluginmgr
26 from mic.imager.loop import LoopImageCreator, load_mountpoints
28 from mic.pluginbase import ImagerPlugin
29 class LoopPlugin(ImagerPlugin):
33 def do_create(self, args):
34 """${cmd_name}: create loop image
37 creatoropts, pkgmgr, recording_pkgs = rt_util.prepare_create(args)
39 creator = LoopImageCreator(creatoropts,
44 if len(recording_pkgs) > 0:
45 creator._recording_pkgs = recording_pkgs
47 image_names = [creator.name + ".img"]
48 image_names.extend(creator.get_image_names())
49 self.check_image_exists(creator.destdir,
52 creatoropts['release'])
55 creator.check_depend_tools()
56 creator.mount(None, creatoropts["cachedir"])
59 creator.configure(creatoropts["repomd"])
61 creator.create_cpio_image()
63 creator.copy_cpio_image()
64 creator.package(creatoropts["destdir"])
65 creator.create_manifest()
67 if creatoropts['release'] is not None:
68 creator.release_output(args.ksfile,
69 creatoropts['destdir'],
70 creatoropts['release'])
71 creator.print_outimage_info()
73 except errors.CreatorError:
78 #Run script of --run_script after image created
79 if creatoropts['run_script']:
80 cmd = creatoropts['run_script']
83 except OSError as err:
84 msger.warning(str(err))
86 msger.info("Finished.")
90 def _do_chroot_tar(cls, target, cmd=[]):
91 mountfp_xml = os.path.splitext(target)[0] + '.xml'
92 if not os.path.exists(mountfp_xml):
93 raise errors.CreatorError("No mount point file found for this tar "
94 "image, please check %s" % mountfp_xml)
97 tar = tarfile.open(target, 'r')
98 tmpdir = misc.mkdtemp()
99 tar.extractall(path=tmpdir)
102 mntdir = misc.mkdtemp()
105 for (mp, label, name, size, fstype) in load_mountpoints(mountfp_xml):
106 if fstype in ("ext2", "ext3", "ext4"):
107 myDiskMount = fs_related.ExtDiskMount
108 elif fstype == "btrfs":
109 myDiskMount = fs_related.BtrfsDiskMount
110 elif fstype in ("vfat", "msdos"):
111 myDiskMount = fs_related.VfatDiskMount
113 raise errors.CreatorError("Cannot support fstype: %s" % fstype)
115 name = os.path.join(tmpdir, name)
116 size = size * 1024 * 1024
117 loop = myDiskMount(fs_related.SparseLoopbackDisk(name, size),
118 os.path.join(mntdir, mp.lstrip('/')),
122 msger.verbose("Mount %s to %s" % (mp, mntdir + mp))
123 fs_related.makedirs(os.path.join(mntdir, mp.lstrip('/')))
128 for lp in reversed(loops):
129 chroot.cleanup_after_chroot("img", lp, None, mntdir)
131 shutil.rmtree(tmpdir, ignore_errors=True)
138 cmdline = "/usr/bin/env HOME=/root " + ' '.join(cmd)
140 cmdline = "/usr/bin/env HOME=/root /bin/bash"
141 chroot.chroot(mntdir, None, cmdline)
143 raise errors.CreatorError("Failed to chroot to %s." % target)
145 for loop in reversed(loops):
146 chroot.cleanup_after_chroot("img", loop, None, mntdir)
148 shutil.rmtree(tmpdir, ignore_errors=True)
151 def do_chroot(cls, target, cmd=[]):
152 if target.endswith('.tar'):
154 if tarfile.is_tarfile(target):
155 LoopPlugin._do_chroot_tar(target, cmd)
158 raise errors.CreatorError("damaged tarball for loop images")
161 imgsize = misc.get_file_size(img) * 1024 * 1024
162 imgtype = misc.get_image_type(img)
163 if imgtype == "btrfsimg":
165 myDiskMount = fs_related.BtrfsDiskMount
166 elif imgtype in ("ext3fsimg", "ext4fsimg"):
168 myDiskMount = fs_related.ExtDiskMount
169 elif imgtype == "f2fsimg":
171 myDiskMount = fs_related.F2fsDiskMount
173 raise errors.CreatorError("Unsupported filesystem type: %s" \
176 extmnt = misc.mkdtemp()
177 extloop = myDiskMount(fs_related.SparseLoopbackDisk(img, imgsize),
185 except errors.MountError:
187 shutil.rmtree(extmnt, ignore_errors=True)
194 cmdline = "/bin/bash"
195 envcmd = fs_related.find_binary_inchroot("env", extmnt)
197 cmdline = "%s HOME=/root %s" % (envcmd, cmdline)
198 chroot.chroot(extmnt, None, cmdline)
200 raise errors.CreatorError("Failed to chroot to %s." % img)
202 chroot.cleanup_after_chroot("img", extloop, None, extmnt)
205 def do_unpack(cls, srcimg):
206 image = os.path.join(tempfile.mkdtemp(dir="/var/tmp", prefix="tmp"),
208 msger.info("Copying file system ...")
209 shutil.copyfile(srcimg, image)