blob: c1ab7f85c366ff9a5beb2318744a495a92bc2576 [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
15import sys
raymes01959ae2013-02-15 04:50:07 +000016from utils import command_executer
17from utils import logger
asharif252df0f2013-02-15 04:46:28 +000018from utils import utils
19
20# Common initializations
raymes01959ae2013-02-15 04:50:07 +000021cmd_executer = command_executer.GetCommandExecuter()
asharif252df0f2013-02-15 04:46:28 +000022
23
asharifda9ac652013-02-15 04:50:09 +000024class MountPoint:
25 def __init__(self, external_dir, mount_dir, owner, options=None):
26 self.external_dir = external_dir
27 self.mount_dir = mount_dir
28 self.owner = owner
29 self.options = options
30
31
32 def CreateMountPoint(self):
33 if not os.path.exists(self.mount_dir):
34 command = "mkdir -p " + self.mount_dir
35 command += " || sudo mkdir -p " + self.mount_dir
36 retval = cmd_executer.RunCommand(command)
37 if retval != 0:
38 return retval
39 command = "sudo chown " + self.owner + " " + self.mount_dir
40 retval = cmd_executer.RunCommand(command)
41 return retval
42
43
44 def DoMount(self):
45 self.CreateMountPoint()
46 self.MountDir()
47
48
49 def MountDir(self):
50 command = "sudo mount --bind " + self.external_dir + " " + self.mount_dir
51 if self.options == "ro":
52 command += " && sudo mount --bind -oremount,ro " + self.mount_dir
53 retval = cmd_executer.RunCommand(command)
54 return retval
55
56
57 def __str__(self):
58 ret = ""
59 ret += self.external_dir + "\n"
60 ret += self.mount_dir + "\n"
61 if self.owner:
62 ret += self.owner + "\n"
63 if self.options:
64 ret += self.options + "\n"
65 return ret
66
67
68def Main(argv, return_output=False):
asharif252df0f2013-02-15 04:46:28 +000069 """The main function."""
70 parser = optparse.OptionParser()
71 parser.add_option("-c", "--chromeos_root", dest="chromeos_root",
asharif0b2f0402013-02-15 04:50:25 +000072 default="../..",
asharif252df0f2013-02-15 04:46:28 +000073 help="ChromeOS root checkout directory.")
74 parser.add_option("-t", "--toolchain_root", dest="toolchain_root",
75 help="Toolchain root directory.")
asharifda9ac652013-02-15 04:50:09 +000076 parser.add_option("-o", "--other_mounts", dest="other_mounts",
77 help="Other mount points in the form: " +
78 "dir:mounted_dir:options")
asharif252df0f2013-02-15 04:46:28 +000079
raymes01959ae2013-02-15 04:50:07 +000080 relevant_argv = []
81 passthrough_argv = []
asharif0269d462013-02-15 04:46:31 +000082 for i in xrange(len(argv)):
83 found = False
84 for option in parser.option_list:
85 for long_opt in option._long_opts:
86 if argv[i].startswith(long_opt):
87 found = True
88 break
89 for short_opt in option._short_opts:
90 if argv[i].startswith(short_opt):
91 found = True
92 break
93
94 if found == True:
95 break
96
97 if found == True:
98 relevant_argv.append(argv[i])
99 else:
100 passthrough_argv.append(argv[i])
101
102 options = parser.parse_args(relevant_argv)[0]
asharif252df0f2013-02-15 04:46:28 +0000103
asharif0b2f0402013-02-15 04:50:25 +0000104 chromeos_root = options.chromeos_root
asharif17621302013-02-15 04:46:35 +0000105
106 chromeos_root = os.path.expanduser(chromeos_root)
asharifda9ac652013-02-15 04:50:09 +0000107 if options.toolchain_root:
108 options.toolchain_root = os.path.expanduser(options.toolchain_root)
asharif17621302013-02-15 04:46:35 +0000109
asharif252df0f2013-02-15 04:46:28 +0000110 chromeos_root = os.path.abspath(chromeos_root)
111
asharif0b2f0402013-02-15 04:50:25 +0000112 if options.toolchain_root is None:
113 logger.GetLogger().LogError("--toolchain_root not specified")
asharif252df0f2013-02-15 04:46:28 +0000114 parser.print_help()
115 sys.exit(1)
116
117 tc_dirs = [options.toolchain_root + "/google_vendor_src_branch/gcc"]
asharif0b2f0402013-02-15 04:50:25 +0000118
119 for tc_dir in tc_dirs:
120 if not os.path.exists(tc_dir):
121 logger.GetLogger().LogError("toolchain path " + options.toolchain_root +
122 tc_dir + " does not exist!")
123 parser.print_help()
124 sys.exit(1)
125
126 if not os.path.exists(chromeos_root):
127 logger.GetLogger().LogError("chromeos_root " + options.chromeos_root +
128 " does not exist!")
129 parser.print_help()
130 sys.exit(1)
131
132 if not os.path.exists(chromeos_root + "/src/scripts/enter_chroot.sh"):
133 logger.GetLogger().LogError(options.chromeos_root +
134 "/src/scripts/enter_chroot.sh"
135 " not found!")
136 parser.print_help()
137 sys.exit(1)
138
raymes01959ae2013-02-15 04:50:07 +0000139 rootdir = utils.GetRoot(sys.argv[0])[0]
asharif252df0f2013-02-15 04:46:28 +0000140 version_dir = rootdir
141
asharif252df0f2013-02-15 04:46:28 +0000142 mounted_tc_root = "/usr/local/toolchain_root"
143 full_mounted_tc_root = chromeos_root + "/chroot/" + mounted_tc_root
144 full_mounted_tc_root = os.path.abspath(full_mounted_tc_root)
asharifda9ac652013-02-15 04:50:09 +0000145
146 mount_points = []
asharif252df0f2013-02-15 04:46:28 +0000147 for tc_dir in tc_dirs:
148 last_dir = utils.GetRoot(tc_dir)[1]
asharifda9ac652013-02-15 04:50:09 +0000149 mount_point = MountPoint(tc_dir, full_mounted_tc_root + "/" + last_dir,
150 getpass.getuser(), "ro")
151 mount_points.append(mount_point)
asharif252df0f2013-02-15 04:46:28 +0000152
asharifda9ac652013-02-15 04:50:09 +0000153 mount_points += CreateMountPointsFromString(options.other_mounts,
154 chromeos_root + "/chroot/")
155
asharif252df0f2013-02-15 04:46:28 +0000156 last_dir = utils.GetRoot(version_dir)[1]
asharifda9ac652013-02-15 04:50:09 +0000157 mount_point = MountPoint(version_dir, full_mounted_tc_root + "/" + last_dir,
158 getpass.getuser())
159 mount_points.append(mount_point)
160
161 for mount_point in mount_points:
162 mount_point.DoMount()
asharif252df0f2013-02-15 04:46:28 +0000163
164 # Finally, create the symlink to build-gcc.
asharifda9ac652013-02-15 04:50:09 +0000165 command = "sudo chown " + getpass.getuser() + " " + full_mounted_tc_root
166 retval = cmd_executer.RunCommand(command)
167
asharif252df0f2013-02-15 04:46:28 +0000168 try:
169 os.symlink(last_dir + "/build-gcc", full_mounted_tc_root + "/build-gcc")
170 except Exception as e:
asharif0b2f0402013-02-15 04:50:25 +0000171 logger.GetLogger().LogError(str(e))
asharif252df0f2013-02-15 04:46:28 +0000172
173 # Now call enter_chroot with the rest of the arguments.
ashariffcf8cfc2013-02-15 04:49:21 +0000174 command = chromeos_root + "/src/scripts/enter_chroot.sh"
asharif252df0f2013-02-15 04:46:28 +0000175
asharif0269d462013-02-15 04:46:31 +0000176 if len(passthrough_argv) > 1:
177 command += " " + " ".join(passthrough_argv[1:])
asharifda9ac652013-02-15 04:50:09 +0000178 retval = cmd_executer.RunCommand(command, return_output)
asharif252df0f2013-02-15 04:46:28 +0000179 return retval
180 else:
181 os.execv(command, [""])
182
183
asharifda9ac652013-02-15 04:50:09 +0000184def CreateMountPointsFromString(mount_strings, chroot_dir):
185 # String has options in the form dir:mount:options
186 mount_points = []
187 if not mount_strings:
188 return mount_points
189 mount_list = mount_strings.split()
190 for mount_string in mount_list:
191 mount_values = mount_string.split(":")
192 external_dir = mount_values[0]
193 mount_dir = mount_values[1]
194 if len(mount_values)>2:
195 options = mount_values[2]
196 else:
197 options = None
198 mount_point = MountPoint(external_dir, chroot_dir + "/" + mount_dir,
199 getpass.getuser(), options)
200 mount_points.append(mount_point)
201 return mount_points
asharif252df0f2013-02-15 04:46:28 +0000202
203
204if __name__ == "__main__":
asharife3668f12013-02-15 04:46:29 +0000205 Main(sys.argv)
asharif252df0f2013-02-15 04:46:28 +0000206