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