def bootstrap(self): # Lets build the multistrap configuration file par = SafeConfigParser() par.add_section("General") par.set("General", "arch", config.arch()) par.set("General", "directory", config.rootfs()) par.set("General", "cleanup", str(True)) par.set("General", "noauth", str(not config.auth())) keyrings = config.keyrings() par.set("General", "bootstrap", " ".join(keyrings.keys())) repros = config.repros() par.set("General", "aptsources", " ".join(repros)) for repro in config.repros(): par.add_section(repro) par.set(repro, "keyring", keyrings[repro]) par.set(repro, "packages", "dash apt apt-utils " + " ".join(config.early_packages()) + " ".join(config.packages())) par.set(repro, "source", config.source(repro)) par.set(repro, "suite", config.suite(repro)) par.set(repro, "components", " ".join(config.components(repro))) par.write(self._msfile) self._msfile.close() r = host.runner cmds = ["multistrap", "-f", self._msfile.name] if r.run(cmds, stdoutfunc=self._stdout, stderrfunc=self._stderr) != 0: raise RaptzException("Multistrap main stage failed")
def configure(self): cfgroot = config.confpath("conf") srcroot, dirs, files = os.walk(cfgroot).next() ch = host.runner i = 0 for d in dirs: src = os.path.join(srcroot, d) dst = tempfile.mkdtemp(dir=config.rootfs("/tmp")) dstinit = config.rmrootfs(os.path.join(dst, "init.sh")) dstarg = config.rmrootfs(dst) self.copy2rootfs(src, dstarg) ret = ch.chroot([dstinit, dstarg], stdoutfunc=self._stdout, stderrfunc=self._stdout) # Run dev scripts if avalible, disabel as of now #dstinit = config.rmrootfs(os.path.join(dst, "init.dev.sh")) #if config.args.dev and os.path.isdir(config.rootfs(dstinit)): # dstarg = config.rmrootfs(dst) # ret = ch.chroot([dstinit, dstarg], # stdoutfunc=self._stdout, # stderrfunc=self._stdout) shutil.rmtree(dst) if ret != 0: raise RaptzException("Configure " + d + " failed") i+=1
def bind(self, path): mp = config.rootfs(path) if not os.path.isdir(mp): os.mkdir(mp) r = self._host.runner ret = r.run(["mount", "--bind", path, mp]) == 0 return ret
def bound(self, path): mp = config.rootfs(path) f = open("/proc/mounts", "r") for line in f: if line.split()[1] == mp: return True return False
def bootstrap(self): """ Will install using debootstrap """ cmds=["debootstrap", "--variant="+self._variant] if config.arch(): cmds.append("--foreign") cmds.append("--arch="+config.arch()) if not config.auth(): cmds.append("--no-check-gpg") keyrings = config.keyrings() for keyring in keyrings: cmds.append("--keyring="+keyring) cmds.append(config.suite()) cmds.append(config.rootfs()) cmds.append(config.source()) cmds.append("/usr/share/debootstrap/scripts/testing") r = host.runner first = [cmds[0], "--print-debs", "--keep-debootstrap-dir" ] + cmds[1:] if r.run(first, stdoutfunc=self._setpaks, stderrfunc=self._stderr) != 0: raise RaptzException("Debootstrap pre stage failed") if r.run(cmds, stdoutfunc=self._stdout, stderrfunc=self._stderr) != 0: raise RaptzException("Debootstrap main stage failed")
def setup(self): self.copy2rootfs(config.confpath("root")) debconfsrc=config.confpath("debconf.cfg") if os.path.isfile(config.confpath("debconf.cfg")): debconfdst=config.rootfs("/tmp/debconf.cfg") shutil.copy2(debconfsrc, debconfdst) if host.runner.chroot(["debconf-set-selections", "-v", "/tmp/debconf.cfg"]): raise RaptzException("Debconf failed") os.unlink(debconfdst)
def copy2rootfs(self, src, dst="/"): for srcroot, dirs, files in os.walk(src): dstroot = config.rootfs(dst + srcroot[len(src):]) for d in dirs: dstd = os.path.join(dstroot, d) if not os.path.isdir(dstd): os.mkdir(dstd) for f in files: srcf = os.path.join(srcroot, f) dstf = os.path.join(dstroot, f) shutil.copy2(srcf, dstf)
def mknod(self, name, mode, type, maj, min): path = config.rootfs("/dev/"+name) cmd = [ "mknod", "-m", "%o" % mode, path ] if type == S_IFCHR: cmd.append("c") else: cmd.append("b") cmd.append(str(maj)) cmd.append(str(min)) r = self._host.runner r.run(cmd)
def run(self, cmds, env={}, stdoutfunc=None, stderrfunc=None, *kargs): envfile = config.rootfs("/lib/fake.env") fakecmd = [ "fakechroot", "fakeroot", "-s", envfile, "-i", envfile, ] #"-c", "fcr", cmds = fakecmd + cmds env["FAKECHROOT_EXCLUDE_PATH"] = self._host.fs.binds() return ChRoot.run(self, cmds, env, stdoutfunc, stderrfunc, *kargs)
def chroot(self, cmds, env={}, stdoutfunc=None, stderrfunc=None, *kargs): self._ch_setup() ch = [ "chroot", config.rootfs() ] cmds = ch + cmds oenv = os.environ oenv["HOME"]="/root" oenv["LANG"]="C" for e in env: oenv[e] = env[e] ret = self.run(cmds, oenv, stdoutfunc, stderrfunc, *kargs) self._ch_setdown() return ret
def finalize(self): listd = config.rootfs("/etc/apt/sources.list.d") if not os.path.isdir(listd): os.makedirs(listd) for repro in config.repros(): listfile = os.path.join(listd, repro.lower() + ".list") source = config.source(repro) suite = config.suite(repro) comp = " ".join(config.components(repro)) f = open(listfile, "w") f.write("deb %s %s %s\n" % (source, suite, comp)) if config.addsrcs(repro): f.write("deb-src %s %s %s\n" % (source, suite, comp))
def _ch_setup(self): shutil.copy(progs.get(QEMU_BIN), self._qemudst) open(self._rcddst, "w").write(self.RCDFILE_CONTENT) os.chmod(self._rcddst, 0755) # resolv.conf handling # If there is a resolv.conf move it to resolv.conf.real # Move host resolv.conf to target resolv.conf and store its md5 in self._resolvhash tconf=config.rootfs(RESOLV_CONF_PATH) if os.path.lexists(tconf): os.rename(tconf, tconf+".real") shutil.copy(RESOLV_CONF_PATH, tconf) open(tconf, "a").write(""" # This is the Hosts resolv.conf. # If changed in any way the target resolv.conf will not be restored. """) self._resolvhash = hashfile(tconf)
def copy_ld(self): libdir=config.rootfs("lib") ld = os.path.join(libdir, "ld-linux.so.3") lddst = os.path.join(libdir, os.readlink(ld)) ldln = "/lib/ld-linux.so.3" try: ldsrc = os.readlink(ldln) if ldsrc == lddst: return True except: pass print "Change" cmd=["ln", "-sf", lddst, ldln] if os.getuid() != 0: print "SUDO" cmd = ["sudo"] + cmd return call(cmd) == 0
def _ch_teardown(self): # resolv.conf handling # Check that resolv.conf is a file and hash matches stored hash and is a file # if there is change. Leave as is and remove resolv.conf.real # if there is not change. Restore .real file if it exists tconf = config.rootfs(RESOLV_CONF_PATH) if not os.path.islink(tconf) and hashfile(tconf) == self._resolvhash: os.unlink(tconf) if os.path.lexists(tconf+".real"): os.rename(tconf+".real", tconf) else: if os.path.lexists(tconf+".real"): os.unlink(tconf + ".real") del self._resolvhash if os.path.isfile(self._rcddst): os.unlink(self._rcddst) if os.path.isfile(self._qemudst): os.unlink(self._qemudst)
def configure(self): cfgroot = config.confpath("conf") srcroot, dirs, files = os.walk(cfgroot).next() ch = host.runner i = 0 for d in dirs: src = os.path.join(srcroot, d) dst = tempfile.mkdtemp(dir=config.rootfs("/tmp")) dstinit = config.rmrootfs(os.path.join(dst, "init.sh")) dstarg = config.rmrootfs(dst) self.copy2rootfs(src, dstarg) ret = ch.chroot([dstinit, dstarg], stdoutfunc=self._stdout, stderrfunc=self._stdout) shutil.rmtree(dst) if ret != 0: raise RaptzException("Configure " + d + " failed") i+=1
def bootstrap(self): """ Will install using debootstrap """ cmds=["cdebootstrap", "--flavour="+self._flavour] if config.arch(): cmds.append("--foreign") cmds.append("--arch="+config.arch()) if not config.auth(): cmds.append("--allow-unauthenticated") keyrings = config.keyrings() for keyring in keyrings: cmds.append("--keyring="+keyring) cmds.append(config.suite()) cmds.append(config.rootfs()) cmds.append(config.source()) # cmds.append("/usr/share/debootstrap/scripts/testing") r = host.runner if r.run(cmds, stdoutfunc=self._stdout, stderrfunc=self._stderr) != 0: raise RaptzException("Debootstrap main stage failed")
def mknod(self, name, mode, type, maj, min): path = config.rootfs("/dev/"+name) if not os.path.exists(path): os.mknod(path, mode | type, os.makedev(maj, min))
def unbind(self, path): mp = config.rootfs(path) r = self._host.runner return r.run(["umount", mp]) == 0
def __init__(self, host): Fs.__init__(self, host) progs.register("mount"); progs.register("umount"); atexit.register(umount_all, config.rootfs())
def __init__(self, host): self._host = host self._rcddst = config.rootfs("/usr/sbin/policy-rc.d") progs.register(QEMU_BIN) progs.register("chroot") self._qemudst = config.rootfs(progs.get(QEMU_BIN))
def symlink(self, src, dst): dst = config.rootfs(dst) if not os.path.exists(dst): os.symlink(src, dst)
def finalize(self): listd = config.rootfs("/etc/apt/sources.list.d") if os.path.isdir(listd): shutil.rmtree(listd) Bootstrap.finalize(self)
def clean(self): if os.path.isdir(config.rootfs()): shutil.rmtree(config.rootfs())