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