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