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 | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame^] | 24 | class MountPoint: |
| 25 | def __init__(self, external_dir, mount_dir, owner, options=None): |
| 26 | self.external_dir = external_dir |
| 27 | self.mount_dir = mount_dir |
| 28 | self.owner = owner |
| 29 | self.options = options |
| 30 | |
| 31 | |
| 32 | def CreateMountPoint(self): |
| 33 | if not os.path.exists(self.mount_dir): |
| 34 | command = "mkdir -p " + self.mount_dir |
| 35 | command += " || sudo mkdir -p " + self.mount_dir |
| 36 | retval = cmd_executer.RunCommand(command) |
| 37 | if retval != 0: |
| 38 | return retval |
| 39 | command = "sudo chown " + self.owner + " " + self.mount_dir |
| 40 | retval = cmd_executer.RunCommand(command) |
| 41 | return retval |
| 42 | |
| 43 | |
| 44 | def DoMount(self): |
| 45 | self.CreateMountPoint() |
| 46 | self.MountDir() |
| 47 | |
| 48 | |
| 49 | def MountDir(self): |
| 50 | command = "sudo mount --bind " + self.external_dir + " " + self.mount_dir |
| 51 | if self.options == "ro": |
| 52 | command += " && sudo mount --bind -oremount,ro " + self.mount_dir |
| 53 | retval = cmd_executer.RunCommand(command) |
| 54 | return retval |
| 55 | |
| 56 | |
| 57 | def __str__(self): |
| 58 | ret = "" |
| 59 | ret += self.external_dir + "\n" |
| 60 | ret += self.mount_dir + "\n" |
| 61 | if self.owner: |
| 62 | ret += self.owner + "\n" |
| 63 | if self.options: |
| 64 | ret += self.options + "\n" |
| 65 | return ret |
| 66 | |
| 67 | |
| 68 | def Main(argv, return_output=False): |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 69 | """The main function.""" |
| 70 | parser = optparse.OptionParser() |
| 71 | parser.add_option("-c", "--chromeos_root", dest="chromeos_root", |
| 72 | help="ChromeOS root checkout directory.") |
| 73 | parser.add_option("-t", "--toolchain_root", dest="toolchain_root", |
| 74 | help="Toolchain root directory.") |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame^] | 75 | parser.add_option("-o", "--other_mounts", dest="other_mounts", |
| 76 | help="Other mount points in the form: " + |
| 77 | "dir:mounted_dir:options") |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 78 | |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 79 | relevant_argv = [] |
| 80 | passthrough_argv = [] |
asharif | 0269d46 | 2013-02-15 04:46:31 +0000 | [diff] [blame] | 81 | for i in xrange(len(argv)): |
| 82 | found = False |
| 83 | for option in parser.option_list: |
| 84 | for long_opt in option._long_opts: |
| 85 | if argv[i].startswith(long_opt): |
| 86 | found = True |
| 87 | break |
| 88 | for short_opt in option._short_opts: |
| 89 | if argv[i].startswith(short_opt): |
| 90 | found = True |
| 91 | break |
| 92 | |
| 93 | if found == True: |
| 94 | break |
| 95 | |
| 96 | if found == True: |
| 97 | relevant_argv.append(argv[i]) |
| 98 | else: |
| 99 | passthrough_argv.append(argv[i]) |
| 100 | |
| 101 | options = parser.parse_args(relevant_argv)[0] |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 102 | |
| 103 | if options.chromeos_root is None: |
| 104 | chromeos_root = "../.." |
| 105 | else: |
| 106 | chromeos_root = options.chromeos_root |
asharif | 1762130 | 2013-02-15 04:46:35 +0000 | [diff] [blame] | 107 | |
| 108 | chromeos_root = os.path.expanduser(chromeos_root) |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame^] | 109 | if options.toolchain_root: |
| 110 | options.toolchain_root = os.path.expanduser(options.toolchain_root) |
asharif | 1762130 | 2013-02-15 04:46:35 +0000 | [diff] [blame] | 111 | |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 112 | chromeos_root = os.path.abspath(chromeos_root) |
| 113 | |
| 114 | if (options.toolchain_root is None or |
| 115 | not os.path.exists(options.toolchain_root) or |
| 116 | not os.path.exists(chromeos_root)): |
| 117 | parser.print_help() |
| 118 | sys.exit(1) |
| 119 | |
| 120 | tc_dirs = [options.toolchain_root + "/google_vendor_src_branch/gcc"] |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 121 | rootdir = utils.GetRoot(sys.argv[0])[0] |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 122 | version_dir = rootdir |
| 123 | |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 124 | mounted_tc_root = "/usr/local/toolchain_root" |
| 125 | full_mounted_tc_root = chromeos_root + "/chroot/" + mounted_tc_root |
| 126 | full_mounted_tc_root = os.path.abspath(full_mounted_tc_root) |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame^] | 127 | |
| 128 | mount_points = [] |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 129 | for tc_dir in tc_dirs: |
| 130 | last_dir = utils.GetRoot(tc_dir)[1] |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame^] | 131 | mount_point = MountPoint(tc_dir, full_mounted_tc_root + "/" + last_dir, |
| 132 | getpass.getuser(), "ro") |
| 133 | mount_points.append(mount_point) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 134 | |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame^] | 135 | mount_points += CreateMountPointsFromString(options.other_mounts, |
| 136 | chromeos_root + "/chroot/") |
| 137 | |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 138 | last_dir = utils.GetRoot(version_dir)[1] |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame^] | 139 | mount_point = MountPoint(version_dir, full_mounted_tc_root + "/" + last_dir, |
| 140 | getpass.getuser()) |
| 141 | mount_points.append(mount_point) |
| 142 | |
| 143 | for mount_point in mount_points: |
| 144 | mount_point.DoMount() |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 145 | |
| 146 | # Finally, create the symlink to build-gcc. |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame^] | 147 | command = "sudo chown " + getpass.getuser() + " " + full_mounted_tc_root |
| 148 | retval = cmd_executer.RunCommand(command) |
| 149 | |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 150 | try: |
| 151 | os.symlink(last_dir + "/build-gcc", full_mounted_tc_root + "/build-gcc") |
| 152 | except Exception as e: |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 153 | logger.GetLogger().LogOutput(str(e)) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 154 | |
| 155 | # Now call enter_chroot with the rest of the arguments. |
asharif | fcf8cfc | 2013-02-15 04:49:21 +0000 | [diff] [blame] | 156 | command = chromeos_root + "/src/scripts/enter_chroot.sh" |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 157 | |
asharif | 0269d46 | 2013-02-15 04:46:31 +0000 | [diff] [blame] | 158 | if len(passthrough_argv) > 1: |
| 159 | command += " " + " ".join(passthrough_argv[1:]) |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame^] | 160 | retval = cmd_executer.RunCommand(command, return_output) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 161 | return retval |
| 162 | else: |
| 163 | os.execv(command, [""]) |
| 164 | |
| 165 | |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame^] | 166 | def CreateMountPointsFromString(mount_strings, chroot_dir): |
| 167 | # String has options in the form dir:mount:options |
| 168 | mount_points = [] |
| 169 | if not mount_strings: |
| 170 | return mount_points |
| 171 | mount_list = mount_strings.split() |
| 172 | for mount_string in mount_list: |
| 173 | mount_values = mount_string.split(":") |
| 174 | external_dir = mount_values[0] |
| 175 | mount_dir = mount_values[1] |
| 176 | if len(mount_values)>2: |
| 177 | options = mount_values[2] |
| 178 | else: |
| 179 | options = None |
| 180 | mount_point = MountPoint(external_dir, chroot_dir + "/" + mount_dir, |
| 181 | getpass.getuser(), options) |
| 182 | mount_points.append(mount_point) |
| 183 | return mount_points |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 184 | |
| 185 | |
| 186 | if __name__ == "__main__": |
asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 187 | Main(sys.argv) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 188 | |