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