blob: d15080d88f8700de3729a657e3cd3798ee141018 [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
asharif252df0f2013-02-15 04:46:28 +000016import sys
raymes01959ae2013-02-15 04:50:07 +000017from utils import command_executer
18from utils import logger
asharif252df0f2013-02-15 04:46:28 +000019from utils import utils
20
21# Common initializations
raymes01959ae2013-02-15 04:50:07 +000022cmd_executer = command_executer.GetCommandExecuter()
asharif252df0f2013-02-15 04:46:28 +000023
24
asharifda9ac652013-02-15 04:50:09 +000025class MountPoint:
26 def __init__(self, external_dir, mount_dir, owner, options=None):
27 self.external_dir = external_dir
28 self.mount_dir = mount_dir
29 self.owner = owner
30 self.options = options
31
32
asharif556f4ff2013-02-15 04:50:35 +000033 def CreateAndOwnDir(self, dir_name):
34 retval = 0
35 if not os.path.exists(dir_name):
36 command = "mkdir -p " + dir_name
37 command += " || sudo mkdir -p " + dir_name
asharifda9ac652013-02-15 04:50:09 +000038 retval = cmd_executer.RunCommand(command)
asharif556f4ff2013-02-15 04:50:35 +000039 if retval != 0:
40 return retval
41 pw = pwd.getpwnam(self.owner)
42 if os.stat(dir_name).st_uid != pw.pw_uid:
43 command = "sudo chown -f " + self.owner + " " + dir_name
44 retval = cmd_executer.RunCommand(command)
asharifda9ac652013-02-15 04:50:09 +000045 return retval
46
47
48 def DoMount(self):
asharif556f4ff2013-02-15 04:50:35 +000049 retval = self.CreateAndOwnDir(self.mount_dir)
50 utils.AssertTrue(retval == 0)
51 retval = self.CreateAndOwnDir(self.external_dir)
52 utils.AssertTrue(retval == 0)
53 retval = self.MountDir()
54 utils.AssertTrue(retval == 0)
55 return retval
asharifda9ac652013-02-15 04:50:09 +000056
57
58 def MountDir(self):
59 command = "sudo mount --bind " + self.external_dir + " " + self.mount_dir
60 if self.options == "ro":
61 command += " && sudo mount --bind -oremount,ro " + self.mount_dir
62 retval = cmd_executer.RunCommand(command)
63 return retval
64
65
66 def __str__(self):
67 ret = ""
68 ret += self.external_dir + "\n"
69 ret += self.mount_dir + "\n"
70 if self.owner:
71 ret += self.owner + "\n"
72 if self.options:
73 ret += self.options + "\n"
74 return ret
75
76
77def Main(argv, return_output=False):
asharif252df0f2013-02-15 04:46:28 +000078 """The main function."""
79 parser = optparse.OptionParser()
80 parser.add_option("-c", "--chromeos_root", dest="chromeos_root",
asharif0b2f0402013-02-15 04:50:25 +000081 default="../..",
asharif252df0f2013-02-15 04:46:28 +000082 help="ChromeOS root checkout directory.")
83 parser.add_option("-t", "--toolchain_root", dest="toolchain_root",
84 help="Toolchain root directory.")
asharif541b6392013-02-15 04:50:38 +000085 parser.add_option("-o", "--output", dest="output",
86 help="Toolchain output directory")
87 parser.add_option("-m", "--other_mounts", dest="other_mounts",
asharifda9ac652013-02-15 04:50:09 +000088 help="Other mount points in the form: " +
89 "dir:mounted_dir:options")
asharif252df0f2013-02-15 04:46:28 +000090
raymes01959ae2013-02-15 04:50:07 +000091 relevant_argv = []
92 passthrough_argv = []
bjanakiraman0d03a172013-02-15 04:50:53 +000093 i = 0
94 while i < len(argv):
asharif0269d462013-02-15 04:46:31 +000095 found = False
96 for option in parser.option_list:
97 for long_opt in option._long_opts:
98 if argv[i].startswith(long_opt):
bjanakiraman0d03a172013-02-15 04:50:53 +000099 relevant_argv.append(argv[i])
asharif0269d462013-02-15 04:46:31 +0000100 found = True
101 break
102 for short_opt in option._short_opts:
103 if argv[i].startswith(short_opt):
bjanakiraman0d03a172013-02-15 04:50:53 +0000104 relevant_argv.append(argv[i])
105 relevant_argv.append(argv[i+1])
106 i = i + 1
asharif0269d462013-02-15 04:46:31 +0000107 found = True
108 break
109
110 if found == True:
111 break
112
bjanakiraman0d03a172013-02-15 04:50:53 +0000113 if found == False:
asharif0269d462013-02-15 04:46:31 +0000114 passthrough_argv.append(argv[i])
bjanakiraman0d03a172013-02-15 04:50:53 +0000115 i = i + 1
asharif0269d462013-02-15 04:46:31 +0000116
117 options = parser.parse_args(relevant_argv)[0]
asharif252df0f2013-02-15 04:46:28 +0000118
asharif0b2f0402013-02-15 04:50:25 +0000119 chromeos_root = options.chromeos_root
asharif17621302013-02-15 04:46:35 +0000120
121 chromeos_root = os.path.expanduser(chromeos_root)
asharifda9ac652013-02-15 04:50:09 +0000122 if options.toolchain_root:
123 options.toolchain_root = os.path.expanduser(options.toolchain_root)
asharif17621302013-02-15 04:46:35 +0000124
asharif252df0f2013-02-15 04:46:28 +0000125 chromeos_root = os.path.abspath(chromeos_root)
126
asharif0b2f0402013-02-15 04:50:25 +0000127 if options.toolchain_root is None:
128 logger.GetLogger().LogError("--toolchain_root not specified")
asharif252df0f2013-02-15 04:46:28 +0000129 parser.print_help()
130 sys.exit(1)
131
asharif28238cf2013-02-15 04:51:01 +0000132 tc_dirs = [options.toolchain_root + "/google_vendor_src_branch/gcc",
133 options.toolchain_root + "/google_vendor_src_branch/binutils"]
asharif0b2f0402013-02-15 04:50:25 +0000134
135 for tc_dir in tc_dirs:
136 if not os.path.exists(tc_dir):
asharifb5493df2013-02-15 04:50:46 +0000137 logger.GetLogger().LogError("toolchain path " +
asharif0b2f0402013-02-15 04:50:25 +0000138 tc_dir + " does not exist!")
139 parser.print_help()
140 sys.exit(1)
141
142 if not os.path.exists(chromeos_root):
143 logger.GetLogger().LogError("chromeos_root " + options.chromeos_root +
144 " does not exist!")
145 parser.print_help()
146 sys.exit(1)
147
148 if not os.path.exists(chromeos_root + "/src/scripts/enter_chroot.sh"):
149 logger.GetLogger().LogError(options.chromeos_root +
150 "/src/scripts/enter_chroot.sh"
151 " not found!")
152 parser.print_help()
153 sys.exit(1)
154
raymes01959ae2013-02-15 04:50:07 +0000155 rootdir = utils.GetRoot(sys.argv[0])[0]
asharif252df0f2013-02-15 04:46:28 +0000156 version_dir = rootdir
157
asharif252df0f2013-02-15 04:46:28 +0000158 mounted_tc_root = "/usr/local/toolchain_root"
159 full_mounted_tc_root = chromeos_root + "/chroot/" + mounted_tc_root
160 full_mounted_tc_root = os.path.abspath(full_mounted_tc_root)
asharifda9ac652013-02-15 04:50:09 +0000161
162 mount_points = []
asharif252df0f2013-02-15 04:46:28 +0000163 for tc_dir in tc_dirs:
164 last_dir = utils.GetRoot(tc_dir)[1]
asharifda9ac652013-02-15 04:50:09 +0000165 mount_point = MountPoint(tc_dir, full_mounted_tc_root + "/" + last_dir,
166 getpass.getuser(), "ro")
167 mount_points.append(mount_point)
asharif252df0f2013-02-15 04:46:28 +0000168
asharif541b6392013-02-15 04:50:38 +0000169 output = options.output
170 if output is None:
171 output = version_dir + "/output"
172 mount_points.append(MountPoint(output, full_mounted_tc_root + "/output",
173 getpass.getuser()))
asharifda9ac652013-02-15 04:50:09 +0000174 mount_points += CreateMountPointsFromString(options.other_mounts,
175 chromeos_root + "/chroot/")
176
asharif252df0f2013-02-15 04:46:28 +0000177 last_dir = utils.GetRoot(version_dir)[1]
asharifda9ac652013-02-15 04:50:09 +0000178 mount_point = MountPoint(version_dir, full_mounted_tc_root + "/" + last_dir,
179 getpass.getuser())
180 mount_points.append(mount_point)
181
182 for mount_point in mount_points:
asharif556f4ff2013-02-15 04:50:35 +0000183 retval = mount_point.DoMount()
184 if retval != 0:
185 return retval
asharif252df0f2013-02-15 04:46:28 +0000186
187 # Finally, create the symlink to build-gcc.
asharifda9ac652013-02-15 04:50:09 +0000188 command = "sudo chown " + getpass.getuser() + " " + full_mounted_tc_root
189 retval = cmd_executer.RunCommand(command)
190
asharif252df0f2013-02-15 04:46:28 +0000191 try:
192 os.symlink(last_dir + "/build-gcc", full_mounted_tc_root + "/build-gcc")
asharif28238cf2013-02-15 04:51:01 +0000193 os.symlink(last_dir + "/build-binutils", full_mounted_tc_root + "/build-binutils")
asharif252df0f2013-02-15 04:46:28 +0000194 except Exception as e:
asharif0b2f0402013-02-15 04:50:25 +0000195 logger.GetLogger().LogError(str(e))
asharif252df0f2013-02-15 04:46:28 +0000196
197 # Now call enter_chroot with the rest of the arguments.
ashariffcf8cfc2013-02-15 04:49:21 +0000198 command = chromeos_root + "/src/scripts/enter_chroot.sh"
asharif252df0f2013-02-15 04:46:28 +0000199
asharif0269d462013-02-15 04:46:31 +0000200 if len(passthrough_argv) > 1:
asharif541b6392013-02-15 04:50:38 +0000201 command += " <<EC_EOF"
asharif0269d462013-02-15 04:46:31 +0000202 command += " " + " ".join(passthrough_argv[1:])
asharif541b6392013-02-15 04:50:38 +0000203 command += "\nEC_EOF"
asharifda9ac652013-02-15 04:50:09 +0000204 retval = cmd_executer.RunCommand(command, return_output)
asharif252df0f2013-02-15 04:46:28 +0000205 return retval
206 else:
asharif556f4ff2013-02-15 04:50:35 +0000207 return os.execv(command, [""])
asharif252df0f2013-02-15 04:46:28 +0000208
209
asharifda9ac652013-02-15 04:50:09 +0000210def CreateMountPointsFromString(mount_strings, chroot_dir):
211 # String has options in the form dir:mount:options
212 mount_points = []
213 if not mount_strings:
214 return mount_points
215 mount_list = mount_strings.split()
216 for mount_string in mount_list:
217 mount_values = mount_string.split(":")
218 external_dir = mount_values[0]
219 mount_dir = mount_values[1]
220 if len(mount_values)>2:
221 options = mount_values[2]
222 else:
223 options = None
224 mount_point = MountPoint(external_dir, chroot_dir + "/" + mount_dir,
225 getpass.getuser(), options)
226 mount_points.append(mount_point)
227 return mount_points
asharif252df0f2013-02-15 04:46:28 +0000228
229
230if __name__ == "__main__":
asharife3668f12013-02-15 04:46:29 +0000231 Main(sys.argv)
asharif252df0f2013-02-15 04:46:28 +0000232