cmd_chroot.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/python -tt
  2. # vim: ai ts=4 sts=4 et sw=4
  3. #
  4. # Copyright (c) 2012 Intel, Inc.
  5. #
  6. # This program is free software; you can redistribute it and/or modify it
  7. # under the terms of the GNU General Public License as published by the Free
  8. # Software Foundation; version 2 of the License
  9. #
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. # for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along
  16. # with this program; if not, write to the Free Software Foundation, Inc., 59
  17. # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. """Implementation of subcmd: chroot
  19. """
  20. import os
  21. import subprocess
  22. from gitbuildsys.errors import GbsError
  23. from gitbuildsys.log import LOGGER as log
  24. def mountHostdirsIntoBuildroot(hostdirs, build_root, silent):
  25. for hostdir in hostdirs:
  26. if not os.access(hostdir, os.W_OK):
  27. raise GbsError('security issue: can not mount %s directory - user %s does not have write access to it' % (hostdir, os.getlogin()))
  28. for hostdir in hostdirs:
  29. if not silent:
  30. log.info('creating %s directory if not exists' % build_root + \
  31. hostdir)
  32. try:
  33. subprocess.call(['sudo', '/bin/mkdir', '-p', build_root + \
  34. hostdir])
  35. except:
  36. log.warning('failed to create %s directory' % build_root + \
  37. hostdir)
  38. if not silent:
  39. log.info('mounting %s directory into %s' % (hostdir,build_root +
  40. hostdir))
  41. try:
  42. subprocess.call(['sudo', '/bin/mount', '-o', 'bind', hostdir, build_root +
  43. hostdir])
  44. except:
  45. log.warning('failed to mount %s directory into %s' % (hostdir, build_root + \
  46. hostdir))
  47. def umountAll(hostdirs, build_root, silent):
  48. for hostdir in reversed(hostdirs):
  49. if not silent:
  50. log.info('umounting %s directory' % (build_root + hostdir))
  51. try:
  52. subprocess.call(['sudo', '/bin/umount', '-l', build_root + \
  53. hostdir])
  54. except:
  55. log.warning('failed to umount %s directory' % build_root + \
  56. hostdir)
  57. def main(args):
  58. """gbs chroot entry point."""
  59. build_root = args.buildroot
  60. silent = False
  61. if args.silent:
  62. silent = args.silent
  63. running_lock = '%s/not-ready' % build_root
  64. if os.path.exists(running_lock):
  65. raise GbsError('build root %s is not ready' % build_root)
  66. if not silent:
  67. log.info('chroot %s' % build_root)
  68. user = 'abuild'
  69. if args.root:
  70. user = 'root'
  71. cmd = ['sudo', 'chroot', build_root, 'su', user]
  72. try:
  73. subprocess.call(['sudo', 'cp', '/etc/resolv.conf', build_root + \
  74. '/etc/resolv.conf'])
  75. except OSError:
  76. log.warning('failed to setup /etc/resolv.conf')
  77. if args.mount:
  78. mountHostdirsIntoBuildroot(args.mount, build_root, silent)
  79. try:
  80. build_env = os.environ
  81. build_env['PS1'] = "(tizen-build-env)@\h \W]\$ "
  82. subprocess.call(cmd, env=build_env)
  83. except OSError, err:
  84. raise GbsError('failed to chroot to %s: %s' % (build_root, err))
  85. except KeyboardInterrupt:
  86. log.info('keyboard interrupt ...')
  87. finally:
  88. if args.mount:
  89. umountAll(args.mount, build_root, silent)