blob: a1db7e6873fdb26c35d54becb6ebefbf8492f8b2 [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):
asharif6c619132013-02-15 21:55:28 +000024 self.external_dir = os.path.realpath(external_dir)
25 self.mount_dir = os.path.realpath(mount_dir)
asharifda9ac652013-02-15 04:50:09 +000026 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):
asharif6c619132013-02-15 21:55:28 +000046 ce = command_executer.GetCommandExecuter()
47 mount_signature = "%s on %s" % (self.external_dir, self.mount_dir)
48 command = "mount"
49 retval, out, err = ce.RunCommand(command, return_output=True)
50 if mount_signature not in out:
51 retval = self.CreateAndOwnDir(self.mount_dir)
52 logger.GetLogger().LogFatalIf(retval, "Cannot create mount_dir!")
53 retval = self.CreateAndOwnDir(self.external_dir)
54 logger.GetLogger().LogFatalIf(retval, "Cannot create external_dir!")
55 retval = self.MountDir()
56 logger.GetLogger().LogFatalIf(retval, "Cannot mount!")
57 return retval
58 else:
59 return 0
asharifda9ac652013-02-15 04:50:09 +000060
61
asharifc97199a2013-02-15 22:48:45 +000062 def UnMount(self):
63 ce = command_executer.GetCommandExecuter()
cmtice0186f572013-02-15 23:44:51 +000064 return ce.RunCommand("sudo umount %s" % self.mount_dir)
asharifc97199a2013-02-15 22:48:45 +000065
66
asharifda9ac652013-02-15 04:50:09 +000067 def MountDir(self):
68 command = "sudo mount --bind " + self.external_dir + " " + self.mount_dir
69 if self.options == "ro":
70 command += " && sudo mount --bind -oremount,ro " + self.mount_dir
asharif8a873872013-02-15 04:56:52 +000071 retval = command_executer.GetCommandExecuter().RunCommand(command)
asharifda9ac652013-02-15 04:50:09 +000072 return retval
73
74
75 def __str__(self):
76 ret = ""
77 ret += self.external_dir + "\n"
78 ret += self.mount_dir + "\n"
79 if self.owner:
80 ret += self.owner + "\n"
81 if self.options:
82 ret += self.options + "\n"
83 return ret
84
85
86def Main(argv, return_output=False):
asharif252df0f2013-02-15 04:46:28 +000087 """The main function."""
88 parser = optparse.OptionParser()
89 parser.add_option("-c", "--chromeos_root", dest="chromeos_root",
asharif0b2f0402013-02-15 04:50:25 +000090 default="../..",
asharif252df0f2013-02-15 04:46:28 +000091 help="ChromeOS root checkout directory.")
92 parser.add_option("-t", "--toolchain_root", dest="toolchain_root",
93 help="Toolchain root directory.")
asharif541b6392013-02-15 04:50:38 +000094 parser.add_option("-o", "--output", dest="output",
95 help="Toolchain output directory")
asharif52284c62013-02-15 19:59:08 +000096 parser.add_option("--sudo", dest="sudo",
97 action="store_true",
98 default=False,
99 help="Run the command with sudo.")
asharif8697d4e2013-02-15 09:18:09 +0000100 parser.add_option("-r", "--third_party", dest="third_party",
101 help="The third_party directory to mount.")
asharif541b6392013-02-15 04:50:38 +0000102 parser.add_option("-m", "--other_mounts", dest="other_mounts",
raymesa7d219c2013-02-15 04:56:23 +0000103 help="Other mount points in the form: " +
asharifda9ac652013-02-15 04:50:09 +0000104 "dir:mounted_dir:options")
asharif1755b432013-02-15 04:55:29 +0000105 parser.add_option("-s", "--mount-scripts-only",
106 dest="mount_scripts_only",
107 action="store_true",
108 default=False,
109 help="Mount only the scripts dir, and not the sources.")
asharif252df0f2013-02-15 04:46:28 +0000110
raymes01959ae2013-02-15 04:50:07 +0000111 passthrough_argv = []
asharif1755b432013-02-15 04:55:29 +0000112 (options, passthrough_argv) = parser.parse_args(argv)
asharif252df0f2013-02-15 04:46:28 +0000113
asharif0b2f0402013-02-15 04:50:25 +0000114 chromeos_root = options.chromeos_root
asharif17621302013-02-15 04:46:35 +0000115
116 chromeos_root = os.path.expanduser(chromeos_root)
asharifda9ac652013-02-15 04:50:09 +0000117 if options.toolchain_root:
118 options.toolchain_root = os.path.expanduser(options.toolchain_root)
asharif17621302013-02-15 04:46:35 +0000119
asharif252df0f2013-02-15 04:46:28 +0000120 chromeos_root = os.path.abspath(chromeos_root)
121
asharif8697d4e2013-02-15 09:18:09 +0000122 tc_dirs = []
asharif642509c2013-02-15 09:19:32 +0000123 if options.toolchain_root is None or options.mount_scripts_only:
asharif8697d4e2013-02-15 09:18:09 +0000124 m = "toolchain_root not specified. Will not mount toolchain dirs."
125 logger.GetLogger().LogWarning(m)
126 else:
127 tc_dirs = [options.toolchain_root + "/google_vendor_src_branch/gcc",
128 options.toolchain_root + "/google_vendor_src_branch/binutils"]
asharif252df0f2013-02-15 04:46:28 +0000129
asharif8697d4e2013-02-15 09:18:09 +0000130 for tc_dir in tc_dirs:
131 if not os.path.exists(tc_dir):
132 logger.GetLogger().LogError("toolchain path " +
133 tc_dir + " does not exist!")
134 parser.print_help()
135 sys.exit(1)
asharif0b2f0402013-02-15 04:50:25 +0000136
137 if not os.path.exists(chromeos_root):
138 logger.GetLogger().LogError("chromeos_root " + options.chromeos_root +
139 " does not exist!")
140 parser.print_help()
141 sys.exit(1)
142
asharifc8e8e122013-02-15 21:19:59 +0000143 if not os.path.exists(chromeos_root + "/src/scripts/build_packages"):
raymesa7d219c2013-02-15 04:56:23 +0000144 logger.GetLogger().LogError(options.chromeos_root +
asharifc8e8e122013-02-15 21:19:59 +0000145 "/src/scripts/build_packages"
asharif0b2f0402013-02-15 04:50:25 +0000146 " not found!")
147 parser.print_help()
148 sys.exit(1)
149
asharifb225e792013-02-15 21:20:11 +0000150 version_dir = os.path.realpath(os.path.expanduser(os.path.dirname(__file__)))
asharif252df0f2013-02-15 04:46:28 +0000151
asharif252df0f2013-02-15 04:46:28 +0000152 mounted_tc_root = "/usr/local/toolchain_root"
153 full_mounted_tc_root = chromeos_root + "/chroot/" + mounted_tc_root
154 full_mounted_tc_root = os.path.abspath(full_mounted_tc_root)
raymesa7d219c2013-02-15 04:56:23 +0000155
asharifda9ac652013-02-15 04:50:09 +0000156 mount_points = []
asharif8697d4e2013-02-15 09:18:09 +0000157 for tc_dir in tc_dirs:
158 last_dir = utils.GetRoot(tc_dir)[1]
159 mount_point = MountPoint(tc_dir, full_mounted_tc_root + "/" + last_dir,
160 getpass.getuser(), "ro")
161 mount_points.append(mount_point)
asharif252df0f2013-02-15 04:46:28 +0000162
asharif8697d4e2013-02-15 09:18:09 +0000163 # Add the third_party mount point if it exists
164 if options.third_party:
165 third_party_dir = options.third_party
kbaclawski6999ada2013-02-15 19:57:09 +0000166 logger.GetLogger().LogFatalIf(not os.path.isdir(third_party_dir),
167 "--third_party option is not a valid dir.")
asharif8697d4e2013-02-15 09:18:09 +0000168 else:
169 third_party_dir = os.path.abspath("%s/../../../third_party" %
170 os.path.dirname(__file__))
171
172 if os.path.isdir(third_party_dir):
173 mount_point = MountPoint(third_party_dir,
174 ("%s/%s" %
175 (full_mounted_tc_root,
176 os.path.basename(third_party_dir))),
177 getpass.getuser())
178 mount_points.append(mount_point)
kbaclawski6999ada2013-02-15 19:57:09 +0000179
asharif541b6392013-02-15 04:50:38 +0000180 output = options.output
asharif8697d4e2013-02-15 09:18:09 +0000181 if output is None and options.toolchain_root:
asharif8697d4e2013-02-15 09:18:09 +0000182 # Mount the output directory at /usr/local/toolchain_root/output
asharif01410cc2013-02-15 09:19:31 +0000183 output = options.toolchain_root + "/output"
184
185 if output:
asharif8697d4e2013-02-15 09:18:09 +0000186 mount_points.append(MountPoint(output, full_mounted_tc_root + "/output",
187 getpass.getuser()))
188
189 # Mount the other mount points
raymesa7d219c2013-02-15 04:56:23 +0000190 mount_points += CreateMountPointsFromString(options.other_mounts,
asharifda9ac652013-02-15 04:50:09 +0000191 chromeos_root + "/chroot/")
192
asharif252df0f2013-02-15 04:46:28 +0000193 last_dir = utils.GetRoot(version_dir)[1]
asharif8697d4e2013-02-15 09:18:09 +0000194
195 # Mount the version dir (v14) at /usr/local/toolchain_root/v14
asharifda9ac652013-02-15 04:50:09 +0000196 mount_point = MountPoint(version_dir, full_mounted_tc_root + "/" + last_dir,
197 getpass.getuser())
198 mount_points.append(mount_point)
199
200 for mount_point in mount_points:
asharif556f4ff2013-02-15 04:50:35 +0000201 retval = mount_point.DoMount()
202 if retval != 0:
203 return retval
asharif252df0f2013-02-15 04:46:28 +0000204
205 # Finally, create the symlink to build-gcc.
asharifda9ac652013-02-15 04:50:09 +0000206 command = "sudo chown " + getpass.getuser() + " " + full_mounted_tc_root
asharif8a873872013-02-15 04:56:52 +0000207 retval = command_executer.GetCommandExecuter().RunCommand(command)
asharifda9ac652013-02-15 04:50:09 +0000208
asharif252df0f2013-02-15 04:46:28 +0000209 try:
asharif8a873872013-02-15 04:56:52 +0000210 CreateSymlink(last_dir + "/build-gcc", full_mounted_tc_root + "/build-gcc")
211 CreateSymlink(last_dir + "/build-binutils", full_mounted_tc_root + "/build-binutils")
asharif252df0f2013-02-15 04:46:28 +0000212 except Exception as e:
asharif0b2f0402013-02-15 04:50:25 +0000213 logger.GetLogger().LogError(str(e))
asharif252df0f2013-02-15 04:46:28 +0000214
asharif36666532013-02-15 21:08:14 +0000215 # Now call cros_sdk --enter with the rest of the arguments.
216 command = "cd %s/src/scripts && cros_sdk --enter" % chromeos_root
asharif252df0f2013-02-15 04:46:28 +0000217
asharif0269d462013-02-15 04:46:31 +0000218 if len(passthrough_argv) > 1:
asharif51516da2013-02-15 04:56:12 +0000219 inner_command = " ".join(passthrough_argv[1:])
220 inner_command = inner_command.strip()
221 if inner_command.startswith("-- "):
222 inner_command = inner_command[3:]
asharifc0f71932013-02-15 04:56:18 +0000223 command_file = "tc_enter_chroot.cmd"
224 command_file_path = chromeos_root + "/src/scripts/" + command_file
asharif8a873872013-02-15 04:56:52 +0000225 retval = command_executer.GetCommandExecuter().RunCommand("sudo rm -f " + command_file_path)
asharifc0f71932013-02-15 04:56:18 +0000226 if retval != 0:
227 return retval
228 f = open(command_file_path, "w")
229 f.write(inner_command)
230 f.close()
raymesa7d219c2013-02-15 04:56:23 +0000231 logger.GetLogger().LogCmd(inner_command)
asharif8a873872013-02-15 04:56:52 +0000232 retval = command_executer.GetCommandExecuter().RunCommand("chmod +x " + command_file_path)
asharifc0f71932013-02-15 04:56:18 +0000233 if retval != 0:
234 return retval
asharif52284c62013-02-15 19:59:08 +0000235
236 if options.sudo:
237 command += " sudo ./" + command_file
238 else:
239 command += " ./" + command_file
asharif8a873872013-02-15 04:56:52 +0000240 retval = command_executer.GetCommandExecuter().RunCommand(command, return_output)
asharif252df0f2013-02-15 04:46:28 +0000241 return retval
242 else:
asharif36666532013-02-15 21:08:14 +0000243 os.chdir("%s/src/scripts" % chromeos_root)
244 ce = command_executer.GetCommandExecuter()
245 [ret, out, err] = ce.RunCommand("which cros_sdk", return_output=True)
246 cros_sdk_binary = out.split()[0]
247 return os.execv(cros_sdk_binary, ["", "--enter"])
asharif252df0f2013-02-15 04:46:28 +0000248
249
asharifda9ac652013-02-15 04:50:09 +0000250def CreateMountPointsFromString(mount_strings, chroot_dir):
251 # String has options in the form dir:mount:options
252 mount_points = []
253 if not mount_strings:
254 return mount_points
255 mount_list = mount_strings.split()
256 for mount_string in mount_list:
257 mount_values = mount_string.split(":")
258 external_dir = mount_values[0]
259 mount_dir = mount_values[1]
raymesa7d219c2013-02-15 04:56:23 +0000260 if len(mount_values) > 2:
asharifda9ac652013-02-15 04:50:09 +0000261 options = mount_values[2]
262 else:
263 options = None
raymesa7d219c2013-02-15 04:56:23 +0000264 mount_point = MountPoint(external_dir, chroot_dir + "/" + mount_dir,
asharifda9ac652013-02-15 04:50:09 +0000265 getpass.getuser(), options)
266 mount_points.append(mount_point)
267 return mount_points
asharif252df0f2013-02-15 04:46:28 +0000268
269
asharif8a873872013-02-15 04:56:52 +0000270def CreateSymlink(target, link_name):
kbaclawski6999ada2013-02-15 19:57:09 +0000271 logger.GetLogger().LogFatalIf(target.startswith("/"),
272 "Can't create symlink to absolute path!")
asharif8a873872013-02-15 04:56:52 +0000273 real_from_file = utils.GetRoot(link_name)[0] + "/" + target
274 if os.path.realpath(real_from_file) != os.path.realpath(link_name):
275 if os.path.exists(link_name):
276 command = "rm -rf " + link_name
277 command_executer.GetCommandExecuter().RunCommand(command)
278 os.symlink(target, link_name)
279
280
asharif252df0f2013-02-15 04:46:28 +0000281if __name__ == "__main__":
asharif2198c512013-02-15 09:21:35 +0000282 retval = Main(sys.argv)
283 sys.exit(retval)