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 |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame^] | 16 | from utils import command_executer |
| 17 | from utils import logger |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 18 | from utils import utils |
| 19 | |
| 20 | # Common initializations |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame^] | 21 | cmd_executer = command_executer.GetCommandExecuter() |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 22 | |
| 23 | |
asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 24 | def Main(argv): |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 25 | """The main function.""" |
| 26 | parser = optparse.OptionParser() |
| 27 | parser.add_option("-c", "--chromeos_root", dest="chromeos_root", |
| 28 | help="ChromeOS root checkout directory.") |
| 29 | parser.add_option("-t", "--toolchain_root", dest="toolchain_root", |
| 30 | help="Toolchain root directory.") |
| 31 | |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame^] | 32 | relevant_argv = [] |
| 33 | passthrough_argv = [] |
asharif | 0269d46 | 2013-02-15 04:46:31 +0000 | [diff] [blame] | 34 | for i in xrange(len(argv)): |
| 35 | found = False |
| 36 | for option in parser.option_list: |
| 37 | for long_opt in option._long_opts: |
| 38 | if argv[i].startswith(long_opt): |
| 39 | found = True |
| 40 | break |
| 41 | for short_opt in option._short_opts: |
| 42 | if argv[i].startswith(short_opt): |
| 43 | found = True |
| 44 | break |
| 45 | |
| 46 | if found == True: |
| 47 | break |
| 48 | |
| 49 | if found == True: |
| 50 | relevant_argv.append(argv[i]) |
| 51 | else: |
| 52 | passthrough_argv.append(argv[i]) |
| 53 | |
| 54 | options = parser.parse_args(relevant_argv)[0] |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 55 | |
| 56 | if options.chromeos_root is None: |
| 57 | chromeos_root = "../.." |
| 58 | else: |
| 59 | chromeos_root = options.chromeos_root |
asharif | 1762130 | 2013-02-15 04:46:35 +0000 | [diff] [blame] | 60 | |
| 61 | chromeos_root = os.path.expanduser(chromeos_root) |
| 62 | options.toolchain_root = os.path.expanduser(options.toolchain_root) |
| 63 | |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 64 | chromeos_root = os.path.abspath(chromeos_root) |
| 65 | |
| 66 | if (options.toolchain_root is None or |
| 67 | not os.path.exists(options.toolchain_root) or |
| 68 | not os.path.exists(chromeos_root)): |
| 69 | parser.print_help() |
| 70 | sys.exit(1) |
| 71 | |
| 72 | tc_dirs = [options.toolchain_root + "/google_vendor_src_branch/gcc"] |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame^] | 73 | rootdir = utils.GetRoot(sys.argv[0])[0] |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 74 | version_dir = rootdir |
| 75 | |
| 76 | all_dirs = tc_dirs[:] |
| 77 | all_dirs.append(rootdir) |
| 78 | |
| 79 | mounted_tc_root = "/usr/local/toolchain_root" |
| 80 | full_mounted_tc_root = chromeos_root + "/chroot/" + mounted_tc_root |
| 81 | full_mounted_tc_root = os.path.abspath(full_mounted_tc_root) |
| 82 | |
| 83 | # First create the mount points |
| 84 | CreateDir(full_mounted_tc_root, getpass.getuser()) |
| 85 | for d in all_dirs: |
| 86 | last_dir = utils.GetRoot(d)[1] |
| 87 | mounted_dir = (full_mounted_tc_root + "/" + last_dir) |
| 88 | CreateDir(mounted_dir, getpass.getuser()) |
| 89 | |
| 90 | # Now mount the toolchain directories. |
| 91 | for tc_dir in tc_dirs: |
| 92 | last_dir = utils.GetRoot(tc_dir)[1] |
| 93 | MountDir(tc_dir, full_mounted_tc_root + "/" + last_dir, "ro") |
| 94 | |
| 95 | # Next, mount the version directory. |
| 96 | last_dir = utils.GetRoot(version_dir)[1] |
| 97 | MountDir(version_dir, full_mounted_tc_root + "/" + last_dir) |
| 98 | |
| 99 | # Finally, create the symlink to build-gcc. |
| 100 | try: |
| 101 | os.symlink(last_dir + "/build-gcc", full_mounted_tc_root + "/build-gcc") |
| 102 | except Exception as e: |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame^] | 103 | logger.GetLogger().LogOutput(str(e)) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 104 | |
| 105 | # Now call enter_chroot with the rest of the arguments. |
asharif | fcf8cfc | 2013-02-15 04:49:21 +0000 | [diff] [blame] | 106 | command = chromeos_root + "/src/scripts/enter_chroot.sh" |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 107 | |
asharif | 0269d46 | 2013-02-15 04:46:31 +0000 | [diff] [blame] | 108 | if len(passthrough_argv) > 1: |
| 109 | command += " " + " ".join(passthrough_argv[1:]) |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame^] | 110 | retval = cmd_executer.RunCommand(command) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 111 | return retval |
| 112 | else: |
| 113 | os.execv(command, [""]) |
| 114 | |
| 115 | |
| 116 | def MountDir(dir_name, mount_point, options=None): |
| 117 | command = "sudo mount --bind " + dir_name + " " + mount_point |
| 118 | if options == "ro": |
| 119 | command += " && sudo mount --bind -oremount,ro " + mount_point |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame^] | 120 | retval = cmd_executer.RunCommand(command) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 121 | return retval |
| 122 | |
| 123 | |
| 124 | def CreateDir(dir_name, owner): |
| 125 | if not os.path.exists(dir_name): |
| 126 | command = "mkdir -p " + dir_name |
| 127 | command += " || sudo mkdir -p " + dir_name |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame^] | 128 | retval = cmd_executer.RunCommand(command) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 129 | if retval != 0: |
| 130 | return retval |
| 131 | command = "sudo chown " + owner + " " + dir_name |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame^] | 132 | retval = cmd_executer.RunCommand(command) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 133 | return retval |
| 134 | |
| 135 | |
| 136 | if __name__ == "__main__": |
asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 137 | Main(sys.argv) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 138 | |