123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- #!/usr/bin/python -tt
- # vim: ai ts=4 sts=4 et sw=4
- #
- # Copyright (c) 2012 Intel, Inc.
- #
- # This program is free software; you can redistribute it and/or modify it
- # under the terms of the GNU General Public License as published by the Free
- # Software Foundation; version 2 of the License
- #
- # This program is distributed in the hope that it will be useful, but
- # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- # for more details.
- #
- # You should have received a copy of the GNU General Public License along
- # with this program; if not, write to the Free Software Foundation, Inc., 59
- # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- """Implementation of subcmd: chroot
- """
- import os
- import subprocess
- from gitbuildsys.errors import GbsError
- from gitbuildsys.log import LOGGER as log
- def mountHostdirsIntoBuildroot(hostdirs, build_root, silent):
- for hostdir in hostdirs:
- if not os.access(hostdir, os.W_OK):
- raise GbsError('security issue: can not mount %s directory - user %s does not have write access to it' % (hostdir, os.getlogin()))
- for hostdir in hostdirs:
- if not silent:
- log.info('creating %s directory if not exists' % build_root + \
- hostdir)
- try:
- subprocess.call(['sudo', '/bin/mkdir', '-p', build_root + \
- hostdir])
- except:
- log.warning('failed to create %s directory' % build_root + \
- hostdir)
- if not silent:
- log.info('mounting %s directory into %s' % (hostdir,build_root +
- hostdir))
- try:
- subprocess.call(['sudo', '/bin/mount', '-o', 'bind', hostdir, build_root +
- hostdir])
- except:
- log.warning('failed to mount %s directory into %s' % (hostdir, build_root + \
- hostdir))
- def umountAll(hostdirs, build_root, silent):
- for hostdir in reversed(hostdirs):
- if not silent:
- log.info('umounting %s directory' % (build_root + hostdir))
- try:
- subprocess.call(['sudo', '/bin/umount', '-l', build_root + \
- hostdir])
- except:
- log.warning('failed to umount %s directory' % build_root + \
- hostdir)
- def main(args):
- """gbs chroot entry point."""
- build_root = args.buildroot
- silent = False
- if args.silent:
- silent = args.silent
- running_lock = '%s/not-ready' % build_root
- if os.path.exists(running_lock):
- raise GbsError('build root %s is not ready' % build_root)
- if not silent:
- log.info('chroot %s' % build_root)
- user = 'abuild'
- if args.root:
- user = 'root'
- cmd = ['sudo', 'chroot', build_root, 'su', user]
- try:
- subprocess.call(['sudo', 'cp', '/etc/resolv.conf', build_root + \
- '/etc/resolv.conf'])
- except OSError:
- log.warning('failed to setup /etc/resolv.conf')
- if args.mount:
- mountHostdirsIntoBuildroot(args.mount, build_root, silent)
- try:
- build_env = os.environ
- build_env['PS1'] = "(tizen-build-env)@\h \W]\$ "
- subprocess.call(cmd, env=build_env)
- except OSError, err:
- raise GbsError('failed to chroot to %s: %s' % (build_root, err))
- except KeyboardInterrupt:
- log.info('keyboard interrupt ...')
- finally:
- if args.mount:
- umountAll(args.mount, build_root, silent)
|