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) |
| 47 | utils.AssertTrue(retval == 0) |
| 48 | retval = self.CreateAndOwnDir(self.external_dir) |
| 49 | utils.AssertTrue(retval == 0) |
| 50 | retval = self.MountDir() |
| 51 | utils.AssertTrue(retval == 0) |
| 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") |
| 84 | parser.add_option("-m", "--other_mounts", dest="other_mounts", |
raymes | a7d219c | 2013-02-15 04:56:23 +0000 | [diff] [blame] | 85 | help="Other mount points in the form: " + |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 86 | "dir:mounted_dir:options") |
asharif | 1755b43 | 2013-02-15 04:55:29 +0000 | [diff] [blame] | 87 | parser.add_option("-s", "--mount-scripts-only", |
| 88 | dest="mount_scripts_only", |
| 89 | action="store_true", |
| 90 | default=False, |
| 91 | help="Mount only the scripts dir, and not the sources.") |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 92 | |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 93 | passthrough_argv = [] |
asharif | 1755b43 | 2013-02-15 04:55:29 +0000 | [diff] [blame] | 94 | (options, passthrough_argv) = parser.parse_args(argv) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 95 | |
asharif | 0b2f040 | 2013-02-15 04:50:25 +0000 | [diff] [blame] | 96 | chromeos_root = options.chromeos_root |
asharif | 1762130 | 2013-02-15 04:46:35 +0000 | [diff] [blame] | 97 | |
| 98 | chromeos_root = os.path.expanduser(chromeos_root) |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 99 | if options.toolchain_root: |
| 100 | options.toolchain_root = os.path.expanduser(options.toolchain_root) |
asharif | 1762130 | 2013-02-15 04:46:35 +0000 | [diff] [blame] | 101 | |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 102 | chromeos_root = os.path.abspath(chromeos_root) |
| 103 | |
asharif | 0b2f040 | 2013-02-15 04:50:25 +0000 | [diff] [blame] | 104 | if options.toolchain_root is None: |
| 105 | logger.GetLogger().LogError("--toolchain_root not specified") |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 106 | parser.print_help() |
| 107 | sys.exit(1) |
| 108 | |
asharif | 28238cf | 2013-02-15 04:51:01 +0000 | [diff] [blame] | 109 | tc_dirs = [options.toolchain_root + "/google_vendor_src_branch/gcc", |
| 110 | options.toolchain_root + "/google_vendor_src_branch/binutils"] |
asharif | 0b2f040 | 2013-02-15 04:50:25 +0000 | [diff] [blame] | 111 | |
asharif | 1755b43 | 2013-02-15 04:55:29 +0000 | [diff] [blame] | 112 | if options.mount_scripts_only == False: |
| 113 | for tc_dir in tc_dirs: |
| 114 | if not os.path.exists(tc_dir): |
| 115 | logger.GetLogger().LogError("toolchain path " + |
| 116 | tc_dir + " does not exist!") |
| 117 | parser.print_help() |
| 118 | sys.exit(1) |
asharif | 0b2f040 | 2013-02-15 04:50:25 +0000 | [diff] [blame] | 119 | |
| 120 | if not os.path.exists(chromeos_root): |
| 121 | logger.GetLogger().LogError("chromeos_root " + options.chromeos_root + |
| 122 | " does not exist!") |
| 123 | parser.print_help() |
| 124 | sys.exit(1) |
| 125 | |
| 126 | if not os.path.exists(chromeos_root + "/src/scripts/enter_chroot.sh"): |
raymes | a7d219c | 2013-02-15 04:56:23 +0000 | [diff] [blame] | 127 | logger.GetLogger().LogError(options.chromeos_root + |
asharif | 0b2f040 | 2013-02-15 04:50:25 +0000 | [diff] [blame] | 128 | "/src/scripts/enter_chroot.sh" |
| 129 | " not found!") |
| 130 | parser.print_help() |
| 131 | sys.exit(1) |
| 132 | |
raymes | 01959ae | 2013-02-15 04:50:07 +0000 | [diff] [blame] | 133 | rootdir = utils.GetRoot(sys.argv[0])[0] |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 134 | version_dir = rootdir |
| 135 | |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 136 | mounted_tc_root = "/usr/local/toolchain_root" |
| 137 | full_mounted_tc_root = chromeos_root + "/chroot/" + mounted_tc_root |
| 138 | full_mounted_tc_root = os.path.abspath(full_mounted_tc_root) |
raymes | a7d219c | 2013-02-15 04:56:23 +0000 | [diff] [blame] | 139 | |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 140 | mount_points = [] |
asharif | 1755b43 | 2013-02-15 04:55:29 +0000 | [diff] [blame] | 141 | if options.mount_scripts_only == False: |
| 142 | for tc_dir in tc_dirs: |
| 143 | last_dir = utils.GetRoot(tc_dir)[1] |
| 144 | mount_point = MountPoint(tc_dir, full_mounted_tc_root + "/" + last_dir, |
| 145 | getpass.getuser(), "ro") |
| 146 | mount_points.append(mount_point) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 147 | |
asharif | 541b639 | 2013-02-15 04:50:38 +0000 | [diff] [blame] | 148 | output = options.output |
| 149 | if output is None: |
asharif | a55807f | 2013-02-15 05:15:39 +0000 | [diff] [blame] | 150 | output = options.toolchain_root + "/output" |
asharif | 541b639 | 2013-02-15 04:50:38 +0000 | [diff] [blame] | 151 | mount_points.append(MountPoint(output, full_mounted_tc_root + "/output", |
| 152 | getpass.getuser())) |
raymes | a7d219c | 2013-02-15 04:56:23 +0000 | [diff] [blame] | 153 | mount_points += CreateMountPointsFromString(options.other_mounts, |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 154 | chromeos_root + "/chroot/") |
| 155 | |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 156 | last_dir = utils.GetRoot(version_dir)[1] |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 157 | mount_point = MountPoint(version_dir, full_mounted_tc_root + "/" + last_dir, |
| 158 | getpass.getuser()) |
| 159 | mount_points.append(mount_point) |
| 160 | |
| 161 | for mount_point in mount_points: |
asharif | 556f4ff | 2013-02-15 04:50:35 +0000 | [diff] [blame] | 162 | retval = mount_point.DoMount() |
| 163 | if retval != 0: |
| 164 | return retval |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 165 | |
| 166 | # Finally, create the symlink to build-gcc. |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 167 | command = "sudo chown " + getpass.getuser() + " " + full_mounted_tc_root |
asharif | 8a87387 | 2013-02-15 04:56:52 +0000 | [diff] [blame] | 168 | retval = command_executer.GetCommandExecuter().RunCommand(command) |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 169 | |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 170 | try: |
asharif | 8a87387 | 2013-02-15 04:56:52 +0000 | [diff] [blame] | 171 | CreateSymlink(last_dir + "/build-gcc", full_mounted_tc_root + "/build-gcc") |
| 172 | CreateSymlink(last_dir + "/build-binutils", full_mounted_tc_root + "/build-binutils") |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 173 | except Exception as e: |
asharif | 0b2f040 | 2013-02-15 04:50:25 +0000 | [diff] [blame] | 174 | logger.GetLogger().LogError(str(e)) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 175 | |
| 176 | # Now call enter_chroot with the rest of the arguments. |
asharif | fcf8cfc | 2013-02-15 04:49:21 +0000 | [diff] [blame] | 177 | command = chromeos_root + "/src/scripts/enter_chroot.sh" |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 178 | |
asharif | 0269d46 | 2013-02-15 04:46:31 +0000 | [diff] [blame] | 179 | if len(passthrough_argv) > 1: |
asharif | 51516da | 2013-02-15 04:56:12 +0000 | [diff] [blame] | 180 | inner_command = " ".join(passthrough_argv[1:]) |
| 181 | inner_command = inner_command.strip() |
| 182 | if inner_command.startswith("-- "): |
| 183 | inner_command = inner_command[3:] |
asharif | c0f7193 | 2013-02-15 04:56:18 +0000 | [diff] [blame] | 184 | command_file = "tc_enter_chroot.cmd" |
| 185 | command_file_path = chromeos_root + "/src/scripts/" + command_file |
asharif | 8a87387 | 2013-02-15 04:56:52 +0000 | [diff] [blame] | 186 | retval = command_executer.GetCommandExecuter().RunCommand("sudo rm -f " + command_file_path) |
asharif | c0f7193 | 2013-02-15 04:56:18 +0000 | [diff] [blame] | 187 | if retval != 0: |
| 188 | return retval |
| 189 | f = open(command_file_path, "w") |
| 190 | f.write(inner_command) |
| 191 | f.close() |
raymes | a7d219c | 2013-02-15 04:56:23 +0000 | [diff] [blame] | 192 | logger.GetLogger().LogCmd(inner_command) |
asharif | 8a87387 | 2013-02-15 04:56:52 +0000 | [diff] [blame] | 193 | retval = command_executer.GetCommandExecuter().RunCommand("chmod +x " + command_file_path) |
asharif | c0f7193 | 2013-02-15 04:56:18 +0000 | [diff] [blame] | 194 | if retval != 0: |
| 195 | return retval |
| 196 | command += " ./" + command_file |
asharif | 8a87387 | 2013-02-15 04:56:52 +0000 | [diff] [blame] | 197 | retval = command_executer.GetCommandExecuter().RunCommand(command, return_output) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 198 | return retval |
| 199 | else: |
asharif | 556f4ff | 2013-02-15 04:50:35 +0000 | [diff] [blame] | 200 | return os.execv(command, [""]) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 201 | |
| 202 | |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 203 | def CreateMountPointsFromString(mount_strings, chroot_dir): |
| 204 | # String has options in the form dir:mount:options |
| 205 | mount_points = [] |
| 206 | if not mount_strings: |
| 207 | return mount_points |
| 208 | mount_list = mount_strings.split() |
| 209 | for mount_string in mount_list: |
| 210 | mount_values = mount_string.split(":") |
| 211 | external_dir = mount_values[0] |
| 212 | mount_dir = mount_values[1] |
raymes | a7d219c | 2013-02-15 04:56:23 +0000 | [diff] [blame] | 213 | if len(mount_values) > 2: |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 214 | options = mount_values[2] |
| 215 | else: |
| 216 | options = None |
raymes | a7d219c | 2013-02-15 04:56:23 +0000 | [diff] [blame] | 217 | mount_point = MountPoint(external_dir, chroot_dir + "/" + mount_dir, |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 218 | getpass.getuser(), options) |
| 219 | mount_points.append(mount_point) |
| 220 | return mount_points |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 221 | |
| 222 | |
asharif | 8a87387 | 2013-02-15 04:56:52 +0000 | [diff] [blame] | 223 | def CreateSymlink(target, link_name): |
| 224 | utils.AssertExit(target.startswith("/") == False) |
| 225 | real_from_file = utils.GetRoot(link_name)[0] + "/" + target |
| 226 | if os.path.realpath(real_from_file) != os.path.realpath(link_name): |
| 227 | if os.path.exists(link_name): |
| 228 | command = "rm -rf " + link_name |
| 229 | command_executer.GetCommandExecuter().RunCommand(command) |
| 230 | os.symlink(target, link_name) |
| 231 | |
| 232 | |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 233 | if __name__ == "__main__": |
asharif | e3668f1 | 2013-02-15 04:46:29 +0000 | [diff] [blame] | 234 | Main(sys.argv) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 235 | |