blob: 4be65f8fa12309818eee41ab66d025ef0169ae6e [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")
asharif1755b432013-02-15 04:55:29 +000090 parser.add_option("-s", "--mount-scripts-only",
91 dest="mount_scripts_only",
92 action="store_true",
93 default=False,
94 help="Mount only the scripts dir, and not the sources.")
asharif252df0f2013-02-15 04:46:28 +000095
raymes01959ae2013-02-15 04:50:07 +000096 passthrough_argv = []
asharif1755b432013-02-15 04:55:29 +000097 (options, passthrough_argv) = parser.parse_args(argv)
asharif252df0f2013-02-15 04:46:28 +000098
asharif0b2f0402013-02-15 04:50:25 +000099 chromeos_root = options.chromeos_root
asharif17621302013-02-15 04:46:35 +0000100
101 chromeos_root = os.path.expanduser(chromeos_root)
asharifda9ac652013-02-15 04:50:09 +0000102 if options.toolchain_root:
103 options.toolchain_root = os.path.expanduser(options.toolchain_root)
asharif17621302013-02-15 04:46:35 +0000104
asharif252df0f2013-02-15 04:46:28 +0000105 chromeos_root = os.path.abspath(chromeos_root)
106
asharif0b2f0402013-02-15 04:50:25 +0000107 if options.toolchain_root is None:
108 logger.GetLogger().LogError("--toolchain_root not specified")
asharif252df0f2013-02-15 04:46:28 +0000109 parser.print_help()
110 sys.exit(1)
111
asharif28238cf2013-02-15 04:51:01 +0000112 tc_dirs = [options.toolchain_root + "/google_vendor_src_branch/gcc",
113 options.toolchain_root + "/google_vendor_src_branch/binutils"]
asharif0b2f0402013-02-15 04:50:25 +0000114
asharif1755b432013-02-15 04:55:29 +0000115 if options.mount_scripts_only == False:
116 for tc_dir in tc_dirs:
117 if not os.path.exists(tc_dir):
118 logger.GetLogger().LogError("toolchain path " +
119 tc_dir + " does not exist!")
120 parser.print_help()
121 sys.exit(1)
asharif0b2f0402013-02-15 04:50:25 +0000122
123 if not os.path.exists(chromeos_root):
124 logger.GetLogger().LogError("chromeos_root " + options.chromeos_root +
125 " does not exist!")
126 parser.print_help()
127 sys.exit(1)
128
129 if not os.path.exists(chromeos_root + "/src/scripts/enter_chroot.sh"):
130 logger.GetLogger().LogError(options.chromeos_root +
131 "/src/scripts/enter_chroot.sh"
132 " not found!")
133 parser.print_help()
134 sys.exit(1)
135
raymes01959ae2013-02-15 04:50:07 +0000136 rootdir = utils.GetRoot(sys.argv[0])[0]
asharif252df0f2013-02-15 04:46:28 +0000137 version_dir = rootdir
138
asharif252df0f2013-02-15 04:46:28 +0000139 mounted_tc_root = "/usr/local/toolchain_root"
140 full_mounted_tc_root = chromeos_root + "/chroot/" + mounted_tc_root
141 full_mounted_tc_root = os.path.abspath(full_mounted_tc_root)
asharifda9ac652013-02-15 04:50:09 +0000142
143 mount_points = []
asharif1755b432013-02-15 04:55:29 +0000144 if options.mount_scripts_only == False:
145 for tc_dir in tc_dirs:
146 last_dir = utils.GetRoot(tc_dir)[1]
147 mount_point = MountPoint(tc_dir, full_mounted_tc_root + "/" + last_dir,
148 getpass.getuser(), "ro")
149 mount_points.append(mount_point)
asharif252df0f2013-02-15 04:46:28 +0000150
asharif541b6392013-02-15 04:50:38 +0000151 output = options.output
152 if output is None:
153 output = version_dir + "/output"
154 mount_points.append(MountPoint(output, full_mounted_tc_root + "/output",
155 getpass.getuser()))
asharifda9ac652013-02-15 04:50:09 +0000156 mount_points += CreateMountPointsFromString(options.other_mounts,
157 chromeos_root + "/chroot/")
158
asharif252df0f2013-02-15 04:46:28 +0000159 last_dir = utils.GetRoot(version_dir)[1]
asharifda9ac652013-02-15 04:50:09 +0000160 mount_point = MountPoint(version_dir, full_mounted_tc_root + "/" + last_dir,
161 getpass.getuser())
162 mount_points.append(mount_point)
163
164 for mount_point in mount_points:
asharif556f4ff2013-02-15 04:50:35 +0000165 retval = mount_point.DoMount()
166 if retval != 0:
167 return retval
asharif252df0f2013-02-15 04:46:28 +0000168
169 # Finally, create the symlink to build-gcc.
asharifda9ac652013-02-15 04:50:09 +0000170 command = "sudo chown " + getpass.getuser() + " " + full_mounted_tc_root
171 retval = cmd_executer.RunCommand(command)
172
asharif252df0f2013-02-15 04:46:28 +0000173 try:
174 os.symlink(last_dir + "/build-gcc", full_mounted_tc_root + "/build-gcc")
asharif28238cf2013-02-15 04:51:01 +0000175 os.symlink(last_dir + "/build-binutils", full_mounted_tc_root + "/build-binutils")
asharif252df0f2013-02-15 04:46:28 +0000176 except Exception as e:
asharif0b2f0402013-02-15 04:50:25 +0000177 logger.GetLogger().LogError(str(e))
asharif252df0f2013-02-15 04:46:28 +0000178
179 # Now call enter_chroot with the rest of the arguments.
ashariffcf8cfc2013-02-15 04:49:21 +0000180 command = chromeos_root + "/src/scripts/enter_chroot.sh"
asharif252df0f2013-02-15 04:46:28 +0000181
asharif0269d462013-02-15 04:46:31 +0000182 if len(passthrough_argv) > 1:
asharif541b6392013-02-15 04:50:38 +0000183 command += " <<EC_EOF"
asharif0269d462013-02-15 04:46:31 +0000184 command += " " + " ".join(passthrough_argv[1:])
asharif541b6392013-02-15 04:50:38 +0000185 command += "\nEC_EOF"
asharifda9ac652013-02-15 04:50:09 +0000186 retval = cmd_executer.RunCommand(command, return_output)
asharif252df0f2013-02-15 04:46:28 +0000187 return retval
188 else:
asharif556f4ff2013-02-15 04:50:35 +0000189 return os.execv(command, [""])
asharif252df0f2013-02-15 04:46:28 +0000190
191
asharifda9ac652013-02-15 04:50:09 +0000192def CreateMountPointsFromString(mount_strings, chroot_dir):
193 # String has options in the form dir:mount:options
194 mount_points = []
195 if not mount_strings:
196 return mount_points
197 mount_list = mount_strings.split()
198 for mount_string in mount_list:
199 mount_values = mount_string.split(":")
200 external_dir = mount_values[0]
201 mount_dir = mount_values[1]
202 if len(mount_values)>2:
203 options = mount_values[2]
204 else:
205 options = None
206 mount_point = MountPoint(external_dir, chroot_dir + "/" + mount_dir,
207 getpass.getuser(), options)
208 mount_points.append(mount_point)
209 return mount_points
asharif252df0f2013-02-15 04:46:28 +0000210
211
212if __name__ == "__main__":
asharife3668f12013-02-15 04:46:29 +0000213 Main(sys.argv)
asharif252df0f2013-02-15 04:46:28 +0000214