Mike Frysinger | 2de7f04 | 2012-07-10 04:45:03 -0400 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 5 | """Manage SDK chroots. |
| 6 | |
| 7 | This script is used for manipulating local chroot environments; creating, |
| 8 | deleting, downloading, etc. If given --enter (or no args), it defaults |
| 9 | to an interactive bash shell within the chroot. |
| 10 | |
| 11 | If given args those are passed to the chroot environment, and executed. |
| 12 | """ |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 13 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 14 | from __future__ import print_function |
| 15 | |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 16 | import argparse |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 17 | import glob |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 18 | import os |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 19 | import pwd |
Brian Norris | d37e2f7 | 2016-08-22 16:09:24 -0700 | [diff] [blame] | 20 | import re |
Ting-Yuan Huang | f56d9af | 2017-06-19 16:08:32 -0700 | [diff] [blame] | 21 | import resource |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 22 | import sys |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 23 | import urlparse |
| 24 | |
Aviv Keshet | b7519e1 | 2016-10-04 00:50:00 -0700 | [diff] [blame] | 25 | from chromite.lib import constants |
Brian Harring | cfe762a | 2012-02-29 13:03:53 -0800 | [diff] [blame] | 26 | from chromite.lib import cgroups |
Brian Harring | b6cf914 | 2012-09-01 20:43:17 -0700 | [diff] [blame] | 27 | from chromite.lib import commandline |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 28 | from chromite.lib import cros_build_lib |
Ralph Nathan | 5990042 | 2015-03-24 10:41:17 -0700 | [diff] [blame] | 29 | from chromite.lib import cros_logging as logging |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 30 | from chromite.lib import locking |
Josh Triplett | e759b23 | 2013-03-08 13:03:43 -0800 | [diff] [blame] | 31 | from chromite.lib import namespaces |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 32 | from chromite.lib import osutils |
Mike Frysinger | e2d8f0d | 2014-11-01 13:09:26 -0400 | [diff] [blame] | 33 | from chromite.lib import process_util |
David James | c93e6a4d | 2014-01-13 11:37:36 -0800 | [diff] [blame] | 34 | from chromite.lib import retry_util |
Mike Frysinger | 8e727a3 | 2013-01-16 16:57:53 -0500 | [diff] [blame] | 35 | from chromite.lib import toolchain |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 36 | |
| 37 | cros_build_lib.STRICT_SUDO = True |
| 38 | |
| 39 | |
Zdenek Behan | aa52cea | 2012-05-30 01:31:11 +0200 | [diff] [blame] | 40 | COMPRESSION_PREFERENCE = ('xz', 'bz2') |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 41 | |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 42 | # TODO(zbehan): Remove the dependency on these, reimplement them in python |
Mike Frysinger | 648ba2d | 2013-01-08 14:19:34 -0500 | [diff] [blame] | 43 | MAKE_CHROOT = [os.path.join(constants.SOURCE_ROOT, |
| 44 | 'src/scripts/sdk_lib/make_chroot.sh')] |
| 45 | ENTER_CHROOT = [os.path.join(constants.SOURCE_ROOT, |
| 46 | 'src/scripts/sdk_lib/enter_chroot.sh')] |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 47 | |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 48 | # Proxy simulator configuration. |
| 49 | PROXY_HOST_IP = '192.168.240.1' |
| 50 | PROXY_PORT = 8080 |
| 51 | PROXY_GUEST_IP = '192.168.240.2' |
| 52 | PROXY_NETMASK = 30 |
| 53 | PROXY_VETH_PREFIX = 'veth' |
| 54 | PROXY_CONNECT_PORTS = (80, 443, 9418) |
| 55 | PROXY_APACHE_FALLBACK_USERS = ('www-data', 'apache', 'nobody') |
| 56 | PROXY_APACHE_MPMS = ('event', 'worker', 'prefork') |
| 57 | PROXY_APACHE_FALLBACK_PATH = ':'.join( |
| 58 | '/usr/lib/apache2/mpm-%s' % mpm for mpm in PROXY_APACHE_MPMS |
| 59 | ) |
| 60 | PROXY_APACHE_MODULE_GLOBS = ('/usr/lib*/apache2/modules', '/usr/lib*/apache2') |
| 61 | |
Josh Triplett | 9a495f6 | 2013-03-15 18:06:55 -0700 | [diff] [blame] | 62 | # We need these tools to run. Very common tools (tar,..) are omitted. |
Josh Triplett | e759b23 | 2013-03-08 13:03:43 -0800 | [diff] [blame] | 63 | NEEDED_TOOLS = ('curl', 'xz') |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 64 | |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 65 | # Tools needed for --proxy-sim only. |
| 66 | PROXY_NEEDED_TOOLS = ('ip',) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 67 | |
Mike Frysinger | cc83883 | 2014-05-24 13:10:30 -0400 | [diff] [blame] | 68 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 69 | def GetArchStageTarballs(version): |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 70 | """Returns the URL for a given arch/version""" |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 71 | extension = {'bz2':'tbz2', 'xz':'tar.xz'} |
Mike Frysinger | 8e727a3 | 2013-01-16 16:57:53 -0500 | [diff] [blame] | 72 | return [toolchain.GetSdkURL(suburl='cros-sdk-%s.%s' |
| 73 | % (version, extension[compressor])) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 74 | for compressor in COMPRESSION_PREFERENCE] |
| 75 | |
| 76 | |
| 77 | def GetStage3Urls(version): |
Mike Frysinger | 8e727a3 | 2013-01-16 16:57:53 -0500 | [diff] [blame] | 78 | return [toolchain.GetSdkURL(suburl='stage3-amd64-%s.tar.%s' % (version, ext)) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 79 | for ext in COMPRESSION_PREFERENCE] |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 80 | |
| 81 | |
Gilad Arnold | 6a8f045 | 2015-06-04 11:25:18 -0700 | [diff] [blame] | 82 | def GetToolchainsOverlayUrls(version, toolchains): |
| 83 | """Returns the URL(s) for a toolchains SDK overlay. |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 84 | |
| 85 | Args: |
| 86 | version: The SDK version used, e.g. 2015.05.27.145939. We use the year and |
| 87 | month components to point to a subdirectory on the SDK bucket where |
Gilad Arnold | 6a8f045 | 2015-06-04 11:25:18 -0700 | [diff] [blame] | 88 | overlays are stored (.../2015/05/ in this case). |
| 89 | toolchains: Iterable of toolchain target strings (e.g. 'i686-pc-linux-gnu'). |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 90 | |
| 91 | Returns: |
Gilad Arnold | 6a8f045 | 2015-06-04 11:25:18 -0700 | [diff] [blame] | 92 | List of alternative download URLs for an SDK overlay tarball that contains |
| 93 | the given toolchains. |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 94 | """ |
Gilad Arnold | 6a8f045 | 2015-06-04 11:25:18 -0700 | [diff] [blame] | 95 | toolchains_desc = '-'.join(sorted(toolchains)) |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 96 | suburl_template = os.path.join( |
| 97 | *(version.split('.')[:2] + |
Gilad Arnold | 6a8f045 | 2015-06-04 11:25:18 -0700 | [diff] [blame] | 98 | ['cros-sdk-overlay-toolchains-%s-%s.tar.%%s' % |
| 99 | (toolchains_desc, version)])) |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 100 | return [toolchain.GetSdkURL(suburl=suburl_template % ext) |
| 101 | for ext in COMPRESSION_PREFERENCE] |
| 102 | |
| 103 | |
| 104 | def FetchRemoteTarballs(storage_dir, urls, desc, allow_none=False): |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 105 | """Fetches a tarball given by url, and place it in |storage_dir|. |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 106 | |
| 107 | Args: |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 108 | storage_dir: Path where to save the tarball. |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 109 | urls: List of URLs to try to download. Download will stop on first success. |
Gilad Arnold | 6a8f045 | 2015-06-04 11:25:18 -0700 | [diff] [blame] | 110 | desc: A string describing what tarball we're downloading (for logging). |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 111 | allow_none: Don't fail if none of the URLs worked. |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 112 | |
| 113 | Returns: |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 114 | Full path to the downloaded file, or None if |allow_none| and no URL worked. |
| 115 | |
| 116 | Raises: |
| 117 | ValueError: If |allow_none| is False and none of the URLs worked. |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 118 | """ |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 119 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 120 | # Note we track content length ourselves since certain versions of curl |
| 121 | # fail if asked to resume a complete file. |
| 122 | # pylint: disable=C0301,W0631 |
| 123 | # https://sourceforge.net/tracker/?func=detail&atid=100976&aid=3482927&group_id=976 |
Gilad Arnold | 6a8f045 | 2015-06-04 11:25:18 -0700 | [diff] [blame] | 124 | logging.notice('Downloading %s tarball...', desc) |
Brian Norris | cf8aef4 | 2016-09-27 10:43:39 -0700 | [diff] [blame] | 125 | status_re = re.compile(r'^HTTP/[0-9]+(\.[0-9]+)? 200') |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 126 | for url in urls: |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 127 | # http://www.logilab.org/ticket/8766 |
| 128 | # pylint: disable=E1101 |
| 129 | parsed = urlparse.urlparse(url) |
| 130 | tarball_name = os.path.basename(parsed.path) |
| 131 | if parsed.scheme in ('', 'file'): |
| 132 | if os.path.exists(parsed.path): |
| 133 | return parsed.path |
| 134 | continue |
| 135 | content_length = 0 |
Ralph Nathan | 7070e6a | 2015-04-02 10:16:43 -0700 | [diff] [blame] | 136 | logging.debug('Attempting download from %s', url) |
David James | c93e6a4d | 2014-01-13 11:37:36 -0800 | [diff] [blame] | 137 | result = retry_util.RunCurl( |
Hidehiko Abe | e55af7f | 2017-05-01 18:38:04 +0900 | [diff] [blame] | 138 | ['-I', url], print_cmd=False, debug_level=logging.NOTICE, |
| 139 | capture_output=True) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 140 | successful = False |
| 141 | for header in result.output.splitlines(): |
Brian Norris | d37e2f7 | 2016-08-22 16:09:24 -0700 | [diff] [blame] | 142 | # We must walk the output to find the 200 code for use cases where |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 143 | # a proxy is involved and may have pushed down the actual header. |
Brian Norris | d37e2f7 | 2016-08-22 16:09:24 -0700 | [diff] [blame] | 144 | if status_re.match(header): |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 145 | successful = True |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 146 | elif header.lower().startswith('content-length:'): |
| 147 | content_length = int(header.split(':', 1)[-1].strip()) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 148 | if successful: |
| 149 | break |
| 150 | if successful: |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 151 | break |
| 152 | else: |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 153 | if allow_none: |
| 154 | return None |
| 155 | raise ValueError('No valid URLs found!') |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 156 | |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 157 | tarball_dest = os.path.join(storage_dir, tarball_name) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 158 | current_size = 0 |
| 159 | if os.path.exists(tarball_dest): |
| 160 | current_size = os.path.getsize(tarball_dest) |
| 161 | if current_size > content_length: |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 162 | osutils.SafeUnlink(tarball_dest) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 163 | current_size = 0 |
Zdenek Behan | b2fa72e | 2012-03-16 04:49:30 +0100 | [diff] [blame] | 164 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 165 | if current_size < content_length: |
David James | c93e6a4d | 2014-01-13 11:37:36 -0800 | [diff] [blame] | 166 | retry_util.RunCurl( |
Hidehiko Abe | e55af7f | 2017-05-01 18:38:04 +0900 | [diff] [blame] | 167 | ['--fail', '-L', '-y', '30', '-C', '-', '--output', tarball_dest, url], |
| 168 | print_cmd=False, debug_level=logging.NOTICE) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 169 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 170 | # Cleanup old tarballs now since we've successfull fetched; only cleanup |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 171 | # the tarballs for our prefix, or unknown ones. This gets a bit tricky |
| 172 | # because we might have partial overlap between known prefixes. |
| 173 | my_prefix = tarball_name.rsplit('-', 1)[0] + '-' |
| 174 | all_prefixes = ('stage3-amd64-', 'cros-sdk-', 'cros-sdk-overlay-') |
| 175 | ignored_prefixes = [prefix for prefix in all_prefixes if prefix != my_prefix] |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 176 | for filename in os.listdir(storage_dir): |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 177 | if (filename == tarball_name or |
| 178 | any([(filename.startswith(p) and |
| 179 | not (len(my_prefix) > len(p) and filename.startswith(my_prefix))) |
| 180 | for p in ignored_prefixes])): |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 181 | continue |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 182 | logging.info('Cleaning up old tarball: %s', filename) |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 183 | osutils.SafeUnlink(os.path.join(storage_dir, filename)) |
Zdenek Behan | 9c644dd | 2012-04-05 06:24:02 +0200 | [diff] [blame] | 184 | |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 185 | return tarball_dest |
| 186 | |
| 187 | |
Gilad Arnold | 6a8f045 | 2015-06-04 11:25:18 -0700 | [diff] [blame] | 188 | def CreateChroot(chroot_path, sdk_tarball, toolchains_overlay_tarball, |
David Pursell | 3528e8e | 2015-07-07 11:22:15 -0700 | [diff] [blame] | 189 | cache_dir, nousepkg=False): |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 190 | """Creates a new chroot from a given SDK""" |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 191 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 192 | cmd = MAKE_CHROOT + ['--stage3_path', sdk_tarball, |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 193 | '--chroot', chroot_path, |
| 194 | '--cache_dir', cache_dir] |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 195 | |
Gilad Arnold | 6a8f045 | 2015-06-04 11:25:18 -0700 | [diff] [blame] | 196 | if toolchains_overlay_tarball: |
| 197 | cmd.extend(['--toolchains_overlay_path', toolchains_overlay_tarball]) |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 198 | |
Mike Frysinger | 2de7f04 | 2012-07-10 04:45:03 -0400 | [diff] [blame] | 199 | if nousepkg: |
| 200 | cmd.append('--nousepkg') |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 201 | |
Ralph Nathan | 7070e6a | 2015-04-02 10:16:43 -0700 | [diff] [blame] | 202 | logging.notice('Creating chroot. This may take a few minutes...') |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 203 | try: |
| 204 | cros_build_lib.RunCommand(cmd, print_cmd=False) |
| 205 | except cros_build_lib.RunCommandError: |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 206 | raise SystemExit('Running %r failed!' % cmd) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 207 | |
| 208 | |
| 209 | def DeleteChroot(chroot_path): |
| 210 | """Deletes an existing chroot""" |
| 211 | cmd = MAKE_CHROOT + ['--chroot', chroot_path, |
| 212 | '--delete'] |
| 213 | try: |
Ralph Nathan | 7070e6a | 2015-04-02 10:16:43 -0700 | [diff] [blame] | 214 | logging.notice('Deleting chroot.') |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 215 | cros_build_lib.RunCommand(cmd, print_cmd=False) |
| 216 | except cros_build_lib.RunCommandError: |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 217 | raise SystemExit('Running %r failed!' % cmd) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 218 | |
| 219 | |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 220 | def EnterChroot(chroot_path, cache_dir, chrome_root, chrome_root_mount, |
Hidehiko Abe | b5daf2f | 2017-03-02 17:57:43 +0900 | [diff] [blame] | 221 | workspace, goma_dir, goma_client_json, additional_args): |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 222 | """Enters an existing SDK chroot""" |
Mike Frysinger | e545697 | 2013-06-13 00:07:23 -0400 | [diff] [blame] | 223 | st = os.statvfs(os.path.join(chroot_path, 'usr', 'bin', 'sudo')) |
| 224 | # The os.ST_NOSUID constant wasn't added until python-3.2. |
| 225 | if st.f_flag & 0x2: |
| 226 | cros_build_lib.Die('chroot cannot be in a nosuid mount') |
| 227 | |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 228 | cmd = ENTER_CHROOT + ['--chroot', chroot_path, '--cache_dir', cache_dir] |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 229 | if chrome_root: |
| 230 | cmd.extend(['--chrome_root', chrome_root]) |
| 231 | if chrome_root_mount: |
| 232 | cmd.extend(['--chrome_root_mount', chrome_root_mount]) |
Don Garrett | 230d1b2 | 2015-03-09 16:21:19 -0700 | [diff] [blame] | 233 | if workspace: |
| 234 | cmd.extend(['--workspace_root', workspace]) |
Hidehiko Abe | b5daf2f | 2017-03-02 17:57:43 +0900 | [diff] [blame] | 235 | if goma_dir: |
| 236 | cmd.extend(['--goma_dir', goma_dir]) |
| 237 | if goma_client_json: |
| 238 | cmd.extend(['--goma_client_json', goma_client_json]) |
Don Garrett | 230d1b2 | 2015-03-09 16:21:19 -0700 | [diff] [blame] | 239 | |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 240 | if len(additional_args) > 0: |
| 241 | cmd.append('--') |
| 242 | cmd.extend(additional_args) |
Brian Harring | 7199e7d | 2012-03-23 04:10:08 -0700 | [diff] [blame] | 243 | |
Ting-Yuan Huang | f56d9af | 2017-06-19 16:08:32 -0700 | [diff] [blame] | 244 | # ThinLTO opens lots of files at the same time. |
| 245 | resource.setrlimit(resource.RLIMIT_NOFILE, (32768, 32768)) |
Ralph Nathan | 549d350 | 2015-03-26 17:38:42 -0700 | [diff] [blame] | 246 | ret = cros_build_lib.RunCommand(cmd, print_cmd=False, error_code_ok=True, |
| 247 | mute_output=False) |
Brian Harring | 7199e7d | 2012-03-23 04:10:08 -0700 | [diff] [blame] | 248 | # If we were in interactive mode, ignore the exit code; it'll be whatever |
| 249 | # they last ran w/in the chroot and won't matter to us one way or another. |
| 250 | # Note this does allow chroot entrance to fail and be ignored during |
| 251 | # interactive; this is however a rare case and the user will immediately |
| 252 | # see it (nor will they be checking the exit code manually). |
| 253 | if ret.returncode != 0 and additional_args: |
Richard Barnette | 5c728a4 | 2015-03-18 11:50:21 -0700 | [diff] [blame] | 254 | raise SystemExit(ret.returncode) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 255 | |
| 256 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 257 | def _SudoCommand(): |
| 258 | """Get the 'sudo' command, along with all needed environment variables.""" |
| 259 | |
David James | 5a73b4d | 2013-03-07 10:23:40 -0800 | [diff] [blame] | 260 | # Pass in the ENVIRONMENT_WHITELIST and ENV_PASSTHRU variables so that |
| 261 | # scripts in the chroot know what variables to pass through. |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 262 | cmd = ['sudo'] |
David James | 5a73b4d | 2013-03-07 10:23:40 -0800 | [diff] [blame] | 263 | for key in constants.CHROOT_ENVIRONMENT_WHITELIST + constants.ENV_PASSTHRU: |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 264 | value = os.environ.get(key) |
| 265 | if value is not None: |
| 266 | cmd += ['%s=%s' % (key, value)] |
| 267 | |
| 268 | # Pass in the path to the depot_tools so that users can access them from |
| 269 | # within the chroot. |
Mike Frysinger | 08e75f1 | 2014-08-13 01:30:09 -0400 | [diff] [blame] | 270 | cmd += ['DEPOT_TOOLS=%s' % constants.DEPOT_TOOLS_DIR] |
Mike Frysinger | 749251e | 2014-01-29 05:04:27 -0500 | [diff] [blame] | 271 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 272 | return cmd |
| 273 | |
| 274 | |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 275 | def _ReportMissing(missing): |
| 276 | """Report missing utilities, then exit. |
| 277 | |
| 278 | Args: |
| 279 | missing: List of missing utilities, as returned by |
| 280 | osutils.FindMissingBinaries. If non-empty, will not return. |
| 281 | """ |
| 282 | |
| 283 | if missing: |
| 284 | raise SystemExit( |
| 285 | 'The tool(s) %s were not found.\n' |
| 286 | 'Please install the appropriate package in your host.\n' |
| 287 | 'Example(ubuntu):\n' |
| 288 | ' sudo apt-get install <packagename>' |
| 289 | % ', '.join(missing)) |
| 290 | |
| 291 | |
| 292 | def _ProxySimSetup(options): |
| 293 | """Set up proxy simulator, and return only in the child environment. |
| 294 | |
| 295 | TODO: Ideally, this should support multiple concurrent invocations of |
| 296 | cros_sdk --proxy-sim; currently, such invocations will conflict with each |
| 297 | other due to the veth device names and IP addresses. Either this code would |
| 298 | need to generate fresh, unused names for all of these before forking, or it |
| 299 | would need to support multiple concurrent cros_sdk invocations sharing one |
| 300 | proxy and allowing it to exit when unused (without counting on any local |
| 301 | service-management infrastructure on the host). |
| 302 | """ |
| 303 | |
| 304 | may_need_mpm = False |
| 305 | apache_bin = osutils.Which('apache2') |
| 306 | if apache_bin is None: |
| 307 | apache_bin = osutils.Which('apache2', PROXY_APACHE_FALLBACK_PATH) |
| 308 | if apache_bin is None: |
| 309 | _ReportMissing(('apache2',)) |
| 310 | else: |
| 311 | may_need_mpm = True |
| 312 | |
| 313 | # Module names and .so names included for ease of grepping. |
| 314 | apache_modules = [('proxy_module', 'mod_proxy.so'), |
| 315 | ('proxy_connect_module', 'mod_proxy_connect.so'), |
| 316 | ('proxy_http_module', 'mod_proxy_http.so'), |
| 317 | ('proxy_ftp_module', 'mod_proxy_ftp.so')] |
| 318 | |
| 319 | # Find the apache module directory, and make sure it has the modules we need. |
| 320 | module_dirs = {} |
| 321 | for g in PROXY_APACHE_MODULE_GLOBS: |
| 322 | for mod, so in apache_modules: |
| 323 | for f in glob.glob(os.path.join(g, so)): |
| 324 | module_dirs.setdefault(os.path.dirname(f), []).append(so) |
| 325 | for apache_module_path, modules_found in module_dirs.iteritems(): |
| 326 | if len(modules_found) == len(apache_modules): |
| 327 | break |
| 328 | else: |
| 329 | # Appease cros lint, which doesn't understand that this else block will not |
| 330 | # fall through to the subsequent code which relies on apache_module_path. |
| 331 | apache_module_path = None |
| 332 | raise SystemExit( |
| 333 | 'Could not find apache module path containing all required modules: %s' |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 334 | % ', '.join(so for mod, so in apache_modules)) |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 335 | |
| 336 | def check_add_module(name): |
| 337 | so = 'mod_%s.so' % name |
| 338 | if os.access(os.path.join(apache_module_path, so), os.F_OK): |
| 339 | mod = '%s_module' % name |
| 340 | apache_modules.append((mod, so)) |
| 341 | return True |
| 342 | return False |
| 343 | |
| 344 | check_add_module('authz_core') |
| 345 | if may_need_mpm: |
| 346 | for mpm in PROXY_APACHE_MPMS: |
| 347 | if check_add_module('mpm_%s' % mpm): |
| 348 | break |
| 349 | |
| 350 | veth_host = '%s-host' % PROXY_VETH_PREFIX |
| 351 | veth_guest = '%s-guest' % PROXY_VETH_PREFIX |
| 352 | |
Mike Frysinger | 77bf4af | 2016-02-26 17:13:15 -0500 | [diff] [blame] | 353 | # Set up locks to sync the net namespace setup. We need the child to create |
| 354 | # the net ns first, and then have the parent assign the guest end of the veth |
| 355 | # interface to the child's new network namespace & bring up the proxy. Only |
| 356 | # then can the child move forward and rely on the network being up. |
| 357 | ns_create_lock = locking.PipeLock() |
| 358 | ns_setup_lock = locking.PipeLock() |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 359 | |
| 360 | pid = os.fork() |
| 361 | if not pid: |
Mike Frysinger | 77bf4af | 2016-02-26 17:13:15 -0500 | [diff] [blame] | 362 | # Create our new isolated net namespace. |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 363 | namespaces.Unshare(namespaces.CLONE_NEWNET) |
Mike Frysinger | 77bf4af | 2016-02-26 17:13:15 -0500 | [diff] [blame] | 364 | |
| 365 | # Signal the parent the ns is ready to be configured. |
| 366 | ns_create_lock.Post() |
| 367 | del ns_create_lock |
| 368 | |
| 369 | # Wait for the parent to finish setting up the ns/proxy. |
| 370 | ns_setup_lock.Wait() |
| 371 | del ns_setup_lock |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 372 | |
| 373 | # Set up child side of the network. |
| 374 | commands = ( |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 375 | ('ip', 'link', 'set', 'up', 'lo'), |
| 376 | ('ip', 'address', 'add', |
| 377 | '%s/%u' % (PROXY_GUEST_IP, PROXY_NETMASK), |
| 378 | 'dev', veth_guest), |
| 379 | ('ip', 'link', 'set', veth_guest, 'up'), |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 380 | ) |
| 381 | try: |
| 382 | for cmd in commands: |
| 383 | cros_build_lib.RunCommand(cmd, print_cmd=False) |
| 384 | except cros_build_lib.RunCommandError: |
| 385 | raise SystemExit('Running %r failed!' % (cmd,)) |
| 386 | |
| 387 | proxy_url = 'http://%s:%u' % (PROXY_HOST_IP, PROXY_PORT) |
| 388 | for proto in ('http', 'https', 'ftp'): |
| 389 | os.environ[proto + '_proxy'] = proxy_url |
| 390 | for v in ('all_proxy', 'RSYNC_PROXY', 'no_proxy'): |
| 391 | os.environ.pop(v, None) |
| 392 | return |
| 393 | |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 394 | # Set up parent side of the network. |
| 395 | uid = int(os.environ.get('SUDO_UID', '0')) |
| 396 | gid = int(os.environ.get('SUDO_GID', '0')) |
| 397 | if uid == 0 or gid == 0: |
| 398 | for username in PROXY_APACHE_FALLBACK_USERS: |
| 399 | try: |
| 400 | pwnam = pwd.getpwnam(username) |
| 401 | uid, gid = pwnam.pw_uid, pwnam.pw_gid |
| 402 | break |
| 403 | except KeyError: |
| 404 | continue |
| 405 | if uid == 0 or gid == 0: |
| 406 | raise SystemExit('Could not find a non-root user to run Apache as') |
| 407 | |
| 408 | chroot_parent, chroot_base = os.path.split(options.chroot) |
| 409 | pid_file = os.path.join(chroot_parent, '.%s-apache-proxy.pid' % chroot_base) |
| 410 | log_file = os.path.join(chroot_parent, '.%s-apache-proxy.log' % chroot_base) |
| 411 | |
Mike Frysinger | 77bf4af | 2016-02-26 17:13:15 -0500 | [diff] [blame] | 412 | # Wait for the child to create the net ns. |
| 413 | ns_create_lock.Wait() |
| 414 | del ns_create_lock |
| 415 | |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 416 | apache_directives = [ |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 417 | 'User #%u' % uid, |
| 418 | 'Group #%u' % gid, |
| 419 | 'PidFile %s' % pid_file, |
| 420 | 'ErrorLog %s' % log_file, |
| 421 | 'Listen %s:%u' % (PROXY_HOST_IP, PROXY_PORT), |
| 422 | 'ServerName %s' % PROXY_HOST_IP, |
| 423 | 'ProxyRequests On', |
| 424 | 'AllowCONNECT %s' % ' '.join(map(str, PROXY_CONNECT_PORTS)), |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 425 | ] + [ |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 426 | 'LoadModule %s %s' % (mod, os.path.join(apache_module_path, so)) |
| 427 | for (mod, so) in apache_modules |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 428 | ] |
| 429 | commands = ( |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 430 | ('ip', 'link', 'add', 'name', veth_host, |
| 431 | 'type', 'veth', 'peer', 'name', veth_guest), |
| 432 | ('ip', 'address', 'add', |
| 433 | '%s/%u' % (PROXY_HOST_IP, PROXY_NETMASK), |
| 434 | 'dev', veth_host), |
| 435 | ('ip', 'link', 'set', veth_host, 'up'), |
| 436 | ([apache_bin, '-f', '/dev/null'] + |
| 437 | [arg for d in apache_directives for arg in ('-C', d)]), |
| 438 | ('ip', 'link', 'set', veth_guest, 'netns', str(pid)), |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 439 | ) |
| 440 | cmd = None # Make cros lint happy. |
| 441 | try: |
| 442 | for cmd in commands: |
| 443 | cros_build_lib.RunCommand(cmd, print_cmd=False) |
| 444 | except cros_build_lib.RunCommandError: |
| 445 | # Clean up existing interfaces, if any. |
| 446 | cmd_cleanup = ('ip', 'link', 'del', veth_host) |
| 447 | try: |
| 448 | cros_build_lib.RunCommand(cmd_cleanup, print_cmd=False) |
| 449 | except cros_build_lib.RunCommandError: |
Ralph Nathan | 5990042 | 2015-03-24 10:41:17 -0700 | [diff] [blame] | 450 | logging.error('running %r failed', cmd_cleanup) |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 451 | raise SystemExit('Running %r failed!' % (cmd,)) |
Mike Frysinger | 77bf4af | 2016-02-26 17:13:15 -0500 | [diff] [blame] | 452 | |
| 453 | # Signal the child that the net ns/proxy is fully configured now. |
| 454 | ns_setup_lock.Post() |
| 455 | del ns_setup_lock |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 456 | |
Mike Frysinger | e2d8f0d | 2014-11-01 13:09:26 -0400 | [diff] [blame] | 457 | process_util.ExitAsStatus(os.waitpid(pid, 0)[1]) |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 458 | |
| 459 | |
Mike Frysinger | a78a56e | 2012-11-20 06:02:30 -0500 | [diff] [blame] | 460 | def _ReExecuteIfNeeded(argv): |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 461 | """Re-execute cros_sdk as root. |
| 462 | |
| 463 | Also unshare the mount namespace so as to ensure that processes outside |
| 464 | the chroot can't mess with our mounts. |
| 465 | """ |
| 466 | if os.geteuid() != 0: |
Mike Frysinger | a78a56e | 2012-11-20 06:02:30 -0500 | [diff] [blame] | 467 | cmd = _SudoCommand() + ['--'] + argv |
| 468 | os.execvp(cmd[0], cmd) |
Mike Frysinger | a78a56e | 2012-11-20 06:02:30 -0500 | [diff] [blame] | 469 | else: |
Mike Frysinger | 80dfce9 | 2014-04-21 10:58:53 -0400 | [diff] [blame] | 470 | # We must set up the cgroups mounts before we enter our own namespace. |
| 471 | # This way it is a shared resource in the root mount namespace. |
Josh Triplett | e759b23 | 2013-03-08 13:03:43 -0800 | [diff] [blame] | 472 | cgroups.Cgroup.InitSystem() |
Mike Frysinger | 7a5fd84 | 2014-11-26 18:10:07 -0500 | [diff] [blame] | 473 | namespaces.SimpleUnshare() |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 474 | |
| 475 | |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 476 | def _CreateParser(sdk_latest_version, bootstrap_latest_version): |
| 477 | """Generate and return the parser with all the options.""" |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 478 | usage = ('usage: %(prog)s [options] ' |
| 479 | '[VAR1=val1 ... VAR2=val2] [--] [command [args]]') |
| 480 | parser = commandline.ArgumentParser(usage=usage, description=__doc__, |
| 481 | caching=True) |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 482 | |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 483 | # Global options. |
Mike Frysinger | 648ba2d | 2013-01-08 14:19:34 -0500 | [diff] [blame] | 484 | default_chroot = os.path.join(constants.SOURCE_ROOT, |
| 485 | constants.DEFAULT_CHROOT_DIR) |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 486 | parser.add_argument( |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 487 | '--chroot', dest='chroot', default=default_chroot, type='path', |
| 488 | help=('SDK chroot dir name [%s]' % constants.DEFAULT_CHROOT_DIR)) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 489 | |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 490 | parser.add_argument('--chrome_root', type='path', |
| 491 | help='Mount this chrome root into the SDK chroot') |
| 492 | parser.add_argument('--chrome_root_mount', type='path', |
| 493 | help='Mount chrome into this path inside SDK chroot') |
| 494 | parser.add_argument('--nousepkg', action='store_true', default=False, |
| 495 | help='Do not use binary packages when creating a chroot.') |
| 496 | parser.add_argument('-u', '--url', dest='sdk_url', |
| 497 | help='Use sdk tarball located at this url. Use file:// ' |
| 498 | 'for local files.') |
| 499 | parser.add_argument('--sdk-version', |
| 500 | help=('Use this sdk version. For prebuilt, current is %r' |
| 501 | ', for bootstrapping it is %r.' |
| 502 | % (sdk_latest_version, bootstrap_latest_version))) |
| 503 | parser.add_argument('--workspace', |
| 504 | help='Workspace directory to mount into the chroot.') |
Hidehiko Abe | b5daf2f | 2017-03-02 17:57:43 +0900 | [diff] [blame] | 505 | parser.add_argument('--goma_dir', type='path', |
| 506 | help='Goma installed directory to mount into the chroot.') |
| 507 | parser.add_argument('--goma_client_json', type='path', |
| 508 | help='Service account json file to use goma on bot. ' |
| 509 | 'Mounted into the chroot.') |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 510 | parser.add_argument('commands', nargs=argparse.REMAINDER) |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 511 | |
Gilad Arnold | 6a8f045 | 2015-06-04 11:25:18 -0700 | [diff] [blame] | 512 | # SDK overlay tarball options (mutually exclusive). |
| 513 | group = parser.add_mutually_exclusive_group() |
| 514 | group.add_argument('--toolchains', |
| 515 | help=('Comma-separated list of toolchains we expect to be ' |
| 516 | 'using on the chroot. Used for downloading a ' |
| 517 | 'corresponding SDK toolchains group (if one is ' |
| 518 | 'found), which may speed up chroot initialization ' |
| 519 | 'when building for the first time. Otherwise this ' |
| 520 | 'has no effect and will not restrict the chroot in ' |
| 521 | 'any way. Ignored if using --bootstrap.')) |
| 522 | group.add_argument('--board', |
| 523 | help=('The board we intend to be building in the chroot. ' |
| 524 | 'Used for deriving the list of required toolchains ' |
| 525 | '(see --toolchains).')) |
| 526 | |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 527 | # Commands. |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 528 | group = parser.add_argument_group('Commands') |
| 529 | group.add_argument( |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 530 | '--enter', action='store_true', default=False, |
| 531 | help='Enter the SDK chroot. Implies --create.') |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 532 | group.add_argument( |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 533 | '--create', action='store_true', default=False, |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 534 | help='Create the chroot only if it does not already exist. ' |
| 535 | 'Implies --download.') |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 536 | group.add_argument( |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 537 | '--bootstrap', action='store_true', default=False, |
| 538 | help='Build everything from scratch, including the sdk. ' |
| 539 | 'Use this only if you need to validate a change ' |
| 540 | 'that affects SDK creation itself (toolchain and ' |
| 541 | 'build are typically the only folk who need this). ' |
| 542 | 'Note this will quite heavily slow down the build. ' |
| 543 | 'This option implies --create --nousepkg.') |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 544 | group.add_argument( |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 545 | '-r', '--replace', action='store_true', default=False, |
| 546 | help='Replace an existing SDK chroot. Basically an alias ' |
| 547 | 'for --delete --create.') |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 548 | group.add_argument( |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 549 | '--delete', action='store_true', default=False, |
| 550 | help='Delete the current SDK chroot if it exists.') |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 551 | group.add_argument( |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 552 | '--download', action='store_true', default=False, |
| 553 | help='Download the sdk.') |
| 554 | commands = group |
| 555 | |
Mike Frysinger | 80dfce9 | 2014-04-21 10:58:53 -0400 | [diff] [blame] | 556 | # Namespace options. |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 557 | group = parser.add_argument_group('Namespaces') |
| 558 | group.add_argument('--proxy-sim', action='store_true', default=False, |
| 559 | help='Simulate a restrictive network requiring an outbound' |
| 560 | ' proxy.') |
| 561 | group.add_argument('--no-ns-pid', dest='ns_pid', |
| 562 | default=True, action='store_false', |
| 563 | help='Do not create a new PID namespace.') |
Mike Frysinger | 80dfce9 | 2014-04-21 10:58:53 -0400 | [diff] [blame] | 564 | |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 565 | # Internal options. |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 566 | group = parser.add_argument_group( |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 567 | 'Internal Chromium OS Build Team Options', |
| 568 | 'Caution: these are for meant for the Chromium OS build team only') |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 569 | group.add_argument('--buildbot-log-version', default=False, |
| 570 | action='store_true', |
| 571 | help='Log SDK version for buildbot consumption') |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 572 | |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 573 | return parser, commands |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 574 | |
| 575 | |
| 576 | def main(argv): |
| 577 | conf = cros_build_lib.LoadKeyValueFile( |
| 578 | os.path.join(constants.SOURCE_ROOT, constants.SDK_VERSION_FILE), |
| 579 | ignore_missing=True) |
| 580 | sdk_latest_version = conf.get('SDK_LATEST_VERSION', '<unknown>') |
| 581 | bootstrap_latest_version = conf.get('BOOTSTRAP_LATEST_VERSION', '<unknown>') |
| 582 | parser, commands = _CreateParser(sdk_latest_version, bootstrap_latest_version) |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 583 | options = parser.parse_args(argv) |
| 584 | chroot_command = options.commands |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 585 | |
| 586 | # Some sanity checks first, before we ask for sudo credentials. |
Mike Frysinger | 8fd67dc | 2012-12-03 23:51:18 -0500 | [diff] [blame] | 587 | cros_build_lib.AssertOutsideChroot() |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 588 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 589 | host = os.uname()[4] |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 590 | if host != 'x86_64': |
Benjamin Gordon | 040a116 | 2017-06-29 13:44:47 -0600 | [diff] [blame] | 591 | cros_build_lib.Die( |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 592 | "cros_sdk is currently only supported on x86_64; you're running" |
| 593 | " %s. Please find a x86_64 machine." % (host,)) |
| 594 | |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 595 | _ReportMissing(osutils.FindMissingBinaries(NEEDED_TOOLS)) |
| 596 | if options.proxy_sim: |
| 597 | _ReportMissing(osutils.FindMissingBinaries(PROXY_NEEDED_TOOLS)) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 598 | |
Benjamin Gordon | 040a116 | 2017-06-29 13:44:47 -0600 | [diff] [blame] | 599 | if (sdk_latest_version == '<unknown>' or |
| 600 | bootstrap_latest_version == '<unknown>'): |
| 601 | cros_build_lib.Die( |
| 602 | 'No SDK version was found. ' |
| 603 | 'Are you in a Chromium source tree instead of Chromium OS?\n\n' |
| 604 | 'Please change to a directory inside your Chromium OS source tree\n' |
| 605 | 'and retry. If you need to setup a Chromium OS source tree, see\n' |
| 606 | ' http://www.chromium.org/chromium-os/developer-guide') |
| 607 | |
David James | 471532c | 2013-01-21 10:23:31 -0800 | [diff] [blame] | 608 | _ReExecuteIfNeeded([sys.argv[0]] + argv) |
Mike Frysinger | 80dfce9 | 2014-04-21 10:58:53 -0400 | [diff] [blame] | 609 | if options.ns_pid: |
Mike Frysinger | e2d8f0d | 2014-11-01 13:09:26 -0400 | [diff] [blame] | 610 | first_pid = namespaces.CreatePidNs() |
Mike Frysinger | 80dfce9 | 2014-04-21 10:58:53 -0400 | [diff] [blame] | 611 | else: |
| 612 | first_pid = None |
David James | 471532c | 2013-01-21 10:23:31 -0800 | [diff] [blame] | 613 | |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 614 | # Expand out the aliases... |
| 615 | if options.replace: |
| 616 | options.delete = options.create = True |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 617 | |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 618 | if options.bootstrap: |
| 619 | options.create = True |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 620 | |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 621 | # If a command is not given, default to enter. |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 622 | # pylint: disable=protected-access |
| 623 | # This _group_actions access sucks, but upstream decided to not include an |
| 624 | # alternative to optparse's option_list, and this is what they recommend. |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 625 | options.enter |= not any(getattr(options, x.dest) |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 626 | for x in commands._group_actions) |
| 627 | # pylint: enable=protected-access |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 628 | options.enter |= bool(chroot_command) |
| 629 | |
| 630 | if options.enter and options.delete and not options.create: |
| 631 | parser.error("Trying to enter the chroot when --delete " |
| 632 | "was specified makes no sense.") |
| 633 | |
Benjamin Gordon | 35194f1 | 2017-07-19 10:26:22 -0600 | [diff] [blame^] | 634 | # Discern if we need to create the chroot. |
| 635 | chroot_exists = os.path.exists(os.path.join( |
| 636 | options.chroot, 'etc', 'cros_chroot_version')) |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 637 | if options.create or options.enter: |
| 638 | # Only create if it's being wiped, or if it doesn't exist. |
| 639 | if not options.delete and chroot_exists: |
| 640 | options.create = False |
| 641 | else: |
| 642 | options.download = True |
| 643 | |
| 644 | # Finally, flip create if necessary. |
| 645 | if options.enter: |
| 646 | options.create |= not chroot_exists |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 647 | |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 648 | if not options.sdk_version: |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 649 | sdk_version = (bootstrap_latest_version if options.bootstrap |
| 650 | else sdk_latest_version) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 651 | else: |
| 652 | sdk_version = options.sdk_version |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 653 | if options.buildbot_log_version: |
Prathmesh Prabhu | 17f0742 | 2015-07-17 11:40:40 -0700 | [diff] [blame] | 654 | logging.PrintBuildbotStepText(sdk_version) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 655 | |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 656 | # Based on selections, determine the tarball to fetch. |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 657 | if options.sdk_url: |
| 658 | urls = [options.sdk_url] |
| 659 | elif options.bootstrap: |
| 660 | urls = GetStage3Urls(sdk_version) |
| 661 | else: |
| 662 | urls = GetArchStageTarballs(sdk_version) |
| 663 | |
Gilad Arnold | 6a8f045 | 2015-06-04 11:25:18 -0700 | [diff] [blame] | 664 | # Get URLs for the toolchains overlay, if one is to be used. |
| 665 | toolchains_overlay_urls = None |
| 666 | if not options.bootstrap: |
| 667 | toolchains = None |
| 668 | if options.toolchains: |
| 669 | toolchains = options.toolchains.split(',') |
| 670 | elif options.board: |
| 671 | toolchains = toolchain.GetToolchainsForBoard(options.board).keys() |
| 672 | |
| 673 | if toolchains: |
| 674 | toolchains_overlay_urls = GetToolchainsOverlayUrls(sdk_version, |
| 675 | toolchains) |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 676 | |
Brian Harring | b6cf914 | 2012-09-01 20:43:17 -0700 | [diff] [blame] | 677 | lock_path = os.path.dirname(options.chroot) |
Gilad Arnold | fbe40e2 | 2015-03-18 14:52:16 -0700 | [diff] [blame] | 678 | lock_path = os.path.join( |
| 679 | lock_path, '.%s_lock' % os.path.basename(options.chroot).lstrip('.')) |
Mike Frysinger | 80dfce9 | 2014-04-21 10:58:53 -0400 | [diff] [blame] | 680 | with cgroups.SimpleContainChildren('cros_sdk', pid=first_pid): |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 681 | with locking.FileLock(lock_path, 'chroot lock') as lock: |
Gilad Arnold | 6a8f045 | 2015-06-04 11:25:18 -0700 | [diff] [blame] | 682 | toolchains_overlay_tarball = None |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 683 | |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 684 | if options.proxy_sim: |
| 685 | _ProxySimSetup(options) |
| 686 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 687 | if options.delete and os.path.exists(options.chroot): |
| 688 | lock.write_lock() |
| 689 | DeleteChroot(options.chroot) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 690 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 691 | sdk_cache = os.path.join(options.cache_dir, 'sdks') |
| 692 | distfiles_cache = os.path.join(options.cache_dir, 'distfiles') |
Yu-Ju Hong | 2c06676 | 2013-10-28 14:05:08 -0700 | [diff] [blame] | 693 | osutils.SafeMakedirsNonRoot(options.cache_dir) |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 694 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 695 | for target in (sdk_cache, distfiles_cache): |
Mike Frysinger | 648ba2d | 2013-01-08 14:19:34 -0500 | [diff] [blame] | 696 | src = os.path.join(constants.SOURCE_ROOT, os.path.basename(target)) |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 697 | if not os.path.exists(src): |
Prathmesh Prabhu | 06a5056 | 2016-10-22 01:41:44 -0700 | [diff] [blame] | 698 | osutils.SafeMakedirsNonRoot(target) |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 699 | continue |
| 700 | lock.write_lock( |
| 701 | "Upgrade to %r needed but chroot is locked; please exit " |
| 702 | "all instances so this upgrade can finish." % src) |
| 703 | if not os.path.exists(src): |
| 704 | # Note that while waiting for the write lock, src may've vanished; |
| 705 | # it's a rare race during the upgrade process that's a byproduct |
| 706 | # of us avoiding taking a write lock to do the src check. If we |
| 707 | # took a write lock for that check, it would effectively limit |
| 708 | # all cros_sdk for a chroot to a single instance. |
Prathmesh Prabhu | 06a5056 | 2016-10-22 01:41:44 -0700 | [diff] [blame] | 709 | osutils.SafeMakedirsNonRoot(target) |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 710 | elif not os.path.exists(target): |
| 711 | # Upgrade occurred, but a reversion, or something whacky |
| 712 | # occurred writing to the old location. Wipe and continue. |
| 713 | os.rename(src, target) |
| 714 | else: |
| 715 | # Upgrade occurred once already, but either a reversion or |
| 716 | # some before/after separate cros_sdk usage is at play. |
| 717 | # Wipe and continue. |
| 718 | osutils.RmDir(src) |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 719 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 720 | if options.download: |
| 721 | lock.write_lock() |
Gilad Arnold | 6a8f045 | 2015-06-04 11:25:18 -0700 | [diff] [blame] | 722 | sdk_tarball = FetchRemoteTarballs( |
| 723 | sdk_cache, urls, 'stage3' if options.bootstrap else 'SDK') |
| 724 | if toolchains_overlay_urls: |
| 725 | toolchains_overlay_tarball = FetchRemoteTarballs( |
| 726 | sdk_cache, toolchains_overlay_urls, 'SDK toolchains overlay', |
| 727 | allow_none=True) |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 728 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 729 | if options.create: |
| 730 | lock.write_lock() |
Gilad Arnold | 6a8f045 | 2015-06-04 11:25:18 -0700 | [diff] [blame] | 731 | CreateChroot(options.chroot, sdk_tarball, toolchains_overlay_tarball, |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 732 | options.cache_dir, |
David Pursell | 3528e8e | 2015-07-07 11:22:15 -0700 | [diff] [blame] | 733 | nousepkg=(options.bootstrap or options.nousepkg)) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 734 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 735 | if options.enter: |
| 736 | lock.read_lock() |
| 737 | EnterChroot(options.chroot, options.cache_dir, options.chrome_root, |
Don Garrett | 230d1b2 | 2015-03-09 16:21:19 -0700 | [diff] [blame] | 738 | options.chrome_root_mount, options.workspace, |
Hidehiko Abe | b5daf2f | 2017-03-02 17:57:43 +0900 | [diff] [blame] | 739 | options.goma_dir, options.goma_client_json, |
Don Garrett | 230d1b2 | 2015-03-09 16:21:19 -0700 | [diff] [blame] | 740 | chroot_command) |