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 |
asharif | 556f4ff | 2013-02-15 04:50:35 +0000 | [diff] [blame] | 15 | import pwd |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 16 | import sys |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 17 | from utils import command_executer |
| 18 | from utils import logger |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 19 | from utils import utils |
| 20 | |
| 21 | # Common initializations |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 22 | cmd_executer = command_executer.GetCommandExecuter() |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 23 | |
| 24 | |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 25 | class MountPoint: |
| 26 | def __init__(self, external_dir, mount_dir, owner, options=None): |
| 27 | self.external_dir = external_dir |
| 28 | self.mount_dir = mount_dir |
| 29 | self.owner = owner |
| 30 | self.options = options |
| 31 | |
| 32 | |
asharif | 556f4ff | 2013-02-15 04:50:35 +0000 | [diff] [blame] | 33 | def CreateAndOwnDir(self, dir_name): |
| 34 | retval = 0 |
| 35 | if not os.path.exists(dir_name): |
| 36 | command = "mkdir -p " + dir_name |
| 37 | command += " || sudo mkdir -p " + dir_name |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 38 | retval = cmd_executer.RunCommand(command) |
asharif | 556f4ff | 2013-02-15 04:50:35 +0000 | [diff] [blame] | 39 | if retval != 0: |
| 40 | return retval |
| 41 | pw = pwd.getpwnam(self.owner) |
| 42 | if os.stat(dir_name).st_uid != pw.pw_uid: |
| 43 | command = "sudo chown -f " + self.owner + " " + dir_name |
| 44 | retval = cmd_executer.RunCommand(command) |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 45 | return retval |
| 46 | |
| 47 | |
| 48 | def DoMount(self): |
asharif | 556f4ff | 2013-02-15 04:50:35 +0000 | [diff] [blame] | 49 | retval = self.CreateAndOwnDir(self.mount_dir) |
| 50 | utils.AssertTrue(retval == 0) |
| 51 | retval = self.CreateAndOwnDir(self.external_dir) |
| 52 | utils.AssertTrue(retval == 0) |
| 53 | retval = self.MountDir() |
| 54 | utils.AssertTrue(retval == 0) |
| 55 | return retval |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 56 | |
| 57 | |
| 58 | def MountDir(self): |
| 59 | command = "sudo mount --bind " + self.external_dir + " " + self.mount_dir |
| 60 | if self.options == "ro": |
| 61 | command += " && sudo mount --bind -oremount,ro " + self.mount_dir |
| 62 | retval = cmd_executer.RunCommand(command) |
| 63 | return retval |
| 64 | |
| 65 | |
| 66 | def __str__(self): |
| 67 | ret = "" |
| 68 | ret += self.external_dir + "\n" |
| 69 | ret += self.mount_dir + "\n" |
| 70 | if self.owner: |
| 71 | ret += self.owner + "\n" |
| 72 | if self.options: |
| 73 | ret += self.options + "\n" |
| 74 | return ret |
| 75 | |
| 76 | |
| 77 | def Main(argv, return_output=False): |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 78 | """The main function.""" |
| 79 | parser = optparse.OptionParser() |
| 80 | parser.add_option("-c", "--chromeos_root", dest="chromeos_root", |
asharif | 0b2f040 | 2013-02-15 04:50:25 +0000 | [diff] [blame] | 81 | default="../..", |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 82 | help="ChromeOS root checkout directory.") |
| 83 | parser.add_option("-t", "--toolchain_root", dest="toolchain_root", |
| 84 | help="Toolchain root directory.") |
asharif | 541b639 | 2013-02-15 04:50:38 +0000 | [diff] [blame] | 85 | parser.add_option("-o", "--output", dest="output", |
| 86 | help="Toolchain output directory") |
| 87 | parser.add_option("-m", "--other_mounts", dest="other_mounts", |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 88 | help="Other mount points in the form: " + |
| 89 | "dir:mounted_dir:options") |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 90 | |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 91 | relevant_argv = [] |
| 92 | passthrough_argv = [] |
bjanakiraman | 0d03a17 | 2013-02-15 04:50:53 +0000 | [diff] [blame] | 93 | i = 0 |
| 94 | while i < len(argv): |
asharif | 0269d46 | 2013-02-15 04:46:31 +0000 | [diff] [blame] | 95 | found = False |
| 96 | for option in parser.option_list: |
| 97 | for long_opt in option._long_opts: |
| 98 | if argv[i].startswith(long_opt): |
bjanakiraman | 0d03a17 | 2013-02-15 04:50:53 +0000 | [diff] [blame] | 99 | relevant_argv.append(argv[i]) |
asharif | 0269d46 | 2013-02-15 04:46:31 +0000 | [diff] [blame] | 100 | found = True |
| 101 | break |
| 102 | for short_opt in option._short_opts: |
| 103 | if argv[i].startswith(short_opt): |
bjanakiraman | 0d03a17 | 2013-02-15 04:50:53 +0000 | [diff] [blame] | 104 | relevant_argv.append(argv[i]) |
| 105 | relevant_argv.append(argv[i+1]) |
| 106 | i = i + 1 |
asharif | 0269d46 | 2013-02-15 04:46:31 +0000 | [diff] [blame] | 107 | found = True |
| 108 | break |
| 109 | |
| 110 | if found == True: |
| 111 | break |
| 112 | |
bjanakiraman | 0d03a17 | 2013-02-15 04:50:53 +0000 | [diff] [blame] | 113 | if found == False: |
asharif | 0269d46 | 2013-02-15 04:46:31 +0000 | [diff] [blame] | 114 | passthrough_argv.append(argv[i]) |
bjanakiraman | 0d03a17 | 2013-02-15 04:50:53 +0000 | [diff] [blame] | 115 | i = i + 1 |
asharif | 0269d46 | 2013-02-15 04:46:31 +0000 | [diff] [blame] | 116 | |
| 117 | options = parser.parse_args(relevant_argv)[0] |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 118 | |
asharif | 0b2f040 | 2013-02-15 04:50:25 +0000 | [diff] [blame] | 119 | chromeos_root = options.chromeos_root |
asharif | 1762130 | 2013-02-15 04:46:35 +0000 | [diff] [blame] | 120 | |
| 121 | chromeos_root = os.path.expanduser(chromeos_root) |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 122 | if options.toolchain_root: |
| 123 | options.toolchain_root = os.path.expanduser(options.toolchain_root) |
asharif | 1762130 | 2013-02-15 04:46:35 +0000 | [diff] [blame] | 124 | |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 125 | chromeos_root = os.path.abspath(chromeos_root) |
| 126 | |
asharif | 0b2f040 | 2013-02-15 04:50:25 +0000 | [diff] [blame] | 127 | if options.toolchain_root is None: |
| 128 | logger.GetLogger().LogError("--toolchain_root not specified") |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 129 | parser.print_help() |
| 130 | sys.exit(1) |
| 131 | |
| 132 | tc_dirs = [options.toolchain_root + "/google_vendor_src_branch/gcc"] |
asharif | 0b2f040 | 2013-02-15 04:50:25 +0000 | [diff] [blame] | 133 | |
| 134 | for tc_dir in tc_dirs: |
| 135 | if not os.path.exists(tc_dir): |
asharif | b5493df | 2013-02-15 04:50:46 +0000 | [diff] [blame] | 136 | logger.GetLogger().LogError("toolchain path " + |
asharif | 0b2f040 | 2013-02-15 04:50:25 +0000 | [diff] [blame] | 137 | tc_dir + " does not exist!") |
| 138 | parser.print_help() |
| 139 | sys.exit(1) |
| 140 | |
| 141 | if not os.path.exists(chromeos_root): |
| 142 | logger.GetLogger().LogError("chromeos_root " + options.chromeos_root + |
| 143 | " does not exist!") |
| 144 | parser.print_help() |
| 145 | sys.exit(1) |
| 146 | |
| 147 | if not os.path.exists(chromeos_root + "/src/scripts/enter_chroot.sh"): |
| 148 | logger.GetLogger().LogError(options.chromeos_root + |
| 149 | "/src/scripts/enter_chroot.sh" |
| 150 | " not found!") |
| 151 | parser.print_help() |
| 152 | sys.exit(1) |
| 153 | |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 154 | rootdir = utils.GetRoot(sys.argv[0])[0] |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 155 | version_dir = rootdir |
| 156 | |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 157 | mounted_tc_root = "/usr/local/toolchain_root" |
| 158 | full_mounted_tc_root = chromeos_root + "/chroot/" + mounted_tc_root |
| 159 | full_mounted_tc_root = os.path.abspath(full_mounted_tc_root) |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 160 | |
| 161 | mount_points = [] |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 162 | for tc_dir in tc_dirs: |
| 163 | last_dir = utils.GetRoot(tc_dir)[1] |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 164 | mount_point = MountPoint(tc_dir, full_mounted_tc_root + "/" + last_dir, |
| 165 | getpass.getuser(), "ro") |
| 166 | mount_points.append(mount_point) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 167 | |
asharif | 541b639 | 2013-02-15 04:50:38 +0000 | [diff] [blame] | 168 | output = options.output |
| 169 | if output is None: |
| 170 | output = version_dir + "/output" |
| 171 | mount_points.append(MountPoint(output, full_mounted_tc_root + "/output", |
| 172 | getpass.getuser())) |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 173 | mount_points += CreateMountPointsFromString(options.other_mounts, |
| 174 | chromeos_root + "/chroot/") |
| 175 | |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 176 | last_dir = utils.GetRoot(version_dir)[1] |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 177 | mount_point = MountPoint(version_dir, full_mounted_tc_root + "/" + last_dir, |
| 178 | getpass.getuser()) |
| 179 | mount_points.append(mount_point) |
| 180 | |
| 181 | for mount_point in mount_points: |
asharif | 556f4ff | 2013-02-15 04:50:35 +0000 | [diff] [blame] | 182 | retval = mount_point.DoMount() |
| 183 | if retval != 0: |
| 184 | return retval |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 185 | |
| 186 | # Finally, create the symlink to build-gcc. |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 187 | command = "sudo chown " + getpass.getuser() + " " + full_mounted_tc_root |
| 188 | retval = cmd_executer.RunCommand(command) |
| 189 | |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 190 | try: |
| 191 | os.symlink(last_dir + "/build-gcc", full_mounted_tc_root + "/build-gcc") |
| 192 | except Exception as e: |
asharif | 0b2f040 | 2013-02-15 04:50:25 +0000 | [diff] [blame] | 193 | logger.GetLogger().LogError(str(e)) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 194 | |
| 195 | # Now call enter_chroot with the rest of the arguments. |
asharif | fcf8cfc | 2013-02-15 04:49:21 +0000 | [diff] [blame] | 196 | command = chromeos_root + "/src/scripts/enter_chroot.sh" |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 197 | |
asharif | 0269d46 | 2013-02-15 04:46:31 +0000 | [diff] [blame] | 198 | if len(passthrough_argv) > 1: |
asharif | 541b639 | 2013-02-15 04:50:38 +0000 | [diff] [blame] | 199 | command += " <<EC_EOF" |
asharif | 0269d46 | 2013-02-15 04:46:31 +0000 | [diff] [blame] | 200 | command += " " + " ".join(passthrough_argv[1:]) |
asharif | 541b639 | 2013-02-15 04:50:38 +0000 | [diff] [blame] | 201 | command += "\nEC_EOF" |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 202 | retval = cmd_executer.RunCommand(command, return_output) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 203 | return retval |
| 204 | else: |
asharif | 556f4ff | 2013-02-15 04:50:35 +0000 | [diff] [blame] | 205 | return os.execv(command, [""]) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 206 | |
| 207 | |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 208 | def CreateMountPointsFromString(mount_strings, chroot_dir): |
| 209 | # String has options in the form dir:mount:options |
| 210 | mount_points = [] |
| 211 | if not mount_strings: |
| 212 | return mount_points |
| 213 | mount_list = mount_strings.split() |
| 214 | for mount_string in mount_list: |
| 215 | mount_values = mount_string.split(":") |
| 216 | external_dir = mount_values[0] |
| 217 | mount_dir = mount_values[1] |
| 218 | if len(mount_values)>2: |
| 219 | options = mount_values[2] |
| 220 | else: |
| 221 | options = None |
| 222 | mount_point = MountPoint(external_dir, chroot_dir + "/" + mount_dir, |
| 223 | getpass.getuser(), options) |
| 224 | mount_points.append(mount_point) |
| 225 | return mount_points |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 226 | |
| 227 | |
| 228 | if __name__ == "__main__": |
asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 229 | Main(sys.argv) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 230 | |