blob: 09c41c84b3acd6dc456ec1de13dadbbf2020ab83 [file] [log] [blame]
Brian Harringb938c782012-02-29 15:14:38 -08001#!/usr/bin/env python
Mike Frysinger2de7f042012-07-10 04:45:03 -04002# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Brian Harringb938c782012-02-29 15:14:38 -08003# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""This script fetches and prepares an SDK chroot.
7"""
8
Josh Triplett472a4182013-03-08 11:48:57 -08009import glob
Brian Harringb938c782012-02-29 15:14:38 -080010import os
Josh Triplett472a4182013-03-08 11:48:57 -080011import pwd
David James56e6c2c2012-10-24 23:54:41 -070012import sys
Brian Harringb938c782012-02-29 15:14:38 -080013import urlparse
14
15from chromite.buildbot import constants
Brian Harringcfe762a2012-02-29 13:03:53 -080016from chromite.lib import cgroups
Brian Harringb6cf9142012-09-01 20:43:17 -070017from chromite.lib import commandline
Brian Harringb938c782012-02-29 15:14:38 -080018from chromite.lib import cros_build_lib
Brian Harringb938c782012-02-29 15:14:38 -080019from chromite.lib import locking
Josh Triplette759b232013-03-08 13:03:43 -080020from chromite.lib import namespaces
Brian Harringae0a5322012-09-15 01:46:51 -070021from chromite.lib import osutils
Mike Frysinger8e727a32013-01-16 16:57:53 -050022from chromite.lib import toolchain
Brian Harringb938c782012-02-29 15:14:38 -080023
24cros_build_lib.STRICT_SUDO = True
25
26
Zdenek Behanaa52cea2012-05-30 01:31:11 +020027COMPRESSION_PREFERENCE = ('xz', 'bz2')
Zdenek Behanfd0efe42012-04-13 04:36:40 +020028
Brian Harringb938c782012-02-29 15:14:38 -080029# TODO(zbehan): Remove the dependency on these, reimplement them in python
Mike Frysinger648ba2d2013-01-08 14:19:34 -050030MAKE_CHROOT = [os.path.join(constants.SOURCE_ROOT,
31 'src/scripts/sdk_lib/make_chroot.sh')]
32ENTER_CHROOT = [os.path.join(constants.SOURCE_ROOT,
33 'src/scripts/sdk_lib/enter_chroot.sh')]
Brian Harringb938c782012-02-29 15:14:38 -080034
Josh Triplett472a4182013-03-08 11:48:57 -080035# Proxy simulator configuration.
36PROXY_HOST_IP = '192.168.240.1'
37PROXY_PORT = 8080
38PROXY_GUEST_IP = '192.168.240.2'
39PROXY_NETMASK = 30
40PROXY_VETH_PREFIX = 'veth'
41PROXY_CONNECT_PORTS = (80, 443, 9418)
42PROXY_APACHE_FALLBACK_USERS = ('www-data', 'apache', 'nobody')
43PROXY_APACHE_MPMS = ('event', 'worker', 'prefork')
44PROXY_APACHE_FALLBACK_PATH = ':'.join(
45 '/usr/lib/apache2/mpm-%s' % mpm for mpm in PROXY_APACHE_MPMS
46)
47PROXY_APACHE_MODULE_GLOBS = ('/usr/lib*/apache2/modules', '/usr/lib*/apache2')
48
Josh Triplett9a495f62013-03-15 18:06:55 -070049# We need these tools to run. Very common tools (tar,..) are omitted.
Josh Triplette759b232013-03-08 13:03:43 -080050NEEDED_TOOLS = ('curl', 'xz')
Brian Harringb938c782012-02-29 15:14:38 -080051
Josh Triplett472a4182013-03-08 11:48:57 -080052# Tools needed for --proxy-sim only.
53PROXY_NEEDED_TOOLS = ('ip',)
Brian Harringb938c782012-02-29 15:14:38 -080054
Brian Harring1790ac42012-09-23 08:53:33 -070055def GetArchStageTarballs(version):
Brian Harringb938c782012-02-29 15:14:38 -080056 """Returns the URL for a given arch/version"""
Brian Harring1790ac42012-09-23 08:53:33 -070057 extension = {'bz2':'tbz2', 'xz':'tar.xz'}
Mike Frysinger8e727a32013-01-16 16:57:53 -050058 return [toolchain.GetSdkURL(suburl='cros-sdk-%s.%s'
59 % (version, extension[compressor]))
Brian Harring1790ac42012-09-23 08:53:33 -070060 for compressor in COMPRESSION_PREFERENCE]
61
62
63def GetStage3Urls(version):
Mike Frysinger8e727a32013-01-16 16:57:53 -050064 return [toolchain.GetSdkURL(suburl='stage3-amd64-%s.tar.%s' % (version, ext))
Brian Harring1790ac42012-09-23 08:53:33 -070065 for ext in COMPRESSION_PREFERENCE]
Brian Harringb938c782012-02-29 15:14:38 -080066
67
Brian Harringae0a5322012-09-15 01:46:51 -070068def FetchRemoteTarballs(storage_dir, urls):
Zdenek Behanfd0efe42012-04-13 04:36:40 +020069 """Fetches a tarball given by url, and place it in sdk/.
70
71 Args:
72 urls: List of URLs to try to download. Download will stop on first success.
73
74 Returns:
75 Full path to the downloaded file
76 """
Zdenek Behanfd0efe42012-04-13 04:36:40 +020077
Brian Harring1790ac42012-09-23 08:53:33 -070078 # Note we track content length ourselves since certain versions of curl
79 # fail if asked to resume a complete file.
80 # pylint: disable=C0301,W0631
81 # https://sourceforge.net/tracker/?func=detail&atid=100976&aid=3482927&group_id=976
Zdenek Behanfd0efe42012-04-13 04:36:40 +020082 for url in urls:
Brian Harring1790ac42012-09-23 08:53:33 -070083 # http://www.logilab.org/ticket/8766
84 # pylint: disable=E1101
85 parsed = urlparse.urlparse(url)
86 tarball_name = os.path.basename(parsed.path)
87 if parsed.scheme in ('', 'file'):
88 if os.path.exists(parsed.path):
89 return parsed.path
90 continue
91 content_length = 0
Zdenek Behanfd0efe42012-04-13 04:36:40 +020092 print 'Attempting download: %s' % url
Brian Harring1790ac42012-09-23 08:53:33 -070093 result = cros_build_lib.RunCurl(
94 ['-I', url], redirect_stdout=True, redirect_stderr=True,
95 print_cmd=False)
96 successful = False
97 for header in result.output.splitlines():
98 # We must walk the output to find the string '200 OK' for use cases where
99 # a proxy is involved and may have pushed down the actual header.
100 if header.find('200 OK') != -1:
101 successful = True
102 elif header.lower().startswith("content-length:"):
103 content_length = int(header.split(":", 1)[-1].strip())
104 if successful:
105 break
106 if successful:
Zdenek Behanfd0efe42012-04-13 04:36:40 +0200107 break
108 else:
109 raise Exception('No valid URLs found!')
110
Brian Harringae0a5322012-09-15 01:46:51 -0700111 tarball_dest = os.path.join(storage_dir, tarball_name)
Brian Harring1790ac42012-09-23 08:53:33 -0700112 current_size = 0
113 if os.path.exists(tarball_dest):
114 current_size = os.path.getsize(tarball_dest)
115 if current_size > content_length:
David James56e6c2c2012-10-24 23:54:41 -0700116 osutils.SafeUnlink(tarball_dest)
Brian Harring1790ac42012-09-23 08:53:33 -0700117 current_size = 0
Zdenek Behanb2fa72e2012-03-16 04:49:30 +0100118
Brian Harring1790ac42012-09-23 08:53:33 -0700119 if current_size < content_length:
120 cros_build_lib.RunCurl(
121 ['-f', '-L', '-y', '30', '-C', '-', '--output', tarball_dest, url],
122 print_cmd=False)
Brian Harringb938c782012-02-29 15:14:38 -0800123
Brian Harring1790ac42012-09-23 08:53:33 -0700124 # Cleanup old tarballs now since we've successfull fetched; only cleanup
125 # the tarballs for our prefix, or unknown ones.
126 ignored_prefix = ('stage3-' if tarball_name.startswith('cros-sdk-')
127 else 'cros-sdk-')
128 for filename in os.listdir(storage_dir):
129 if filename == tarball_name or filename.startswith(ignored_prefix):
130 continue
Brian Harringb938c782012-02-29 15:14:38 -0800131
Brian Harring1790ac42012-09-23 08:53:33 -0700132 print 'Cleaning up old tarball: %s' % (filename,)
David James56e6c2c2012-10-24 23:54:41 -0700133 osutils.SafeUnlink(os.path.join(storage_dir, filename))
Zdenek Behan9c644dd2012-04-05 06:24:02 +0200134
Brian Harringb938c782012-02-29 15:14:38 -0800135 return tarball_dest
136
137
Brian Harring1790ac42012-09-23 08:53:33 -0700138def CreateChroot(chroot_path, sdk_tarball, cache_dir, nousepkg=False):
Brian Harringb938c782012-02-29 15:14:38 -0800139 """Creates a new chroot from a given SDK"""
Brian Harringb938c782012-02-29 15:14:38 -0800140
Brian Harring1790ac42012-09-23 08:53:33 -0700141 cmd = MAKE_CHROOT + ['--stage3_path', sdk_tarball,
Brian Harringae0a5322012-09-15 01:46:51 -0700142 '--chroot', chroot_path,
143 '--cache_dir', cache_dir]
Mike Frysinger2de7f042012-07-10 04:45:03 -0400144 if nousepkg:
145 cmd.append('--nousepkg')
Brian Harringb938c782012-02-29 15:14:38 -0800146
147 try:
148 cros_build_lib.RunCommand(cmd, print_cmd=False)
149 except cros_build_lib.RunCommandError:
Brian Harring98b54902012-03-23 04:05:42 -0700150 raise SystemExit('Running %r failed!' % cmd)
Brian Harringb938c782012-02-29 15:14:38 -0800151
152
153def DeleteChroot(chroot_path):
154 """Deletes an existing chroot"""
155 cmd = MAKE_CHROOT + ['--chroot', chroot_path,
156 '--delete']
157 try:
158 cros_build_lib.RunCommand(cmd, print_cmd=False)
159 except cros_build_lib.RunCommandError:
Brian Harring98b54902012-03-23 04:05:42 -0700160 raise SystemExit('Running %r failed!' % cmd)
Brian Harringb938c782012-02-29 15:14:38 -0800161
162
Brian Harringae0a5322012-09-15 01:46:51 -0700163def EnterChroot(chroot_path, cache_dir, chrome_root, chrome_root_mount,
164 additional_args):
Brian Harringb938c782012-02-29 15:14:38 -0800165 """Enters an existing SDK chroot"""
Brian Harringae0a5322012-09-15 01:46:51 -0700166 cmd = ENTER_CHROOT + ['--chroot', chroot_path, '--cache_dir', cache_dir]
Brian Harringb938c782012-02-29 15:14:38 -0800167 if chrome_root:
168 cmd.extend(['--chrome_root', chrome_root])
169 if chrome_root_mount:
170 cmd.extend(['--chrome_root_mount', chrome_root_mount])
171 if len(additional_args) > 0:
172 cmd.append('--')
173 cmd.extend(additional_args)
Brian Harring7199e7d2012-03-23 04:10:08 -0700174
175 ret = cros_build_lib.RunCommand(cmd, print_cmd=False, error_code_ok=True)
176 # If we were in interactive mode, ignore the exit code; it'll be whatever
177 # they last ran w/in the chroot and won't matter to us one way or another.
178 # Note this does allow chroot entrance to fail and be ignored during
179 # interactive; this is however a rare case and the user will immediately
180 # see it (nor will they be checking the exit code manually).
181 if ret.returncode != 0 and additional_args:
182 raise SystemExit('Running %r failed with exit code %i'
183 % (cmd, ret.returncode))
Brian Harringb938c782012-02-29 15:14:38 -0800184
185
David James56e6c2c2012-10-24 23:54:41 -0700186def _SudoCommand():
187 """Get the 'sudo' command, along with all needed environment variables."""
188
David James5a73b4d2013-03-07 10:23:40 -0800189 # Pass in the ENVIRONMENT_WHITELIST and ENV_PASSTHRU variables so that
190 # scripts in the chroot know what variables to pass through.
David James56e6c2c2012-10-24 23:54:41 -0700191 cmd = ['sudo']
David James5a73b4d2013-03-07 10:23:40 -0800192 for key in constants.CHROOT_ENVIRONMENT_WHITELIST + constants.ENV_PASSTHRU:
David James56e6c2c2012-10-24 23:54:41 -0700193 value = os.environ.get(key)
194 if value is not None:
195 cmd += ['%s=%s' % (key, value)]
196
197 # Pass in the path to the depot_tools so that users can access them from
198 # within the chroot.
Stefan Zager9c13a672013-08-30 12:24:10 -0700199 cmd += ['DEPOT_TOOLS=%s' % cros_build_lib.FindDepotTools()]
David James56e6c2c2012-10-24 23:54:41 -0700200
201 return cmd
202
203
Josh Triplett472a4182013-03-08 11:48:57 -0800204def _ReportMissing(missing):
205 """Report missing utilities, then exit.
206
207 Args:
208 missing: List of missing utilities, as returned by
209 osutils.FindMissingBinaries. If non-empty, will not return.
210 """
211
212 if missing:
213 raise SystemExit(
214 'The tool(s) %s were not found.\n'
215 'Please install the appropriate package in your host.\n'
216 'Example(ubuntu):\n'
217 ' sudo apt-get install <packagename>'
218 % ', '.join(missing))
219
220
221def _ProxySimSetup(options):
222 """Set up proxy simulator, and return only in the child environment.
223
224 TODO: Ideally, this should support multiple concurrent invocations of
225 cros_sdk --proxy-sim; currently, such invocations will conflict with each
226 other due to the veth device names and IP addresses. Either this code would
227 need to generate fresh, unused names for all of these before forking, or it
228 would need to support multiple concurrent cros_sdk invocations sharing one
229 proxy and allowing it to exit when unused (without counting on any local
230 service-management infrastructure on the host).
231 """
232
233 may_need_mpm = False
234 apache_bin = osutils.Which('apache2')
235 if apache_bin is None:
236 apache_bin = osutils.Which('apache2', PROXY_APACHE_FALLBACK_PATH)
237 if apache_bin is None:
238 _ReportMissing(('apache2',))
239 else:
240 may_need_mpm = True
241
242 # Module names and .so names included for ease of grepping.
243 apache_modules = [('proxy_module', 'mod_proxy.so'),
244 ('proxy_connect_module', 'mod_proxy_connect.so'),
245 ('proxy_http_module', 'mod_proxy_http.so'),
246 ('proxy_ftp_module', 'mod_proxy_ftp.so')]
247
248 # Find the apache module directory, and make sure it has the modules we need.
249 module_dirs = {}
250 for g in PROXY_APACHE_MODULE_GLOBS:
251 for mod, so in apache_modules:
252 for f in glob.glob(os.path.join(g, so)):
253 module_dirs.setdefault(os.path.dirname(f), []).append(so)
254 for apache_module_path, modules_found in module_dirs.iteritems():
255 if len(modules_found) == len(apache_modules):
256 break
257 else:
258 # Appease cros lint, which doesn't understand that this else block will not
259 # fall through to the subsequent code which relies on apache_module_path.
260 apache_module_path = None
261 raise SystemExit(
262 'Could not find apache module path containing all required modules: %s'
263 % ', '.join(so for mod, so in apache_modules))
264
265 def check_add_module(name):
266 so = 'mod_%s.so' % name
267 if os.access(os.path.join(apache_module_path, so), os.F_OK):
268 mod = '%s_module' % name
269 apache_modules.append((mod, so))
270 return True
271 return False
272
273 check_add_module('authz_core')
274 if may_need_mpm:
275 for mpm in PROXY_APACHE_MPMS:
276 if check_add_module('mpm_%s' % mpm):
277 break
278
279 veth_host = '%s-host' % PROXY_VETH_PREFIX
280 veth_guest = '%s-guest' % PROXY_VETH_PREFIX
281
282 # Set up pipes from parent to child and vice versa.
283 # The child writes a byte to the parent after calling unshare, so that the
284 # parent can then assign the guest end of the veth interface to the child's
285 # new network namespace. The parent then writes a byte to the child after
286 # assigning the guest interface, so that the child can then configure that
287 # interface. In both cases, if we get back an EOF when reading from the
288 # pipe, we assume the other end exited with an error message, so just exit.
289 parent_readfd, child_writefd = os.pipe()
290 child_readfd, parent_writefd = os.pipe()
291 SUCCESS_FLAG = '+'
292
293 pid = os.fork()
294 if not pid:
295 os.close(parent_readfd)
296 os.close(parent_writefd)
297
298 namespaces.Unshare(namespaces.CLONE_NEWNET)
299 os.write(child_writefd, SUCCESS_FLAG)
300 os.close(child_writefd)
301 if os.read(child_readfd, 1) != SUCCESS_FLAG:
302 # Parent failed; it will have already have outputted an error message.
303 sys.exit(1)
304 os.close(child_readfd)
305
306 # Set up child side of the network.
307 commands = (
308 ('ip', 'address', 'add',
309 '%s/%u' % (PROXY_GUEST_IP, PROXY_NETMASK),
310 'dev', veth_guest),
311 ('ip', 'link', 'set', veth_guest, 'up'),
312 )
313 try:
314 for cmd in commands:
315 cros_build_lib.RunCommand(cmd, print_cmd=False)
316 except cros_build_lib.RunCommandError:
317 raise SystemExit('Running %r failed!' % (cmd,))
318
319 proxy_url = 'http://%s:%u' % (PROXY_HOST_IP, PROXY_PORT)
320 for proto in ('http', 'https', 'ftp'):
321 os.environ[proto + '_proxy'] = proxy_url
322 for v in ('all_proxy', 'RSYNC_PROXY', 'no_proxy'):
323 os.environ.pop(v, None)
324 return
325
326 os.close(child_readfd)
327 os.close(child_writefd)
328
329 if os.read(parent_readfd, 1) != SUCCESS_FLAG:
330 # Child failed; it will have already have outputted an error message.
331 sys.exit(1)
332 os.close(parent_readfd)
333
334 # Set up parent side of the network.
335 uid = int(os.environ.get('SUDO_UID', '0'))
336 gid = int(os.environ.get('SUDO_GID', '0'))
337 if uid == 0 or gid == 0:
338 for username in PROXY_APACHE_FALLBACK_USERS:
339 try:
340 pwnam = pwd.getpwnam(username)
341 uid, gid = pwnam.pw_uid, pwnam.pw_gid
342 break
343 except KeyError:
344 continue
345 if uid == 0 or gid == 0:
346 raise SystemExit('Could not find a non-root user to run Apache as')
347
348 chroot_parent, chroot_base = os.path.split(options.chroot)
349 pid_file = os.path.join(chroot_parent, '.%s-apache-proxy.pid' % chroot_base)
350 log_file = os.path.join(chroot_parent, '.%s-apache-proxy.log' % chroot_base)
351
352 apache_directives = [
353 'User #%u' % uid,
354 'Group #%u' % gid,
355 'PidFile %s' % pid_file,
356 'ErrorLog %s' % log_file,
357 'Listen %s:%u' % (PROXY_HOST_IP, PROXY_PORT),
358 'ServerName %s' % PROXY_HOST_IP,
359 'ProxyRequests On',
360 'AllowCONNECT %s' % ' '.join(map(str, PROXY_CONNECT_PORTS)),
361 ] + [
362 'LoadModule %s %s' % (mod, os.path.join(apache_module_path, so))
363 for (mod, so) in apache_modules
364 ]
365 commands = (
366 ('ip', 'link', 'add', 'name', veth_host,
367 'type', 'veth', 'peer', 'name', veth_guest),
368 ('ip', 'address', 'add',
369 '%s/%u' % (PROXY_HOST_IP, PROXY_NETMASK),
370 'dev', veth_host),
371 ('ip', 'link', 'set', veth_host, 'up'),
372 [apache_bin, '-f', '/dev/null']
373 + [arg for d in apache_directives for arg in ('-C', d)],
374 ('ip', 'link', 'set', veth_guest, 'netns', str(pid)),
375 )
376 cmd = None # Make cros lint happy.
377 try:
378 for cmd in commands:
379 cros_build_lib.RunCommand(cmd, print_cmd=False)
380 except cros_build_lib.RunCommandError:
381 # Clean up existing interfaces, if any.
382 cmd_cleanup = ('ip', 'link', 'del', veth_host)
383 try:
384 cros_build_lib.RunCommand(cmd_cleanup, print_cmd=False)
385 except cros_build_lib.RunCommandError:
386 cros_build_lib.Error('running %r failed', cmd_cleanup)
387 raise SystemExit('Running %r failed!' % (cmd,))
388 os.write(parent_writefd, SUCCESS_FLAG)
389 os.close(parent_writefd)
390
391 sys.exit(os.waitpid(pid, 0)[1])
392
393
Mike Frysingera78a56e2012-11-20 06:02:30 -0500394def _ReExecuteIfNeeded(argv):
David James56e6c2c2012-10-24 23:54:41 -0700395 """Re-execute cros_sdk as root.
396
397 Also unshare the mount namespace so as to ensure that processes outside
398 the chroot can't mess with our mounts.
399 """
400 if os.geteuid() != 0:
Mike Frysingera78a56e2012-11-20 06:02:30 -0500401 cmd = _SudoCommand() + ['--'] + argv
402 os.execvp(cmd[0], cmd)
Mike Frysingera78a56e2012-11-20 06:02:30 -0500403 else:
Josh Triplette759b232013-03-08 13:03:43 -0800404 cgroups.Cgroup.InitSystem()
405 namespaces.Unshare(namespaces.CLONE_NEWNS)
David James56e6c2c2012-10-24 23:54:41 -0700406
407
Brian Harring6be2efc2012-03-01 05:04:00 -0800408def main(argv):
Brian Harring218e13c2012-10-10 16:21:26 -0700409 usage = """usage: %prog [options] [VAR1=val1 .. VARn=valn -- args]
Brian Harringb938c782012-02-29 15:14:38 -0800410
Brian Harring218e13c2012-10-10 16:21:26 -0700411This script is used for manipulating local chroot environments; creating,
412deleting, downloading, etc. If given --enter (or no args), it defaults
413to an interactive bash shell within the chroot.
Brian Harringb938c782012-02-29 15:14:38 -0800414
Brian Harring218e13c2012-10-10 16:21:26 -0700415If given args those are passed to the chroot environment, and executed."""
Mike Frysinger648ba2d2013-01-08 14:19:34 -0500416 conf = cros_build_lib.LoadKeyValueFile(
417 os.path.join(constants.SOURCE_ROOT, constants.SDK_VERSION_FILE),
418 ignore_missing=True)
Brian Harring1790ac42012-09-23 08:53:33 -0700419 sdk_latest_version = conf.get('SDK_LATEST_VERSION', '<unknown>')
420 bootstrap_latest_version = conf.get('BOOTSTRAP_LATEST_VERSION', '<unknown>')
421
Brian Harring218e13c2012-10-10 16:21:26 -0700422 parser = commandline.OptionParser(usage=usage, caching=True)
423
424 commands = parser.add_option_group("Commands")
425 commands.add_option(
426 '--enter', action='store_true', default=False,
427 help='Enter the SDK chroot. Implies --create.')
428 commands.add_option(
429 '--create', action='store_true',default=False,
430 help='Create the chroot only if it does not already exist. '
431 'Implies --download.')
432 commands.add_option(
433 '--bootstrap', action='store_true', default=False,
434 help='Build everything from scratch, including the sdk. '
435 'Use this only if you need to validate a change '
436 'that affects SDK creation itself (toolchain and '
437 'build are typically the only folk who need this). '
438 'Note this will quite heavily slow down the build. '
439 'This option implies --create --nousepkg.')
440 commands.add_option(
441 '-r', '--replace', action='store_true', default=False,
442 help='Replace an existing SDK chroot. Basically an alias '
443 'for --delete --create.')
444 commands.add_option(
445 '--delete', action='store_true', default=False,
446 help='Delete the current SDK chroot if it exists.')
447 commands.add_option(
448 '--download', action='store_true', default=False,
449 help='Download the sdk.')
Brian Harringb938c782012-02-29 15:14:38 -0800450
451 # Global options:
Mike Frysinger648ba2d2013-01-08 14:19:34 -0500452 default_chroot = os.path.join(constants.SOURCE_ROOT,
453 constants.DEFAULT_CHROOT_DIR)
Brian Harring218e13c2012-10-10 16:21:26 -0700454 parser.add_option(
455 '--chroot', dest='chroot', default=default_chroot, type='path',
456 help=('SDK chroot dir name [%s]' % constants.DEFAULT_CHROOT_DIR))
Brian Harringb938c782012-02-29 15:14:38 -0800457
Brian Harring218e13c2012-10-10 16:21:26 -0700458 parser.add_option('--chrome_root', default=None, type='path',
459 help='Mount this chrome root into the SDK chroot')
460 parser.add_option('--chrome_root_mount', default=None, type='path',
461 help='Mount chrome into this path inside SDK chroot')
462 parser.add_option('--nousepkg', action='store_true', default=False,
463 help='Do not use binary packages when creating a chroot.')
Josh Triplett472a4182013-03-08 11:48:57 -0800464 parser.add_option('--proxy-sim', action='store_true', default=False,
465 help='Simulate a restrictive network requiring an outbound'
466 ' proxy.')
Brian Harringb938c782012-02-29 15:14:38 -0800467 parser.add_option('-u', '--url',
Brian Harringb6cf9142012-09-01 20:43:17 -0700468 dest='sdk_url', default=None,
Brian Harringb938c782012-02-29 15:14:38 -0800469 help=('''Use sdk tarball located at this url.
470 Use file:// for local files.'''))
Brian Harring1790ac42012-09-23 08:53:33 -0700471 parser.add_option('--sdk-version', default=None,
472 help='Use this sdk version. For prebuilt, current is %r'
473 ', for bootstrapping its %r.'
474 % (sdk_latest_version, bootstrap_latest_version))
Brian Harring218e13c2012-10-10 16:21:26 -0700475 options, chroot_command = parser.parse_args(argv)
Brian Harringb938c782012-02-29 15:14:38 -0800476
477 # Some sanity checks first, before we ask for sudo credentials.
Mike Frysinger8fd67dc2012-12-03 23:51:18 -0500478 cros_build_lib.AssertOutsideChroot()
Brian Harringb938c782012-02-29 15:14:38 -0800479
Brian Harring1790ac42012-09-23 08:53:33 -0700480 host = os.uname()[4]
Brian Harring1790ac42012-09-23 08:53:33 -0700481 if host != 'x86_64':
482 parser.error(
483 "cros_sdk is currently only supported on x86_64; you're running"
484 " %s. Please find a x86_64 machine." % (host,))
485
Josh Triplett472a4182013-03-08 11:48:57 -0800486 _ReportMissing(osutils.FindMissingBinaries(NEEDED_TOOLS))
487 if options.proxy_sim:
488 _ReportMissing(osutils.FindMissingBinaries(PROXY_NEEDED_TOOLS))
Brian Harringb938c782012-02-29 15:14:38 -0800489
David James471532c2013-01-21 10:23:31 -0800490 _ReExecuteIfNeeded([sys.argv[0]] + argv)
491
Brian Harring218e13c2012-10-10 16:21:26 -0700492 # Expand out the aliases...
493 if options.replace:
494 options.delete = options.create = True
Brian Harringb938c782012-02-29 15:14:38 -0800495
Brian Harring218e13c2012-10-10 16:21:26 -0700496 if options.bootstrap:
497 options.create = True
Brian Harringb938c782012-02-29 15:14:38 -0800498
Brian Harring218e13c2012-10-10 16:21:26 -0700499 # If a command is not given, default to enter.
500 options.enter |= not any(getattr(options, x.dest)
501 for x in commands.option_list)
502 options.enter |= bool(chroot_command)
503
504 if options.enter and options.delete and not options.create:
505 parser.error("Trying to enter the chroot when --delete "
506 "was specified makes no sense.")
507
508 # Finally, discern if we need to create the chroot.
509 chroot_exists = os.path.exists(options.chroot)
510 if options.create or options.enter:
511 # Only create if it's being wiped, or if it doesn't exist.
512 if not options.delete and chroot_exists:
513 options.create = False
514 else:
515 options.download = True
516
517 # Finally, flip create if necessary.
518 if options.enter:
519 options.create |= not chroot_exists
Brian Harringb938c782012-02-29 15:14:38 -0800520
Brian Harringb938c782012-02-29 15:14:38 -0800521 if not options.sdk_version:
Brian Harring1790ac42012-09-23 08:53:33 -0700522 sdk_version = (bootstrap_latest_version if options.bootstrap
523 else sdk_latest_version)
Brian Harringb938c782012-02-29 15:14:38 -0800524 else:
525 sdk_version = options.sdk_version
526
Brian Harring1790ac42012-09-23 08:53:33 -0700527 # Based on selections, fetch the tarball.
528 if options.sdk_url:
529 urls = [options.sdk_url]
530 elif options.bootstrap:
531 urls = GetStage3Urls(sdk_version)
532 else:
533 urls = GetArchStageTarballs(sdk_version)
534
Brian Harringb6cf9142012-09-01 20:43:17 -0700535 lock_path = os.path.dirname(options.chroot)
Brian Harringb938c782012-02-29 15:14:38 -0800536 lock_path = os.path.join(lock_path,
Brian Harringb6cf9142012-09-01 20:43:17 -0700537 '.%s_lock' % os.path.basename(options.chroot))
David James56e6c2c2012-10-24 23:54:41 -0700538 with cgroups.SimpleContainChildren('cros_sdk'):
539 with locking.FileLock(lock_path, 'chroot lock') as lock:
Brian Harring1790ac42012-09-23 08:53:33 -0700540
Josh Triplett472a4182013-03-08 11:48:57 -0800541 if options.proxy_sim:
542 _ProxySimSetup(options)
543
David James56e6c2c2012-10-24 23:54:41 -0700544 if options.delete and os.path.exists(options.chroot):
545 lock.write_lock()
546 DeleteChroot(options.chroot)
Brian Harringb938c782012-02-29 15:14:38 -0800547
David James56e6c2c2012-10-24 23:54:41 -0700548 sdk_cache = os.path.join(options.cache_dir, 'sdks')
549 distfiles_cache = os.path.join(options.cache_dir, 'distfiles')
550 osutils.SafeMakedirs(options.cache_dir)
Brian Harringae0a5322012-09-15 01:46:51 -0700551
David James56e6c2c2012-10-24 23:54:41 -0700552 for target in (sdk_cache, distfiles_cache):
Mike Frysinger648ba2d2013-01-08 14:19:34 -0500553 src = os.path.join(constants.SOURCE_ROOT, os.path.basename(target))
David James56e6c2c2012-10-24 23:54:41 -0700554 if not os.path.exists(src):
555 osutils.SafeMakedirs(target)
556 continue
557 lock.write_lock(
558 "Upgrade to %r needed but chroot is locked; please exit "
559 "all instances so this upgrade can finish." % src)
560 if not os.path.exists(src):
561 # Note that while waiting for the write lock, src may've vanished;
562 # it's a rare race during the upgrade process that's a byproduct
563 # of us avoiding taking a write lock to do the src check. If we
564 # took a write lock for that check, it would effectively limit
565 # all cros_sdk for a chroot to a single instance.
566 osutils.SafeMakedirs(target)
567 elif not os.path.exists(target):
568 # Upgrade occurred, but a reversion, or something whacky
569 # occurred writing to the old location. Wipe and continue.
570 os.rename(src, target)
571 else:
572 # Upgrade occurred once already, but either a reversion or
573 # some before/after separate cros_sdk usage is at play.
574 # Wipe and continue.
575 osutils.RmDir(src)
Brian Harringae0a5322012-09-15 01:46:51 -0700576
David James56e6c2c2012-10-24 23:54:41 -0700577 if options.download:
578 lock.write_lock()
579 sdk_tarball = FetchRemoteTarballs(sdk_cache, urls)
Brian Harring218e13c2012-10-10 16:21:26 -0700580
David James56e6c2c2012-10-24 23:54:41 -0700581 if options.create:
582 lock.write_lock()
583 CreateChroot(options.chroot, sdk_tarball, options.cache_dir,
584 nousepkg=(options.bootstrap or options.nousepkg))
Brian Harring1790ac42012-09-23 08:53:33 -0700585
David James56e6c2c2012-10-24 23:54:41 -0700586 if options.enter:
587 lock.read_lock()
588 EnterChroot(options.chroot, options.cache_dir, options.chrome_root,
589 options.chrome_root_mount, chroot_command)