blob: 573e558504b4754f46a82cd6f9155b4128504089 [file] [log] [blame]
Caroline Tice88272d42016-01-13 09:48:29 -08001#!/usr/bin/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()
89 parser.add_argument('-c',
90 '--chromeos_root',
91 dest='chromeos_root',
92 default='../..',
93 help='ChromeOS root checkout directory.')
94 parser.add_argument('-t',
95 '--toolchain_root',
96 dest='toolchain_root',
97 help='Toolchain root directory.')
98 parser.add_argument('-o',
99 '--output',
100 dest='output',
101 help='Toolchain output directory')
102 parser.add_argument('--sudo',
103 dest='sudo',
104 action='store_true',
105 default=False,
106 help='Run the command with sudo.')
107 parser.add_argument('-r',
108 '--third_party',
109 dest='third_party',
110 help='The third_party directory to mount.')
111 parser.add_argument('-m',
112 '--other_mounts',
113 dest='other_mounts',
114 help='Other mount points in the form: '
115 'dir:mounted_dir:options')
116 parser.add_argument('-s',
117 '--mount-scripts-only',
118 dest='mount_scripts_only',
119 action='store_true',
120 default=False,
121 help='Mount only the scripts dir, and not the sources.')
122 parser.add_argument('passthrough_argv', nargs='*',
123 help='Command to be executed inside the chroot.')
124
125 options = parser.parse_args(argv)
asharif252df0f2013-02-15 04:46:28 +0000126
asharif0b2f0402013-02-15 04:50:25 +0000127 chromeos_root = options.chromeos_root
asharif17621302013-02-15 04:46:35 +0000128
129 chromeos_root = os.path.expanduser(chromeos_root)
asharifda9ac652013-02-15 04:50:09 +0000130 if options.toolchain_root:
131 options.toolchain_root = os.path.expanduser(options.toolchain_root)
asharif17621302013-02-15 04:46:35 +0000132
asharif252df0f2013-02-15 04:46:28 +0000133 chromeos_root = os.path.abspath(chromeos_root)
134
asharif8697d4e2013-02-15 09:18:09 +0000135 tc_dirs = []
asharif642509c2013-02-15 09:19:32 +0000136 if options.toolchain_root is None or options.mount_scripts_only:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800137 m = 'toolchain_root not specified. Will not mount toolchain dirs.'
asharif8697d4e2013-02-15 09:18:09 +0000138 logger.GetLogger().LogWarning(m)
139 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800140 tc_dirs = [options.toolchain_root + '/google_vendor_src_branch/gcc',
141 options.toolchain_root + '/google_vendor_src_branch/binutils']
asharif252df0f2013-02-15 04:46:28 +0000142
asharif8697d4e2013-02-15 09:18:09 +0000143 for tc_dir in tc_dirs:
144 if not os.path.exists(tc_dir):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800145 logger.GetLogger().LogError('toolchain path ' + tc_dir +
146 ' does not exist!')
asharif8697d4e2013-02-15 09:18:09 +0000147 parser.print_help()
148 sys.exit(1)
asharif0b2f0402013-02-15 04:50:25 +0000149
150 if not os.path.exists(chromeos_root):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800151 logger.GetLogger().LogError('chromeos_root ' + options.chromeos_root +
152 ' does not exist!')
asharif0b2f0402013-02-15 04:50:25 +0000153 parser.print_help()
154 sys.exit(1)
155
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800156 if not os.path.exists(chromeos_root + '/src/scripts/build_packages'):
157 logger.GetLogger(
158 ).LogError(options.chromeos_root + '/src/scripts/build_packages'
159 ' not found!')
asharif0b2f0402013-02-15 04:50:25 +0000160 parser.print_help()
161 sys.exit(1)
162
asharifb225e792013-02-15 21:20:11 +0000163 version_dir = os.path.realpath(os.path.expanduser(os.path.dirname(__file__)))
asharif252df0f2013-02-15 04:46:28 +0000164
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800165 mounted_tc_root = '/usr/local/toolchain_root'
166 full_mounted_tc_root = chromeos_root + '/chroot/' + mounted_tc_root
asharif252df0f2013-02-15 04:46:28 +0000167 full_mounted_tc_root = os.path.abspath(full_mounted_tc_root)
raymesa7d219c2013-02-15 04:56:23 +0000168
asharifda9ac652013-02-15 04:50:09 +0000169 mount_points = []
asharif8697d4e2013-02-15 09:18:09 +0000170 for tc_dir in tc_dirs:
kbaclawski20082a02013-02-16 02:12:57 +0000171 last_dir = misc.GetRoot(tc_dir)[1]
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800172 mount_point = MountPoint(tc_dir, full_mounted_tc_root + '/' + last_dir,
173 getpass.getuser(), 'ro')
asharif8697d4e2013-02-15 09:18:09 +0000174 mount_points.append(mount_point)
asharif252df0f2013-02-15 04:46:28 +0000175
asharif8697d4e2013-02-15 09:18:09 +0000176 # Add the third_party mount point if it exists
177 if options.third_party:
178 third_party_dir = options.third_party
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800179 logger.GetLogger().LogFatalIf(
180 not os.path.isdir(third_party_dir),
181 '--third_party option is not a valid dir.')
asharif8697d4e2013-02-15 09:18:09 +0000182 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800183 third_party_dir = os.path.abspath('%s/../../../third_party' %
asharif8697d4e2013-02-15 09:18:09 +0000184 os.path.dirname(__file__))
185
186 if os.path.isdir(third_party_dir):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800187 mount_point = MountPoint(third_party_dir, ('%s/%s' % (
188 full_mounted_tc_root, os.path.basename(third_party_dir))),
189 getpass.getuser())
asharif8697d4e2013-02-15 09:18:09 +0000190 mount_points.append(mount_point)
kbaclawski6999ada2013-02-15 19:57:09 +0000191
asharif541b6392013-02-15 04:50:38 +0000192 output = options.output
asharif8697d4e2013-02-15 09:18:09 +0000193 if output is None and options.toolchain_root:
asharif8697d4e2013-02-15 09:18:09 +0000194 # Mount the output directory at /usr/local/toolchain_root/output
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800195 output = options.toolchain_root + '/output'
asharif01410cc2013-02-15 09:19:31 +0000196
197 if output:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800198 mount_points.append(MountPoint(output, full_mounted_tc_root + '/output',
asharif8697d4e2013-02-15 09:18:09 +0000199 getpass.getuser()))
200
201 # Mount the other mount points
raymesa7d219c2013-02-15 04:56:23 +0000202 mount_points += CreateMountPointsFromString(options.other_mounts,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800203 chromeos_root + '/chroot/')
asharifda9ac652013-02-15 04:50:09 +0000204
kbaclawski20082a02013-02-16 02:12:57 +0000205 last_dir = misc.GetRoot(version_dir)[1]
asharif8697d4e2013-02-15 09:18:09 +0000206
207 # Mount the version dir (v14) at /usr/local/toolchain_root/v14
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800208 mount_point = MountPoint(version_dir, full_mounted_tc_root + '/' + last_dir,
asharifda9ac652013-02-15 04:50:09 +0000209 getpass.getuser())
210 mount_points.append(mount_point)
211
212 for mount_point in mount_points:
Caroline Tice88272d42016-01-13 09:48:29 -0800213 retv = mount_point.DoMount()
214 if retv != 0:
215 return retv
asharif252df0f2013-02-15 04:46:28 +0000216
217 # Finally, create the symlink to build-gcc.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800218 command = 'sudo chown ' + getpass.getuser() + ' ' + full_mounted_tc_root
Caroline Tice88272d42016-01-13 09:48:29 -0800219 retv = command_executer.GetCommandExecuter().RunCommand(command)
asharifda9ac652013-02-15 04:50:09 +0000220
asharif252df0f2013-02-15 04:46:28 +0000221 try:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800222 CreateSymlink(last_dir + '/build-gcc', full_mounted_tc_root + '/build-gcc')
223 CreateSymlink(last_dir + '/build-binutils',
224 full_mounted_tc_root + '/build-binutils')
asharif252df0f2013-02-15 04:46:28 +0000225 except Exception as e:
asharif0b2f0402013-02-15 04:50:25 +0000226 logger.GetLogger().LogError(str(e))
asharif252df0f2013-02-15 04:46:28 +0000227
asharif36666532013-02-15 21:08:14 +0000228 # Now call cros_sdk --enter with the rest of the arguments.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800229 command = 'cd %s/src/scripts && cros_sdk --enter' % chromeos_root
asharif252df0f2013-02-15 04:46:28 +0000230
Caroline Tice88272d42016-01-13 09:48:29 -0800231 if len(options.passthrough_argv) > 1:
232 inner_command = ' '.join(options.passthrough_argv[1:])
asharif51516da2013-02-15 04:56:12 +0000233 inner_command = inner_command.strip()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800234 if inner_command.startswith('-- '):
asharif51516da2013-02-15 04:56:12 +0000235 inner_command = inner_command[3:]
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800236 command_file = 'tc_enter_chroot.cmd'
237 command_file_path = chromeos_root + '/src/scripts/' + command_file
Caroline Tice88272d42016-01-13 09:48:29 -0800238 retv = command_executer.GetCommandExecuter().RunCommand('sudo rm -f ' +
239 command_file_path)
240 if retv != 0:
241 return retv
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800242 f = open(command_file_path, 'w')
asharifc0f71932013-02-15 04:56:18 +0000243 f.write(inner_command)
244 f.close()
raymesa7d219c2013-02-15 04:56:23 +0000245 logger.GetLogger().LogCmd(inner_command)
Caroline Tice88272d42016-01-13 09:48:29 -0800246 retv = command_executer.GetCommandExecuter().RunCommand('chmod +x ' +
247 command_file_path)
248 if retv != 0:
249 return retv
asharif52284c62013-02-15 19:59:08 +0000250
251 if options.sudo:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800252 command += ' sudo ./' + command_file
asharif52284c62013-02-15 19:59:08 +0000253 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800254 command += ' ./' + command_file
Caroline Tice88272d42016-01-13 09:48:29 -0800255 retv = command_executer.GetCommandExecuter().RunCommandGeneric(
Luis Lozano036c9232015-12-10 10:47:01 -0800256 command, return_output)
Caroline Tice88272d42016-01-13 09:48:29 -0800257 return retv
asharif252df0f2013-02-15 04:46:28 +0000258 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800259 os.chdir('%s/src/scripts' % chromeos_root)
asharif36666532013-02-15 21:08:14 +0000260 ce = command_executer.GetCommandExecuter()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800261 _, out, _ = ce.RunCommandWOutput('which cros_sdk')
asharif36666532013-02-15 21:08:14 +0000262 cros_sdk_binary = out.split()[0]
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800263 return os.execv(cros_sdk_binary, ['', '--enter'])
asharif252df0f2013-02-15 04:46:28 +0000264
265
asharifda9ac652013-02-15 04:50:09 +0000266def CreateMountPointsFromString(mount_strings, chroot_dir):
267 # String has options in the form dir:mount:options
268 mount_points = []
269 if not mount_strings:
270 return mount_points
271 mount_list = mount_strings.split()
272 for mount_string in mount_list:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800273 mount_values = mount_string.split(':')
asharifda9ac652013-02-15 04:50:09 +0000274 external_dir = mount_values[0]
275 mount_dir = mount_values[1]
raymesa7d219c2013-02-15 04:56:23 +0000276 if len(mount_values) > 2:
asharifda9ac652013-02-15 04:50:09 +0000277 options = mount_values[2]
278 else:
279 options = None
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800280 mount_point = MountPoint(external_dir, chroot_dir + '/' + mount_dir,
asharifda9ac652013-02-15 04:50:09 +0000281 getpass.getuser(), options)
282 mount_points.append(mount_point)
283 return mount_points
asharif252df0f2013-02-15 04:46:28 +0000284
285
asharif8a873872013-02-15 04:56:52 +0000286def CreateSymlink(target, link_name):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800287 logger.GetLogger().LogFatalIf(
288 target.startswith('/'), "Can't create symlink to absolute path!")
289 real_from_file = misc.GetRoot(link_name)[0] + '/' + target
asharif8a873872013-02-15 04:56:52 +0000290 if os.path.realpath(real_from_file) != os.path.realpath(link_name):
291 if os.path.exists(link_name):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800292 command = 'rm -rf ' + link_name
asharif8a873872013-02-15 04:56:52 +0000293 command_executer.GetCommandExecuter().RunCommand(command)
294 os.symlink(target, link_name)
295
296
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800297if __name__ == '__main__':
asharif2198c512013-02-15 09:21:35 +0000298 retval = Main(sys.argv)
299 sys.exit(retval)