Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Mike Frysinger | 2de7f04 | 2012-07-10 04:45:03 -0400 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 6 | """Manage SDK chroots. |
| 7 | |
| 8 | This script is used for manipulating local chroot environments; creating, |
| 9 | deleting, downloading, etc. If given --enter (or no args), it defaults |
| 10 | to an interactive bash shell within the chroot. |
| 11 | |
| 12 | If given args those are passed to the chroot environment, and executed. |
| 13 | """ |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 14 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 15 | from __future__ import print_function |
| 16 | |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 17 | import argparse |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 18 | import glob |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 19 | import os |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 20 | import pwd |
Benjamin Gordon | 2d7bf58 | 2017-07-12 10:11:26 -0600 | [diff] [blame] | 21 | import random |
Brian Norris | d37e2f7 | 2016-08-22 16:09:24 -0700 | [diff] [blame] | 22 | import re |
Ting-Yuan Huang | f56d9af | 2017-06-19 16:08:32 -0700 | [diff] [blame] | 23 | import resource |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 24 | import sys |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 25 | import urlparse |
| 26 | |
Aviv Keshet | b7519e1 | 2016-10-04 00:50:00 -0700 | [diff] [blame] | 27 | from chromite.lib import constants |
Brian Harring | cfe762a | 2012-02-29 13:03:53 -0800 | [diff] [blame] | 28 | from chromite.lib import cgroups |
Brian Harring | b6cf914 | 2012-09-01 20:43:17 -0700 | [diff] [blame] | 29 | from chromite.lib import commandline |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 30 | from chromite.lib import cros_build_lib |
Ralph Nathan | 5990042 | 2015-03-24 10:41:17 -0700 | [diff] [blame] | 31 | from chromite.lib import cros_logging as logging |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 32 | from chromite.lib import locking |
Josh Triplett | e759b23 | 2013-03-08 13:03:43 -0800 | [diff] [blame] | 33 | from chromite.lib import namespaces |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 34 | from chromite.lib import osutils |
Yong Hong | 84ba917 | 2018-02-07 01:37:42 +0800 | [diff] [blame] | 35 | from chromite.lib import path_util |
Mike Frysinger | e2d8f0d | 2014-11-01 13:09:26 -0400 | [diff] [blame] | 36 | from chromite.lib import process_util |
David James | c93e6a4d | 2014-01-13 11:37:36 -0800 | [diff] [blame] | 37 | from chromite.lib import retry_util |
Mike Frysinger | 8e727a3 | 2013-01-16 16:57:53 -0500 | [diff] [blame] | 38 | from chromite.lib import toolchain |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 39 | |
| 40 | cros_build_lib.STRICT_SUDO = True |
| 41 | |
| 42 | |
Zdenek Behan | aa52cea | 2012-05-30 01:31:11 +0200 | [diff] [blame] | 43 | COMPRESSION_PREFERENCE = ('xz', 'bz2') |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 44 | |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 45 | # TODO(zbehan): Remove the dependency on these, reimplement them in python |
Mike Frysinger | 648ba2d | 2013-01-08 14:19:34 -0500 | [diff] [blame] | 46 | MAKE_CHROOT = [os.path.join(constants.SOURCE_ROOT, |
| 47 | 'src/scripts/sdk_lib/make_chroot.sh')] |
| 48 | ENTER_CHROOT = [os.path.join(constants.SOURCE_ROOT, |
| 49 | 'src/scripts/sdk_lib/enter_chroot.sh')] |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 50 | |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 51 | # Proxy simulator configuration. |
| 52 | PROXY_HOST_IP = '192.168.240.1' |
| 53 | PROXY_PORT = 8080 |
| 54 | PROXY_GUEST_IP = '192.168.240.2' |
| 55 | PROXY_NETMASK = 30 |
| 56 | PROXY_VETH_PREFIX = 'veth' |
| 57 | PROXY_CONNECT_PORTS = (80, 443, 9418) |
| 58 | PROXY_APACHE_FALLBACK_USERS = ('www-data', 'apache', 'nobody') |
| 59 | PROXY_APACHE_MPMS = ('event', 'worker', 'prefork') |
| 60 | PROXY_APACHE_FALLBACK_PATH = ':'.join( |
| 61 | '/usr/lib/apache2/mpm-%s' % mpm for mpm in PROXY_APACHE_MPMS |
| 62 | ) |
| 63 | PROXY_APACHE_MODULE_GLOBS = ('/usr/lib*/apache2/modules', '/usr/lib*/apache2') |
| 64 | |
Josh Triplett | 9a495f6 | 2013-03-15 18:06:55 -0700 | [diff] [blame] | 65 | # We need these tools to run. Very common tools (tar,..) are omitted. |
Josh Triplett | e759b23 | 2013-03-08 13:03:43 -0800 | [diff] [blame] | 66 | NEEDED_TOOLS = ('curl', 'xz') |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 67 | |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 68 | # Tools needed for --proxy-sim only. |
| 69 | PROXY_NEEDED_TOOLS = ('ip',) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 70 | |
Benjamin Gordon | 386b9eb | 2017-07-20 09:21:33 -0600 | [diff] [blame] | 71 | # Tools needed when use_image is true (the default). |
| 72 | IMAGE_NEEDED_TOOLS = ('losetup', 'lvchange', 'lvcreate', 'lvs', 'mke2fs', |
Benjamin Gordon | cfa9c16 | 2017-08-03 13:49:29 -0600 | [diff] [blame] | 73 | 'pvscan', 'thin_check', 'vgchange', 'vgcreate', 'vgs') |
Benjamin Gordon | 386b9eb | 2017-07-20 09:21:33 -0600 | [diff] [blame] | 74 | |
Benjamin Gordon | e3d5bd1 | 2017-11-16 15:42:28 -0700 | [diff] [blame] | 75 | # As space is used inside the chroot, the empty space in chroot.img is |
| 76 | # allocated. Deleting files inside the chroot doesn't automatically return the |
| 77 | # used space to the OS. Over time, this tends to make the sparse chroot.img |
| 78 | # less sparse even if the chroot contents don't currently need much space. We |
| 79 | # can recover most of this unused space with fstrim, but that takes too much |
| 80 | # time to run it every time. Instead, check the used space against the image |
| 81 | # size after mounting the chroot and only call fstrim if it looks like we could |
| 82 | # recover at least this many GiB. |
| 83 | MAX_UNUSED_IMAGE_GBS = 20 |
| 84 | |
Mike Frysinger | cc83883 | 2014-05-24 13:10:30 -0400 | [diff] [blame] | 85 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 86 | def GetArchStageTarballs(version): |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 87 | """Returns the URL for a given arch/version""" |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 88 | extension = {'bz2':'tbz2', 'xz':'tar.xz'} |
Mike Frysinger | 8e727a3 | 2013-01-16 16:57:53 -0500 | [diff] [blame] | 89 | return [toolchain.GetSdkURL(suburl='cros-sdk-%s.%s' |
| 90 | % (version, extension[compressor])) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 91 | for compressor in COMPRESSION_PREFERENCE] |
| 92 | |
| 93 | |
| 94 | def GetStage3Urls(version): |
Mike Frysinger | 8e727a3 | 2013-01-16 16:57:53 -0500 | [diff] [blame] | 95 | return [toolchain.GetSdkURL(suburl='stage3-amd64-%s.tar.%s' % (version, ext)) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 96 | for ext in COMPRESSION_PREFERENCE] |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 97 | |
| 98 | |
Gilad Arnold | 6a8f045 | 2015-06-04 11:25:18 -0700 | [diff] [blame] | 99 | def GetToolchainsOverlayUrls(version, toolchains): |
| 100 | """Returns the URL(s) for a toolchains SDK overlay. |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 101 | |
| 102 | Args: |
| 103 | version: The SDK version used, e.g. 2015.05.27.145939. We use the year and |
| 104 | month components to point to a subdirectory on the SDK bucket where |
Gilad Arnold | 6a8f045 | 2015-06-04 11:25:18 -0700 | [diff] [blame] | 105 | overlays are stored (.../2015/05/ in this case). |
| 106 | toolchains: Iterable of toolchain target strings (e.g. 'i686-pc-linux-gnu'). |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 107 | |
| 108 | Returns: |
Gilad Arnold | 6a8f045 | 2015-06-04 11:25:18 -0700 | [diff] [blame] | 109 | List of alternative download URLs for an SDK overlay tarball that contains |
| 110 | the given toolchains. |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 111 | """ |
Gilad Arnold | 6a8f045 | 2015-06-04 11:25:18 -0700 | [diff] [blame] | 112 | toolchains_desc = '-'.join(sorted(toolchains)) |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 113 | suburl_template = os.path.join( |
| 114 | *(version.split('.')[:2] + |
Gilad Arnold | 6a8f045 | 2015-06-04 11:25:18 -0700 | [diff] [blame] | 115 | ['cros-sdk-overlay-toolchains-%s-%s.tar.%%s' % |
| 116 | (toolchains_desc, version)])) |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 117 | return [toolchain.GetSdkURL(suburl=suburl_template % ext) |
| 118 | for ext in COMPRESSION_PREFERENCE] |
| 119 | |
| 120 | |
| 121 | def FetchRemoteTarballs(storage_dir, urls, desc, allow_none=False): |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 122 | """Fetches a tarball given by url, and place it in |storage_dir|. |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 123 | |
| 124 | Args: |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 125 | storage_dir: Path where to save the tarball. |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 126 | 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] | 127 | desc: A string describing what tarball we're downloading (for logging). |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 128 | allow_none: Don't fail if none of the URLs worked. |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 129 | |
| 130 | Returns: |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 131 | Full path to the downloaded file, or None if |allow_none| and no URL worked. |
| 132 | |
| 133 | Raises: |
| 134 | ValueError: If |allow_none| is False and none of the URLs worked. |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 135 | """ |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 136 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 137 | # Note we track content length ourselves since certain versions of curl |
| 138 | # fail if asked to resume a complete file. |
| 139 | # pylint: disable=C0301,W0631 |
| 140 | # 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] | 141 | logging.notice('Downloading %s tarball...', desc) |
Brian Norris | cf8aef4 | 2016-09-27 10:43:39 -0700 | [diff] [blame] | 142 | status_re = re.compile(r'^HTTP/[0-9]+(\.[0-9]+)? 200') |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 143 | for url in urls: |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 144 | # http://www.logilab.org/ticket/8766 |
| 145 | # pylint: disable=E1101 |
| 146 | parsed = urlparse.urlparse(url) |
| 147 | tarball_name = os.path.basename(parsed.path) |
| 148 | if parsed.scheme in ('', 'file'): |
| 149 | if os.path.exists(parsed.path): |
| 150 | return parsed.path |
| 151 | continue |
| 152 | content_length = 0 |
Ralph Nathan | 7070e6a | 2015-04-02 10:16:43 -0700 | [diff] [blame] | 153 | logging.debug('Attempting download from %s', url) |
David James | c93e6a4d | 2014-01-13 11:37:36 -0800 | [diff] [blame] | 154 | result = retry_util.RunCurl( |
Hidehiko Abe | e55af7f | 2017-05-01 18:38:04 +0900 | [diff] [blame] | 155 | ['-I', url], print_cmd=False, debug_level=logging.NOTICE, |
| 156 | capture_output=True) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 157 | successful = False |
| 158 | for header in result.output.splitlines(): |
Brian Norris | d37e2f7 | 2016-08-22 16:09:24 -0700 | [diff] [blame] | 159 | # 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] | 160 | # a proxy is involved and may have pushed down the actual header. |
Brian Norris | d37e2f7 | 2016-08-22 16:09:24 -0700 | [diff] [blame] | 161 | if status_re.match(header): |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 162 | successful = True |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 163 | elif header.lower().startswith('content-length:'): |
| 164 | content_length = int(header.split(':', 1)[-1].strip()) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 165 | if successful: |
| 166 | break |
| 167 | if successful: |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 168 | break |
| 169 | else: |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 170 | if allow_none: |
| 171 | return None |
| 172 | raise ValueError('No valid URLs found!') |
Zdenek Behan | fd0efe4 | 2012-04-13 04:36:40 +0200 | [diff] [blame] | 173 | |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 174 | tarball_dest = os.path.join(storage_dir, tarball_name) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 175 | current_size = 0 |
| 176 | if os.path.exists(tarball_dest): |
| 177 | current_size = os.path.getsize(tarball_dest) |
| 178 | if current_size > content_length: |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 179 | osutils.SafeUnlink(tarball_dest) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 180 | current_size = 0 |
Zdenek Behan | b2fa72e | 2012-03-16 04:49:30 +0100 | [diff] [blame] | 181 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 182 | if current_size < content_length: |
David James | c93e6a4d | 2014-01-13 11:37:36 -0800 | [diff] [blame] | 183 | retry_util.RunCurl( |
Hidehiko Abe | e55af7f | 2017-05-01 18:38:04 +0900 | [diff] [blame] | 184 | ['--fail', '-L', '-y', '30', '-C', '-', '--output', tarball_dest, url], |
| 185 | print_cmd=False, debug_level=logging.NOTICE) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 186 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 187 | # Cleanup old tarballs now since we've successfull fetched; only cleanup |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 188 | # the tarballs for our prefix, or unknown ones. This gets a bit tricky |
| 189 | # because we might have partial overlap between known prefixes. |
| 190 | my_prefix = tarball_name.rsplit('-', 1)[0] + '-' |
| 191 | all_prefixes = ('stage3-amd64-', 'cros-sdk-', 'cros-sdk-overlay-') |
| 192 | ignored_prefixes = [prefix for prefix in all_prefixes if prefix != my_prefix] |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 193 | for filename in os.listdir(storage_dir): |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 194 | if (filename == tarball_name or |
| 195 | any([(filename.startswith(p) and |
| 196 | not (len(my_prefix) > len(p) and filename.startswith(my_prefix))) |
| 197 | for p in ignored_prefixes])): |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 198 | continue |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 199 | logging.info('Cleaning up old tarball: %s', filename) |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 200 | osutils.SafeUnlink(os.path.join(storage_dir, filename)) |
Zdenek Behan | 9c644dd | 2012-04-05 06:24:02 +0200 | [diff] [blame] | 201 | |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 202 | return tarball_dest |
| 203 | |
| 204 | |
Gilad Arnold | 6a8f045 | 2015-06-04 11:25:18 -0700 | [diff] [blame] | 205 | def CreateChroot(chroot_path, sdk_tarball, toolchains_overlay_tarball, |
Benjamin Gordon | abb3e37 | 2017-08-09 10:21:05 -0600 | [diff] [blame] | 206 | cache_dir, nousepkg=False): |
| 207 | """Creates a new chroot from a given SDK. |
| 208 | |
| 209 | Args: |
| 210 | chroot_path: Path where the new chroot will be created. |
| 211 | sdk_tarball: Path to a downloaded Gentoo Stage3 or Chromium OS SDK tarball. |
| 212 | toolchains_overlay_tarball: Optional path to a second tarball that will be |
| 213 | unpacked into the chroot on top of the SDK tarball. |
| 214 | cache_dir: Path to a directory that will be used for caching portage files, |
| 215 | etc. |
| 216 | nousepkg: If True, pass --nousepkg to cros_setup_toolchains inside the |
| 217 | chroot. |
| 218 | """ |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 219 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 220 | cmd = MAKE_CHROOT + ['--stage3_path', sdk_tarball, |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 221 | '--chroot', chroot_path, |
| 222 | '--cache_dir', cache_dir] |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 223 | |
Gilad Arnold | 6a8f045 | 2015-06-04 11:25:18 -0700 | [diff] [blame] | 224 | if toolchains_overlay_tarball: |
| 225 | cmd.extend(['--toolchains_overlay_path', toolchains_overlay_tarball]) |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 226 | |
Mike Frysinger | 2de7f04 | 2012-07-10 04:45:03 -0400 | [diff] [blame] | 227 | if nousepkg: |
| 228 | cmd.append('--nousepkg') |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 229 | |
Ralph Nathan | 7070e6a | 2015-04-02 10:16:43 -0700 | [diff] [blame] | 230 | logging.notice('Creating chroot. This may take a few minutes...') |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 231 | try: |
| 232 | cros_build_lib.RunCommand(cmd, print_cmd=False) |
| 233 | except cros_build_lib.RunCommandError: |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 234 | raise SystemExit('Running %r failed!' % cmd) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 235 | |
| 236 | |
| 237 | def DeleteChroot(chroot_path): |
| 238 | """Deletes an existing chroot""" |
| 239 | cmd = MAKE_CHROOT + ['--chroot', chroot_path, |
| 240 | '--delete'] |
| 241 | try: |
Ralph Nathan | 7070e6a | 2015-04-02 10:16:43 -0700 | [diff] [blame] | 242 | logging.notice('Deleting chroot.') |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 243 | cros_build_lib.RunCommand(cmd, print_cmd=False) |
| 244 | except cros_build_lib.RunCommandError: |
Brian Harring | 98b5490 | 2012-03-23 04:05:42 -0700 | [diff] [blame] | 245 | raise SystemExit('Running %r failed!' % cmd) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 246 | |
| 247 | |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 248 | def EnterChroot(chroot_path, cache_dir, chrome_root, chrome_root_mount, |
Yong Hong | 84ba917 | 2018-02-07 01:37:42 +0800 | [diff] [blame] | 249 | workspace, goma_dir, goma_client_json, working_dir, |
| 250 | additional_args): |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 251 | """Enters an existing SDK chroot""" |
Mike Frysinger | e545697 | 2013-06-13 00:07:23 -0400 | [diff] [blame] | 252 | st = os.statvfs(os.path.join(chroot_path, 'usr', 'bin', 'sudo')) |
| 253 | # The os.ST_NOSUID constant wasn't added until python-3.2. |
| 254 | if st.f_flag & 0x2: |
| 255 | cros_build_lib.Die('chroot cannot be in a nosuid mount') |
| 256 | |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 257 | cmd = ENTER_CHROOT + ['--chroot', chroot_path, '--cache_dir', cache_dir] |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 258 | if chrome_root: |
| 259 | cmd.extend(['--chrome_root', chrome_root]) |
| 260 | if chrome_root_mount: |
| 261 | cmd.extend(['--chrome_root_mount', chrome_root_mount]) |
Don Garrett | 230d1b2 | 2015-03-09 16:21:19 -0700 | [diff] [blame] | 262 | if workspace: |
| 263 | cmd.extend(['--workspace_root', workspace]) |
Hidehiko Abe | b5daf2f | 2017-03-02 17:57:43 +0900 | [diff] [blame] | 264 | if goma_dir: |
| 265 | cmd.extend(['--goma_dir', goma_dir]) |
| 266 | if goma_client_json: |
| 267 | cmd.extend(['--goma_client_json', goma_client_json]) |
Yong Hong | 84ba917 | 2018-02-07 01:37:42 +0800 | [diff] [blame] | 268 | if working_dir is not None: |
| 269 | cmd.extend(['--working_dir', working_dir]) |
Don Garrett | 230d1b2 | 2015-03-09 16:21:19 -0700 | [diff] [blame] | 270 | |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 271 | if len(additional_args) > 0: |
| 272 | cmd.append('--') |
| 273 | cmd.extend(additional_args) |
Brian Harring | 7199e7d | 2012-03-23 04:10:08 -0700 | [diff] [blame] | 274 | |
Ting-Yuan Huang | f56d9af | 2017-06-19 16:08:32 -0700 | [diff] [blame] | 275 | # ThinLTO opens lots of files at the same time. |
| 276 | resource.setrlimit(resource.RLIMIT_NOFILE, (32768, 32768)) |
Ralph Nathan | 549d350 | 2015-03-26 17:38:42 -0700 | [diff] [blame] | 277 | ret = cros_build_lib.RunCommand(cmd, print_cmd=False, error_code_ok=True, |
| 278 | mute_output=False) |
Brian Harring | 7199e7d | 2012-03-23 04:10:08 -0700 | [diff] [blame] | 279 | # If we were in interactive mode, ignore the exit code; it'll be whatever |
| 280 | # they last ran w/in the chroot and won't matter to us one way or another. |
| 281 | # Note this does allow chroot entrance to fail and be ignored during |
| 282 | # interactive; this is however a rare case and the user will immediately |
| 283 | # see it (nor will they be checking the exit code manually). |
| 284 | if ret.returncode != 0 and additional_args: |
Richard Barnette | 5c728a4 | 2015-03-18 11:50:21 -0700 | [diff] [blame] | 285 | raise SystemExit(ret.returncode) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 286 | |
| 287 | |
Benjamin Gordon | abb3e37 | 2017-08-09 10:21:05 -0600 | [diff] [blame] | 288 | def _ImageFileForChroot(chroot): |
| 289 | """Find the image file that should be associated with |chroot|. |
| 290 | |
| 291 | This function does not check if the image exists; it simply returns the |
| 292 | filename that would be used. |
| 293 | |
| 294 | Args: |
| 295 | chroot: Path to the chroot. |
| 296 | |
| 297 | Returns: |
| 298 | Path to an image file that would be associated with chroot. |
| 299 | """ |
| 300 | return chroot.rstrip('/') + '.img' |
| 301 | |
| 302 | |
Benjamin Gordon | 2d7bf58 | 2017-07-12 10:11:26 -0600 | [diff] [blame] | 303 | def CreateChrootSnapshot(snapshot_name, chroot_vg, chroot_lv): |
| 304 | """Create a snapshot for the specified chroot VG/LV. |
| 305 | |
| 306 | Args: |
| 307 | snapshot_name: The name of the new snapshot. |
| 308 | chroot_vg: The name of the VG containing the origin LV. |
| 309 | chroot_lv: The name of the origin LV. |
| 310 | |
| 311 | Returns: |
| 312 | True if the snapshot was created, or False if a snapshot with the same |
| 313 | name already exists. |
| 314 | |
| 315 | Raises: |
| 316 | SystemExit: The lvcreate command failed. |
| 317 | """ |
| 318 | if snapshot_name in ListChrootSnapshots(chroot_vg, chroot_lv): |
| 319 | logging.error('Cannot create snapshot %s: A volume with that name already ' |
| 320 | 'exists.', snapshot_name) |
| 321 | return False |
| 322 | |
| 323 | cmd = ['lvcreate', '-s', '--name', snapshot_name, '%s/%s' % ( |
| 324 | chroot_vg, chroot_lv)] |
| 325 | try: |
| 326 | logging.notice('Creating snapshot %s from %s in VG %s.', snapshot_name, |
| 327 | chroot_lv, chroot_vg) |
| 328 | cros_build_lib.RunCommand(cmd, print_cmd=False, capture_output=True) |
| 329 | return True |
| 330 | except cros_build_lib.RunCommandError: |
| 331 | raise SystemExit('Running %r failed!' % cmd) |
| 332 | |
| 333 | |
| 334 | def DeleteChrootSnapshot(snapshot_name, chroot_vg, chroot_lv): |
| 335 | """Delete the named snapshot from the specified chroot VG. |
| 336 | |
| 337 | If the requested snapshot is not found, nothing happens. The main chroot LV |
| 338 | and internal thinpool LV cannot be deleted with this function. |
| 339 | |
| 340 | Args: |
| 341 | snapshot_name: The name of the snapshot to delete. |
| 342 | chroot_vg: The name of the VG containing the origin LV. |
| 343 | chroot_lv: The name of the origin LV. |
| 344 | |
| 345 | Raises: |
| 346 | SystemExit: The lvremove command failed. |
| 347 | """ |
| 348 | if snapshot_name in (cros_build_lib.CHROOT_LV_NAME, |
| 349 | cros_build_lib.CHROOT_THINPOOL_NAME): |
| 350 | logging.error('Cannot remove LV %s as a snapshot. Use cros_sdk --delete ' |
| 351 | 'if you want to remove the whole chroot.', snapshot_name) |
| 352 | return |
| 353 | |
| 354 | if snapshot_name not in ListChrootSnapshots(chroot_vg, chroot_lv): |
| 355 | return |
| 356 | |
| 357 | cmd = ['lvremove', '-f', '%s/%s' % (chroot_vg, snapshot_name)] |
| 358 | try: |
| 359 | logging.notice('Deleting snapshot %s in VG %s.', snapshot_name, chroot_vg) |
| 360 | cros_build_lib.RunCommand(cmd, print_cmd=False, capture_output=True) |
| 361 | except cros_build_lib.RunCommandError: |
| 362 | raise SystemExit('Running %r failed!' % cmd) |
| 363 | |
| 364 | |
| 365 | def RestoreChrootSnapshot(snapshot_name, chroot_vg, chroot_lv): |
| 366 | """Restore the chroot to an existing snapshot. |
| 367 | |
| 368 | This is done by renaming the original |chroot_lv| LV to a temporary name, |
| 369 | renaming the snapshot named |snapshot_name| to |chroot_lv|, and deleting the |
| 370 | now unused LV. If an error occurs, attempts to rename the original snapshot |
| 371 | back to |chroot_lv| to leave the chroot unchanged. |
| 372 | |
| 373 | The chroot must be unmounted before calling this function, and will be left |
| 374 | unmounted after this function returns. |
| 375 | |
| 376 | Args: |
| 377 | snapshot_name: The name of the snapshot to restore. This snapshot will no |
| 378 | longer be accessible at its original name after this function finishes. |
| 379 | chroot_vg: The VG containing the chroot LV and snapshot LV. |
| 380 | chroot_lv: The name of the original chroot LV. |
| 381 | |
| 382 | Returns: |
| 383 | True if the chroot was restored to the requested snapshot, or False if |
| 384 | the snapshot wasn't found or isn't valid. |
| 385 | |
| 386 | Raises: |
| 387 | SystemExit: Any of the LVM commands failed. |
| 388 | """ |
| 389 | valid_snapshots = ListChrootSnapshots(chroot_vg, chroot_lv) |
| 390 | if (snapshot_name in (cros_build_lib.CHROOT_LV_NAME, |
| 391 | cros_build_lib.CHROOT_THINPOOL_NAME) or |
| 392 | snapshot_name not in valid_snapshots): |
| 393 | logging.error('Chroot cannot be restored to %s. Valid snapshots: %s', |
| 394 | snapshot_name, ', '.join(valid_snapshots)) |
| 395 | return False |
| 396 | |
| 397 | backup_chroot_name = 'chroot-bak-%d' % random.randint(0, 1000) |
| 398 | cmd = ['lvrename', chroot_vg, chroot_lv, backup_chroot_name] |
| 399 | try: |
| 400 | cros_build_lib.RunCommand(cmd, print_cmd=False, capture_output=True) |
| 401 | except cros_build_lib.RunCommandError: |
| 402 | raise SystemExit('Running %r failed!' % cmd) |
| 403 | |
| 404 | cmd = ['lvrename', chroot_vg, snapshot_name, chroot_lv] |
| 405 | try: |
| 406 | cros_build_lib.RunCommand(cmd, print_cmd=False, capture_output=True) |
| 407 | except cros_build_lib.RunCommandError: |
| 408 | cmd = ['lvrename', chroot_vg, backup_chroot_name, chroot_lv] |
| 409 | try: |
| 410 | cros_build_lib.RunCommand(cmd, print_cmd=False, capture_output=True) |
| 411 | except cros_build_lib.RunCommandError: |
| 412 | raise SystemExit('Failed to rename %s to chroot and failed to restore ' |
| 413 | '%s back to chroot. Failed command: %r' % |
| 414 | (snapshot_name, backup_chroot_name, cmd)) |
| 415 | raise SystemExit('Failed to rename %s to chroot. Original chroot LV has ' |
| 416 | 'been restored. Failed command: %r' % |
| 417 | (snapshot_name, cmd)) |
| 418 | |
| 419 | # Some versions of LVM set snapshots to be skipped at auto-activate time. |
| 420 | # Other versions don't have this flag at all. We run lvchange to try |
| 421 | # disabling auto-skip and activating the volume, but ignore errors. Versions |
| 422 | # that don't have the flag should be auto-activated. |
| 423 | chroot_lv_path = '%s/%s' % (chroot_vg, chroot_lv) |
| 424 | cmd = ['lvchange', '-kn', chroot_lv_path] |
| 425 | cros_build_lib.RunCommand(cmd, print_cmd=False, capture_output=True, |
| 426 | error_code_ok=True) |
| 427 | |
| 428 | # Activate the LV in case the lvchange above was needed. Activating an LV |
| 429 | # that is already active shouldn't do anything, so this is safe to run even if |
| 430 | # the -kn wasn't needed. |
| 431 | cmd = ['lvchange', '-ay', chroot_lv_path] |
| 432 | cros_build_lib.RunCommand(cmd, print_cmd=False, capture_output=True) |
| 433 | |
| 434 | cmd = ['lvremove', '-f', '%s/%s' % (chroot_vg, backup_chroot_name)] |
| 435 | try: |
| 436 | cros_build_lib.RunCommand(cmd, print_cmd=False, capture_output=True) |
| 437 | except cros_build_lib.RunCommandError: |
| 438 | raise SystemExit('Failed to remove backup LV %s/%s. Failed command: %r' % |
| 439 | (chroot_vg, backup_chroot_name, cmd)) |
| 440 | |
| 441 | return True |
| 442 | |
| 443 | |
| 444 | def ListChrootSnapshots(chroot_vg, chroot_lv): |
| 445 | """Return all snapshots in |chroot_vg| regardless of origin volume. |
| 446 | |
| 447 | Args: |
| 448 | chroot_vg: The name of the VG containing the chroot. |
| 449 | chroot_lv: The name of the chroot LV. |
| 450 | |
| 451 | Returns: |
| 452 | A (possibly-empty) list of snapshot LVs found in |chroot_vg|. |
| 453 | |
| 454 | Raises: |
| 455 | SystemExit: The lvs command failed. |
| 456 | """ |
| 457 | if not chroot_vg or not chroot_lv: |
| 458 | return [] |
| 459 | |
| 460 | cmd = ['lvs', '-o', 'lv_name,pool_lv,lv_attr', '-O', 'lv_name', |
| 461 | '--noheadings', '--separator', '\t', chroot_vg] |
| 462 | try: |
| 463 | result = cros_build_lib.RunCommand(cmd, print_cmd=False, |
| 464 | redirect_stdout=True) |
| 465 | except cros_build_lib.RunCommandError: |
| 466 | raise SystemExit('Running %r failed!' % cmd) |
| 467 | |
| 468 | # Once the thin origin volume has been deleted, there's no way to tell a |
| 469 | # snapshot apart from any other volume. Since this VG is created and managed |
| 470 | # by cros_sdk, we'll assume that all volumes that share the same thin pool are |
| 471 | # valid snapshots. |
| 472 | snapshots = [] |
| 473 | snapshot_attrs = re.compile(r'^V.....t.{2,}') # Matches a thin volume. |
| 474 | for line in result.output.splitlines(): |
| 475 | lv_name, pool_lv, lv_attr = line.lstrip().split('\t') |
| 476 | if (lv_name == chroot_lv or |
| 477 | lv_name == cros_build_lib.CHROOT_THINPOOL_NAME or |
| 478 | pool_lv != cros_build_lib.CHROOT_THINPOOL_NAME or |
| 479 | not snapshot_attrs.match(lv_attr)): |
| 480 | continue |
| 481 | snapshots.append(lv_name) |
| 482 | return snapshots |
| 483 | |
| 484 | |
Benjamin Gordon | 386b9eb | 2017-07-20 09:21:33 -0600 | [diff] [blame] | 485 | def _FindSubmounts(*args): |
| 486 | """Find all mounts matching each of the paths in |args| and any submounts. |
| 487 | |
| 488 | Returns: |
| 489 | A list of all matching mounts in the order found in /proc/mounts. |
| 490 | """ |
| 491 | mounts = [] |
| 492 | paths = [p.rstrip('/') for p in args] |
| 493 | for mtab in osutils.IterateMountPoints(): |
| 494 | for path in paths: |
| 495 | if mtab.destination == path or mtab.destination.startswith(path + '/'): |
| 496 | mounts.append(mtab.destination) |
| 497 | break |
| 498 | |
| 499 | return mounts |
| 500 | |
| 501 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 502 | def _SudoCommand(): |
| 503 | """Get the 'sudo' command, along with all needed environment variables.""" |
| 504 | |
David James | 5a73b4d | 2013-03-07 10:23:40 -0800 | [diff] [blame] | 505 | # Pass in the ENVIRONMENT_WHITELIST and ENV_PASSTHRU variables so that |
| 506 | # scripts in the chroot know what variables to pass through. |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 507 | cmd = ['sudo'] |
David James | 5a73b4d | 2013-03-07 10:23:40 -0800 | [diff] [blame] | 508 | for key in constants.CHROOT_ENVIRONMENT_WHITELIST + constants.ENV_PASSTHRU: |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 509 | value = os.environ.get(key) |
| 510 | if value is not None: |
| 511 | cmd += ['%s=%s' % (key, value)] |
| 512 | |
| 513 | # Pass in the path to the depot_tools so that users can access them from |
| 514 | # within the chroot. |
Mike Frysinger | 08e75f1 | 2014-08-13 01:30:09 -0400 | [diff] [blame] | 515 | cmd += ['DEPOT_TOOLS=%s' % constants.DEPOT_TOOLS_DIR] |
Mike Frysinger | 749251e | 2014-01-29 05:04:27 -0500 | [diff] [blame] | 516 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 517 | return cmd |
| 518 | |
| 519 | |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 520 | def _ReportMissing(missing): |
| 521 | """Report missing utilities, then exit. |
| 522 | |
| 523 | Args: |
| 524 | missing: List of missing utilities, as returned by |
| 525 | osutils.FindMissingBinaries. If non-empty, will not return. |
| 526 | """ |
| 527 | |
| 528 | if missing: |
| 529 | raise SystemExit( |
| 530 | 'The tool(s) %s were not found.\n' |
| 531 | 'Please install the appropriate package in your host.\n' |
| 532 | 'Example(ubuntu):\n' |
| 533 | ' sudo apt-get install <packagename>' |
| 534 | % ', '.join(missing)) |
| 535 | |
| 536 | |
| 537 | def _ProxySimSetup(options): |
| 538 | """Set up proxy simulator, and return only in the child environment. |
| 539 | |
| 540 | TODO: Ideally, this should support multiple concurrent invocations of |
| 541 | cros_sdk --proxy-sim; currently, such invocations will conflict with each |
| 542 | other due to the veth device names and IP addresses. Either this code would |
| 543 | need to generate fresh, unused names for all of these before forking, or it |
| 544 | would need to support multiple concurrent cros_sdk invocations sharing one |
| 545 | proxy and allowing it to exit when unused (without counting on any local |
| 546 | service-management infrastructure on the host). |
| 547 | """ |
| 548 | |
| 549 | may_need_mpm = False |
| 550 | apache_bin = osutils.Which('apache2') |
| 551 | if apache_bin is None: |
| 552 | apache_bin = osutils.Which('apache2', PROXY_APACHE_FALLBACK_PATH) |
| 553 | if apache_bin is None: |
| 554 | _ReportMissing(('apache2',)) |
| 555 | else: |
| 556 | may_need_mpm = True |
| 557 | |
| 558 | # Module names and .so names included for ease of grepping. |
| 559 | apache_modules = [('proxy_module', 'mod_proxy.so'), |
| 560 | ('proxy_connect_module', 'mod_proxy_connect.so'), |
| 561 | ('proxy_http_module', 'mod_proxy_http.so'), |
| 562 | ('proxy_ftp_module', 'mod_proxy_ftp.so')] |
| 563 | |
| 564 | # Find the apache module directory, and make sure it has the modules we need. |
| 565 | module_dirs = {} |
| 566 | for g in PROXY_APACHE_MODULE_GLOBS: |
| 567 | for mod, so in apache_modules: |
| 568 | for f in glob.glob(os.path.join(g, so)): |
| 569 | module_dirs.setdefault(os.path.dirname(f), []).append(so) |
| 570 | for apache_module_path, modules_found in module_dirs.iteritems(): |
| 571 | if len(modules_found) == len(apache_modules): |
| 572 | break |
| 573 | else: |
| 574 | # Appease cros lint, which doesn't understand that this else block will not |
| 575 | # fall through to the subsequent code which relies on apache_module_path. |
| 576 | apache_module_path = None |
| 577 | raise SystemExit( |
| 578 | 'Could not find apache module path containing all required modules: %s' |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 579 | % ', '.join(so for mod, so in apache_modules)) |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 580 | |
| 581 | def check_add_module(name): |
| 582 | so = 'mod_%s.so' % name |
| 583 | if os.access(os.path.join(apache_module_path, so), os.F_OK): |
| 584 | mod = '%s_module' % name |
| 585 | apache_modules.append((mod, so)) |
| 586 | return True |
| 587 | return False |
| 588 | |
| 589 | check_add_module('authz_core') |
| 590 | if may_need_mpm: |
| 591 | for mpm in PROXY_APACHE_MPMS: |
| 592 | if check_add_module('mpm_%s' % mpm): |
| 593 | break |
| 594 | |
| 595 | veth_host = '%s-host' % PROXY_VETH_PREFIX |
| 596 | veth_guest = '%s-guest' % PROXY_VETH_PREFIX |
| 597 | |
Mike Frysinger | 77bf4af | 2016-02-26 17:13:15 -0500 | [diff] [blame] | 598 | # Set up locks to sync the net namespace setup. We need the child to create |
| 599 | # the net ns first, and then have the parent assign the guest end of the veth |
| 600 | # interface to the child's new network namespace & bring up the proxy. Only |
| 601 | # then can the child move forward and rely on the network being up. |
| 602 | ns_create_lock = locking.PipeLock() |
| 603 | ns_setup_lock = locking.PipeLock() |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 604 | |
| 605 | pid = os.fork() |
| 606 | if not pid: |
Mike Frysinger | 77bf4af | 2016-02-26 17:13:15 -0500 | [diff] [blame] | 607 | # Create our new isolated net namespace. |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 608 | namespaces.Unshare(namespaces.CLONE_NEWNET) |
Mike Frysinger | 77bf4af | 2016-02-26 17:13:15 -0500 | [diff] [blame] | 609 | |
| 610 | # Signal the parent the ns is ready to be configured. |
| 611 | ns_create_lock.Post() |
| 612 | del ns_create_lock |
| 613 | |
| 614 | # Wait for the parent to finish setting up the ns/proxy. |
| 615 | ns_setup_lock.Wait() |
| 616 | del ns_setup_lock |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 617 | |
| 618 | # Set up child side of the network. |
| 619 | commands = ( |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 620 | ('ip', 'link', 'set', 'up', 'lo'), |
| 621 | ('ip', 'address', 'add', |
| 622 | '%s/%u' % (PROXY_GUEST_IP, PROXY_NETMASK), |
| 623 | 'dev', veth_guest), |
| 624 | ('ip', 'link', 'set', veth_guest, 'up'), |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 625 | ) |
| 626 | try: |
| 627 | for cmd in commands: |
| 628 | cros_build_lib.RunCommand(cmd, print_cmd=False) |
| 629 | except cros_build_lib.RunCommandError: |
| 630 | raise SystemExit('Running %r failed!' % (cmd,)) |
| 631 | |
| 632 | proxy_url = 'http://%s:%u' % (PROXY_HOST_IP, PROXY_PORT) |
| 633 | for proto in ('http', 'https', 'ftp'): |
| 634 | os.environ[proto + '_proxy'] = proxy_url |
| 635 | for v in ('all_proxy', 'RSYNC_PROXY', 'no_proxy'): |
| 636 | os.environ.pop(v, None) |
| 637 | return |
| 638 | |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 639 | # Set up parent side of the network. |
| 640 | uid = int(os.environ.get('SUDO_UID', '0')) |
| 641 | gid = int(os.environ.get('SUDO_GID', '0')) |
| 642 | if uid == 0 or gid == 0: |
| 643 | for username in PROXY_APACHE_FALLBACK_USERS: |
| 644 | try: |
| 645 | pwnam = pwd.getpwnam(username) |
| 646 | uid, gid = pwnam.pw_uid, pwnam.pw_gid |
| 647 | break |
| 648 | except KeyError: |
| 649 | continue |
| 650 | if uid == 0 or gid == 0: |
| 651 | raise SystemExit('Could not find a non-root user to run Apache as') |
| 652 | |
| 653 | chroot_parent, chroot_base = os.path.split(options.chroot) |
| 654 | pid_file = os.path.join(chroot_parent, '.%s-apache-proxy.pid' % chroot_base) |
| 655 | log_file = os.path.join(chroot_parent, '.%s-apache-proxy.log' % chroot_base) |
| 656 | |
Mike Frysinger | 77bf4af | 2016-02-26 17:13:15 -0500 | [diff] [blame] | 657 | # Wait for the child to create the net ns. |
| 658 | ns_create_lock.Wait() |
| 659 | del ns_create_lock |
| 660 | |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 661 | apache_directives = [ |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 662 | 'User #%u' % uid, |
| 663 | 'Group #%u' % gid, |
| 664 | 'PidFile %s' % pid_file, |
| 665 | 'ErrorLog %s' % log_file, |
| 666 | 'Listen %s:%u' % (PROXY_HOST_IP, PROXY_PORT), |
| 667 | 'ServerName %s' % PROXY_HOST_IP, |
| 668 | 'ProxyRequests On', |
| 669 | 'AllowCONNECT %s' % ' '.join(map(str, PROXY_CONNECT_PORTS)), |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 670 | ] + [ |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 671 | 'LoadModule %s %s' % (mod, os.path.join(apache_module_path, so)) |
| 672 | for (mod, so) in apache_modules |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 673 | ] |
| 674 | commands = ( |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 675 | ('ip', 'link', 'add', 'name', veth_host, |
| 676 | 'type', 'veth', 'peer', 'name', veth_guest), |
| 677 | ('ip', 'address', 'add', |
| 678 | '%s/%u' % (PROXY_HOST_IP, PROXY_NETMASK), |
| 679 | 'dev', veth_host), |
| 680 | ('ip', 'link', 'set', veth_host, 'up'), |
| 681 | ([apache_bin, '-f', '/dev/null'] + |
| 682 | [arg for d in apache_directives for arg in ('-C', d)]), |
| 683 | ('ip', 'link', 'set', veth_guest, 'netns', str(pid)), |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 684 | ) |
| 685 | cmd = None # Make cros lint happy. |
| 686 | try: |
| 687 | for cmd in commands: |
| 688 | cros_build_lib.RunCommand(cmd, print_cmd=False) |
| 689 | except cros_build_lib.RunCommandError: |
| 690 | # Clean up existing interfaces, if any. |
| 691 | cmd_cleanup = ('ip', 'link', 'del', veth_host) |
| 692 | try: |
| 693 | cros_build_lib.RunCommand(cmd_cleanup, print_cmd=False) |
| 694 | except cros_build_lib.RunCommandError: |
Ralph Nathan | 5990042 | 2015-03-24 10:41:17 -0700 | [diff] [blame] | 695 | logging.error('running %r failed', cmd_cleanup) |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 696 | raise SystemExit('Running %r failed!' % (cmd,)) |
Mike Frysinger | 77bf4af | 2016-02-26 17:13:15 -0500 | [diff] [blame] | 697 | |
| 698 | # Signal the child that the net ns/proxy is fully configured now. |
| 699 | ns_setup_lock.Post() |
| 700 | del ns_setup_lock |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 701 | |
Mike Frysinger | e2d8f0d | 2014-11-01 13:09:26 -0400 | [diff] [blame] | 702 | process_util.ExitAsStatus(os.waitpid(pid, 0)[1]) |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 703 | |
| 704 | |
Mike Frysinger | a78a56e | 2012-11-20 06:02:30 -0500 | [diff] [blame] | 705 | def _ReExecuteIfNeeded(argv): |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 706 | """Re-execute cros_sdk as root. |
| 707 | |
| 708 | Also unshare the mount namespace so as to ensure that processes outside |
| 709 | the chroot can't mess with our mounts. |
| 710 | """ |
| 711 | if os.geteuid() != 0: |
Mike Frysinger | a78a56e | 2012-11-20 06:02:30 -0500 | [diff] [blame] | 712 | cmd = _SudoCommand() + ['--'] + argv |
| 713 | os.execvp(cmd[0], cmd) |
Mike Frysinger | a78a56e | 2012-11-20 06:02:30 -0500 | [diff] [blame] | 714 | else: |
Mike Frysinger | 80dfce9 | 2014-04-21 10:58:53 -0400 | [diff] [blame] | 715 | # We must set up the cgroups mounts before we enter our own namespace. |
| 716 | # This way it is a shared resource in the root mount namespace. |
Josh Triplett | e759b23 | 2013-03-08 13:03:43 -0800 | [diff] [blame] | 717 | cgroups.Cgroup.InitSystem() |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 718 | |
| 719 | |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 720 | def _CreateParser(sdk_latest_version, bootstrap_latest_version): |
| 721 | """Generate and return the parser with all the options.""" |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 722 | usage = ('usage: %(prog)s [options] ' |
| 723 | '[VAR1=val1 ... VAR2=val2] [--] [command [args]]') |
| 724 | parser = commandline.ArgumentParser(usage=usage, description=__doc__, |
| 725 | caching=True) |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 726 | |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 727 | # Global options. |
Mike Frysinger | 648ba2d | 2013-01-08 14:19:34 -0500 | [diff] [blame] | 728 | default_chroot = os.path.join(constants.SOURCE_ROOT, |
| 729 | constants.DEFAULT_CHROOT_DIR) |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 730 | parser.add_argument( |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 731 | '--chroot', dest='chroot', default=default_chroot, type='path', |
| 732 | help=('SDK chroot dir name [%s]' % constants.DEFAULT_CHROOT_DIR)) |
Benjamin Gordon | 386b9eb | 2017-07-20 09:21:33 -0600 | [diff] [blame] | 733 | parser.add_argument('--nouse-image', dest='use_image', action='store_false', |
| 734 | default=True, |
| 735 | help='Do not mount the chroot on a loopback image; ' |
| 736 | 'instead, create it directly in a directory.') |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 737 | |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 738 | parser.add_argument('--chrome_root', type='path', |
| 739 | help='Mount this chrome root into the SDK chroot') |
| 740 | parser.add_argument('--chrome_root_mount', type='path', |
| 741 | help='Mount chrome into this path inside SDK chroot') |
| 742 | parser.add_argument('--nousepkg', action='store_true', default=False, |
| 743 | help='Do not use binary packages when creating a chroot.') |
| 744 | parser.add_argument('-u', '--url', dest='sdk_url', |
| 745 | help='Use sdk tarball located at this url. Use file:// ' |
| 746 | 'for local files.') |
| 747 | parser.add_argument('--sdk-version', |
| 748 | help=('Use this sdk version. For prebuilt, current is %r' |
| 749 | ', for bootstrapping it is %r.' |
| 750 | % (sdk_latest_version, bootstrap_latest_version))) |
| 751 | parser.add_argument('--workspace', |
| 752 | help='Workspace directory to mount into the chroot.') |
Hidehiko Abe | b5daf2f | 2017-03-02 17:57:43 +0900 | [diff] [blame] | 753 | parser.add_argument('--goma_dir', type='path', |
| 754 | help='Goma installed directory to mount into the chroot.') |
| 755 | parser.add_argument('--goma_client_json', type='path', |
| 756 | help='Service account json file to use goma on bot. ' |
| 757 | 'Mounted into the chroot.') |
Yong Hong | 84ba917 | 2018-02-07 01:37:42 +0800 | [diff] [blame] | 758 | |
| 759 | # Use type=str instead of type='path' to prevent the given path from being |
| 760 | # transfered to absolute path automatically. |
| 761 | parser.add_argument('--working-dir', type=str, |
| 762 | help='Run the command in specific working directory in ' |
| 763 | 'chroot. If the given directory is a relative ' |
| 764 | 'path, this program will transfer the path to ' |
| 765 | 'the corresponding one inside chroot.') |
| 766 | |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 767 | parser.add_argument('commands', nargs=argparse.REMAINDER) |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 768 | |
Gilad Arnold | 6a8f045 | 2015-06-04 11:25:18 -0700 | [diff] [blame] | 769 | # SDK overlay tarball options (mutually exclusive). |
| 770 | group = parser.add_mutually_exclusive_group() |
| 771 | group.add_argument('--toolchains', |
| 772 | help=('Comma-separated list of toolchains we expect to be ' |
| 773 | 'using on the chroot. Used for downloading a ' |
| 774 | 'corresponding SDK toolchains group (if one is ' |
| 775 | 'found), which may speed up chroot initialization ' |
| 776 | 'when building for the first time. Otherwise this ' |
| 777 | 'has no effect and will not restrict the chroot in ' |
| 778 | 'any way. Ignored if using --bootstrap.')) |
| 779 | group.add_argument('--board', |
| 780 | help=('The board we intend to be building in the chroot. ' |
| 781 | 'Used for deriving the list of required toolchains ' |
| 782 | '(see --toolchains).')) |
| 783 | |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 784 | # Commands. |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 785 | group = parser.add_argument_group('Commands') |
| 786 | group.add_argument( |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 787 | '--enter', action='store_true', default=False, |
| 788 | help='Enter the SDK chroot. Implies --create.') |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 789 | group.add_argument( |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 790 | '--create', action='store_true', default=False, |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 791 | help='Create the chroot only if it does not already exist. ' |
| 792 | 'Implies --download.') |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 793 | group.add_argument( |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 794 | '--bootstrap', action='store_true', default=False, |
| 795 | help='Build everything from scratch, including the sdk. ' |
| 796 | 'Use this only if you need to validate a change ' |
| 797 | 'that affects SDK creation itself (toolchain and ' |
| 798 | 'build are typically the only folk who need this). ' |
| 799 | 'Note this will quite heavily slow down the build. ' |
| 800 | 'This option implies --create --nousepkg.') |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 801 | group.add_argument( |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 802 | '-r', '--replace', action='store_true', default=False, |
| 803 | help='Replace an existing SDK chroot. Basically an alias ' |
| 804 | 'for --delete --create.') |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 805 | group.add_argument( |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 806 | '--delete', action='store_true', default=False, |
| 807 | help='Delete the current SDK chroot if it exists.') |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 808 | group.add_argument( |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 809 | '--download', action='store_true', default=False, |
| 810 | help='Download the sdk.') |
Benjamin Gordon | 2d7bf58 | 2017-07-12 10:11:26 -0600 | [diff] [blame] | 811 | group.add_argument( |
| 812 | '--snapshot-create', metavar='SNAPSHOT_NAME', |
| 813 | help='Create a snapshot of the chroot. Requires that the chroot was ' |
| 814 | 'created without the --nouse-image option.') |
| 815 | group.add_argument( |
| 816 | '--snapshot-restore', metavar='SNAPSHOT_NAME', |
| 817 | help='Restore the chroot to a previously created snapshot.') |
| 818 | group.add_argument( |
| 819 | '--snapshot-delete', metavar='SNAPSHOT_NAME', |
| 820 | help='Delete a previously created snapshot. Deleting a snapshot that ' |
| 821 | 'does not exist is not an error.') |
| 822 | group.add_argument( |
| 823 | '--snapshot-list', action='store_true', default=False, |
| 824 | help='List existing snapshots of the chroot and exit.') |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 825 | commands = group |
| 826 | |
Mike Frysinger | 80dfce9 | 2014-04-21 10:58:53 -0400 | [diff] [blame] | 827 | # Namespace options. |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 828 | group = parser.add_argument_group('Namespaces') |
| 829 | group.add_argument('--proxy-sim', action='store_true', default=False, |
| 830 | help='Simulate a restrictive network requiring an outbound' |
| 831 | ' proxy.') |
| 832 | group.add_argument('--no-ns-pid', dest='ns_pid', |
| 833 | default=True, action='store_false', |
| 834 | help='Do not create a new PID namespace.') |
Mike Frysinger | 80dfce9 | 2014-04-21 10:58:53 -0400 | [diff] [blame] | 835 | |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 836 | # Internal options. |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 837 | group = parser.add_argument_group( |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 838 | 'Internal Chromium OS Build Team Options', |
| 839 | 'Caution: these are for meant for the Chromium OS build team only') |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 840 | group.add_argument('--buildbot-log-version', default=False, |
| 841 | action='store_true', |
| 842 | help='Log SDK version for buildbot consumption') |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 843 | |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 844 | return parser, commands |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 845 | |
| 846 | |
| 847 | def main(argv): |
| 848 | conf = cros_build_lib.LoadKeyValueFile( |
| 849 | os.path.join(constants.SOURCE_ROOT, constants.SDK_VERSION_FILE), |
| 850 | ignore_missing=True) |
| 851 | sdk_latest_version = conf.get('SDK_LATEST_VERSION', '<unknown>') |
| 852 | bootstrap_latest_version = conf.get('BOOTSTRAP_LATEST_VERSION', '<unknown>') |
| 853 | parser, commands = _CreateParser(sdk_latest_version, bootstrap_latest_version) |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 854 | options = parser.parse_args(argv) |
| 855 | chroot_command = options.commands |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 856 | |
| 857 | # Some sanity checks first, before we ask for sudo credentials. |
Mike Frysinger | 8fd67dc | 2012-12-03 23:51:18 -0500 | [diff] [blame] | 858 | cros_build_lib.AssertOutsideChroot() |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 859 | |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 860 | host = os.uname()[4] |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 861 | if host != 'x86_64': |
Benjamin Gordon | 040a116 | 2017-06-29 13:44:47 -0600 | [diff] [blame] | 862 | cros_build_lib.Die( |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 863 | "cros_sdk is currently only supported on x86_64; you're running" |
| 864 | " %s. Please find a x86_64 machine." % (host,)) |
| 865 | |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 866 | _ReportMissing(osutils.FindMissingBinaries(NEEDED_TOOLS)) |
| 867 | if options.proxy_sim: |
| 868 | _ReportMissing(osutils.FindMissingBinaries(PROXY_NEEDED_TOOLS)) |
Benjamin Gordon | abb3e37 | 2017-08-09 10:21:05 -0600 | [diff] [blame] | 869 | missing_image_tools = osutils.FindMissingBinaries(IMAGE_NEEDED_TOOLS) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 870 | |
Benjamin Gordon | 040a116 | 2017-06-29 13:44:47 -0600 | [diff] [blame] | 871 | if (sdk_latest_version == '<unknown>' or |
| 872 | bootstrap_latest_version == '<unknown>'): |
| 873 | cros_build_lib.Die( |
| 874 | 'No SDK version was found. ' |
| 875 | 'Are you in a Chromium source tree instead of Chromium OS?\n\n' |
| 876 | 'Please change to a directory inside your Chromium OS source tree\n' |
| 877 | 'and retry. If you need to setup a Chromium OS source tree, see\n' |
| 878 | ' http://www.chromium.org/chromium-os/developer-guide') |
| 879 | |
Benjamin Gordon | 2d7bf58 | 2017-07-12 10:11:26 -0600 | [diff] [blame] | 880 | any_snapshot_operation = (options.snapshot_create or options.snapshot_restore |
| 881 | or options.snapshot_delete or options.snapshot_list) |
| 882 | if any_snapshot_operation and not options.use_image: |
| 883 | cros_build_lib.Die('Snapshot operations are not compatible with ' |
| 884 | '--nouse-image.') |
| 885 | |
| 886 | if (options.snapshot_delete and options.snapshot_delete == |
| 887 | options.snapshot_restore): |
| 888 | parser.error('Cannot --snapshot_delete the same snapshot you are ' |
| 889 | 'restoring with --snapshot_restore.') |
| 890 | |
David James | 471532c | 2013-01-21 10:23:31 -0800 | [diff] [blame] | 891 | _ReExecuteIfNeeded([sys.argv[0]] + argv) |
| 892 | |
Benjamin Gordon | abb3e37 | 2017-08-09 10:21:05 -0600 | [diff] [blame] | 893 | lock_path = os.path.dirname(options.chroot) |
| 894 | lock_path = os.path.join( |
| 895 | lock_path, '.%s_lock' % os.path.basename(options.chroot).lstrip('.')) |
| 896 | |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 897 | # Expand out the aliases... |
| 898 | if options.replace: |
| 899 | options.delete = options.create = True |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 900 | |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 901 | if options.bootstrap: |
| 902 | options.create = True |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 903 | |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 904 | # If a command is not given, default to enter. |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 905 | # pylint: disable=protected-access |
| 906 | # This _group_actions access sucks, but upstream decided to not include an |
| 907 | # alternative to optparse's option_list, and this is what they recommend. |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 908 | options.enter |= not any(getattr(options, x.dest) |
Mike Frysinger | 2f95cfc | 2015-06-04 04:00:26 -0400 | [diff] [blame] | 909 | for x in commands._group_actions) |
| 910 | # pylint: enable=protected-access |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 911 | options.enter |= bool(chroot_command) |
| 912 | |
Benjamin Gordon | 2d7bf58 | 2017-07-12 10:11:26 -0600 | [diff] [blame] | 913 | if (options.delete and not options.create and |
| 914 | (options.enter or any_snapshot_operation)): |
| 915 | parser.error("Trying to enter or snapshot the chroot when --delete " |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 916 | "was specified makes no sense.") |
| 917 | |
Yong Hong | 84ba917 | 2018-02-07 01:37:42 +0800 | [diff] [blame] | 918 | if options.working_dir is not None and not os.path.isabs(options.working_dir): |
| 919 | options.working_dir = path_util.ToChrootPath(options.working_dir) |
| 920 | |
Benjamin Gordon | abb3e37 | 2017-08-09 10:21:05 -0600 | [diff] [blame] | 921 | # Clean up potential leftovers from previous interrupted builds. |
| 922 | # TODO(bmgordon): Remove this at the end of 2017. That should be long enough |
| 923 | # to get rid of them all. |
| 924 | chroot_build_path = options.chroot + '.build' |
| 925 | if options.use_image and os.path.exists(chroot_build_path): |
| 926 | try: |
| 927 | with cgroups.SimpleContainChildren('cros_sdk'): |
| 928 | with locking.FileLock(lock_path, 'chroot lock') as lock: |
| 929 | logging.notice('Cleaning up leftover build directory %s', |
| 930 | chroot_build_path) |
| 931 | lock.write_lock() |
| 932 | osutils.UmountTree(chroot_build_path) |
| 933 | osutils.RmDir(chroot_build_path) |
| 934 | except cros_build_lib.RunCommandError as e: |
Benjamin Gordon | 2d7bf58 | 2017-07-12 10:11:26 -0600 | [diff] [blame] | 935 | logging.warning('Unable to remove %s: %s', chroot_build_path, e) |
Benjamin Gordon | abb3e37 | 2017-08-09 10:21:05 -0600 | [diff] [blame] | 936 | |
Benjamin Gordon | 35194f1 | 2017-07-19 10:26:22 -0600 | [diff] [blame] | 937 | # Discern if we need to create the chroot. |
Benjamin Gordon | abb3e37 | 2017-08-09 10:21:05 -0600 | [diff] [blame] | 938 | chroot_ver_file = os.path.join(options.chroot, 'etc', 'cros_chroot_version') |
| 939 | chroot_exists = os.path.exists(chroot_ver_file) |
| 940 | if (options.use_image and not chroot_exists and not options.delete and |
| 941 | not missing_image_tools and |
| 942 | os.path.exists(_ImageFileForChroot(options.chroot))): |
| 943 | # Try to re-mount an existing image in case the user has rebooted. |
| 944 | with cgroups.SimpleContainChildren('cros_sdk'): |
| 945 | with locking.FileLock(lock_path, 'chroot lock') as lock: |
| 946 | logging.debug('Checking if existing chroot image can be mounted.') |
| 947 | lock.write_lock() |
| 948 | cros_build_lib.MountChroot(options.chroot, create=False) |
| 949 | chroot_exists = os.path.exists(chroot_ver_file) |
| 950 | if chroot_exists: |
| 951 | logging.notice('Mounted existing image %s on chroot', |
| 952 | _ImageFileForChroot(options.chroot)) |
Benjamin Gordon | 2d7bf58 | 2017-07-12 10:11:26 -0600 | [diff] [blame] | 953 | if (options.create or options.enter or options.snapshot_create or |
| 954 | options.snapshot_restore): |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 955 | # Only create if it's being wiped, or if it doesn't exist. |
| 956 | if not options.delete and chroot_exists: |
| 957 | options.create = False |
| 958 | else: |
| 959 | options.download = True |
| 960 | |
| 961 | # Finally, flip create if necessary. |
Benjamin Gordon | 2d7bf58 | 2017-07-12 10:11:26 -0600 | [diff] [blame] | 962 | if options.enter or options.snapshot_create: |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 963 | options.create |= not chroot_exists |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 964 | |
Benjamin Gordon | 2d7bf58 | 2017-07-12 10:11:26 -0600 | [diff] [blame] | 965 | # Anything that needs to manipulate the main chroot mount or communicate with |
| 966 | # LVM needs to be done here before we enter the new namespaces. |
| 967 | |
| 968 | # If deleting, do it regardless of the use_image flag so that a |
| 969 | # previously-created loopback chroot can also be cleaned up. |
Benjamin Gordon | abb3e37 | 2017-08-09 10:21:05 -0600 | [diff] [blame] | 970 | # TODO(bmgordon): See if the DeleteChroot call below can be removed in |
| 971 | # favor of this block. |
| 972 | chroot_deleted = False |
Benjamin Gordon | 386b9eb | 2017-07-20 09:21:33 -0600 | [diff] [blame] | 973 | if options.delete: |
Benjamin Gordon | abb3e37 | 2017-08-09 10:21:05 -0600 | [diff] [blame] | 974 | with cgroups.SimpleContainChildren('cros_sdk'): |
| 975 | with locking.FileLock(lock_path, 'chroot lock') as lock: |
| 976 | lock.write_lock() |
| 977 | if missing_image_tools: |
| 978 | logging.notice('Unmounting chroot.') |
| 979 | osutils.UmountTree(options.chroot) |
| 980 | else: |
| 981 | logging.notice('Deleting chroot.') |
| 982 | cros_build_lib.CleanupChrootMount(options.chroot, delete_image=True) |
| 983 | osutils.RmDir(options.chroot, ignore_missing=True) |
| 984 | chroot_deleted = True |
| 985 | |
Benjamin Gordon | 2d7bf58 | 2017-07-12 10:11:26 -0600 | [diff] [blame] | 986 | # Make sure the main chroot mount is visible. Contents will be filled in |
| 987 | # below if needed. |
Benjamin Gordon | abb3e37 | 2017-08-09 10:21:05 -0600 | [diff] [blame] | 988 | if options.create and options.use_image: |
| 989 | if missing_image_tools: |
| 990 | raise SystemExit( |
| 991 | '''The tool(s) %s were not found. |
| 992 | Please make sure the lvm2 and thin-provisioning-tools packages |
| 993 | are installed on your host. |
| 994 | Example(ubuntu): |
| 995 | sudo apt-get install lvm2 thin-provisioning-tools |
| 996 | |
| 997 | If you want to run without lvm2, pass --nouse-image (chroot |
| 998 | snapshots will be unavailable).''' % ', '.join(missing_image_tools)) |
Benjamin Gordon | 2d7bf58 | 2017-07-12 10:11:26 -0600 | [diff] [blame] | 999 | |
Benjamin Gordon | abb3e37 | 2017-08-09 10:21:05 -0600 | [diff] [blame] | 1000 | logging.debug('Making sure chroot image is mounted.') |
| 1001 | with cgroups.SimpleContainChildren('cros_sdk'): |
| 1002 | with locking.FileLock(lock_path, 'chroot lock') as lock: |
| 1003 | lock.write_lock() |
| 1004 | if not cros_build_lib.MountChroot(options.chroot, create=True): |
| 1005 | cros_build_lib.Die('Unable to mount %s on chroot', |
| 1006 | _ImageFileForChroot(options.chroot)) |
| 1007 | logging.notice('Mounted %s on chroot', |
| 1008 | _ImageFileForChroot(options.chroot)) |
Benjamin Gordon | 386b9eb | 2017-07-20 09:21:33 -0600 | [diff] [blame] | 1009 | |
Benjamin Gordon | 2d7bf58 | 2017-07-12 10:11:26 -0600 | [diff] [blame] | 1010 | # Snapshot operations will always need the VG/LV, but other actions won't. |
| 1011 | if any_snapshot_operation: |
| 1012 | with cgroups.SimpleContainChildren('cros_sdk'): |
| 1013 | with locking.FileLock(lock_path, 'chroot lock') as lock: |
| 1014 | chroot_vg, chroot_lv = cros_build_lib.FindChrootMountSource( |
| 1015 | options.chroot) |
| 1016 | if not chroot_vg or not chroot_lv: |
| 1017 | cros_build_lib.Die('Unable to find VG/LV for chroot %s', |
| 1018 | options.chroot) |
| 1019 | |
| 1020 | # Delete snapshot before creating a new one. This allows the user to |
| 1021 | # throw out old state, create a new snapshot, and enter the chroot in a |
| 1022 | # single call to cros_sdk. Since restore involves deleting, also do it |
| 1023 | # before creating. |
| 1024 | if options.snapshot_restore: |
| 1025 | lock.write_lock() |
| 1026 | valid_snapshots = ListChrootSnapshots(chroot_vg, chroot_lv) |
| 1027 | if options.snapshot_restore not in valid_snapshots: |
| 1028 | cros_build_lib.Die('%s is not a valid snapshot to restore to. ' |
| 1029 | 'Valid snapshots: %s', options.snapshot_restore, |
| 1030 | ', '.join(valid_snapshots)) |
| 1031 | osutils.UmountTree(options.chroot) |
| 1032 | if not RestoreChrootSnapshot(options.snapshot_restore, chroot_vg, |
| 1033 | chroot_lv): |
| 1034 | cros_build_lib.Die('Unable to restore chroot to snapshot.') |
| 1035 | if not cros_build_lib.MountChroot(options.chroot, create=False): |
| 1036 | cros_build_lib.Die('Unable to mount restored snapshot onto chroot.') |
| 1037 | |
| 1038 | # Use a read lock for snapshot delete and create even though they modify |
| 1039 | # the filesystem, because they don't modify the mounted chroot itself. |
| 1040 | # The underlying LVM commands take their own locks, so conflicting |
| 1041 | # concurrent operations here may crash cros_sdk, but won't corrupt the |
| 1042 | # chroot image. This tradeoff seems worth it to allow snapshot |
| 1043 | # operations on chroots that have a process inside. |
| 1044 | if options.snapshot_delete: |
| 1045 | lock.read_lock() |
| 1046 | DeleteChrootSnapshot(options.snapshot_delete, chroot_vg, chroot_lv) |
| 1047 | |
| 1048 | if options.snapshot_create: |
| 1049 | lock.read_lock() |
| 1050 | if not CreateChrootSnapshot(options.snapshot_create, chroot_vg, |
| 1051 | chroot_lv): |
| 1052 | cros_build_lib.Die('Unable to create snapshot.') |
| 1053 | |
Benjamin Gordon | e3d5bd1 | 2017-11-16 15:42:28 -0700 | [diff] [blame] | 1054 | img_path = _ImageFileForChroot(options.chroot) |
| 1055 | if (options.use_image and os.path.exists(options.chroot) and |
| 1056 | os.path.exists(img_path)): |
| 1057 | img_stat = os.stat(img_path) |
| 1058 | img_used_bytes = img_stat.st_blocks * 512 |
| 1059 | |
| 1060 | mount_stat = os.statvfs(options.chroot) |
| 1061 | mount_used_bytes = mount_stat.f_frsize * (mount_stat.f_blocks - |
| 1062 | mount_stat.f_bfree) |
| 1063 | |
| 1064 | extra_gbs = (img_used_bytes - mount_used_bytes) / 2**30 |
| 1065 | if extra_gbs > MAX_UNUSED_IMAGE_GBS: |
| 1066 | logging.notice('%s is using %s GiB more than needed. Running ' |
| 1067 | 'fstrim.', img_path, extra_gbs) |
| 1068 | cmd = ['fstrim', options.chroot] |
| 1069 | try: |
| 1070 | cros_build_lib.RunCommand(cmd, print_cmd=False) |
| 1071 | except cros_build_lib.RunCommandError as e: |
| 1072 | logging.warning('Running fstrim failed. Consider running fstrim on ' |
| 1073 | 'your chroot manually.\nError: %s', e) |
| 1074 | |
Benjamin Gordon | 2d7bf58 | 2017-07-12 10:11:26 -0600 | [diff] [blame] | 1075 | # Enter a new set of namespaces. Everything after here cannot directly affect |
| 1076 | # the hosts's mounts or alter LVM volumes. |
Benjamin Gordon | 386b9eb | 2017-07-20 09:21:33 -0600 | [diff] [blame] | 1077 | namespaces.SimpleUnshare() |
| 1078 | if options.ns_pid: |
| 1079 | first_pid = namespaces.CreatePidNs() |
| 1080 | else: |
| 1081 | first_pid = None |
| 1082 | |
Benjamin Gordon | 2d7bf58 | 2017-07-12 10:11:26 -0600 | [diff] [blame] | 1083 | if options.snapshot_list: |
| 1084 | for snap in ListChrootSnapshots(chroot_vg, chroot_lv): |
| 1085 | print(snap) |
| 1086 | sys.exit(0) |
| 1087 | |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 1088 | if not options.sdk_version: |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 1089 | sdk_version = (bootstrap_latest_version if options.bootstrap |
| 1090 | else sdk_latest_version) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 1091 | else: |
| 1092 | sdk_version = options.sdk_version |
Mike Frysinger | 34db869 | 2013-11-11 14:54:08 -0500 | [diff] [blame] | 1093 | if options.buildbot_log_version: |
Prathmesh Prabhu | 17f0742 | 2015-07-17 11:40:40 -0700 | [diff] [blame] | 1094 | logging.PrintBuildbotStepText(sdk_version) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 1095 | |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 1096 | # Based on selections, determine the tarball to fetch. |
Yong Hong | 4e29b62 | 2018-02-05 14:31:10 +0800 | [diff] [blame] | 1097 | if options.download: |
| 1098 | if options.sdk_url: |
| 1099 | urls = [options.sdk_url] |
| 1100 | elif options.bootstrap: |
| 1101 | urls = GetStage3Urls(sdk_version) |
| 1102 | else: |
| 1103 | urls = GetArchStageTarballs(sdk_version) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 1104 | |
Gilad Arnold | 6a8f045 | 2015-06-04 11:25:18 -0700 | [diff] [blame] | 1105 | # Get URLs for the toolchains overlay, if one is to be used. |
| 1106 | toolchains_overlay_urls = None |
| 1107 | if not options.bootstrap: |
| 1108 | toolchains = None |
| 1109 | if options.toolchains: |
| 1110 | toolchains = options.toolchains.split(',') |
| 1111 | elif options.board: |
| 1112 | toolchains = toolchain.GetToolchainsForBoard(options.board).keys() |
| 1113 | |
| 1114 | if toolchains: |
| 1115 | toolchains_overlay_urls = GetToolchainsOverlayUrls(sdk_version, |
| 1116 | toolchains) |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 1117 | |
Mike Frysinger | 80dfce9 | 2014-04-21 10:58:53 -0400 | [diff] [blame] | 1118 | with cgroups.SimpleContainChildren('cros_sdk', pid=first_pid): |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 1119 | with locking.FileLock(lock_path, 'chroot lock') as lock: |
Gilad Arnold | 6a8f045 | 2015-06-04 11:25:18 -0700 | [diff] [blame] | 1120 | toolchains_overlay_tarball = None |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 1121 | |
Josh Triplett | 472a418 | 2013-03-08 11:48:57 -0800 | [diff] [blame] | 1122 | if options.proxy_sim: |
| 1123 | _ProxySimSetup(options) |
| 1124 | |
Benjamin Gordon | abb3e37 | 2017-08-09 10:21:05 -0600 | [diff] [blame] | 1125 | if (options.delete and not chroot_deleted and |
| 1126 | (os.path.exists(options.chroot) or |
| 1127 | os.path.exists(_ImageFileForChroot(options.chroot)))): |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 1128 | lock.write_lock() |
| 1129 | DeleteChroot(options.chroot) |
Brian Harring | b938c78 | 2012-02-29 15:14:38 -0800 | [diff] [blame] | 1130 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 1131 | sdk_cache = os.path.join(options.cache_dir, 'sdks') |
| 1132 | distfiles_cache = os.path.join(options.cache_dir, 'distfiles') |
Yu-Ju Hong | 2c06676 | 2013-10-28 14:05:08 -0700 | [diff] [blame] | 1133 | osutils.SafeMakedirsNonRoot(options.cache_dir) |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 1134 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 1135 | for target in (sdk_cache, distfiles_cache): |
Mike Frysinger | 648ba2d | 2013-01-08 14:19:34 -0500 | [diff] [blame] | 1136 | src = os.path.join(constants.SOURCE_ROOT, os.path.basename(target)) |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 1137 | if not os.path.exists(src): |
Prathmesh Prabhu | 06a5056 | 2016-10-22 01:41:44 -0700 | [diff] [blame] | 1138 | osutils.SafeMakedirsNonRoot(target) |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 1139 | continue |
| 1140 | lock.write_lock( |
| 1141 | "Upgrade to %r needed but chroot is locked; please exit " |
| 1142 | "all instances so this upgrade can finish." % src) |
| 1143 | if not os.path.exists(src): |
| 1144 | # Note that while waiting for the write lock, src may've vanished; |
| 1145 | # it's a rare race during the upgrade process that's a byproduct |
| 1146 | # of us avoiding taking a write lock to do the src check. If we |
| 1147 | # took a write lock for that check, it would effectively limit |
| 1148 | # all cros_sdk for a chroot to a single instance. |
Prathmesh Prabhu | 06a5056 | 2016-10-22 01:41:44 -0700 | [diff] [blame] | 1149 | osutils.SafeMakedirsNonRoot(target) |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 1150 | elif not os.path.exists(target): |
| 1151 | # Upgrade occurred, but a reversion, or something whacky |
| 1152 | # occurred writing to the old location. Wipe and continue. |
| 1153 | os.rename(src, target) |
| 1154 | else: |
| 1155 | # Upgrade occurred once already, but either a reversion or |
| 1156 | # some before/after separate cros_sdk usage is at play. |
| 1157 | # Wipe and continue. |
| 1158 | osutils.RmDir(src) |
Brian Harring | ae0a532 | 2012-09-15 01:46:51 -0700 | [diff] [blame] | 1159 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 1160 | if options.download: |
| 1161 | lock.write_lock() |
Gilad Arnold | 6a8f045 | 2015-06-04 11:25:18 -0700 | [diff] [blame] | 1162 | sdk_tarball = FetchRemoteTarballs( |
| 1163 | sdk_cache, urls, 'stage3' if options.bootstrap else 'SDK') |
| 1164 | if toolchains_overlay_urls: |
| 1165 | toolchains_overlay_tarball = FetchRemoteTarballs( |
| 1166 | sdk_cache, toolchains_overlay_urls, 'SDK toolchains overlay', |
| 1167 | allow_none=True) |
Brian Harring | 218e13c | 2012-10-10 16:21:26 -0700 | [diff] [blame] | 1168 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 1169 | if options.create: |
| 1170 | lock.write_lock() |
Gilad Arnold | 6a8f045 | 2015-06-04 11:25:18 -0700 | [diff] [blame] | 1171 | CreateChroot(options.chroot, sdk_tarball, toolchains_overlay_tarball, |
Gilad Arnold | ecc86fa | 2015-05-22 12:06:04 -0700 | [diff] [blame] | 1172 | options.cache_dir, |
Benjamin Gordon | abb3e37 | 2017-08-09 10:21:05 -0600 | [diff] [blame] | 1173 | nousepkg=(options.bootstrap or options.nousepkg)) |
Brian Harring | 1790ac4 | 2012-09-23 08:53:33 -0700 | [diff] [blame] | 1174 | |
David James | 56e6c2c | 2012-10-24 23:54:41 -0700 | [diff] [blame] | 1175 | if options.enter: |
| 1176 | lock.read_lock() |
| 1177 | EnterChroot(options.chroot, options.cache_dir, options.chrome_root, |
Don Garrett | 230d1b2 | 2015-03-09 16:21:19 -0700 | [diff] [blame] | 1178 | options.chrome_root_mount, options.workspace, |
Hidehiko Abe | b5daf2f | 2017-03-02 17:57:43 +0900 | [diff] [blame] | 1179 | options.goma_dir, options.goma_client_json, |
Yong Hong | 84ba917 | 2018-02-07 01:37:42 +0800 | [diff] [blame] | 1180 | options.working_dir, chroot_command) |