def cleanup_chrootenv(chrootdir, bindmounts=None, globalmounts=[]): global chroot_lockfd, chroot_lock def bind_unmount(chrootmounts): chrootmounts.reverse() for b in chrootmounts: msger.info("bind_unmount: %s -> %s" % (b.src, b.dest)) b.unmount() def cleanup_resolv(chrootdir): try: fd = open(chrootdir + "/etc/resolv.conf", "w") fd.truncate(0) fd.close() except: pass def cleanup_mountdir(chrootdir, bindmounts): if bindmounts == "" or bindmounts == None: return chrootmounts = [] for mount in bindmounts.split(";"): if not mount: continue srcdst = mount.split(":") if len(srcdst) == 1: srcdst.append("none") if srcdst[1] == "" or srcdst[1] == "none": srcdst[1] = srcdst[0] srcdst[1] = os.path.abspath(os.path.expanduser(srcdst[1])) tmpdir = chrootdir + "/" + srcdst[1] if os.path.isdir(tmpdir): if len(os.listdir(tmpdir)) == 0: shutil.rmtree(tmpdir, ignore_errors=True) else: msger.warning("Warning: dir %s isn't empty." % tmpdir) chroot_lockfd.close() bind_unmount(globalmounts) if not fs_related.my_fuser(chroot_lock): tmpdir = chrootdir + "/parentroot" if len(os.listdir(tmpdir)) == 0: shutil.rmtree(tmpdir, ignore_errors=True) cleanup_resolv(chrootdir) kill_processes(chrootdir) cleanup_mountdir(chrootdir, bindmounts)
def cleanup_chrootenv(chrootdir, bindmounts = None, globalmounts = []): global chroot_lockfd, chroot_lock def bind_unmount(chrootmounts): chrootmounts.reverse() for b in chrootmounts: msger.info("bind_unmount: %s -> %s" % (b.src, b.dest)) b.unmount() def cleanup_resolv(chrootdir): try: fd = open(chrootdir + "/etc/resolv.conf", "w") fd.truncate(0) fd.close() except: pass def cleanup_mountdir(chrootdir, bindmounts): if bindmounts == "" or bindmounts == None: return chrootmounts = [] for mount in bindmounts.split(";"): if not mount: continue srcdst = mount.split(":") if len(srcdst) == 1: srcdst.append("none") if srcdst[1] == "" or srcdst[1] == "none": srcdst[1] = srcdst[0] srcdst[1] = os.path.abspath(os.path.expanduser(srcdst[1])) tmpdir = chrootdir + "/" + srcdst[1] if os.path.isdir(tmpdir): if len(os.listdir(tmpdir)) == 0: shutil.rmtree(tmpdir, ignore_errors = True) else: msger.warning("Warning: dir %s isn't empty." % tmpdir) chroot_lockfd.close() bind_unmount(globalmounts) if not fs_related.my_fuser(chroot_lock): tmpdir = chrootdir + "/parentroot" if len(os.listdir(tmpdir)) == 0: shutil.rmtree(tmpdir, ignore_errors = True) cleanup_resolv(chrootdir) kill_processes(chrootdir) cleanup_mountdir(chrootdir, bindmounts)
def cleanup_chrootenv(chrootdir, bindmounts=None, globalmounts=()): global chroot_lockfd, chroot_lock def bind_unmount(chrootmounts): for b in reversed(chrootmounts): msger.verbose("bind_unmount: %s -> %s" % (b.src, b.dest)) b.unmount() def cleanup_resolv(chrootdir): try: fd = open(chrootdir + "/etc/resolv.conf", "w") fd.truncate(0) fd.close() except: pass def kill_processes(chrootdir): import glob for fp in glob.glob("/proc/*/root"): try: if os.readlink(fp) == chrootdir: pid = int(fp.split("/")[2]) os.kill(pid, 9) except: pass def cleanup_mountdir(chrootdir, bindmounts): if bindmounts == "" or bindmounts == None: return chrootmounts = [] for mount in bindmounts.split(";"): if not mount: continue srcdst = mount.split(":") if len(srcdst) == 1: srcdst.append("none") if srcdst[0] == "/": continue if srcdst[1] == "" or srcdst[1] == "none": srcdst[1] = srcdst[0] srcdst[1] = os.path.abspath(os.path.expanduser(srcdst[1])) tmpdir = chrootdir + "/" + srcdst[1] if os.path.isdir(tmpdir): if len(os.listdir(tmpdir)) == 0: shutil.rmtree(tmpdir, ignore_errors = True) else: msger.warning("Warning: dir %s isn't empty." % tmpdir) chroot_lockfd.close() bind_unmount(globalmounts) if not fs_related.my_fuser(chroot_lock): tmpdir = chrootdir + "/parentroot" if os.path.exists(tmpdir) and len(os.listdir(tmpdir)) == 0: shutil.rmtree(tmpdir, ignore_errors = True) cleanup_resolv(chrootdir) if os.path.exists(chrootdir + "/etc/mtab"): os.unlink(chrootdir + "/etc/mtab") kill_processes(chrootdir) cleanup_mountdir(chrootdir, bindmounts)
def cleanup_chrootenv(chrootdir, bindmounts=None, globalmounts=()): global chroot_lockfd, chroot_lock def bind_unmount(chrootmounts): for b in reversed(chrootmounts): msger.verbose("bind_unmount: %s -> %s" % (b.src, b.dest)) b.unmount() def cleanup_resolv(chrootdir): try: fd = open(chrootdir + "/etc/resolv.conf", "w") fd.truncate(0) fd.close() except: pass def kill_processes(chrootdir): import glob for fp in glob.glob("/proc/*/root"): try: if os.readlink(fp) == chrootdir: pid = int(fp.split("/")[2]) os.kill(pid, 9) except: pass def cleanup_mountdir(chrootdir, bindmounts): if bindmounts == "" or bindmounts == None: return chrootmounts = [] for mount in bindmounts.split(";"): if not mount: continue srcdst = mount.split(":") if len(srcdst) == 1: srcdst.append("none") if srcdst[0] == "/": continue if srcdst[1] == "" or srcdst[1] == "none": srcdst[1] = srcdst[0] srcdst[1] = os.path.abspath(os.path.expanduser(srcdst[1])) tmpdir = chrootdir + "/" + srcdst[1] if os.path.isdir(tmpdir): if len(os.listdir(tmpdir)) == 0: shutil.rmtree(tmpdir, ignore_errors=True) else: msger.warning("Warning: dir %s isn't empty." % tmpdir) chroot_lockfd.close() bind_unmount(globalmounts) if not fs_related.my_fuser(chroot_lock): tmpdir = chrootdir + "/parentroot" if os.path.exists(tmpdir) and len(os.listdir(tmpdir)) == 0: shutil.rmtree(tmpdir, ignore_errors=True) cleanup_resolv(chrootdir) if os.path.exists(chrootdir + "/etc/mtab"): os.unlink(chrootdir + "/etc/mtab") kill_processes(chrootdir) cleanup_mountdir(chrootdir, bindmounts)