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_emulator = misc.setup_qemu_emulator(chrootdir, "arm") elif arch == "mipsel": qemu_emulator = misc.setup_qemu_emulator(chrootdir, "mipsel") else: qemu_emulator = None 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 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 mount(self, base_on = None, cachedir = None): """Setup the target filesystem in preparation for an install. This function sets up the filesystem which the ImageCreator will install into and configure. The ImageCreator class merely creates an install root directory, bind mounts some system directories (e.g. /dev) and writes out /etc/fstab. Other subclasses may also e.g. create a sparse file, format it and loopback mount it to the install root. base_on -- a previous install on which to base this install; defaults to None, causing a new image to be created cachedir -- a directory in which to store the Yum cache; defaults to None, causing a new cache to be created; by setting this to another directory, the same cache can be reused across multiple installs. """ self.__ensure_builddir() fs.makedirs(self._instroot) fs.makedirs(self._outdir) self._mount_instroot(base_on) for d in ("/dev/pts", "/etc", "/boot", "/var/log", "/sys", "/proc", "/usr/bin"): fs.makedirs(self._instroot + d) if self.target_arch and self.target_arch.startswith("arm"): self.qemu_emulator = misc.setup_qemu_emulator(self._instroot, self.target_arch) self.get_cachedir(cachedir) # bind mount system directories into _instroot for (f, dest) in [("/sys", None), ("/proc", None), ("/proc/sys/fs/binfmt_misc", None), ("/dev/pts", None)]: self.__bindmounts.append(fs.BindChrootMount(f, self._instroot, dest)) self._do_bindmounts() self.__create_minimal_dev() if os.path.exists(self._instroot + "/etc/mtab"): os.unlink(self._instroot + "/etc/mtab") os.symlink("../proc/mounts", self._instroot + "/etc/mtab") self.__write_fstab() # get size of available space in 'instroot' fs self._root_fs_avail = misc.get_filesystem_avail(self._instroot)
def mount(self, base_on=None, cachedir=None): """Setup the target filesystem in preparation for an install. This function sets up the filesystem which the ImageCreator will install into and configure. The ImageCreator class merely creates an install root directory, bind mounts some system directories (e.g. /dev) and writes out /etc/fstab. Other subclasses may also e.g. create a sparse file, format it and loopback mount it to the install root. base_on -- a previous install on which to base this install; defaults to None, causing a new image to be created cachedir -- a directory in which to store the Yum cache; defaults to None, causing a new cache to be created; by setting this to another directory, the same cache can be reused across multiple installs. """ self.__setup_tmpdir() self.__ensure_builddir() # prevent popup dialog in Ubuntu(s) misc.hide_loopdev_presentation() fs.makedirs(self._instroot) fs.makedirs(self._outdir) self._mount_instroot(base_on) for d in ("/dev/pts", "/etc", "/boot", "/var/log", "/sys", "/proc", "/usr/bin"): fs.makedirs(self._instroot + d) if self.target_arch and self.target_arch.startswith("arm"): self.qemu_emulator = misc.setup_qemu_emulator( self._instroot, self.target_arch) self.get_cachedir(cachedir) # bind mount system directories into _instroot for (f, dest) in [("/sys", None), ("/proc", None), ("/proc/sys/fs/binfmt_misc", None), ("/dev/pts", None)]: self.__bindmounts.append( fs.BindChrootMount(f, self._instroot, dest)) self._do_bindmounts() self.__create_minimal_dev() if os.path.exists(self._instroot + "/etc/mtab"): os.unlink(self._instroot + "/etc/mtab") os.symlink("../proc/mounts", self._instroot + "/etc/mtab") self.__write_fstab() # get size of available space in 'instroot' fs self._root_fs_avail = misc.get_filesystem_avail(self._instroot)
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 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 chroot(chrootdir, bindmounts = None, execute = "/bin/bash"): def mychroot(): os.chroot(chrootdir) os.chdir("/") if configmgr.chroot['saveto']: savefs = True saveto = configmgr.chroot['saveto'] wrnmsg = "Can't save chroot fs for dir %s exists" % saveto if saveto == chrootdir: savefs = False wrnmsg = "Dir %s is being used to chroot" % saveto elif os.path.exists(saveto): if msger.ask("Dir %s already exists, cleanup and continue?" % saveto): shutil.rmtree(saveto, ignore_errors = True) savefs = True else: savefs = False if savefs: msger.info("Saving image to directory %s" % saveto) fs_related.makedirs(os.path.dirname(os.path.abspath(saveto))) runner.quiet("cp -af %s %s" % (chrootdir, saveto)) devs = ['dev/fd', 'dev/stdin', 'dev/stdout', 'dev/stderr', 'etc/mtab'] ignlst = [os.path.join(saveto, x) for x in devs] map(os.unlink, filter(os.path.exists, ignlst)) else: msger.warning(wrnmsg) 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 chroot(chrootdir, bindmounts=None, execute="/bin/bash"): def mychroot(): os.chroot(chrootdir) os.chdir("/") if configmgr.chroot['saveto']: savefs = True saveto = configmgr.chroot['saveto'] wrnmsg = "Can't save chroot fs for dir %s exists" % saveto if saveto == chrootdir: savefs = False wrnmsg = "Dir %s is being used to chroot" % saveto elif os.path.exists(saveto): if msger.ask("Dir %s already exists, cleanup and continue?" % saveto): shutil.rmtree(saveto, ignore_errors=True) savefs = True else: savefs = False if savefs: msger.info("Saving image to directory %s" % saveto) runner.quiet("cp -af %s %s" % (chrootdir, saveto)) devs = [ 'dev/fd', 'dev/stdin', 'dev/stdout', 'dev/stderr', 'etc/mtab' ] ignlst = [os.path.join(saveto, x) for x in devs] map(os.unlink, filter(os.path.exists, ignlst)) else: msger.warning(wrnmsg) 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))