def do_chroot(cls, target): os_image = cls.do_unpack(target) os_image_dir = os.path.dirname(os_image) # unpack image to target dir imgsize = misc.get_file_size(os_image) * 1024L * 1024L imgtype = misc.get_image_type(os_image) if imgtype == "btrfsimg": fstype = "btrfs" myDiskMount = fs_related.BtrfsDiskMount elif imgtype in ("ext3fsimg", "ext4fsimg"): fstype = imgtype[:4] myDiskMount = fs_related.ExtDiskMount else: raise errors.CreatorError("Unsupported filesystem type: %s" % fstype) extmnt = misc.mkdtemp() extloop = myDiskMount(fs_related.SparseLoopbackDisk(os_image, imgsize), extmnt, fstype, 4096, "%s label" % fstype) try: extloop.mount() except errors.MountError: extloop.cleanup() shutil.rmtree(extmnt, ignore_errors=True) raise try: chroot.chroot(extmnt, None, "/bin/env HOME=/root /bin/bash") except: raise errors.CreatorError("Failed to chroot to %s." % target) finally: chroot.cleanup_after_chroot("img", extloop, os_image_dir, extmnt)
def main(parser, args, argv): """mic choot entry point.""" #args is argparser namespace, argv is the input cmd line if args is None: raise errors.Usage("Invalid arguments") targetimage = args.imagefile if not os.path.exists(targetimage): raise errors.CreatorError("Cannot find the image: %s" % targetimage) _root_confirm() configmgr.chroot['saveto'] = args.saveto imagetype = misc.get_image_type(targetimage) if imagetype in ("ext3fsimg", "ext4fsimg", "btrfsimg"): imagetype = "loop" chrootclass = None for pname, pcls in pluginmgr.get_plugins('imager').iteritems(): if pname == imagetype and hasattr(pcls, "do_chroot"): chrootclass = pcls break if not chrootclass: raise errors.CreatorError("Cannot support image type: %s" \ % imagetype) chrootclass.do_chroot(targetimage, args.cmd)
def creatoropts(args): if not args: raise errors.Usage("need one argument as the path of ks file") if len(args) != 1: raise errors.Usage("Extra arguments given") creatoropts = configmgr.create ksconf = args[0] if not os.path.exists(ksconf): raise errors.CreatorError("Can't find the file: %s" % ksconf) if not 'record_pkgs' in creatoropts: creatoropts['record_pkgs'] = [] if creatoropts['release'] is not None: if 'name' not in creatoropts['record_pkgs']: creatoropts['record_pkgs'].append('name') ksconf = misc.normalize_ksfile(ksconf, creatoropts['tokenmap']) configmgr._ksconf = ksconf # Called After setting the configmgr._ksconf as the creatoropts['name'] is reset there. if creatoropts['release'] is not None: creatoropts['outdir'] = "%s/%s/images/%s/" % ( creatoropts['outdir'], creatoropts['release'], creatoropts['name']) # try to find the pkgmgr pkgmgr = None for (key, pcls) in pluginmgr.get_plugins('backend').items(): if key == creatoropts['pkgmgr']: pkgmgr = pcls break if not pkgmgr: pkgmgrs = list(pluginmgr.get_plugins('backend').keys()) raise errors.CreatorError( "Can't find package manager: %s (availables: %s)" % (creatoropts['pkgmgr'], ', '.join(pkgmgrs))) creatoropts['pkgmgr_pcls'] = pkgmgr if creatoropts['runtime']: rt_util.runmic_in_runtime(creatoropts['runtime'], creatoropts, ksconf, None) # Write the normalized kickstart to outdir # It has to be done this way in case the source ks is same as dest ks mkdir_p(creatoropts['outdir']) dst_ks = "%s/%s.ks" % (creatoropts['outdir'], creatoropts['name']) with open(configmgr._ksconf, 'r') as src_ksf: src_ks = src_ksf.read() with open(dst_ks, 'w') as dst_ksf: dst_ksf.write(src_ks) creatoropts['dst_ks'] = dst_ks return creatoropts
def _do_chroot_tar(cls, target, cmd=[]): mountfp_xml = os.path.splitext(target)[0] + '.xml' if not os.path.exists(mountfp_xml): raise errors.CreatorError("No mount point file found for this tar " "image, please check %s" % mountfp_xml) import tarfile tar = tarfile.open(target, 'r') tmpdir = misc.mkdtemp() tar.extractall(path=tmpdir) tar.close() mntdir = misc.mkdtemp() loops = [] for (mp, label, name, size, fstype) in load_mountpoints(mountfp_xml): if fstype in ("ext2", "ext3", "ext4"): myDiskMount = fs_related.ExtDiskMount elif fstype == "btrfs": myDiskMount = fs_related.BtrfsDiskMount elif fstype in ("vfat", "msdos"): myDiskMount = fs_related.VfatDiskMount else: raise errors.CreatorError("Cannot support fstype: %s" % fstype) name = os.path.join(tmpdir, name) size = size * 1024L * 1024L loop = myDiskMount(fs_related.SparseLoopbackDisk(name, size), os.path.join(mntdir, mp.lstrip('/')), fstype, size, label) try: msger.verbose("Mount %s to %s" % (mp, mntdir + mp)) fs_related.makedirs(os.path.join(mntdir, mp.lstrip('/'))) loop.mount() except: loop.cleanup() for lp in reversed(loops): chroot.cleanup_after_chroot("img", lp, None, mntdir) shutil.rmtree(tmpdir, ignore_errors=True) raise loops.append(loop) try: if len(cmd) != 0: cmdline = "/usr/bin/env HOME=/root " + ' '.join(cmd) else: cmdline = "/usr/bin/env HOME=/root /bin/bash" chroot.chroot(mntdir, None, cmdline) except: raise errors.CreatorError("Failed to chroot to %s." % target) finally: for loop in reversed(loops): chroot.cleanup_after_chroot("img", loop, None, mntdir) shutil.rmtree(tmpdir, ignore_errors=True)
def do_chroot(cls, target, cmd=[]): if target.endswith('.tar'): import tarfile if tarfile.is_tarfile(target): LoopPlugin._do_chroot_tar(target, cmd) return else: raise errors.CreatorError("damaged tarball for loop images") img = target imgsize = misc.get_file_size(img) * 1024L * 1024L imgtype = misc.get_image_type(img) if imgtype == "btrfsimg": fstype = "btrfs" myDiskMount = fs_related.BtrfsDiskMount elif imgtype in ("ext3fsimg", "ext4fsimg"): fstype = imgtype[:4] myDiskMount = fs_related.ExtDiskMount else: raise errors.CreatorError("Unsupported filesystem type: %s" \ % imgtype) extmnt = misc.mkdtemp() extloop = myDiskMount(fs_related.SparseLoopbackDisk(img, imgsize), extmnt, fstype, 4096, "%s label" % fstype) try: extloop.mount() except errors.MountError: extloop.cleanup() shutil.rmtree(extmnt, ignore_errors=True) raise try: if cmd is not None: cmdline = cmd else: cmdline = "/bin/bash" envcmd = fs_related.find_binary_inchroot("env", extmnt) if envcmd: cmdline = "%s HOME=/root %s" % (envcmd, cmdline) chroot.chroot(extmnt, None, cmdline) except: raise errors.CreatorError("Failed to chroot to %s." % img) finally: chroot.cleanup_after_chroot("img", extloop, None, extmnt)
def do_unpack(cls, srcimg): srcimgsize = (misc.get_file_size(srcimg)) * 1024L * 1024L srcmnt = misc.mkdtemp("srcmnt") disk = fs_related.SparseLoopbackDisk(srcimg, srcimgsize) srcloop = PartitionedMount({'/dev/sdb': disk}, srcmnt, skipformat=True) srcloop.add_partition(srcimgsize / 1024 / 1024, "/dev/sdb", "/", "ext3", boot=False) try: srcloop.mount() except errors.MountError: srcloop.cleanup() raise image = os.path.join(tempfile.mkdtemp(dir="/var/tmp", prefix="tmp"), "target.img") args = [ 'dd', "if=%s" % srcloop.partitions[0]['device'], "of=%s" % image ] msger.info("`dd` image ...") rc = runner.show(args) srcloop.cleanup() shutil.rmtree(os.path.dirname(srcmnt), ignore_errors=True) if rc != 0: raise errors.CreatorError("Failed to dd") else: return image
def chroot(chrootdir, bindmounts=None, execute="/bin/bash"): """ chroot the chrootdir and execute the command """ def mychroot(): """ pre-execute function """ os.chroot(chrootdir) os.chdir("/") arch = ELF_arch(chrootdir) if arch == "arm": qemu_emulators = misc.setup_qemu_emulator(chrootdir, "arm") elif arch == "mipsel": qemu_emulators = misc.setup_qemu_emulator(chrootdir, "mipsel") else: qemu_emulators = [] savefs_before_chroot(chrootdir, None) globalmounts = None try: msger.info("Launching shell. Exit to continue.\n" "----------------------------------") globalmounts = setup_chrootenv(chrootdir, bindmounts) subprocess.call(execute, preexec_fn=mychroot, shell=True) except OSError, err: raise errors.CreatorError("chroot err: %s" % str(err))
def set_runtime(self, runtime): if runtime not in ("bootstrap", "native"): raise errors.CreatorError("Invalid runtime mode: %s" % runtime) if misc.get_distro()[0] in ("tizen", "Tizen"): runtime = "native" self.create['runtime'] = runtime
def __mkinitrd(instance): kernelver = instance._get_kernel_versions().values()[0][0] args = [ "/usr/libexec/mkliveinitrd", "/boot/initrd-%s.img" % kernelver, "%s" % kernelver ] try: subprocess.call(args, preexec_fn = instance._chroot) except OSError, (err, msg): raise errors.CreatorError("Failed to execute /usr/libexec/mkliveinitrd: %s" % msg)
def set_runtime(self, runtime): if runtime != "bootstrap": raise errors.CreatorError("Invalid runtime mode: %s, only 'bootstrap' mode is allowed." % runtime) if misc.get_distro()[0] in ("tizen", "Tizen"): runtime = "native" self.create['runtime'] = runtime
def chroot(chrootdir, bindmounts=None, execute="/bin/bash"): def mychroot(): os.chroot(chrootdir) os.chdir("/") dev_null = os.open("/dev/null", os.O_WRONLY) files_to_check = ["/bin/bash", "/sbin/init"] architecture_found = False """ Register statically-linked qemu-arm if it is an ARM fs """ qemu_emulator = None for ftc in files_to_check: ftc = "%s/%s" % (chrootdir, ftc) # Return code of 'file' is "almost always" 0 based on some man pages # so we need to check the file existance first. if not os.path.exists(ftc): continue for line in runner.outs(['file', ftc]).splitlines(): if 'ARM' in line: qemu_emulator = misc.setup_qemu_emulator(chrootdir, "arm") architecture_found = True break if 'Intel' in line: architecture_found = True break if architecture_found: break os.close(dev_null) if not architecture_found: raise errors.CreatorError("Failed to get architecture from any of the " "following files %s from chroot." \ % files_to_check) try: msger.info("Launching shell. Exit to continue.\n" "----------------------------------") globalmounts = setup_chrootenv(chrootdir, bindmounts) subprocess.call(execute, preexec_fn=mychroot, shell=True) except OSError, err: raise errors.CreatorError("chroot err: %s" % str(err))
def bootstrap_mic(argv=None): def mychroot(): os.chroot(rootdir) os.chdir(cwd) # by default, sys.argv is used to run mic in bootstrap if not argv: argv = sys.argv if argv[0] not in ('/usr/bin/mic', 'mic'): argv[0] = '/usr/bin/mic' cropts = configmgr.create bsopts = configmgr.bootstrap distro = bsopts['distro_name'].lower() rootdir = bsopts['rootdir'] pkglist = bsopts['packages'] cwd = os.getcwd() # create bootstrap and run mic in bootstrap bsenv = bootstrap.Bootstrap(rootdir, distro, cropts['arch']) bsenv.logfile = cropts['logfile'] # rootdir is regenerated as a temp dir rootdir = bsenv.rootdir if 'optional' in bsopts: optlist = bsopts['optional'] else: optlist = [] try: msger.info("Creating %s bootstrap ..." % distro) bsenv.create(cropts['repomd'], pkglist, optlist) # bootstrap is relocated under "bootstrap" if os.path.exists(os.path.join(rootdir, "bootstrap")): rootdir = os.path.join(rootdir, "bootstrap") bsenv.dirsetup(rootdir) if cropts['use_mic_in_bootstrap']: msger.info("No copy host mic") else: msger.info("Copy host mic to bootstrap") sync_mic(rootdir, plugin=cropts['plugin_dir']) #FIXME: sync the ks file to bootstrap if "/" == os.path.dirname(os.path.abspath(configmgr._ksconf)): safecopy(configmgr._ksconf, rootdir) msger.info("Start mic in bootstrap: %s\n" % rootdir) bsarch = ELF_arch(rootdir) if bsarch in personality_defs: condPersonality(bsarch) bindmounts = get_bindmounts(cropts) ret = bsenv.run(argv, cwd, rootdir, bindmounts) except errors.BootstrapError, err: raise errors.CreatorError("Failed to download/install bootstrap package " \ "or the package is in bad format: %s" % err)
def __run_post_cleanups(instance): kernelver = instance._get_kernel_versions().values()[0][0] args = ["rm", "-f", "/boot/initrd-%s.img" % kernelver] try: subprocess.call(args, preexec_fn = instance._chroot) except OSError, (err, msg): raise errors.CreatorError("Failed to run post cleanups: %s" % msg)
def do_auto(self, subcmd, opts, *args): """${cmd_name}: auto detect image type from magic header Usage: ${name} ${cmd_name} <ksfile> ${cmd_option_list} """ def parse_magic_line(re_str, pstr, ptype='mic'): ptn = re.compile(re_str) m = ptn.match(pstr) if not m or not m.groups(): return None inline_argv = m.group(1).strip() if ptype == 'mic': m2 = re.search('(?P<format>\w+)', inline_argv) elif ptype == 'mic2': m2 = re.search('(-f|--format(=)?)\s*(?P<format>\w+)', inline_argv) else: return None if m2: cmdname = m2.group('format') inline_argv = inline_argv.replace(m2.group(0), '') return (cmdname, inline_argv) return None if not args: self.do_help(['help', subcmd]) return None if len(args) != 1: raise errors.Usage("Extra arguments given") if not os.path.exists(args[0]): raise errors.CreatorError("Can't find the file: %s" % args[0]) with open(args[0], 'r') as rf: first_line = rf.readline() mic_re = '^#\s*-\*-mic-options-\*-\s+(.*)\s+-\*-mic-options-\*-' mic2_re = '^#\s*-\*-mic2-options-\*-\s+(.*)\s+-\*-mic2-options-\*-' result = parse_magic_line(mic_re, first_line, 'mic') \ or parse_magic_line(mic2_re, first_line, 'mic2') if not result: raise errors.KsError("Invalid magic line in file: %s" % args[0]) if result[0] not in self._subcmds: raise errors.KsError("Unsupport format '%s' in %s" % (result[0], args[0])) argv = ' '.join(result + args).split() self.main(argv)
def runtool(cmdln_or_args, catch=1): """ wrapper for most of the subprocess calls input: cmdln_or_args: can be both args and cmdln str (shell=True) catch: 0, quitely run 1, only STDOUT 2, only STDERR 3, both STDOUT and STDERR return: (rc, output) if catch==0: the output will always None """ if catch not in (0, 1, 2, 3): # invalid catch selection, will cause exception, that's good return None if isinstance(cmdln_or_args, list): cmd = cmdln_or_args[0] shell = False else: import shlex cmd = shlex.split(cmdln_or_args)[0] shell = True if catch != 3: dev_null = os.open("/dev/null", os.O_WRONLY) if catch == 0: sout = dev_null serr = dev_null elif catch == 1: sout = subprocess.PIPE serr = dev_null elif catch == 2: sout = dev_null serr = subprocess.PIPE elif catch == 3: sout = subprocess.PIPE serr = subprocess.STDOUT try: p = subprocess.Popen(cmdln_or_args, stdout=sout, stderr=serr, shell=shell) (sout, serr) = p.communicate() # combine stdout and stderr, filter None out out = ''.join(filter(None, [sout, serr])) except OSError, e: if e.errno == 2: # [Errno 2] No such file or directory raise errors.CreatorError( 'Cannot run command: %s, lost dependency?' % cmd) else: raise # relay
def get_plugins(self, ptype): """ the return value is dict of name:class pairs """ if ptype not in PLUGIN_TYPES: raise errors.CreatorError('%s is not valid plugin type' % ptype) self._add_plugindir(os.path.join(self.plugin_dir, ptype)) self._load_all() return pluginbase.get_plugins(ptype)
def get_plugins(self, ptype): """ the return value is dict of name:class pairs """ if ptype not in PLUGIN_TYPES: raise errors.CreatorError('%s is not valid plugin type' % ptype) plugins_dir = self._build_plugin_dir_list(self.plugin_dir, ptype) self.append_dirs(plugins_dir) return pluginbase.get_plugins(ptype)
def do_auto(parser, ksfile, argv): """${cmd_name}: auto detect image type from magic header Usage: ${name} ${cmd_name} <ksfile> ${cmd_option_list} """ def parse_magic_line(re_str, pstr, ptype='mic'): ptn = re.compile(re_str) m = ptn.match(pstr) if not m or not m.groups(): return None inline_argv = m.group(1).strip() if ptype == 'mic': m2 = re.search('(?P<format>\w+)', inline_argv) elif ptype == 'mic2': m2 = re.search('(-f|--format(=)?)\s*(?P<format>\w+)', inline_argv) else: return None if m2: cmdname = m2.group('format') inline_argv = inline_argv.replace(m2.group(0), '') return (cmdname, inline_argv) return None if not os.path.exists(ksfile): raise errors.CreatorError("Can't find the file: %s" % ksfile) with open(ksfile, 'r') as rf: first_line = rf.readline() mic_re = '^#\s*-\*-mic-options-\*-\s+(.*)\s+-\*-mic-options-\*-' mic2_re = '^#\s*-\*-mic2-options-\*-\s+(.*)\s+-\*-mic2-options-\*-' result = parse_magic_line(mic_re, first_line, 'mic') \ or parse_magic_line(mic2_re, first_line, 'mic2') if not result: raise errors.KsError("Invalid magic line in file: %s" % ksfile) ksargv = ' '.join(result).split() argv.remove("auto") index = argv.index("create") #insert the subcommand argv.insert(index + 1, ksargv[0]) options = argv + ksargv[1:] args = parser.parse_args(options) main(parser, args, options)
def do_unpack(cls, srcimg): img = srcimg imgsize = misc.get_file_size(img) * 1024L * 1024L imgmnt = misc.mkdtemp() disk = fs_related.SparseLoopbackDisk(img, imgsize) imgloop = PartitionedMount(imgmnt, skipformat=True) imgloop.add_disk('/dev/sdb', disk) imgloop.add_partition(imgsize / 1024 / 1024, "/dev/sdb", "/", "vfat", boot=False) try: imgloop.mount() except errors.MountError: imgloop.cleanup() raise # legacy LiveOS filesystem layout support, remove for F9 or F10 if os.path.exists(imgmnt + "/squashfs.img"): squashimg = imgmnt + "/squashfs.img" else: squashimg = imgmnt + "/LiveOS/squashfs.img" tmpoutdir = misc.mkdtemp() # unsquashfs requires outdir mustn't exist shutil.rmtree(tmpoutdir, ignore_errors=True) misc.uncompress_squashfs(squashimg, tmpoutdir) try: # legacy LiveOS filesystem layout support, remove for F9 or F10 if os.path.exists(tmpoutdir + "/os.img"): os_image = tmpoutdir + "/os.img" else: os_image = tmpoutdir + "/LiveOS/ext3fs.img" if not os.path.exists(os_image): raise errors.CreatorError( "'%s' is not a valid live CD ISO : neither " "LiveOS/ext3fs.img nor os.img exist" % img) imgname = os.path.basename(srcimg) imgname = os.path.splitext(imgname)[0] + ".img" rtimage = os.path.join( tempfile.mkdtemp(dir="/var/tmp", prefix="tmp"), imgname) shutil.copyfile(os_image, rtimage) finally: imgloop.cleanup() shutil.rmtree(tmpoutdir, ignore_errors=True) shutil.rmtree(imgmnt, ignore_errors=True) return rtimage
def do_chroot(cls, target): import tarfile if tarfile.is_tarfile(target): LoopPlugin._do_chroot_tar(target) return img = target imgsize = misc.get_file_size(img) * 1024L * 1024L imgtype = misc.get_image_type(img) if imgtype == "btrfsimg": fstype = "btrfs" myDiskMount = fs_related.BtrfsDiskMount elif imgtype in ("ext3fsimg", "ext4fsimg"): fstype = imgtype[:4] myDiskMount = fs_related.ExtDiskMount else: raise errors.CreatorError("Unsupported filesystem type: %s" \ % imgtype) extmnt = misc.mkdtemp() extloop = myDiskMount(fs_related.SparseLoopbackDisk(img, imgsize), extmnt, fstype, 4096, "%s label" % fstype) try: extloop.mount() except errors.MountError: extloop.cleanup() shutil.rmtree(extmnt, ignore_errors=True) raise try: chroot.chroot(extmnt, None, "/bin/env HOME=/root /bin/bash") except: raise errors.CreatorError("Failed to chroot to %s." % img) finally: chroot.cleanup_after_chroot("img", extloop, None, extmnt)
def do_pack(cls, base_on): import subprocess def __mkinitrd(instance): kernelver = list(instance._get_kernel_versions().values())[0][0] args = [ "/usr/libexec/mkliveinitrd", "/boot/initrd-%s.img" % kernelver, "%s" % kernelver ] try: subprocess.call(args, preexec_fn=instance._chroot) except OSError as xxx_todo_changeme: (err, msg) = xxx_todo_changeme.args raise errors.CreatorError( "Failed to execute /usr/libexec/mkliveinitrd: %s" % msg) def __run_post_cleanups(instance): kernelver = list(instance._get_kernel_versions().values())[0][0] args = ["rm", "-f", "/boot/initrd-%s.img" % kernelver] try: subprocess.call(args, preexec_fn=instance._chroot) except OSError as xxx_todo_changeme1: (err, msg) = xxx_todo_changeme1.args raise errors.CreatorError("Failed to run post cleanups: %s" % msg) convertoropts = configmgr.convert convertoropts['name'] = os.path.splitext(os.path.basename(base_on))[0] convertor = livecd.LiveCDImageCreator(convertoropts) imgtype = misc.get_image_type(base_on) if imgtype == "btrfsimg": fstype = "btrfs" elif imgtype in ("ext3fsimg", "ext4fsimg"): fstype = imgtype[:4] else: raise errors.CreatorError("Unsupported filesystem type: %s" % fstype) convertor._set_fstype(fstype) try: convertor.mount(base_on) __mkinitrd(convertor) convertor._create_bootconfig() __run_post_cleanups(convertor) convertor.launch_shell(convertoropts['shell']) convertor.unmount() convertor.package() convertor.print_outimage_info() finally: shutil.rmtree(os.path.dirname(base_on), ignore_errors=True)
def do_unpack(cls, srcimg): img = srcimg imgmnt = misc.mkdtemp() imgloop = fs_related.DiskMount(fs_related.LoopbackDisk(img, 0), imgmnt) try: imgloop.mount() except errors.MountError: imgloop.cleanup() raise # legacy LiveOS filesystem layout support, remove for F9 or F10 if os.path.exists(imgmnt + "/squashfs.img"): squashimg = imgmnt + "/squashfs.img" else: squashimg = imgmnt + "/LiveOS/squashfs.img" tmpoutdir = misc.mkdtemp() # unsquashfs requires outdir mustn't exist shutil.rmtree(tmpoutdir, ignore_errors=True) misc.uncompress_squashfs(squashimg, tmpoutdir) try: # legacy LiveOS filesystem layout support, remove for F9 or F10 if os.path.exists(tmpoutdir + "/os.img"): os_image = tmpoutdir + "/os.img" else: os_image = tmpoutdir + "/LiveOS/ext3fs.img" if not os.path.exists(os_image): raise errors.CreatorError( "'%s' is not a valid live CD ISO : neither " "LiveOS/ext3fs.img nor os.img exist" % img) imgname = os.path.basename(srcimg) imgname = os.path.splitext(imgname)[0] + ".img" rtimage = os.path.join( tempfile.mkdtemp(dir="/var/tmp", prefix="tmp"), imgname) shutil.copyfile(os_image, rtimage) finally: imgloop.cleanup() shutil.rmtree(tmpoutdir, ignore_errors=True) shutil.rmtree(imgmnt, ignore_errors=True) return rtimage
def ELF_arch(chrootdir): """ detect the architecture of an ELF file """ #FIXME: if chkfiles are symlink, it will be complex chkfiles = ('/bin/bash', '/sbin/init') # regular expression to arch mapping mapping = { r"Intel 80[0-9]86": "i686", r"x86-64": "x86_64", r"ARM": "arm", } for path in chkfiles: cpath = os.path.join(chrootdir, path.lstrip('/')) if not os.path.exists(cpath): continue outs = runner.outs(['file', cpath]) for ptn in mapping.keys(): if re.search(ptn, outs): return mapping[ptn] raise errors.CreatorError("Failed to detect architecture of chroot: %s" % chrootdir)
def do_create(self, subcmd, opts, *args): """${cmd_name}: create raw image Usage: ${name} ${cmd_name} <ksfile> [OPTS] ${cmd_option_list} """ if not args: raise errors.Usage("need one argument as the path of ks file") if len(args) != 1: raise errors.Usage("Extra arguments given") creatoropts = configmgr.create ksconf = args[0] if not os.path.exists(ksconf): raise errors.CreatorError("Can't find the file: %s" % ksconf) recording_pkgs = [] if len(creatoropts['record_pkgs']) > 0: recording_pkgs = creatoropts['record_pkgs'] if creatoropts['release'] is not None: if 'name' not in recording_pkgs: recording_pkgs.append('name') ksconf = misc.save_ksconf_file(ksconf, creatoropts['release']) configmgr._ksconf = ksconf # Called After setting the configmgr._ksconf as the creatoropts['name'] is reset there. if creatoropts['release'] is not None: creatoropts['outdir'] = "%s/%s/images/%s/" % ( creatoropts['outdir'], creatoropts['release'], creatoropts['name']) # try to find the pkgmgr pkgmgr = None for (key, pcls) in pluginmgr.get_plugins('backend').iteritems(): if key == creatoropts['pkgmgr']: pkgmgr = pcls break if not pkgmgr: pkgmgrs = pluginmgr.get_plugins('backend').keys() raise errors.CreatorError( "Can't find package manager: %s (availables: %s)" % (creatoropts['pkgmgr'], ', '.join(pkgmgrs))) if creatoropts['runtime']: rt_util.runmic_in_runtime(creatoropts['runtime'], creatoropts, ksconf, None) creator = raw.RawImageCreator(creatoropts, pkgmgr) if len(recording_pkgs) > 0: creator._recording_pkgs = recording_pkgs if creatoropts['release'] is None: for item in creator.get_diskinfo(): imagefile = "%s-%s.raw" % (os.path.join( creator.destdir, creator.name), item['name']) if os.path.exists(imagefile): if msger.ask( 'The target image: %s already exists, cleanup and continue?' % imagefile): os.unlink(imagefile) else: raise errors.Abort('Canceled') try: creator.check_depend_tools() creator.mount(None, creatoropts["cachedir"]) creator.install() creator.configure(creatoropts["repomd"]) creator.unmount() creator.package(creatoropts["outdir"]) if creatoropts['release'] is not None: creator.release_output(ksconf, creatoropts['outdir'], creatoropts['release']) creator.print_outimage_info() except errors.CreatorError: raise finally: creator.cleanup() msger.info("Finished.") return 0
def do_chroot(cls, target): img = target imgsize = misc.get_file_size(img) * 1024L * 1024L partedcmd = fs_related.find_binary_path("parted") disk = fs_related.SparseLoopbackDisk(img, imgsize) imgmnt = misc.mkdtemp() imgloop = PartitionedMount({'/dev/sdb': disk}, imgmnt, skipformat=True) img_fstype = "ext3" # Check the partitions from raw disk. root_mounted = False partition_mounts = 0 for line in runner.outs([partedcmd, "-s", img, "unit", "B", "print"]).splitlines(): line = line.strip() # Lines that start with number are the partitions, # because parted can be translated we can't refer to any text lines. if not line or not line[0].isdigit(): continue # Some vars have extra , as list seperator. line = line.replace(",", "") # Example of parted output lines that are handled: # Number Start End Size Type File system Flags # 1 512B 3400000511B 3400000000B primary # 2 3400531968B 3656384511B 255852544B primary linux-swap(v1) # 3 3656384512B 3720347647B 63963136B primary fat16 boot, lba partition_info = re.split("\s+", line) size = partition_info[3].split("B")[0] if len(partition_info) < 6 or partition_info[5] in ["boot"]: # No filesystem can be found from partition line. Assuming # btrfs, because that is the only MeeGo fs that parted does # not recognize properly. # TODO: Can we make better assumption? fstype = "btrfs" elif partition_info[5] in ["ext2", "ext3", "ext4", "btrfs"]: fstype = partition_info[5] elif partition_info[5] in ["fat16", "fat32"]: fstype = "vfat" elif "swap" in partition_info[5]: fstype = "swap" else: raise errors.CreatorError( "Could not recognize partition fs type '%s'." % partition_info[5]) if not root_mounted and fstype in [ "ext2", "ext3", "ext4", "btrfs" ]: # TODO: Check that this is actually the valid root partition from /etc/fstab mountpoint = "/" root_mounted = True elif fstype == "swap": mountpoint = "swap" else: # TODO: Assing better mount points for the rest of the partitions. partition_mounts += 1 mountpoint = "/media/partition_%d" % partition_mounts if "boot" in partition_info: boot = True else: boot = False msger.verbose( "Size: %s Bytes, fstype: %s, mountpoint: %s, boot: %s" % (size, fstype, mountpoint, boot)) # TODO: add_partition should take bytes as size parameter. imgloop.add_partition((int)(size) / 1024 / 1024, "/dev/sdb", mountpoint, fstype=fstype, boot=boot) try: imgloop.mount() except errors.MountError: imgloop.cleanup() raise try: chroot.chroot(imgmnt, None, "/bin/env HOME=/root /bin/bash") except: raise errors.CreatorError("Failed to chroot to %s." % img) finally: chroot.cleanup_after_chroot("img", imgloop, None, imgmnt)
def do_create(self, subcmd, opts, *args): """${cmd_name}: create liveusb image Usage: ${name} ${cmd_name} <ksfile> [OPTS] ${cmd_option_list} """ if len(args) != 1: raise errors.Usage("Extra arguments given") creatoropts = configmgr.create ksconf = args[0] if creatoropts['runtime'] == "bootstrap": configmgr._ksconf = ksconf rt_util.bootstrap_mic() elif not rt_util.inbootstrap(): try: fs_related.find_binary_path('mic-native') except errors.CreatorError: if not msger.ask( "Subpackage \"mic-native\" has not been " "installed in your host system, still " "continue with \"native\" running mode?", False): raise errors.Abort("Abort because subpackage 'mic-native' " "has not been installed") if creatoropts['arch'] and creatoropts['arch'].startswith('arm'): msger.warning('liveusb cannot support arm images, Quit') return recording_pkgs = [] if len(creatoropts['record_pkgs']) > 0: recording_pkgs = creatoropts['record_pkgs'] if creatoropts['release'] is not None: if 'name' not in recording_pkgs: recording_pkgs.append('name') if 'vcs' not in recording_pkgs: recording_pkgs.append('vcs') configmgr._ksconf = ksconf # try to find the pkgmgr pkgmgr = None backends = pluginmgr.get_plugins('backend') if 'auto' == creatoropts['pkgmgr']: for key in configmgr.prefer_backends: if key in backends: pkgmgr = backends[key] break else: for key in backends.keys(): if key == creatoropts['pkgmgr']: pkgmgr = backends[key] break if not pkgmgr: raise errors.CreatorError( "Can't find backend: %s, " "available choices: %s" % (creatoropts['pkgmgr'], ','.join(backends.keys()))) creator = liveusb.LiveUSBImageCreator(creatoropts, pkgmgr) if len(recording_pkgs) > 0: creator._recording_pkgs = recording_pkgs self.check_image_exists(creator.destdir, creator.pack_to, [creator.name + ".usbimg"], creatoropts['release']) try: creator.check_depend_tools() creator.mount(None, creatoropts["cachedir"]) creator.install() creator.configure(creatoropts["repomd"]) creator.copy_kernel() creator.unmount() creator.package(creatoropts["destdir"]) creator.create_manifest() if creatoropts['release'] is not None: creator.release_output(ksconf, creatoropts['destdir'], creatoropts['release']) creator.print_outimage_info() except errors.CreatorError: raise finally: creator.cleanup() msger.info("Finished.") return 0
def do_create(self, subcmd, opts, *args): """${cmd_name}: create fs image Usage: ${name} ${cmd_name} <ksfile> [OPTS] ${cmd_option_list} """ if len(args) != 1: raise errors.Usage("Extra arguments given") creatoropts = configmgr.create ksconf = args[0] if creatoropts['runtime'] == 'bootstrap': configmgr._ksconf = ksconf rt_util.bootstrap_mic() recording_pkgs = [] if len(creatoropts['record_pkgs']) > 0: recording_pkgs = creatoropts['record_pkgs'] if creatoropts['release'] is not None: if 'name' not in recording_pkgs: recording_pkgs.append('name') if 'vcs' not in recording_pkgs: recording_pkgs.append('vcs') configmgr._ksconf = ksconf # Called After setting the configmgr._ksconf as the creatoropts['name'] is reset there. if creatoropts['release'] is not None: creatoropts['outdir'] = "%s/%s/images/%s/" % ( creatoropts['outdir'], creatoropts['release'], creatoropts['name']) # try to find the pkgmgr pkgmgr = None backends = pluginmgr.get_plugins('backend') if 'auto' == creatoropts['pkgmgr']: for key in configmgr.prefer_backends: if key in backends: pkgmgr = backends[key] break else: for key in backends.keys(): if key == creatoropts['pkgmgr']: pkgmgr = backends[key] break if not pkgmgr: raise errors.CreatorError( "Can't find backend: %s, " "available choices: %s" % (creatoropts['pkgmgr'], ','.join(backends.keys()))) creator = fs.FsImageCreator(creatoropts, pkgmgr) creator._include_src = opts.include_src if len(recording_pkgs) > 0: creator._recording_pkgs = recording_pkgs self.check_image_exists(creator.destdir, creator.pack_to, [creator.name], creatoropts['release']) try: creator.check_depend_tools() creator.mount(None, creatoropts["cachedir"]) creator.install() #Download the source packages ###private options if opts.include_src: installed_pkgs = creator.get_installed_packages() msger.info( '--------------------------------------------------') msger.info( 'Generating the image with source rpms included ...') if not misc.SrcpkgsDownload( installed_pkgs, creatoropts["repomd"], creator._instroot, creatoropts["cachedir"]): msger.warning("Source packages can't be downloaded") creator.configure(creatoropts["repomd"]) creator.copy_kernel() creator.unmount() creator.package(creatoropts["outdir"]) if creatoropts['release'] is not None: creator.release_output(ksconf, creatoropts['outdir'], creatoropts['release']) creator.print_outimage_info() except errors.CreatorError: raise finally: creator.cleanup() msger.info("Finished.") return 0
try: subprocess.call(args, preexec_fn=instance._chroot) except OSError, (err, msg): raise errors.CreatorError("Failed to run post cleanups: %s" % msg) convertoropts = configmgr.convert convertoropts['name'] = os.path.splitext(os.path.basename(base_on))[0] convertor = liveusb.LiveUSBImageCreator(convertoropts) imgtype = misc.get_image_type(base_on) if imgtype == "btrfsimg": fstype = "btrfs" elif imgtype in ("ext3fsimg", "ext4fsimg"): fstype = imgtype[:4] else: raise errors.CreatorError("Unsupported filesystem type: %s" % fstyp) convertor._set_fstype(fstype) try: convertor.mount(base_on) __mkinitrd(convertor) convertor._create_bootconfig() __run_post_cleanups(convertor) convertor.launch_shell(convertoropts['shell']) convertor.unmount() convertor.package() convertor.print_outimage_info() finally: shutil.rmtree(os.path.dirname(base_on), ignore_errors=True) @classmethod def do_unpack(cls, srcimg):
def do_create(self, subcmd, opts, *args): """${cmd_name}: create liveusb image Usage: ${name} ${cmd_name} <ksfile> [OPTS] ${cmd_option_list} """ if len(args) != 1: raise errors.Usage("Extra arguments given") creatoropts = configmgr.create ksconf = args[0] if creatoropts['runtime'] == "bootstrap": configmgr._ksconf = ksconf rt_util.bootstrap_mic() if creatoropts['arch'] and creatoropts['arch'].startswith('arm'): msger.warning('liveusb cannot support arm images, Quit') return recording_pkgs = [] if len(creatoropts['record_pkgs']) > 0: recording_pkgs = creatoropts['record_pkgs'] if creatoropts['release'] is not None: if 'name' not in recording_pkgs: recording_pkgs.append('name') if 'vcs' not in recording_pkgs: recording_pkgs.append('vcs') configmgr._ksconf = ksconf # Called After setting the configmgr._ksconf as the creatoropts['name'] is reset there. if creatoropts['release'] is not None: creatoropts['outdir'] = "%s/%s/images/%s/" % ( creatoropts['outdir'], creatoropts['release'], creatoropts['name']) # try to find the pkgmgr pkgmgr = None backends = pluginmgr.get_plugins('backend') if 'auto' == creatoropts['pkgmgr']: for key in configmgr.prefer_backends: if key in backends: pkgmgr = backends[key] break else: for key in backends.keys(): if key == creatoropts['pkgmgr']: pkgmgr = backends[key] break if not pkgmgr: raise errors.CreatorError( "Can't find backend: %s, " "available choices: %s" % (creatoropts['pkgmgr'], ','.join(backends.keys()))) creator = liveusb.LiveUSBImageCreator(creatoropts, pkgmgr) if len(recording_pkgs) > 0: creator._recording_pkgs = recording_pkgs self.check_image_exists(creator.destdir, creator.pack_to, [creator.name + ".usbimg"], creatoropts['release']) try: creator.check_depend_tools() creator.mount(None, creatoropts["cachedir"]) creator.install() creator.configure(creatoropts["repomd"]) creator.copy_kernel() creator.unmount() creator.package(creatoropts["outdir"]) if creatoropts['release'] is not None: creator.release_output(ksconf, creatoropts['outdir'], creatoropts['release']) creator.print_outimage_info() except errors.CreatorError: raise finally: creator.cleanup() msger.info("Finished.") return 0
def do_create(self, args): """${cmd_name}: create raw image Usage: ${name} ${cmd_name} <ksfile> [OPTS] ${cmd_option_list} """ creatoropts = configmgr.create ksconf = args.ksfile if creatoropts['runtime'] == "bootstrap": configmgr._ksconf = ksconf rt_util.bootstrap_mic() recording_pkgs = [] if len(creatoropts['record_pkgs']) > 0: recording_pkgs = creatoropts['record_pkgs'] if creatoropts['release'] is not None: if 'name' not in recording_pkgs: recording_pkgs.append('name') if 'vcs' not in recording_pkgs: recording_pkgs.append('vcs') configmgr._ksconf = ksconf # try to find the pkgmgr pkgmgr = None backends = pluginmgr.get_plugins('backend') if 'auto' == creatoropts['pkgmgr']: for key in configmgr.prefer_backends: if key in backends: pkgmgr = backends[key] break else: for key in backends.keys(): if key == creatoropts['pkgmgr']: pkgmgr = backends[key] break if not pkgmgr: raise errors.CreatorError( "Can't find backend: %s, " "available choices: %s" % (creatoropts['pkgmgr'], ','.join(backends.keys()))) creator = raw.RawImageCreator(creatoropts, pkgmgr, args.compress_image, args.generate_bmap, args.fstab_entry) if len(recording_pkgs) > 0: creator._recording_pkgs = recording_pkgs images = [ "%s-%s.raw" % (creator.name, disk_name) for disk_name in creator.get_disk_names() ] self.check_image_exists(creator.destdir, creator.pack_to, images, creatoropts['release']) try: creator.check_depend_tools() creator.mount(None, creatoropts["cachedir"]) creator.install() creator.configure(creatoropts["repomd"]) creator.copy_kernel() creator.unmount() creator.generate_bmap() creator.package(creatoropts["destdir"]) creator.create_manifest() if creatoropts['release'] is not None: creator.release_output(ksconf, creatoropts['destdir'], creatoropts['release']) creator.print_outimage_info() except errors.CreatorError: raise finally: creator.cleanup() #Run script of --run_script after image created if creatoropts['run_script']: cmd = creatoropts['run_script'] try: runner.show(cmd) except OSError, err: msger.warning(str(err))