asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 1 | #!/usr/bin/python2.6 |
| 2 | # |
| 3 | # Copyright 2010 Google Inc. All Rights Reserved. |
| 4 | |
| 5 | """Script to enter the ChromeOS chroot with mounted sources. |
| 6 | |
| 7 | This script enters the chroot with mounted sources. |
| 8 | """ |
| 9 | |
| 10 | __author__ = "asharif@google.com (Ahmad Sharif)" |
| 11 | |
| 12 | import getpass |
| 13 | import optparse |
| 14 | import os |
| 15 | import sys |
| 16 | from utils import utils |
| 17 | |
| 18 | # Common initializations |
| 19 | (rootdir, basename) = utils.GetRoot(sys.argv[0]) |
| 20 | utils.InitLogger(rootdir, basename) |
| 21 | |
| 22 | |
asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 23 | def Main(argv): |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 24 | """The main function.""" |
| 25 | parser = optparse.OptionParser() |
| 26 | parser.add_option("-c", "--chromeos_root", dest="chromeos_root", |
| 27 | help="ChromeOS root checkout directory.") |
| 28 | parser.add_option("-t", "--toolchain_root", dest="toolchain_root", |
| 29 | help="Toolchain root directory.") |
| 30 | |
asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 31 | (options, args) = parser.parse_args(argv) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 32 | |
| 33 | if options.chromeos_root is None: |
| 34 | chromeos_root = "../.." |
| 35 | else: |
| 36 | chromeos_root = options.chromeos_root |
| 37 | chromeos_root = os.path.abspath(chromeos_root) |
| 38 | |
| 39 | if (options.toolchain_root is None or |
| 40 | not os.path.exists(options.toolchain_root) or |
| 41 | not os.path.exists(chromeos_root)): |
| 42 | parser.print_help() |
| 43 | sys.exit(1) |
| 44 | |
| 45 | tc_dirs = [options.toolchain_root + "/google_vendor_src_branch/gcc"] |
| 46 | version_dir = rootdir |
| 47 | |
| 48 | all_dirs = tc_dirs[:] |
| 49 | all_dirs.append(rootdir) |
| 50 | |
| 51 | mounted_tc_root = "/usr/local/toolchain_root" |
| 52 | full_mounted_tc_root = chromeos_root + "/chroot/" + mounted_tc_root |
| 53 | full_mounted_tc_root = os.path.abspath(full_mounted_tc_root) |
| 54 | |
| 55 | # First create the mount points |
| 56 | CreateDir(full_mounted_tc_root, getpass.getuser()) |
| 57 | for d in all_dirs: |
| 58 | last_dir = utils.GetRoot(d)[1] |
| 59 | mounted_dir = (full_mounted_tc_root + "/" + last_dir) |
| 60 | CreateDir(mounted_dir, getpass.getuser()) |
| 61 | |
| 62 | # Now mount the toolchain directories. |
| 63 | for tc_dir in tc_dirs: |
| 64 | last_dir = utils.GetRoot(tc_dir)[1] |
| 65 | MountDir(tc_dir, full_mounted_tc_root + "/" + last_dir, "ro") |
| 66 | |
| 67 | # Next, mount the version directory. |
| 68 | last_dir = utils.GetRoot(version_dir)[1] |
| 69 | MountDir(version_dir, full_mounted_tc_root + "/" + last_dir) |
| 70 | |
| 71 | # Finally, create the symlink to build-gcc. |
| 72 | try: |
| 73 | os.symlink(last_dir + "/build-gcc", full_mounted_tc_root + "/build-gcc") |
| 74 | except Exception as e: |
| 75 | utils.main_logger.LogOutput(str(e)) |
| 76 | |
| 77 | # Now call enter_chroot with the rest of the arguments. |
| 78 | command = "./enter_chroot.sh" |
| 79 | |
asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 80 | if len(args) > 1: |
| 81 | command += " -- " + " ".join(args[1:]) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 82 | retval = utils.RunCommand(command) |
| 83 | return retval |
| 84 | else: |
| 85 | os.execv(command, [""]) |
| 86 | |
| 87 | |
| 88 | def MountDir(dir_name, mount_point, options=None): |
| 89 | command = "sudo mount --bind " + dir_name + " " + mount_point |
| 90 | if options == "ro": |
| 91 | command += " && sudo mount --bind -oremount,ro " + mount_point |
| 92 | retval = utils.RunCommand(command) |
| 93 | return retval |
| 94 | |
| 95 | |
| 96 | def CreateDir(dir_name, owner): |
| 97 | if not os.path.exists(dir_name): |
| 98 | command = "mkdir -p " + dir_name |
| 99 | command += " || sudo mkdir -p " + dir_name |
| 100 | retval = utils.RunCommand(command) |
| 101 | if retval != 0: |
| 102 | return retval |
| 103 | command = "sudo chown " + owner + " " + dir_name |
| 104 | retval = utils.RunCommand(command) |
| 105 | return retval |
| 106 | |
| 107 | |
| 108 | if __name__ == "__main__": |
asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 109 | Main(sys.argv) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 110 | |