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