diff options
| author | Adrian Dudau <adrian.dudau@enea.com> | 2013-12-12 13:38:32 +0100 |
|---|---|---|
| committer | Adrian Dudau <adrian.dudau@enea.com> | 2013-12-12 13:50:20 +0100 |
| commit | e2e6f6fe07049f33cb6348780fa975162752e421 (patch) | |
| tree | b1813295411235d1297a0ed642b1346b24fdfb12 /scripts/lib/mic/plugins/imager/loop_plugin.py | |
| download | poky-e2e6f6fe07049f33cb6348780fa975162752e421.tar.gz | |
initial commit of Enea Linux 3.1
Migrated from the internal git server on the dora-enea branch
Signed-off-by: Adrian Dudau <adrian.dudau@enea.com>
Diffstat (limited to 'scripts/lib/mic/plugins/imager/loop_plugin.py')
| -rw-r--r-- | scripts/lib/mic/plugins/imager/loop_plugin.py | 255 |
1 files changed, 255 insertions, 0 deletions
diff --git a/scripts/lib/mic/plugins/imager/loop_plugin.py b/scripts/lib/mic/plugins/imager/loop_plugin.py new file mode 100644 index 0000000000..8f4b030f6b --- /dev/null +++ b/scripts/lib/mic/plugins/imager/loop_plugin.py | |||
| @@ -0,0 +1,255 @@ | |||
| 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 chroot, msger, rt_util | ||
| 23 | from mic.utils import misc, fs_related, errors, cmdln | ||
| 24 | from mic.conf import configmgr | ||
| 25 | from mic.plugin import pluginmgr | ||
| 26 | from mic.imager.loop import LoopImageCreator, load_mountpoints | ||
| 27 | |||
| 28 | from mic.pluginbase import ImagerPlugin | ||
| 29 | class LoopPlugin(ImagerPlugin): | ||
| 30 | name = 'loop' | ||
| 31 | |||
| 32 | @classmethod | ||
| 33 | @cmdln.option("--compress-disk-image", dest="compress_image", | ||
| 34 | type='choice', choices=("gz", "bz2"), default=None, | ||
| 35 | help="Same with --compress-image") | ||
| 36 | # alias to compress-image for compatibility | ||
| 37 | @cmdln.option("--compress-image", dest="compress_image", | ||
| 38 | type='choice', choices=("gz", "bz2"), default=None, | ||
| 39 | help="Compress all loop images with 'gz' or 'bz2'") | ||
| 40 | @cmdln.option("--shrink", action='store_true', default=False, | ||
| 41 | help="Whether to shrink loop images to minimal size") | ||
| 42 | def do_create(self, subcmd, opts, *args): | ||
| 43 | """${cmd_name}: create loop image | ||
| 44 | |||
| 45 | Usage: | ||
| 46 | ${name} ${cmd_name} <ksfile> [OPTS] | ||
| 47 | |||
| 48 | ${cmd_option_list} | ||
| 49 | """ | ||
| 50 | |||
| 51 | if len(args) != 1: | ||
| 52 | raise errors.Usage("Extra arguments given") | ||
| 53 | |||
| 54 | creatoropts = configmgr.create | ||
| 55 | ksconf = args[0] | ||
| 56 | |||
| 57 | if creatoropts['runtime'] == "bootstrap": | ||
| 58 | configmgr._ksconf = ksconf | ||
| 59 | rt_util.bootstrap_mic() | ||
| 60 | |||
| 61 | recording_pkgs = [] | ||
| 62 | if len(creatoropts['record_pkgs']) > 0: | ||
| 63 | recording_pkgs = creatoropts['record_pkgs'] | ||
| 64 | |||
| 65 | if creatoropts['release'] is not None: | ||
| 66 | if 'name' not in recording_pkgs: | ||
| 67 | recording_pkgs.append('name') | ||
| 68 | if 'vcs' not in recording_pkgs: | ||
| 69 | recording_pkgs.append('vcs') | ||
| 70 | |||
| 71 | configmgr._ksconf = ksconf | ||
| 72 | |||
| 73 | # Called After setting the configmgr._ksconf | ||
| 74 | # as the creatoropts['name'] is reset there. | ||
| 75 | if creatoropts['release'] is not None: | ||
| 76 | creatoropts['outdir'] = "%s/%s/images/%s/" % (creatoropts['outdir'], | ||
| 77 | creatoropts['release'], | ||
| 78 | creatoropts['name']) | ||
| 79 | # try to find the pkgmgr | ||
| 80 | pkgmgr = None | ||
| 81 | backends = pluginmgr.get_plugins('backend') | ||
| 82 | if 'auto' == creatoropts['pkgmgr']: | ||
| 83 | for key in configmgr.prefer_backends: | ||
| 84 | if key in backends: | ||
| 85 | pkgmgr = backends[key] | ||
| 86 | break | ||
| 87 | else: | ||
| 88 | for key in backends.keys(): | ||
| 89 | if key == creatoropts['pkgmgr']: | ||
| 90 | pkgmgr = backends[key] | ||
| 91 | break | ||
| 92 | |||
| 93 | if not pkgmgr: | ||
| 94 | raise errors.CreatorError("Can't find backend: %s, " | ||
| 95 | "available choices: %s" % | ||
| 96 | (creatoropts['pkgmgr'], | ||
| 97 | ','.join(backends.keys()))) | ||
| 98 | |||
| 99 | creator = LoopImageCreator(creatoropts, | ||
| 100 | pkgmgr, | ||
| 101 | opts.compress_image, | ||
| 102 | opts.shrink) | ||
| 103 | |||
| 104 | if len(recording_pkgs) > 0: | ||
| 105 | creator._recording_pkgs = recording_pkgs | ||
| 106 | |||
| 107 | image_names = [creator.name + ".img"] | ||
| 108 | image_names.extend(creator.get_image_names()) | ||
| 109 | self.check_image_exists(creator.destdir, | ||
| 110 | creator.pack_to, | ||
| 111 | image_names, | ||
| 112 | creatoropts['release']) | ||
| 113 | |||
| 114 | try: | ||
| 115 | creator.check_depend_tools() | ||
| 116 | creator.mount(None, creatoropts["cachedir"]) | ||
| 117 | creator.install() | ||
| 118 | creator.configure(creatoropts["repomd"]) | ||
| 119 | creator.copy_kernel() | ||
| 120 | creator.unmount() | ||
| 121 | creator.package(creatoropts["outdir"]) | ||
| 122 | |||
| 123 | if creatoropts['release'] is not None: | ||
| 124 | creator.release_output(ksconf, | ||
| 125 | creatoropts['outdir'], | ||
| 126 | creatoropts['release']) | ||
| 127 | creator.print_outimage_info() | ||
| 128 | |||
| 129 | except errors.CreatorError: | ||
| 130 | raise | ||
| 131 | finally: | ||
| 132 | creator.cleanup() | ||
| 133 | |||
| 134 | msger.info("Finished.") | ||
| 135 | return 0 | ||
| 136 | |||
| 137 | @classmethod | ||
| 138 | def _do_chroot_tar(cls, target, cmd=[]): | ||
| 139 | mountfp_xml = os.path.splitext(target)[0] + '.xml' | ||
| 140 | if not os.path.exists(mountfp_xml): | ||
| 141 | raise errors.CreatorError("No mount point file found for this tar " | ||
| 142 | "image, please check %s" % mountfp_xml) | ||
| 143 | |||
| 144 | import tarfile | ||
| 145 | tar = tarfile.open(target, 'r') | ||
| 146 | tmpdir = misc.mkdtemp() | ||
| 147 | tar.extractall(path=tmpdir) | ||
| 148 | tar.close() | ||
| 149 | |||
| 150 | mntdir = misc.mkdtemp() | ||
| 151 | |||
| 152 | loops = [] | ||
| 153 | for (mp, label, name, size, fstype) in load_mountpoints(mountfp_xml): | ||
| 154 | if fstype in ("ext2", "ext3", "ext4"): | ||
| 155 | myDiskMount = fs_related.ExtDiskMount | ||
| 156 | elif fstype == "btrfs": | ||
| 157 | myDiskMount = fs_related.BtrfsDiskMount | ||
| 158 | elif fstype in ("vfat", "msdos"): | ||
| 159 | myDiskMount = fs_related.VfatDiskMount | ||
| 160 | else: | ||
| 161 | msger.error("Cannot support fstype: %s" % fstype) | ||
| 162 | |||
| 163 | name = os.path.join(tmpdir, name) | ||
| 164 | size = size * 1024L * 1024L | ||
| 165 | loop = myDiskMount(fs_related.SparseLoopbackDisk(name, size), | ||
| 166 | os.path.join(mntdir, mp.lstrip('/')), | ||
| 167 | fstype, size, label) | ||
| 168 | |||
| 169 | try: | ||
| 170 | msger.verbose("Mount %s to %s" % (mp, mntdir + mp)) | ||
| 171 | fs_related.makedirs(os.path.join(mntdir, mp.lstrip('/'))) | ||
| 172 | loop.mount() | ||
| 173 | |||
| 174 | except: | ||
| 175 | loop.cleanup() | ||
| 176 | for lp in reversed(loops): | ||
| 177 | chroot.cleanup_after_chroot("img", lp, None, mntdir) | ||
| 178 | |||
| 179 | shutil.rmtree(tmpdir, ignore_errors=True) | ||
| 180 | raise | ||
| 181 | |||
| 182 | loops.append(loop) | ||
| 183 | |||
| 184 | try: | ||
| 185 | if len(cmd) != 0: | ||
| 186 | cmdline = "/usr/bin/env HOME=/root " + ' '.join(cmd) | ||
| 187 | else: | ||
| 188 | cmdline = "/usr/bin/env HOME=/root /bin/bash" | ||
| 189 | chroot.chroot(mntdir, None, cmdline) | ||
| 190 | except: | ||
| 191 | raise errors.CreatorError("Failed to chroot to %s." % target) | ||
| 192 | finally: | ||
| 193 | for loop in reversed(loops): | ||
| 194 | chroot.cleanup_after_chroot("img", loop, None, mntdir) | ||
| 195 | |||
| 196 | shutil.rmtree(tmpdir, ignore_errors=True) | ||
| 197 | |||
| 198 | @classmethod | ||
| 199 | def do_chroot(cls, target, cmd=[]): | ||
| 200 | if target.endswith('.tar'): | ||
| 201 | import tarfile | ||
| 202 | if tarfile.is_tarfile(target): | ||
| 203 | LoopPlugin._do_chroot_tar(target, cmd) | ||
| 204 | return | ||
| 205 | else: | ||
| 206 | raise errors.CreatorError("damaged tarball for loop images") | ||
| 207 | |||
| 208 | img = target | ||
| 209 | imgsize = misc.get_file_size(img) * 1024L * 1024L | ||
| 210 | imgtype = misc.get_image_type(img) | ||
| 211 | if imgtype == "btrfsimg": | ||
| 212 | fstype = "btrfs" | ||
| 213 | myDiskMount = fs_related.BtrfsDiskMount | ||
| 214 | elif imgtype in ("ext3fsimg", "ext4fsimg"): | ||
| 215 | fstype = imgtype[:4] | ||
| 216 | myDiskMount = fs_related.ExtDiskMount | ||
| 217 | else: | ||
| 218 | raise errors.CreatorError("Unsupported filesystem type: %s" \ | ||
| 219 | % imgtype) | ||
| 220 | |||
| 221 | extmnt = misc.mkdtemp() | ||
| 222 | extloop = myDiskMount(fs_related.SparseLoopbackDisk(img, imgsize), | ||
| 223 | extmnt, | ||
| 224 | fstype, | ||
| 225 | 4096, | ||
| 226 | "%s label" % fstype) | ||
| 227 | try: | ||
| 228 | extloop.mount() | ||
| 229 | |||
| 230 | except errors.MountError: | ||
| 231 | extloop.cleanup() | ||
| 232 | shutil.rmtree(extmnt, ignore_errors=True) | ||
| 233 | raise | ||
| 234 | |||
| 235 | try: | ||
| 236 | if len(cmd) != 0: | ||
| 237 | cmdline = ' '.join(cmd) | ||
| 238 | else: | ||
| 239 | cmdline = "/bin/bash" | ||
| 240 | envcmd = fs_related.find_binary_inchroot("env", extmnt) | ||
| 241 | if envcmd: | ||
| 242 | cmdline = "%s HOME=/root %s" % (envcmd, cmdline) | ||
| 243 | chroot.chroot(extmnt, None, cmdline) | ||
| 244 | except: | ||
| 245 | raise errors.CreatorError("Failed to chroot to %s." % img) | ||
| 246 | finally: | ||
| 247 | chroot.cleanup_after_chroot("img", extloop, None, extmnt) | ||
| 248 | |||
| 249 | @classmethod | ||
| 250 | def do_unpack(cls, srcimg): | ||
| 251 | image = os.path.join(tempfile.mkdtemp(dir="/var/tmp", prefix="tmp"), | ||
| 252 | "target.img") | ||
| 253 | msger.info("Copying file system ...") | ||
| 254 | shutil.copyfile(srcimg, image) | ||
| 255 | return image | ||
