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