blob: 8125fdf34863f82811e0ca5aeda92ce1d65b335d [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
22# Common initializations
raymes01959ae2013-02-15 04:50:07 +000023cmd_executer = command_executer.GetCommandExecuter()
asharif252df0f2013-02-15 04:46:28 +000024
25
asharifda9ac652013-02-15 04:50:09 +000026class 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
asharif556f4ff2013-02-15 04:50:35 +000034 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
asharifda9ac652013-02-15 04:50:09 +000039 retval = cmd_executer.RunCommand(command)
asharif556f4ff2013-02-15 04:50:35 +000040 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)
asharifda9ac652013-02-15 04:50:09 +000046 return retval
47
48
49 def DoMount(self):
asharif556f4ff2013-02-15 04:50:35 +000050 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
asharifda9ac652013-02-15 04:50:09 +000057
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
78def Main(argv, return_output=False):
asharif252df0f2013-02-15 04:46:28 +000079 """The main function."""
80 parser = optparse.OptionParser()
81 parser.add_option("-c", "--chromeos_root", dest="chromeos_root",
asharif0b2f0402013-02-15 04:50:25 +000082 default="../..",
asharif252df0f2013-02-15 04:46:28 +000083 help="ChromeOS root checkout directory.")
84 parser.add_option("-t", "--toolchain_root", dest="toolchain_root",
85 help="Toolchain root directory.")
asharif541b6392013-02-15 04:50:38 +000086 parser.add_option("-o", "--output", dest="output",
87 help="Toolchain output directory")
88 parser.add_option("-m", "--other_mounts", dest="other_mounts",
raymesa7d219c2013-02-15 04:56:23 +000089 help="Other mount points in the form: " +
asharifda9ac652013-02-15 04:50:09 +000090 "dir:mounted_dir:options")
asharif1755b432013-02-15 04:55:29 +000091 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.")
asharif252df0f2013-02-15 04:46:28 +000096
raymes01959ae2013-02-15 04:50:07 +000097 passthrough_argv = []
asharif1755b432013-02-15 04:55:29 +000098 (options, passthrough_argv) = parser.parse_args(argv)
asharif252df0f2013-02-15 04:46:28 +000099
asharif0b2f0402013-02-15 04:50:25 +0000100 chromeos_root = options.chromeos_root
asharif17621302013-02-15 04:46:35 +0000101
102 chromeos_root = os.path.expanduser(chromeos_root)
asharifda9ac652013-02-15 04:50:09 +0000103 if options.toolchain_root:
104 options.toolchain_root = os.path.expanduser(options.toolchain_root)
asharif17621302013-02-15 04:46:35 +0000105
asharif252df0f2013-02-15 04:46:28 +0000106 chromeos_root = os.path.abspath(chromeos_root)
107
asharif0b2f0402013-02-15 04:50:25 +0000108 if options.toolchain_root is None:
109 logger.GetLogger().LogError("--toolchain_root not specified")
asharif252df0f2013-02-15 04:46:28 +0000110 parser.print_help()
111 sys.exit(1)
112
asharif28238cf2013-02-15 04:51:01 +0000113 tc_dirs = [options.toolchain_root + "/google_vendor_src_branch/gcc",
114 options.toolchain_root + "/google_vendor_src_branch/binutils"]
asharif0b2f0402013-02-15 04:50:25 +0000115
asharif1755b432013-02-15 04:55:29 +0000116 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)
asharif0b2f0402013-02-15 04:50:25 +0000123
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"):
raymesa7d219c2013-02-15 04:56:23 +0000131 logger.GetLogger().LogError(options.chromeos_root +
asharif0b2f0402013-02-15 04:50:25 +0000132 "/src/scripts/enter_chroot.sh"
133 " not found!")
134 parser.print_help()
135 sys.exit(1)
136
raymes01959ae2013-02-15 04:50:07 +0000137 rootdir = utils.GetRoot(sys.argv[0])[0]
asharif252df0f2013-02-15 04:46:28 +0000138 version_dir = rootdir
139
asharif252df0f2013-02-15 04:46:28 +0000140 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)
raymesa7d219c2013-02-15 04:56:23 +0000143
asharifda9ac652013-02-15 04:50:09 +0000144 mount_points = []
asharif1755b432013-02-15 04:55:29 +0000145 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)
asharif252df0f2013-02-15 04:46:28 +0000151
asharif541b6392013-02-15 04:50:38 +0000152 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()))
raymesa7d219c2013-02-15 04:56:23 +0000157 mount_points += CreateMountPointsFromString(options.other_mounts,
asharifda9ac652013-02-15 04:50:09 +0000158 chromeos_root + "/chroot/")
159
asharif252df0f2013-02-15 04:46:28 +0000160 last_dir = utils.GetRoot(version_dir)[1]
asharifda9ac652013-02-15 04:50:09 +0000161 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:
asharif556f4ff2013-02-15 04:50:35 +0000166 retval = mount_point.DoMount()
167 if retval != 0:
168 return retval
asharif252df0f2013-02-15 04:46:28 +0000169
170 # Finally, create the symlink to build-gcc.
asharifda9ac652013-02-15 04:50:09 +0000171 command = "sudo chown " + getpass.getuser() + " " + full_mounted_tc_root
172 retval = cmd_executer.RunCommand(command)
173
asharif252df0f2013-02-15 04:46:28 +0000174 try:
175 os.symlink(last_dir + "/build-gcc", full_mounted_tc_root + "/build-gcc")
asharif28238cf2013-02-15 04:51:01 +0000176 os.symlink(last_dir + "/build-binutils", full_mounted_tc_root + "/build-binutils")
asharif252df0f2013-02-15 04:46:28 +0000177 except Exception as e:
asharif0b2f0402013-02-15 04:50:25 +0000178 logger.GetLogger().LogError(str(e))
asharif252df0f2013-02-15 04:46:28 +0000179
180 # Now call enter_chroot with the rest of the arguments.
ashariffcf8cfc2013-02-15 04:49:21 +0000181 command = chromeos_root + "/src/scripts/enter_chroot.sh"
asharif252df0f2013-02-15 04:46:28 +0000182
asharif0269d462013-02-15 04:46:31 +0000183 if len(passthrough_argv) > 1:
asharif51516da2013-02-15 04:56:12 +0000184 inner_command = " ".join(passthrough_argv[1:])
185 inner_command = inner_command.strip()
186 if inner_command.startswith("-- "):
187 inner_command = inner_command[3:]
asharifc0f71932013-02-15 04:56:18 +0000188 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()
raymesa7d219c2013-02-15 04:56:23 +0000196 logger.GetLogger().LogCmd(inner_command)
asharifc0f71932013-02-15 04:56:18 +0000197 retval = cmd_executer.RunCommand("chmod +x " + command_file_path)
198 if retval != 0:
199 return retval
200 command += " ./" + command_file
asharifda9ac652013-02-15 04:50:09 +0000201 retval = cmd_executer.RunCommand(command, return_output)
asharif252df0f2013-02-15 04:46:28 +0000202 return retval
203 else:
asharif556f4ff2013-02-15 04:50:35 +0000204 return os.execv(command, [""])
asharif252df0f2013-02-15 04:46:28 +0000205
206
asharifda9ac652013-02-15 04:50:09 +0000207def 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]
raymesa7d219c2013-02-15 04:56:23 +0000217 if len(mount_values) > 2:
asharifda9ac652013-02-15 04:50:09 +0000218 options = mount_values[2]
219 else:
220 options = None
raymesa7d219c2013-02-15 04:56:23 +0000221 mount_point = MountPoint(external_dir, chroot_dir + "/" + mount_dir,
asharifda9ac652013-02-15 04:50:09 +0000222 getpass.getuser(), options)
223 mount_points.append(mount_point)
224 return mount_points
asharif252df0f2013-02-15 04:46:28 +0000225
226
227if __name__ == "__main__":
asharife3668f12013-02-15 04:46:29 +0000228 Main(sys.argv)
asharif252df0f2013-02-15 04:46:28 +0000229