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