blob: d919c96d6526d4670626b5453a900b806a319c29 [file] [log] [blame]
Caroline Ticef6ef4392017-04-06 17:16:05 -07001#!/usr/bin/env python2
asharif252df0f2013-02-15 04:46:28 +00002#
3# Copyright 2010 Google Inc. All Rights Reserved.
asharif252df0f2013-02-15 04:46:28 +00004"""Script to enter the ChromeOS chroot with mounted sources.
5
6This script enters the chroot with mounted sources.
7"""
8
Caroline Tice88272d42016-01-13 09:48:29 -08009from __future__ import print_function
10
Luis Lozanof2a3ef42015-12-15 13:49:30 -080011__author__ = 'asharif@google.com (Ahmad Sharif)'
asharif252df0f2013-02-15 04:46:28 +000012
Caroline Tice88272d42016-01-13 09:48:29 -080013import argparse
asharif252df0f2013-02-15 04:46:28 +000014import getpass
asharif252df0f2013-02-15 04:46:28 +000015import os
asharif556f4ff2013-02-15 04:50:35 +000016import pwd
asharif252df0f2013-02-15 04:46:28 +000017import sys
kbaclawski20082a02013-02-16 02:12:57 +000018
Caroline Tice88272d42016-01-13 09:48:29 -080019from cros_utils import command_executer
20from cros_utils import logger
21from cros_utils import misc
kbaclawski20082a02013-02-16 02:12:57 +000022
asharif252df0f2013-02-15 04:46:28 +000023
Caroline Tice88272d42016-01-13 09:48:29 -080024class MountPoint(object):
25 """Mount point class"""
Luis Lozanof2a3ef42015-12-15 13:49:30 -080026
asharifda9ac652013-02-15 04:50:09 +000027 def __init__(self, external_dir, mount_dir, owner, options=None):
asharif6c619132013-02-15 21:55:28 +000028 self.external_dir = os.path.realpath(external_dir)
29 self.mount_dir = os.path.realpath(mount_dir)
asharifda9ac652013-02-15 04:50:09 +000030 self.owner = owner
31 self.options = options
32
asharif556f4ff2013-02-15 04:50:35 +000033 def CreateAndOwnDir(self, dir_name):
Caroline Tice88272d42016-01-13 09:48:29 -080034 retv = 0
asharif556f4ff2013-02-15 04:50:35 +000035 if not os.path.exists(dir_name):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080036 command = 'mkdir -p ' + dir_name
37 command += ' || sudo mkdir -p ' + dir_name
Caroline Tice88272d42016-01-13 09:48:29 -080038 retv = command_executer.GetCommandExecuter().RunCommand(command)
39 if retv != 0:
40 return retv
asharif556f4ff2013-02-15 04:50:35 +000041 pw = pwd.getpwnam(self.owner)
42 if os.stat(dir_name).st_uid != pw.pw_uid:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080043 command = 'sudo chown -f ' + self.owner + ' ' + dir_name
Caroline Tice88272d42016-01-13 09:48:29 -080044 retv = command_executer.GetCommandExecuter().RunCommand(command)
45 return retv
asharifda9ac652013-02-15 04:50:09 +000046
asharifda9ac652013-02-15 04:50:09 +000047 def DoMount(self):
asharif6c619132013-02-15 21:55:28 +000048 ce = command_executer.GetCommandExecuter()
Luis Lozanof2a3ef42015-12-15 13:49:30 -080049 mount_signature = '%s on %s' % (self.external_dir, self.mount_dir)
50 command = 'mount'
Caroline Tice88272d42016-01-13 09:48:29 -080051 retv, out, _ = ce.RunCommandWOutput(command)
asharif6c619132013-02-15 21:55:28 +000052 if mount_signature not in out:
Caroline Tice88272d42016-01-13 09:48:29 -080053 retv = self.CreateAndOwnDir(self.mount_dir)
54 logger.GetLogger().LogFatalIf(retv, 'Cannot create mount_dir!')
55 retv = self.CreateAndOwnDir(self.external_dir)
56 logger.GetLogger().LogFatalIf(retv, 'Cannot create external_dir!')
57 retv = self.MountDir()
58 logger.GetLogger().LogFatalIf(retv, 'Cannot mount!')
59 return retv
asharif6c619132013-02-15 21:55:28 +000060 else:
61 return 0
asharifda9ac652013-02-15 04:50:09 +000062
asharifc97199a2013-02-15 22:48:45 +000063 def UnMount(self):
64 ce = command_executer.GetCommandExecuter()
Luis Lozanof2a3ef42015-12-15 13:49:30 -080065 return ce.RunCommand('sudo umount %s' % self.mount_dir)
asharifc97199a2013-02-15 22:48:45 +000066
asharifda9ac652013-02-15 04:50:09 +000067 def MountDir(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080068 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
Caroline Tice88272d42016-01-13 09:48:29 -080071 retv = command_executer.GetCommandExecuter().RunCommand(command)
72 return retv
asharifda9ac652013-02-15 04:50:09 +000073
asharifda9ac652013-02-15 04:50:09 +000074 def __str__(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080075 ret = ''
76 ret += self.external_dir + '\n'
77 ret += self.mount_dir + '\n'
asharifda9ac652013-02-15 04:50:09 +000078 if self.owner:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080079 ret += self.owner + '\n'
asharifda9ac652013-02-15 04:50:09 +000080 if self.options:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080081 ret += self.options + '\n'
asharifda9ac652013-02-15 04:50:09 +000082 return ret
83
84
85def Main(argv, return_output=False):
asharif252df0f2013-02-15 04:46:28 +000086 """The main function."""
asharif252df0f2013-02-15 04:46:28 +000087
Caroline Tice88272d42016-01-13 09:48:29 -080088 parser = argparse.ArgumentParser()
Caroline Ticef6ef4392017-04-06 17:16:05 -070089 parser.add_argument(
90 '-c',
91 '--chromeos_root',
92 dest='chromeos_root',
93 default='../..',
94 help='ChromeOS root checkout directory.')
95 parser.add_argument(
96 '-t',
97 '--toolchain_root',
98 dest='toolchain_root',
99 help='Toolchain root directory.')
100 parser.add_argument(
101 '-o', '--output', dest='output', help='Toolchain output directory')
102 parser.add_argument(
103 '--sudo',
104 dest='sudo',
105 action='store_true',
106 default=False,
107 help='Run the command with sudo.')
108 parser.add_argument(
109 '-r',
110 '--third_party',
111 dest='third_party',
112 help='The third_party directory to mount.')
113 parser.add_argument(
114 '-m',
115 '--other_mounts',
116 dest='other_mounts',
117 help='Other mount points in the form: '
118 'dir:mounted_dir:options')
119 parser.add_argument(
120 '-s',
121 '--mount-scripts-only',
122 dest='mount_scripts_only',
123 action='store_true',
124 default=False,
125 help='Mount only the scripts dir, and not the sources.')
126 parser.add_argument(
127 'passthrough_argv',
128 nargs='*',
129 help='Command to be executed inside the chroot.')
Caroline Tice88272d42016-01-13 09:48:29 -0800130
131 options = parser.parse_args(argv)
asharif252df0f2013-02-15 04:46:28 +0000132
asharif0b2f0402013-02-15 04:50:25 +0000133 chromeos_root = options.chromeos_root
asharif17621302013-02-15 04:46:35 +0000134
135 chromeos_root = os.path.expanduser(chromeos_root)
asharifda9ac652013-02-15 04:50:09 +0000136 if options.toolchain_root:
137 options.toolchain_root = os.path.expanduser(options.toolchain_root)
asharif17621302013-02-15 04:46:35 +0000138
asharif252df0f2013-02-15 04:46:28 +0000139 chromeos_root = os.path.abspath(chromeos_root)
140
asharif8697d4e2013-02-15 09:18:09 +0000141 tc_dirs = []
asharif642509c2013-02-15 09:19:32 +0000142 if options.toolchain_root is None or options.mount_scripts_only:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800143 m = 'toolchain_root not specified. Will not mount toolchain dirs.'
asharif8697d4e2013-02-15 09:18:09 +0000144 logger.GetLogger().LogWarning(m)
145 else:
Caroline Ticef6ef4392017-04-06 17:16:05 -0700146 tc_dirs = [
147 options.toolchain_root + '/google_vendor_src_branch/gcc',
148 options.toolchain_root + '/google_vendor_src_branch/binutils'
149 ]
asharif252df0f2013-02-15 04:46:28 +0000150
asharif8697d4e2013-02-15 09:18:09 +0000151 for tc_dir in tc_dirs:
152 if not os.path.exists(tc_dir):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800153 logger.GetLogger().LogError('toolchain path ' + tc_dir +
154 ' does not exist!')
asharif8697d4e2013-02-15 09:18:09 +0000155 parser.print_help()
156 sys.exit(1)
asharif0b2f0402013-02-15 04:50:25 +0000157
158 if not os.path.exists(chromeos_root):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800159 logger.GetLogger().LogError('chromeos_root ' + options.chromeos_root +
160 ' does not exist!')
asharif0b2f0402013-02-15 04:50:25 +0000161 parser.print_help()
162 sys.exit(1)
163
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800164 if not os.path.exists(chromeos_root + '/src/scripts/build_packages'):
Caroline Ticef6ef4392017-04-06 17:16:05 -0700165 logger.GetLogger().LogError(options.chromeos_root +
166 '/src/scripts/build_packages'
167 ' not found!')
asharif0b2f0402013-02-15 04:50:25 +0000168 parser.print_help()
169 sys.exit(1)
170
asharifb225e792013-02-15 21:20:11 +0000171 version_dir = os.path.realpath(os.path.expanduser(os.path.dirname(__file__)))
asharif252df0f2013-02-15 04:46:28 +0000172
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800173 mounted_tc_root = '/usr/local/toolchain_root'
174 full_mounted_tc_root = chromeos_root + '/chroot/' + mounted_tc_root
asharif252df0f2013-02-15 04:46:28 +0000175 full_mounted_tc_root = os.path.abspath(full_mounted_tc_root)
raymesa7d219c2013-02-15 04:56:23 +0000176
asharifda9ac652013-02-15 04:50:09 +0000177 mount_points = []
asharif8697d4e2013-02-15 09:18:09 +0000178 for tc_dir in tc_dirs:
kbaclawski20082a02013-02-16 02:12:57 +0000179 last_dir = misc.GetRoot(tc_dir)[1]
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800180 mount_point = MountPoint(tc_dir, full_mounted_tc_root + '/' + last_dir,
181 getpass.getuser(), 'ro')
asharif8697d4e2013-02-15 09:18:09 +0000182 mount_points.append(mount_point)
asharif252df0f2013-02-15 04:46:28 +0000183
asharif8697d4e2013-02-15 09:18:09 +0000184 # Add the third_party mount point if it exists
185 if options.third_party:
186 third_party_dir = options.third_party
Caroline Ticef6ef4392017-04-06 17:16:05 -0700187 logger.GetLogger().LogFatalIf(not os.path.isdir(third_party_dir),
188 '--third_party option is not a valid dir.')
asharif8697d4e2013-02-15 09:18:09 +0000189 else:
Caroline Ticef6ef4392017-04-06 17:16:05 -0700190 third_party_dir = os.path.abspath(
191 '%s/../../../third_party' % os.path.dirname(__file__))
asharif8697d4e2013-02-15 09:18:09 +0000192
193 if os.path.isdir(third_party_dir):
Caroline Ticef6ef4392017-04-06 17:16:05 -0700194 mount_point = MountPoint(third_party_dir,
195 ('%s/%s' % (full_mounted_tc_root,
196 os.path.basename(third_party_dir))),
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800197 getpass.getuser())
asharif8697d4e2013-02-15 09:18:09 +0000198 mount_points.append(mount_point)
kbaclawski6999ada2013-02-15 19:57:09 +0000199
asharif541b6392013-02-15 04:50:38 +0000200 output = options.output
asharif8697d4e2013-02-15 09:18:09 +0000201 if output is None and options.toolchain_root:
asharif8697d4e2013-02-15 09:18:09 +0000202 # Mount the output directory at /usr/local/toolchain_root/output
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800203 output = options.toolchain_root + '/output'
asharif01410cc2013-02-15 09:19:31 +0000204
205 if output:
Caroline Ticef6ef4392017-04-06 17:16:05 -0700206 mount_points.append(
207 MountPoint(output, full_mounted_tc_root + '/output', getpass.getuser()))
asharif8697d4e2013-02-15 09:18:09 +0000208
209 # Mount the other mount points
raymesa7d219c2013-02-15 04:56:23 +0000210 mount_points += CreateMountPointsFromString(options.other_mounts,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800211 chromeos_root + '/chroot/')
asharifda9ac652013-02-15 04:50:09 +0000212
kbaclawski20082a02013-02-16 02:12:57 +0000213 last_dir = misc.GetRoot(version_dir)[1]
asharif8697d4e2013-02-15 09:18:09 +0000214
215 # Mount the version dir (v14) at /usr/local/toolchain_root/v14
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800216 mount_point = MountPoint(version_dir, full_mounted_tc_root + '/' + last_dir,
asharifda9ac652013-02-15 04:50:09 +0000217 getpass.getuser())
218 mount_points.append(mount_point)
219
220 for mount_point in mount_points:
Caroline Tice88272d42016-01-13 09:48:29 -0800221 retv = mount_point.DoMount()
222 if retv != 0:
223 return retv
asharif252df0f2013-02-15 04:46:28 +0000224
225 # Finally, create the symlink to build-gcc.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800226 command = 'sudo chown ' + getpass.getuser() + ' ' + full_mounted_tc_root
Caroline Tice88272d42016-01-13 09:48:29 -0800227 retv = command_executer.GetCommandExecuter().RunCommand(command)
asharifda9ac652013-02-15 04:50:09 +0000228
asharif252df0f2013-02-15 04:46:28 +0000229 try:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800230 CreateSymlink(last_dir + '/build-gcc', full_mounted_tc_root + '/build-gcc')
231 CreateSymlink(last_dir + '/build-binutils',
232 full_mounted_tc_root + '/build-binutils')
asharif252df0f2013-02-15 04:46:28 +0000233 except Exception as e:
asharif0b2f0402013-02-15 04:50:25 +0000234 logger.GetLogger().LogError(str(e))
asharif252df0f2013-02-15 04:46:28 +0000235
asharif36666532013-02-15 21:08:14 +0000236 # Now call cros_sdk --enter with the rest of the arguments.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800237 command = 'cd %s/src/scripts && cros_sdk --enter' % chromeos_root
asharif252df0f2013-02-15 04:46:28 +0000238
Caroline Tice88272d42016-01-13 09:48:29 -0800239 if len(options.passthrough_argv) > 1:
240 inner_command = ' '.join(options.passthrough_argv[1:])
asharif51516da2013-02-15 04:56:12 +0000241 inner_command = inner_command.strip()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800242 if inner_command.startswith('-- '):
asharif51516da2013-02-15 04:56:12 +0000243 inner_command = inner_command[3:]
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800244 command_file = 'tc_enter_chroot.cmd'
245 command_file_path = chromeos_root + '/src/scripts/' + command_file
Caroline Ticef6ef4392017-04-06 17:16:05 -0700246 retv = command_executer.GetCommandExecuter().RunCommand(
247 'sudo rm -f ' + command_file_path)
Caroline Tice88272d42016-01-13 09:48:29 -0800248 if retv != 0:
249 return retv
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800250 f = open(command_file_path, 'w')
asharifc0f71932013-02-15 04:56:18 +0000251 f.write(inner_command)
252 f.close()
raymesa7d219c2013-02-15 04:56:23 +0000253 logger.GetLogger().LogCmd(inner_command)
Caroline Ticef6ef4392017-04-06 17:16:05 -0700254 retv = command_executer.GetCommandExecuter().RunCommand(
255 'chmod +x ' + command_file_path)
Caroline Tice88272d42016-01-13 09:48:29 -0800256 if retv != 0:
257 return retv
asharif52284c62013-02-15 19:59:08 +0000258
259 if options.sudo:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800260 command += ' sudo ./' + command_file
asharif52284c62013-02-15 19:59:08 +0000261 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800262 command += ' ./' + command_file
Caroline Tice88272d42016-01-13 09:48:29 -0800263 retv = command_executer.GetCommandExecuter().RunCommandGeneric(
Luis Lozano036c9232015-12-10 10:47:01 -0800264 command, return_output)
Caroline Tice88272d42016-01-13 09:48:29 -0800265 return retv
asharif252df0f2013-02-15 04:46:28 +0000266 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800267 os.chdir('%s/src/scripts' % chromeos_root)
asharif36666532013-02-15 21:08:14 +0000268 ce = command_executer.GetCommandExecuter()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800269 _, out, _ = ce.RunCommandWOutput('which cros_sdk')
asharif36666532013-02-15 21:08:14 +0000270 cros_sdk_binary = out.split()[0]
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800271 return os.execv(cros_sdk_binary, ['', '--enter'])
asharif252df0f2013-02-15 04:46:28 +0000272
273
asharifda9ac652013-02-15 04:50:09 +0000274def CreateMountPointsFromString(mount_strings, chroot_dir):
275 # String has options in the form dir:mount:options
276 mount_points = []
277 if not mount_strings:
278 return mount_points
279 mount_list = mount_strings.split()
280 for mount_string in mount_list:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800281 mount_values = mount_string.split(':')
asharifda9ac652013-02-15 04:50:09 +0000282 external_dir = mount_values[0]
283 mount_dir = mount_values[1]
raymesa7d219c2013-02-15 04:56:23 +0000284 if len(mount_values) > 2:
asharifda9ac652013-02-15 04:50:09 +0000285 options = mount_values[2]
286 else:
287 options = None
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800288 mount_point = MountPoint(external_dir, chroot_dir + '/' + mount_dir,
asharifda9ac652013-02-15 04:50:09 +0000289 getpass.getuser(), options)
290 mount_points.append(mount_point)
291 return mount_points
asharif252df0f2013-02-15 04:46:28 +0000292
293
asharif8a873872013-02-15 04:56:52 +0000294def CreateSymlink(target, link_name):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800295 logger.GetLogger().LogFatalIf(
296 target.startswith('/'), "Can't create symlink to absolute path!")
297 real_from_file = misc.GetRoot(link_name)[0] + '/' + target
asharif8a873872013-02-15 04:56:52 +0000298 if os.path.realpath(real_from_file) != os.path.realpath(link_name):
299 if os.path.exists(link_name):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800300 command = 'rm -rf ' + link_name
asharif8a873872013-02-15 04:56:52 +0000301 command_executer.GetCommandExecuter().RunCommand(command)
302 os.symlink(target, link_name)
303
304
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800305if __name__ == '__main__':
asharif2198c512013-02-15 09:21:35 +0000306 retval = Main(sys.argv)
307 sys.exit(retval)