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