blob: d776af55ba26c02e7d63d6c69a56fc20ab458327 [file] [log] [blame]
asharif252df0f2013-02-15 04:46:28 +00001#!/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
7This script enters the chroot with mounted sources.
8"""
9
10__author__ = "asharif@google.com (Ahmad Sharif)"
11
12import getpass
13import optparse
14import os
asharif556f4ff2013-02-15 04:50:35 +000015import pwd
asharifc0f71932013-02-15 04:56:18 +000016import stat
asharif252df0f2013-02-15 04:46:28 +000017import sys
raymes01959ae2013-02-15 04:50:07 +000018from utils import command_executer
19from utils import logger
asharif252df0f2013-02-15 04:46:28 +000020from utils import utils
21
asharifda9ac652013-02-15 04:50:09 +000022class 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
asharif556f4ff2013-02-15 04:50:35 +000030 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
asharif8a873872013-02-15 04:56:52 +000035 retval = command_executer.GetCommandExecuter().RunCommand(command)
asharif556f4ff2013-02-15 04:50:35 +000036 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
asharif8a873872013-02-15 04:56:52 +000041 retval = command_executer.GetCommandExecuter().RunCommand(command)
asharifda9ac652013-02-15 04:50:09 +000042 return retval
43
44
45 def DoMount(self):
asharif556f4ff2013-02-15 04:50:35 +000046 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
asharifda9ac652013-02-15 04:50:09 +000053
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
asharif8a873872013-02-15 04:56:52 +000059 retval = command_executer.GetCommandExecuter().RunCommand(command)
asharifda9ac652013-02-15 04:50:09 +000060 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
74def Main(argv, return_output=False):
asharif252df0f2013-02-15 04:46:28 +000075 """The main function."""
76 parser = optparse.OptionParser()
77 parser.add_option("-c", "--chromeos_root", dest="chromeos_root",
asharif0b2f0402013-02-15 04:50:25 +000078 default="../..",
asharif252df0f2013-02-15 04:46:28 +000079 help="ChromeOS root checkout directory.")
80 parser.add_option("-t", "--toolchain_root", dest="toolchain_root",
81 help="Toolchain root directory.")
asharif541b6392013-02-15 04:50:38 +000082 parser.add_option("-o", "--output", dest="output",
83 help="Toolchain output directory")
asharif8697d4e2013-02-15 09:18:09 +000084 parser.add_option("-r", "--third_party", dest="third_party",
85 help="The third_party directory to mount.")
asharif541b6392013-02-15 04:50:38 +000086 parser.add_option("-m", "--other_mounts", dest="other_mounts",
raymesa7d219c2013-02-15 04:56:23 +000087 help="Other mount points in the form: " +
asharifda9ac652013-02-15 04:50:09 +000088 "dir:mounted_dir:options")
asharif1755b432013-02-15 04:55:29 +000089 parser.add_option("-s", "--mount-scripts-only",
90 dest="mount_scripts_only",
91 action="store_true",
92 default=False,
93 help="Mount only the scripts dir, and not the sources.")
asharif252df0f2013-02-15 04:46:28 +000094
raymes01959ae2013-02-15 04:50:07 +000095 passthrough_argv = []
asharif1755b432013-02-15 04:55:29 +000096 (options, passthrough_argv) = parser.parse_args(argv)
asharif252df0f2013-02-15 04:46:28 +000097
asharif0b2f0402013-02-15 04:50:25 +000098 chromeos_root = options.chromeos_root
asharif17621302013-02-15 04:46:35 +000099
100 chromeos_root = os.path.expanduser(chromeos_root)
asharifda9ac652013-02-15 04:50:09 +0000101 if options.toolchain_root:
102 options.toolchain_root = os.path.expanduser(options.toolchain_root)
asharif17621302013-02-15 04:46:35 +0000103
asharif252df0f2013-02-15 04:46:28 +0000104 chromeos_root = os.path.abspath(chromeos_root)
105
asharif8697d4e2013-02-15 09:18:09 +0000106 tc_dirs = []
asharif642509c2013-02-15 09:19:32 +0000107 if options.toolchain_root is None or options.mount_scripts_only:
asharif8697d4e2013-02-15 09:18:09 +0000108 m = "toolchain_root not specified. Will not mount toolchain dirs."
109 logger.GetLogger().LogWarning(m)
110 else:
111 tc_dirs = [options.toolchain_root + "/google_vendor_src_branch/gcc",
112 options.toolchain_root + "/google_vendor_src_branch/binutils"]
asharif252df0f2013-02-15 04:46:28 +0000113
asharif8697d4e2013-02-15 09:18:09 +0000114 for tc_dir in tc_dirs:
115 if not os.path.exists(tc_dir):
116 logger.GetLogger().LogError("toolchain path " +
117 tc_dir + " does not exist!")
118 parser.print_help()
119 sys.exit(1)
asharif0b2f0402013-02-15 04:50:25 +0000120
121 if not os.path.exists(chromeos_root):
122 logger.GetLogger().LogError("chromeos_root " + options.chromeos_root +
123 " does not exist!")
124 parser.print_help()
125 sys.exit(1)
126
127 if not os.path.exists(chromeos_root + "/src/scripts/enter_chroot.sh"):
raymesa7d219c2013-02-15 04:56:23 +0000128 logger.GetLogger().LogError(options.chromeos_root +
asharif0b2f0402013-02-15 04:50:25 +0000129 "/src/scripts/enter_chroot.sh"
130 " not found!")
131 parser.print_help()
132 sys.exit(1)
133
asharif8697d4e2013-02-15 09:18:09 +0000134 rootdir = utils.GetRoot(__file__)[0]
asharif252df0f2013-02-15 04:46:28 +0000135 version_dir = rootdir
136
asharif252df0f2013-02-15 04:46:28 +0000137 mounted_tc_root = "/usr/local/toolchain_root"
138 full_mounted_tc_root = chromeos_root + "/chroot/" + mounted_tc_root
139 full_mounted_tc_root = os.path.abspath(full_mounted_tc_root)
raymesa7d219c2013-02-15 04:56:23 +0000140
asharifda9ac652013-02-15 04:50:09 +0000141 mount_points = []
asharif8697d4e2013-02-15 09:18:09 +0000142 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)
asharif252df0f2013-02-15 04:46:28 +0000147
asharif8697d4e2013-02-15 09:18:09 +0000148 # Add the third_party mount point if it exists
149 if options.third_party:
150 third_party_dir = options.third_party
151 utils.AssertExit(os.path.isdir(third_party_dir),
152 "--third_party option is not a valid dir.")
153 else:
154 third_party_dir = os.path.abspath("%s/../../../third_party" %
155 os.path.dirname(__file__))
156
157 if os.path.isdir(third_party_dir):
158 mount_point = MountPoint(third_party_dir,
159 ("%s/%s" %
160 (full_mounted_tc_root,
161 os.path.basename(third_party_dir))),
162 getpass.getuser())
163 mount_points.append(mount_point)
164
asharif541b6392013-02-15 04:50:38 +0000165 output = options.output
asharif8697d4e2013-02-15 09:18:09 +0000166 if output is None and options.toolchain_root:
asharif8697d4e2013-02-15 09:18:09 +0000167 # Mount the output directory at /usr/local/toolchain_root/output
asharif01410cc2013-02-15 09:19:31 +0000168 output = options.toolchain_root + "/output"
169
170 if output:
asharif8697d4e2013-02-15 09:18:09 +0000171 mount_points.append(MountPoint(output, full_mounted_tc_root + "/output",
172 getpass.getuser()))
173
174 # Mount the other mount points
raymesa7d219c2013-02-15 04:56:23 +0000175 mount_points += CreateMountPointsFromString(options.other_mounts,
asharifda9ac652013-02-15 04:50:09 +0000176 chromeos_root + "/chroot/")
177
asharif252df0f2013-02-15 04:46:28 +0000178 last_dir = utils.GetRoot(version_dir)[1]
asharif8697d4e2013-02-15 09:18:09 +0000179
180 # Mount the version dir (v14) at /usr/local/toolchain_root/v14
asharifda9ac652013-02-15 04:50:09 +0000181 mount_point = MountPoint(version_dir, full_mounted_tc_root + "/" + last_dir,
182 getpass.getuser())
183 mount_points.append(mount_point)
184
185 for mount_point in mount_points:
asharif556f4ff2013-02-15 04:50:35 +0000186 retval = mount_point.DoMount()
187 if retval != 0:
188 return retval
asharif252df0f2013-02-15 04:46:28 +0000189
190 # Finally, create the symlink to build-gcc.
asharifda9ac652013-02-15 04:50:09 +0000191 command = "sudo chown " + getpass.getuser() + " " + full_mounted_tc_root
asharif8a873872013-02-15 04:56:52 +0000192 retval = command_executer.GetCommandExecuter().RunCommand(command)
asharifda9ac652013-02-15 04:50:09 +0000193
asharif252df0f2013-02-15 04:46:28 +0000194 try:
asharif8a873872013-02-15 04:56:52 +0000195 CreateSymlink(last_dir + "/build-gcc", full_mounted_tc_root + "/build-gcc")
196 CreateSymlink(last_dir + "/build-binutils", full_mounted_tc_root + "/build-binutils")
asharif252df0f2013-02-15 04:46:28 +0000197 except Exception as e:
asharif0b2f0402013-02-15 04:50:25 +0000198 logger.GetLogger().LogError(str(e))
asharif252df0f2013-02-15 04:46:28 +0000199
200 # Now call enter_chroot with the rest of the arguments.
ashariffcf8cfc2013-02-15 04:49:21 +0000201 command = chromeos_root + "/src/scripts/enter_chroot.sh"
asharif252df0f2013-02-15 04:46:28 +0000202
asharif0269d462013-02-15 04:46:31 +0000203 if len(passthrough_argv) > 1:
asharif51516da2013-02-15 04:56:12 +0000204 inner_command = " ".join(passthrough_argv[1:])
205 inner_command = inner_command.strip()
206 if inner_command.startswith("-- "):
207 inner_command = inner_command[3:]
asharifc0f71932013-02-15 04:56:18 +0000208 command_file = "tc_enter_chroot.cmd"
209 command_file_path = chromeos_root + "/src/scripts/" + command_file
asharif8a873872013-02-15 04:56:52 +0000210 retval = command_executer.GetCommandExecuter().RunCommand("sudo rm -f " + command_file_path)
asharifc0f71932013-02-15 04:56:18 +0000211 if retval != 0:
212 return retval
213 f = open(command_file_path, "w")
214 f.write(inner_command)
215 f.close()
raymesa7d219c2013-02-15 04:56:23 +0000216 logger.GetLogger().LogCmd(inner_command)
asharif8a873872013-02-15 04:56:52 +0000217 retval = command_executer.GetCommandExecuter().RunCommand("chmod +x " + command_file_path)
asharifc0f71932013-02-15 04:56:18 +0000218 if retval != 0:
219 return retval
220 command += " ./" + command_file
asharif8a873872013-02-15 04:56:52 +0000221 retval = command_executer.GetCommandExecuter().RunCommand(command, return_output)
asharif252df0f2013-02-15 04:46:28 +0000222 return retval
223 else:
asharif556f4ff2013-02-15 04:50:35 +0000224 return os.execv(command, [""])
asharif252df0f2013-02-15 04:46:28 +0000225
226
asharifda9ac652013-02-15 04:50:09 +0000227def CreateMountPointsFromString(mount_strings, chroot_dir):
228 # String has options in the form dir:mount:options
229 mount_points = []
230 if not mount_strings:
231 return mount_points
232 mount_list = mount_strings.split()
233 for mount_string in mount_list:
234 mount_values = mount_string.split(":")
235 external_dir = mount_values[0]
236 mount_dir = mount_values[1]
raymesa7d219c2013-02-15 04:56:23 +0000237 if len(mount_values) > 2:
asharifda9ac652013-02-15 04:50:09 +0000238 options = mount_values[2]
239 else:
240 options = None
raymesa7d219c2013-02-15 04:56:23 +0000241 mount_point = MountPoint(external_dir, chroot_dir + "/" + mount_dir,
asharifda9ac652013-02-15 04:50:09 +0000242 getpass.getuser(), options)
243 mount_points.append(mount_point)
244 return mount_points
asharif252df0f2013-02-15 04:46:28 +0000245
246
asharif8a873872013-02-15 04:56:52 +0000247def CreateSymlink(target, link_name):
248 utils.AssertExit(target.startswith("/") == False)
249 real_from_file = utils.GetRoot(link_name)[0] + "/" + target
250 if os.path.realpath(real_from_file) != os.path.realpath(link_name):
251 if os.path.exists(link_name):
252 command = "rm -rf " + link_name
253 command_executer.GetCommandExecuter().RunCommand(command)
254 os.symlink(target, link_name)
255
256
asharif252df0f2013-02-15 04:46:28 +0000257if __name__ == "__main__":
asharif2198c512013-02-15 09:21:35 +0000258 retval = Main(sys.argv)
259 sys.exit(retval)
asharif252df0f2013-02-15 04:46:28 +0000260