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 | c0f7193 | 2013-02-15 04:56:18 +0000 | [diff] [blame] | 16 | import stat |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 17 | import sys |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 18 | from utils import command_executer |
| 19 | from utils import logger |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 20 | from utils import utils |
| 21 | |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 22 | class MountPoint: |
| 23 | def __init__(self, external_dir, mount_dir, owner, options=None): |
| 24 | self.external_dir = external_dir |
| 25 | self.mount_dir = mount_dir |
| 26 | self.owner = owner |
| 27 | self.options = options |
| 28 | |
| 29 | |
asharif | 556f4ff | 2013-02-15 04:50:35 +0000 | [diff] [blame] | 30 | def CreateAndOwnDir(self, dir_name): |
| 31 | retval = 0 |
| 32 | if not os.path.exists(dir_name): |
| 33 | command = "mkdir -p " + dir_name |
| 34 | command += " || sudo mkdir -p " + dir_name |
asharif | 8a87387 | 2013-02-15 04:56:52 +0000 | [diff] [blame] | 35 | retval = command_executer.GetCommandExecuter().RunCommand(command) |
asharif | 556f4ff | 2013-02-15 04:50:35 +0000 | [diff] [blame] | 36 | if retval != 0: |
| 37 | return retval |
| 38 | pw = pwd.getpwnam(self.owner) |
| 39 | if os.stat(dir_name).st_uid != pw.pw_uid: |
| 40 | command = "sudo chown -f " + self.owner + " " + dir_name |
asharif | 8a87387 | 2013-02-15 04:56:52 +0000 | [diff] [blame] | 41 | retval = command_executer.GetCommandExecuter().RunCommand(command) |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 42 | return retval |
| 43 | |
| 44 | |
| 45 | def DoMount(self): |
asharif | 556f4ff | 2013-02-15 04:50:35 +0000 | [diff] [blame] | 46 | retval = self.CreateAndOwnDir(self.mount_dir) |
kbaclawski | 6999ada | 2013-02-15 19:57:09 +0000 | [diff] [blame] | 47 | logger.GetLogger().LogFatalIf(retval, "Cannot create mount_dir!") |
asharif | 556f4ff | 2013-02-15 04:50:35 +0000 | [diff] [blame] | 48 | retval = self.CreateAndOwnDir(self.external_dir) |
kbaclawski | 6999ada | 2013-02-15 19:57:09 +0000 | [diff] [blame] | 49 | logger.GetLogger().LogFatalIf(retval, "Cannot create external_dir!") |
asharif | 556f4ff | 2013-02-15 04:50:35 +0000 | [diff] [blame] | 50 | retval = self.MountDir() |
kbaclawski | 6999ada | 2013-02-15 19:57:09 +0000 | [diff] [blame] | 51 | logger.GetLogger().LogFatalIf(retval, "Cannot mount!") |
asharif | 556f4ff | 2013-02-15 04:50:35 +0000 | [diff] [blame] | 52 | return retval |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 53 | |
| 54 | |
| 55 | def MountDir(self): |
| 56 | command = "sudo mount --bind " + self.external_dir + " " + self.mount_dir |
| 57 | if self.options == "ro": |
| 58 | command += " && sudo mount --bind -oremount,ro " + self.mount_dir |
asharif | 8a87387 | 2013-02-15 04:56:52 +0000 | [diff] [blame] | 59 | retval = command_executer.GetCommandExecuter().RunCommand(command) |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 60 | return retval |
| 61 | |
| 62 | |
| 63 | def __str__(self): |
| 64 | ret = "" |
| 65 | ret += self.external_dir + "\n" |
| 66 | ret += self.mount_dir + "\n" |
| 67 | if self.owner: |
| 68 | ret += self.owner + "\n" |
| 69 | if self.options: |
| 70 | ret += self.options + "\n" |
| 71 | return ret |
| 72 | |
| 73 | |
| 74 | def Main(argv, return_output=False): |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 75 | """The main function.""" |
| 76 | parser = optparse.OptionParser() |
| 77 | parser.add_option("-c", "--chromeos_root", dest="chromeos_root", |
asharif | 0b2f040 | 2013-02-15 04:50:25 +0000 | [diff] [blame] | 78 | default="../..", |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 79 | help="ChromeOS root checkout directory.") |
| 80 | parser.add_option("-t", "--toolchain_root", dest="toolchain_root", |
| 81 | help="Toolchain root directory.") |
asharif | 541b639 | 2013-02-15 04:50:38 +0000 | [diff] [blame] | 82 | parser.add_option("-o", "--output", dest="output", |
| 83 | help="Toolchain output directory") |
asharif | 52284c6 | 2013-02-15 19:59:08 +0000 | [diff] [blame] | 84 | parser.add_option("--sudo", dest="sudo", |
| 85 | action="store_true", |
| 86 | default=False, |
| 87 | help="Run the command with sudo.") |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 88 | parser.add_option("-r", "--third_party", dest="third_party", |
| 89 | help="The third_party directory to mount.") |
asharif | 541b639 | 2013-02-15 04:50:38 +0000 | [diff] [blame] | 90 | parser.add_option("-m", "--other_mounts", dest="other_mounts", |
raymes | a7d219c | 2013-02-15 04:56:23 +0000 | [diff] [blame] | 91 | help="Other mount points in the form: " + |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 92 | "dir:mounted_dir:options") |
asharif | 1755b43 | 2013-02-15 04:55:29 +0000 | [diff] [blame] | 93 | parser.add_option("-s", "--mount-scripts-only", |
| 94 | dest="mount_scripts_only", |
| 95 | action="store_true", |
| 96 | default=False, |
| 97 | help="Mount only the scripts dir, and not the sources.") |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 98 | |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 99 | passthrough_argv = [] |
asharif | 1755b43 | 2013-02-15 04:55:29 +0000 | [diff] [blame] | 100 | (options, passthrough_argv) = parser.parse_args(argv) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 101 | |
asharif | 0b2f040 | 2013-02-15 04:50:25 +0000 | [diff] [blame] | 102 | chromeos_root = options.chromeos_root |
asharif | 1762130 | 2013-02-15 04:46:35 +0000 | [diff] [blame] | 103 | |
| 104 | chromeos_root = os.path.expanduser(chromeos_root) |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 105 | if options.toolchain_root: |
| 106 | options.toolchain_root = os.path.expanduser(options.toolchain_root) |
asharif | 1762130 | 2013-02-15 04:46:35 +0000 | [diff] [blame] | 107 | |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 108 | chromeos_root = os.path.abspath(chromeos_root) |
| 109 | |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 110 | tc_dirs = [] |
asharif | 642509c | 2013-02-15 09:19:32 +0000 | [diff] [blame] | 111 | if options.toolchain_root is None or options.mount_scripts_only: |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 112 | m = "toolchain_root not specified. Will not mount toolchain dirs." |
| 113 | logger.GetLogger().LogWarning(m) |
| 114 | else: |
| 115 | tc_dirs = [options.toolchain_root + "/google_vendor_src_branch/gcc", |
| 116 | options.toolchain_root + "/google_vendor_src_branch/binutils"] |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 117 | |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 118 | for tc_dir in tc_dirs: |
| 119 | if not os.path.exists(tc_dir): |
| 120 | logger.GetLogger().LogError("toolchain path " + |
| 121 | tc_dir + " does not exist!") |
| 122 | parser.print_help() |
| 123 | sys.exit(1) |
asharif | 0b2f040 | 2013-02-15 04:50:25 +0000 | [diff] [blame] | 124 | |
| 125 | if not os.path.exists(chromeos_root): |
| 126 | logger.GetLogger().LogError("chromeos_root " + options.chromeos_root + |
| 127 | " does not exist!") |
| 128 | parser.print_help() |
| 129 | sys.exit(1) |
| 130 | |
asharif | c8e8e12 | 2013-02-15 21:19:59 +0000 | [diff] [blame] | 131 | if not os.path.exists(chromeos_root + "/src/scripts/build_packages"): |
raymes | a7d219c | 2013-02-15 04:56:23 +0000 | [diff] [blame] | 132 | logger.GetLogger().LogError(options.chromeos_root + |
asharif | c8e8e12 | 2013-02-15 21:19:59 +0000 | [diff] [blame] | 133 | "/src/scripts/build_packages" |
asharif | 0b2f040 | 2013-02-15 04:50:25 +0000 | [diff] [blame] | 134 | " not found!") |
| 135 | parser.print_help() |
| 136 | sys.exit(1) |
| 137 | |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 138 | rootdir = utils.GetRoot(__file__)[0] |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 139 | version_dir = rootdir |
| 140 | |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 141 | mounted_tc_root = "/usr/local/toolchain_root" |
| 142 | full_mounted_tc_root = chromeos_root + "/chroot/" + mounted_tc_root |
| 143 | full_mounted_tc_root = os.path.abspath(full_mounted_tc_root) |
raymes | a7d219c | 2013-02-15 04:56:23 +0000 | [diff] [blame] | 144 | |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 145 | mount_points = [] |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 146 | for tc_dir in tc_dirs: |
| 147 | last_dir = utils.GetRoot(tc_dir)[1] |
| 148 | mount_point = MountPoint(tc_dir, full_mounted_tc_root + "/" + last_dir, |
| 149 | getpass.getuser(), "ro") |
| 150 | mount_points.append(mount_point) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 151 | |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 152 | # Add the third_party mount point if it exists |
| 153 | if options.third_party: |
| 154 | third_party_dir = options.third_party |
kbaclawski | 6999ada | 2013-02-15 19:57:09 +0000 | [diff] [blame] | 155 | logger.GetLogger().LogFatalIf(not os.path.isdir(third_party_dir), |
| 156 | "--third_party option is not a valid dir.") |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 157 | else: |
| 158 | third_party_dir = os.path.abspath("%s/../../../third_party" % |
| 159 | os.path.dirname(__file__)) |
| 160 | |
| 161 | if os.path.isdir(third_party_dir): |
| 162 | mount_point = MountPoint(third_party_dir, |
| 163 | ("%s/%s" % |
| 164 | (full_mounted_tc_root, |
| 165 | os.path.basename(third_party_dir))), |
| 166 | getpass.getuser()) |
| 167 | mount_points.append(mount_point) |
kbaclawski | 6999ada | 2013-02-15 19:57:09 +0000 | [diff] [blame] | 168 | |
asharif | 541b639 | 2013-02-15 04:50:38 +0000 | [diff] [blame] | 169 | output = options.output |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 170 | if output is None and options.toolchain_root: |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 171 | # Mount the output directory at /usr/local/toolchain_root/output |
asharif | 01410cc | 2013-02-15 09:19:31 +0000 | [diff] [blame] | 172 | output = options.toolchain_root + "/output" |
| 173 | |
| 174 | if output: |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 175 | mount_points.append(MountPoint(output, full_mounted_tc_root + "/output", |
| 176 | getpass.getuser())) |
| 177 | |
| 178 | # Mount the other mount points |
raymes | a7d219c | 2013-02-15 04:56:23 +0000 | [diff] [blame] | 179 | mount_points += CreateMountPointsFromString(options.other_mounts, |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 180 | chromeos_root + "/chroot/") |
| 181 | |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 182 | last_dir = utils.GetRoot(version_dir)[1] |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 183 | |
| 184 | # Mount the version dir (v14) at /usr/local/toolchain_root/v14 |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 185 | mount_point = MountPoint(version_dir, full_mounted_tc_root + "/" + last_dir, |
| 186 | getpass.getuser()) |
| 187 | mount_points.append(mount_point) |
| 188 | |
| 189 | for mount_point in mount_points: |
asharif | 556f4ff | 2013-02-15 04:50:35 +0000 | [diff] [blame] | 190 | retval = mount_point.DoMount() |
| 191 | if retval != 0: |
| 192 | return retval |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 193 | |
| 194 | # Finally, create the symlink to build-gcc. |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 195 | command = "sudo chown " + getpass.getuser() + " " + full_mounted_tc_root |
asharif | 8a87387 | 2013-02-15 04:56:52 +0000 | [diff] [blame] | 196 | retval = command_executer.GetCommandExecuter().RunCommand(command) |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 197 | |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 198 | try: |
asharif | 8a87387 | 2013-02-15 04:56:52 +0000 | [diff] [blame] | 199 | CreateSymlink(last_dir + "/build-gcc", full_mounted_tc_root + "/build-gcc") |
| 200 | CreateSymlink(last_dir + "/build-binutils", full_mounted_tc_root + "/build-binutils") |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 201 | except Exception as e: |
asharif | 0b2f040 | 2013-02-15 04:50:25 +0000 | [diff] [blame] | 202 | logger.GetLogger().LogError(str(e)) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 203 | |
asharif | 3666653 | 2013-02-15 21:08:14 +0000 | [diff] [blame] | 204 | # Now call cros_sdk --enter with the rest of the arguments. |
| 205 | command = "cd %s/src/scripts && cros_sdk --enter" % chromeos_root |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 206 | |
asharif | 0269d46 | 2013-02-15 04:46:31 +0000 | [diff] [blame] | 207 | if len(passthrough_argv) > 1: |
asharif | 51516da | 2013-02-15 04:56:12 +0000 | [diff] [blame] | 208 | inner_command = " ".join(passthrough_argv[1:]) |
| 209 | inner_command = inner_command.strip() |
| 210 | if inner_command.startswith("-- "): |
| 211 | inner_command = inner_command[3:] |
asharif | c0f7193 | 2013-02-15 04:56:18 +0000 | [diff] [blame] | 212 | command_file = "tc_enter_chroot.cmd" |
| 213 | command_file_path = chromeos_root + "/src/scripts/" + command_file |
asharif | 8a87387 | 2013-02-15 04:56:52 +0000 | [diff] [blame] | 214 | retval = command_executer.GetCommandExecuter().RunCommand("sudo rm -f " + command_file_path) |
asharif | c0f7193 | 2013-02-15 04:56:18 +0000 | [diff] [blame] | 215 | if retval != 0: |
| 216 | return retval |
| 217 | f = open(command_file_path, "w") |
| 218 | f.write(inner_command) |
| 219 | f.close() |
raymes | a7d219c | 2013-02-15 04:56:23 +0000 | [diff] [blame] | 220 | logger.GetLogger().LogCmd(inner_command) |
asharif | 8a87387 | 2013-02-15 04:56:52 +0000 | [diff] [blame] | 221 | retval = command_executer.GetCommandExecuter().RunCommand("chmod +x " + command_file_path) |
asharif | c0f7193 | 2013-02-15 04:56:18 +0000 | [diff] [blame] | 222 | if retval != 0: |
| 223 | return retval |
asharif | 52284c6 | 2013-02-15 19:59:08 +0000 | [diff] [blame] | 224 | |
| 225 | if options.sudo: |
| 226 | command += " sudo ./" + command_file |
| 227 | else: |
| 228 | command += " ./" + command_file |
asharif | 8a87387 | 2013-02-15 04:56:52 +0000 | [diff] [blame] | 229 | retval = command_executer.GetCommandExecuter().RunCommand(command, return_output) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 230 | return retval |
| 231 | else: |
asharif | 3666653 | 2013-02-15 21:08:14 +0000 | [diff] [blame] | 232 | os.chdir("%s/src/scripts" % chromeos_root) |
| 233 | ce = command_executer.GetCommandExecuter() |
| 234 | [ret, out, err] = ce.RunCommand("which cros_sdk", return_output=True) |
| 235 | cros_sdk_binary = out.split()[0] |
| 236 | return os.execv(cros_sdk_binary, ["", "--enter"]) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 237 | |
| 238 | |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 239 | def CreateMountPointsFromString(mount_strings, chroot_dir): |
| 240 | # String has options in the form dir:mount:options |
| 241 | mount_points = [] |
| 242 | if not mount_strings: |
| 243 | return mount_points |
| 244 | mount_list = mount_strings.split() |
| 245 | for mount_string in mount_list: |
| 246 | mount_values = mount_string.split(":") |
| 247 | external_dir = mount_values[0] |
| 248 | mount_dir = mount_values[1] |
raymes | a7d219c | 2013-02-15 04:56:23 +0000 | [diff] [blame] | 249 | if len(mount_values) > 2: |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 250 | options = mount_values[2] |
| 251 | else: |
| 252 | options = None |
raymes | a7d219c | 2013-02-15 04:56:23 +0000 | [diff] [blame] | 253 | mount_point = MountPoint(external_dir, chroot_dir + "/" + mount_dir, |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 254 | getpass.getuser(), options) |
| 255 | mount_points.append(mount_point) |
| 256 | return mount_points |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 257 | |
| 258 | |
asharif | 8a87387 | 2013-02-15 04:56:52 +0000 | [diff] [blame] | 259 | def CreateSymlink(target, link_name): |
kbaclawski | 6999ada | 2013-02-15 19:57:09 +0000 | [diff] [blame] | 260 | logger.GetLogger().LogFatalIf(target.startswith("/"), |
| 261 | "Can't create symlink to absolute path!") |
asharif | 8a87387 | 2013-02-15 04:56:52 +0000 | [diff] [blame] | 262 | real_from_file = utils.GetRoot(link_name)[0] + "/" + target |
| 263 | if os.path.realpath(real_from_file) != os.path.realpath(link_name): |
| 264 | if os.path.exists(link_name): |
| 265 | command = "rm -rf " + link_name |
| 266 | command_executer.GetCommandExecuter().RunCommand(command) |
| 267 | os.symlink(target, link_name) |
| 268 | |
| 269 | |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 270 | if __name__ == "__main__": |
asharif | 2198c51 | 2013-02-15 09:21:35 +0000 | [diff] [blame] | 271 | retval = Main(sys.argv) |
| 272 | sys.exit(retval) |