blob: 39bb7dc4b1e6783dc5557e7a59a41a93ded17066 [file] [log] [blame]
Mike Frysingerc7f15932013-03-20 13:43:35 -04001#!/usr/bin/python
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
Luis Lozanof2a3ef42015-12-15 13:49:30 -08009__author__ = 'asharif@google.com (Ahmad Sharif)'
asharif252df0f2013-02-15 04:46:28 +000010
11import getpass
12import optparse
13import os
asharif556f4ff2013-02-15 04:50:35 +000014import pwd
asharifc0f71932013-02-15 04:56:18 +000015import stat
asharif252df0f2013-02-15 04:46:28 +000016import sys
kbaclawski20082a02013-02-16 02:12:57 +000017
raymes01959ae2013-02-15 04:50:07 +000018from utils import command_executer
19from utils import logger
kbaclawski20082a02013-02-16 02:12:57 +000020from utils import misc
21
asharif252df0f2013-02-15 04:46:28 +000022
asharifda9ac652013-02-15 04:50:09 +000023class MountPoint:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080024
asharifda9ac652013-02-15 04:50:09 +000025 def __init__(self, external_dir, mount_dir, owner, options=None):
asharif6c619132013-02-15 21:55:28 +000026 self.external_dir = os.path.realpath(external_dir)
27 self.mount_dir = os.path.realpath(mount_dir)
asharifda9ac652013-02-15 04:50:09 +000028 self.owner = owner
29 self.options = options
30
asharif556f4ff2013-02-15 04:50:35 +000031 def CreateAndOwnDir(self, dir_name):
32 retval = 0
33 if not os.path.exists(dir_name):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080034 command = 'mkdir -p ' + dir_name
35 command += ' || sudo mkdir -p ' + dir_name
asharif8a873872013-02-15 04:56:52 +000036 retval = command_executer.GetCommandExecuter().RunCommand(command)
asharif556f4ff2013-02-15 04:50:35 +000037 if retval != 0:
38 return retval
39 pw = pwd.getpwnam(self.owner)
40 if os.stat(dir_name).st_uid != pw.pw_uid:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080041 command = 'sudo chown -f ' + self.owner + ' ' + dir_name
asharif8a873872013-02-15 04:56:52 +000042 retval = command_executer.GetCommandExecuter().RunCommand(command)
asharifda9ac652013-02-15 04:50:09 +000043 return retval
44
asharifda9ac652013-02-15 04:50:09 +000045 def DoMount(self):
asharif6c619132013-02-15 21:55:28 +000046 ce = command_executer.GetCommandExecuter()
Luis Lozanof2a3ef42015-12-15 13:49:30 -080047 mount_signature = '%s on %s' % (self.external_dir, self.mount_dir)
48 command = 'mount'
Luis Lozano036c9232015-12-10 10:47:01 -080049 retval, out, err = ce.RunCommandWOutput(command)
asharif6c619132013-02-15 21:55:28 +000050 if mount_signature not in out:
51 retval = self.CreateAndOwnDir(self.mount_dir)
Luis Lozanof2a3ef42015-12-15 13:49:30 -080052 logger.GetLogger().LogFatalIf(retval, 'Cannot create mount_dir!')
asharif6c619132013-02-15 21:55:28 +000053 retval = self.CreateAndOwnDir(self.external_dir)
Luis Lozanof2a3ef42015-12-15 13:49:30 -080054 logger.GetLogger().LogFatalIf(retval, 'Cannot create external_dir!')
asharif6c619132013-02-15 21:55:28 +000055 retval = self.MountDir()
Luis Lozanof2a3ef42015-12-15 13:49:30 -080056 logger.GetLogger().LogFatalIf(retval, 'Cannot mount!')
asharif6c619132013-02-15 21:55:28 +000057 return retval
58 else:
59 return 0
asharifda9ac652013-02-15 04:50:09 +000060
asharifc97199a2013-02-15 22:48:45 +000061 def UnMount(self):
62 ce = command_executer.GetCommandExecuter()
Luis Lozanof2a3ef42015-12-15 13:49:30 -080063 return ce.RunCommand('sudo umount %s' % self.mount_dir)
asharifc97199a2013-02-15 22:48:45 +000064
asharifda9ac652013-02-15 04:50:09 +000065 def MountDir(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080066 command = 'sudo mount --bind ' + self.external_dir + ' ' + self.mount_dir
67 if self.options == 'ro':
68 command += ' && sudo mount --bind -oremount,ro ' + self.mount_dir
asharif8a873872013-02-15 04:56:52 +000069 retval = command_executer.GetCommandExecuter().RunCommand(command)
asharifda9ac652013-02-15 04:50:09 +000070 return retval
71
asharifda9ac652013-02-15 04:50:09 +000072 def __str__(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080073 ret = ''
74 ret += self.external_dir + '\n'
75 ret += self.mount_dir + '\n'
asharifda9ac652013-02-15 04:50:09 +000076 if self.owner:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080077 ret += self.owner + '\n'
asharifda9ac652013-02-15 04:50:09 +000078 if self.options:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080079 ret += self.options + '\n'
asharifda9ac652013-02-15 04:50:09 +000080 return ret
81
82
83def Main(argv, return_output=False):
asharif252df0f2013-02-15 04:46:28 +000084 """The main function."""
85 parser = optparse.OptionParser()
Luis Lozanof2a3ef42015-12-15 13:49:30 -080086 parser.add_option('-c',
87 '--chromeos_root',
88 dest='chromeos_root',
89 default='../..',
90 help='ChromeOS root checkout directory.')
91 parser.add_option('-t',
92 '--toolchain_root',
93 dest='toolchain_root',
94 help='Toolchain root directory.')
95 parser.add_option('-o',
96 '--output',
97 dest='output',
98 help='Toolchain output directory')
99 parser.add_option('--sudo',
100 dest='sudo',
101 action='store_true',
asharif52284c62013-02-15 19:59:08 +0000102 default=False,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800103 help='Run the command with sudo.')
104 parser.add_option('-r',
105 '--third_party',
106 dest='third_party',
107 help='The third_party directory to mount.')
108 parser.add_option(
109 '-m',
110 '--other_mounts',
111 dest='other_mounts',
112 help='Other mount points in the form: ' + 'dir:mounted_dir:options')
113 parser.add_option('-s',
114 '--mount-scripts-only',
115 dest='mount_scripts_only',
116 action='store_true',
asharif1755b432013-02-15 04:55:29 +0000117 default=False,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800118 help='Mount only the scripts dir, and not the sources.')
asharif252df0f2013-02-15 04:46:28 +0000119
raymes01959ae2013-02-15 04:50:07 +0000120 passthrough_argv = []
asharif1755b432013-02-15 04:55:29 +0000121 (options, passthrough_argv) = parser.parse_args(argv)
asharif252df0f2013-02-15 04:46:28 +0000122
asharif0b2f0402013-02-15 04:50:25 +0000123 chromeos_root = options.chromeos_root
asharif17621302013-02-15 04:46:35 +0000124
125 chromeos_root = os.path.expanduser(chromeos_root)
asharifda9ac652013-02-15 04:50:09 +0000126 if options.toolchain_root:
127 options.toolchain_root = os.path.expanduser(options.toolchain_root)
asharif17621302013-02-15 04:46:35 +0000128
asharif252df0f2013-02-15 04:46:28 +0000129 chromeos_root = os.path.abspath(chromeos_root)
130
asharif8697d4e2013-02-15 09:18:09 +0000131 tc_dirs = []
asharif642509c2013-02-15 09:19:32 +0000132 if options.toolchain_root is None or options.mount_scripts_only:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800133 m = 'toolchain_root not specified. Will not mount toolchain dirs.'
asharif8697d4e2013-02-15 09:18:09 +0000134 logger.GetLogger().LogWarning(m)
135 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800136 tc_dirs = [options.toolchain_root + '/google_vendor_src_branch/gcc',
137 options.toolchain_root + '/google_vendor_src_branch/binutils']
asharif252df0f2013-02-15 04:46:28 +0000138
asharif8697d4e2013-02-15 09:18:09 +0000139 for tc_dir in tc_dirs:
140 if not os.path.exists(tc_dir):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800141 logger.GetLogger().LogError('toolchain path ' + tc_dir +
142 ' does not exist!')
asharif8697d4e2013-02-15 09:18:09 +0000143 parser.print_help()
144 sys.exit(1)
asharif0b2f0402013-02-15 04:50:25 +0000145
146 if not os.path.exists(chromeos_root):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800147 logger.GetLogger().LogError('chromeos_root ' + options.chromeos_root +
148 ' does not exist!')
asharif0b2f0402013-02-15 04:50:25 +0000149 parser.print_help()
150 sys.exit(1)
151
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800152 if not os.path.exists(chromeos_root + '/src/scripts/build_packages'):
153 logger.GetLogger(
154 ).LogError(options.chromeos_root + '/src/scripts/build_packages'
155 ' not found!')
asharif0b2f0402013-02-15 04:50:25 +0000156 parser.print_help()
157 sys.exit(1)
158
asharifb225e792013-02-15 21:20:11 +0000159 version_dir = os.path.realpath(os.path.expanduser(os.path.dirname(__file__)))
asharif252df0f2013-02-15 04:46:28 +0000160
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800161 mounted_tc_root = '/usr/local/toolchain_root'
162 full_mounted_tc_root = chromeos_root + '/chroot/' + mounted_tc_root
asharif252df0f2013-02-15 04:46:28 +0000163 full_mounted_tc_root = os.path.abspath(full_mounted_tc_root)
raymesa7d219c2013-02-15 04:56:23 +0000164
asharifda9ac652013-02-15 04:50:09 +0000165 mount_points = []
asharif8697d4e2013-02-15 09:18:09 +0000166 for tc_dir in tc_dirs:
kbaclawski20082a02013-02-16 02:12:57 +0000167 last_dir = misc.GetRoot(tc_dir)[1]
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800168 mount_point = MountPoint(tc_dir, full_mounted_tc_root + '/' + last_dir,
169 getpass.getuser(), 'ro')
asharif8697d4e2013-02-15 09:18:09 +0000170 mount_points.append(mount_point)
asharif252df0f2013-02-15 04:46:28 +0000171
asharif8697d4e2013-02-15 09:18:09 +0000172 # Add the third_party mount point if it exists
173 if options.third_party:
174 third_party_dir = options.third_party
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800175 logger.GetLogger().LogFatalIf(
176 not os.path.isdir(third_party_dir),
177 '--third_party option is not a valid dir.')
asharif8697d4e2013-02-15 09:18:09 +0000178 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800179 third_party_dir = os.path.abspath('%s/../../../third_party' %
asharif8697d4e2013-02-15 09:18:09 +0000180 os.path.dirname(__file__))
181
182 if os.path.isdir(third_party_dir):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800183 mount_point = MountPoint(third_party_dir, ('%s/%s' % (
184 full_mounted_tc_root, os.path.basename(third_party_dir))),
185 getpass.getuser())
asharif8697d4e2013-02-15 09:18:09 +0000186 mount_points.append(mount_point)
kbaclawski6999ada2013-02-15 19:57:09 +0000187
asharif541b6392013-02-15 04:50:38 +0000188 output = options.output
asharif8697d4e2013-02-15 09:18:09 +0000189 if output is None and options.toolchain_root:
asharif8697d4e2013-02-15 09:18:09 +0000190 # Mount the output directory at /usr/local/toolchain_root/output
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800191 output = options.toolchain_root + '/output'
asharif01410cc2013-02-15 09:19:31 +0000192
193 if output:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800194 mount_points.append(MountPoint(output, full_mounted_tc_root + '/output',
asharif8697d4e2013-02-15 09:18:09 +0000195 getpass.getuser()))
196
197 # Mount the other mount points
raymesa7d219c2013-02-15 04:56:23 +0000198 mount_points += CreateMountPointsFromString(options.other_mounts,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800199 chromeos_root + '/chroot/')
asharifda9ac652013-02-15 04:50:09 +0000200
kbaclawski20082a02013-02-16 02:12:57 +0000201 last_dir = misc.GetRoot(version_dir)[1]
asharif8697d4e2013-02-15 09:18:09 +0000202
203 # Mount the version dir (v14) at /usr/local/toolchain_root/v14
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800204 mount_point = MountPoint(version_dir, full_mounted_tc_root + '/' + last_dir,
asharifda9ac652013-02-15 04:50:09 +0000205 getpass.getuser())
206 mount_points.append(mount_point)
207
208 for mount_point in mount_points:
asharif556f4ff2013-02-15 04:50:35 +0000209 retval = mount_point.DoMount()
210 if retval != 0:
211 return retval
asharif252df0f2013-02-15 04:46:28 +0000212
213 # Finally, create the symlink to build-gcc.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800214 command = 'sudo chown ' + getpass.getuser() + ' ' + full_mounted_tc_root
asharif8a873872013-02-15 04:56:52 +0000215 retval = command_executer.GetCommandExecuter().RunCommand(command)
asharifda9ac652013-02-15 04:50:09 +0000216
asharif252df0f2013-02-15 04:46:28 +0000217 try:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800218 CreateSymlink(last_dir + '/build-gcc', full_mounted_tc_root + '/build-gcc')
219 CreateSymlink(last_dir + '/build-binutils',
220 full_mounted_tc_root + '/build-binutils')
asharif252df0f2013-02-15 04:46:28 +0000221 except Exception as e:
asharif0b2f0402013-02-15 04:50:25 +0000222 logger.GetLogger().LogError(str(e))
asharif252df0f2013-02-15 04:46:28 +0000223
asharif36666532013-02-15 21:08:14 +0000224 # Now call cros_sdk --enter with the rest of the arguments.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800225 command = 'cd %s/src/scripts && cros_sdk --enter' % chromeos_root
asharif252df0f2013-02-15 04:46:28 +0000226
asharif0269d462013-02-15 04:46:31 +0000227 if len(passthrough_argv) > 1:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800228 inner_command = ' '.join(passthrough_argv[1:])
asharif51516da2013-02-15 04:56:12 +0000229 inner_command = inner_command.strip()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800230 if inner_command.startswith('-- '):
asharif51516da2013-02-15 04:56:12 +0000231 inner_command = inner_command[3:]
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800232 command_file = 'tc_enter_chroot.cmd'
233 command_file_path = chromeos_root + '/src/scripts/' + command_file
234 retval = command_executer.GetCommandExecuter().RunCommand('sudo rm -f ' +
235 command_file_path)
asharifc0f71932013-02-15 04:56:18 +0000236 if retval != 0:
237 return retval
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800238 f = open(command_file_path, 'w')
asharifc0f71932013-02-15 04:56:18 +0000239 f.write(inner_command)
240 f.close()
raymesa7d219c2013-02-15 04:56:23 +0000241 logger.GetLogger().LogCmd(inner_command)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800242 retval = command_executer.GetCommandExecuter().RunCommand('chmod +x ' +
243 command_file_path)
asharifc0f71932013-02-15 04:56:18 +0000244 if retval != 0:
245 return retval
asharif52284c62013-02-15 19:59:08 +0000246
247 if options.sudo:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800248 command += ' sudo ./' + command_file
asharif52284c62013-02-15 19:59:08 +0000249 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800250 command += ' ./' + command_file
Luis Lozano036c9232015-12-10 10:47:01 -0800251 retval = command_executer.GetCommandExecuter().RunCommandGeneric(
252 command, return_output)
asharif252df0f2013-02-15 04:46:28 +0000253 return retval
254 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800255 os.chdir('%s/src/scripts' % chromeos_root)
asharif36666532013-02-15 21:08:14 +0000256 ce = command_executer.GetCommandExecuter()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800257 _, out, _ = ce.RunCommandWOutput('which cros_sdk')
asharif36666532013-02-15 21:08:14 +0000258 cros_sdk_binary = out.split()[0]
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800259 return os.execv(cros_sdk_binary, ['', '--enter'])
asharif252df0f2013-02-15 04:46:28 +0000260
261
asharifda9ac652013-02-15 04:50:09 +0000262def CreateMountPointsFromString(mount_strings, chroot_dir):
263 # String has options in the form dir:mount:options
264 mount_points = []
265 if not mount_strings:
266 return mount_points
267 mount_list = mount_strings.split()
268 for mount_string in mount_list:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800269 mount_values = mount_string.split(':')
asharifda9ac652013-02-15 04:50:09 +0000270 external_dir = mount_values[0]
271 mount_dir = mount_values[1]
raymesa7d219c2013-02-15 04:56:23 +0000272 if len(mount_values) > 2:
asharifda9ac652013-02-15 04:50:09 +0000273 options = mount_values[2]
274 else:
275 options = None
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800276 mount_point = MountPoint(external_dir, chroot_dir + '/' + mount_dir,
asharifda9ac652013-02-15 04:50:09 +0000277 getpass.getuser(), options)
278 mount_points.append(mount_point)
279 return mount_points
asharif252df0f2013-02-15 04:46:28 +0000280
281
asharif8a873872013-02-15 04:56:52 +0000282def CreateSymlink(target, link_name):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800283 logger.GetLogger().LogFatalIf(
284 target.startswith('/'), "Can't create symlink to absolute path!")
285 real_from_file = misc.GetRoot(link_name)[0] + '/' + target
asharif8a873872013-02-15 04:56:52 +0000286 if os.path.realpath(real_from_file) != os.path.realpath(link_name):
287 if os.path.exists(link_name):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800288 command = 'rm -rf ' + link_name
asharif8a873872013-02-15 04:56:52 +0000289 command_executer.GetCommandExecuter().RunCommand(command)
290 os.symlink(target, link_name)
291
292
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800293if __name__ == '__main__':
asharif2198c512013-02-15 09:21:35 +0000294 retval = Main(sys.argv)
295 sys.exit(retval)