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