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