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