#####################################################################
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
chrootdir)
def get_bindmounts(chrootdir, bindmounts = None):
+ """ calculate all bind mount entries for global usage """
# bindmounts should be a string like '/dev:/dev'
# FIXME: refine the bindmounts from string to dict
global chroot_bindmounts
#####################################################################
def bind_mount(chrootmounts):
+ """ perform bind mounting """
for b in chrootmounts:
msger.verbose("bind_mount: %s -> %s" % (b.src, b.dest))
b.mount()
def setup_resolv(chrootdir):
+ """ resolve network """
try:
shutil.copyfile("/etc/resolv.conf", chrootdir + "/etc/resolv.conf")
except:
pass
def setup_mtab(chrootdir):
+ """ adjust mount table """
mtab = "/etc/mtab"
dstmtab = chrootdir + mtab
if not os.path.islink(dstmtab):
shutil.copyfile(mtab, dstmtab)
def setup_chrootenv(chrootdir, bindmounts = None):
+ """ setup chroot environment """
global chroot_lock
# acquire the lock
######################################################################
def bind_unmount(chrootmounts):
+ """ perform bind unmounting """
for b in reversed(chrootmounts):
msger.verbose("bind_unmount: %s -> %s" % (b.src, b.dest))
b.unmount()
def cleanup_resolv(chrootdir):
+ """ clear resolv.conf """
try:
fd = open(chrootdir + "/etc/resolv.conf", "w")
fd.truncate(0)
pass
def kill_proc_inchroot(chrootdir):
+ """ kill all processes running inside chrootdir """
import glob
for fp in glob.glob("/proc/*/root"):
try:
pass
def cleanup_mtab(chrootdir):
+ """ remove mtab file """
if os.path.exists(chrootdir + "/etc/mtab"):
os.unlink(chrootdir + "/etc/mtab")
def cleanup_mounts(chrootdir):
+ """ clean up all mount entries owned by chrootdir """
umountcmd = misc.find_binary_path("umount")
mounts = open('/proc/mounts').readlines()
for line in reversed(mounts):
msger.warning("%s is not directory or is not empty" % point)
def cleanup_chrootenv(chrootdir, bindmounts=None, globalmounts=()):
+ """ clean up chroot environment """
# kill processes
kill_proc_inchroot(chrootdir)
# clean mtab
#####################################################################
def savefs_before_chroot(chrootdir, saveto = None):
+ """ backup chrootdir to another directory before chrooting in """
if configmgr.chroot['saveto']:
savefs = True
saveto = configmgr.chroot['saveto']
msger.warning(wrnmsg)
def cleanup_after_chroot(targettype, imgmount, tmpdir, tmpmnt):
+ """ clean up all temporary directories after chrooting """
if imgmount and targettype == "img":
imgmount.cleanup()
shutil.rmtree(tmpmnt, ignore_errors = True)
def chroot(chrootdir, bindmounts = None, execute = "/bin/bash"):
+ """ chroot the chrootdir and execute the command """
def mychroot():
os.chroot(chrootdir)
os.chdir("/")