blob: 6cb82a0e82e36a5a68a64486c0c6ecf5ac0f5089 [file] [log] [blame]
Mike Frysinger2de7f042012-07-10 04:45:03 -04001# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Brian Harringb938c782012-02-29 15:14:38 -08002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
Manoj Gupta7fad04d2019-06-14 20:12:25 -07004
Mike Frysinger2f95cfc2015-06-04 04:00:26 -04005"""Manage SDK chroots.
6
7This script is used for manipulating local chroot environments; creating,
8deleting, downloading, etc. If given --enter (or no args), it defaults
9to an interactive bash shell within the chroot.
10
11If given args those are passed to the chroot environment, and executed.
12"""
Brian Harringb938c782012-02-29 15:14:38 -080013
Mike Frysinger2f95cfc2015-06-04 04:00:26 -040014import argparse
Josh Triplett472a4182013-03-08 11:48:57 -080015import glob
Chris McDonaldb55b7032021-06-17 16:41:32 -060016import logging
Brian Harringb938c782012-02-29 15:14:38 -080017import os
Mike Frysinger23b5cf52021-06-16 23:18:00 -040018from pathlib import Path
Josh Triplett472a4182013-03-08 11:48:57 -080019import pwd
Benjamin Gordon2d7bf582017-07-12 10:11:26 -060020import random
Brian Norrisd37e2f72016-08-22 16:09:24 -070021import re
Ting-Yuan Huangf56d9af2017-06-19 16:08:32 -070022import resource
Sergey Frolov1cb46ec2020-12-09 21:46:16 -070023import subprocess
David James56e6c2c2012-10-24 23:54:41 -070024import sys
Mike Frysingere852b072021-05-21 12:39:03 -040025import urllib.parse
Brian Harringb938c782012-02-29 15:14:38 -080026
Chris McDonaldb55b7032021-06-17 16:41:32 -060027from chromite.cbuildbot import cbuildbot_alerts
Brian Harringb6cf9142012-09-01 20:43:17 -070028from chromite.lib import commandline
Chris McDonaldb55b7032021-06-17 16:41:32 -060029from chromite.lib import constants
Brian Harringb938c782012-02-29 15:14:38 -080030from chromite.lib import cros_build_lib
Benjamin Gordon74645232018-05-04 17:40:42 -060031from chromite.lib import cros_sdk_lib
Brian Harringb938c782012-02-29 15:14:38 -080032from chromite.lib import locking
Josh Triplette759b232013-03-08 13:03:43 -080033from chromite.lib import namespaces
Brian Harringae0a5322012-09-15 01:46:51 -070034from chromite.lib import osutils
Yong Hong84ba9172018-02-07 01:37:42 +080035from chromite.lib import path_util
Mike Frysingere2d8f0d2014-11-01 13:09:26 -040036from chromite.lib import process_util
David Jamesc93e6a4d2014-01-13 11:37:36 -080037from chromite.lib import retry_util
Michael Mortensenbf296fb2020-06-18 18:21:54 -060038from chromite.lib import timeout_util
Mike Frysinger8e727a32013-01-16 16:57:53 -050039from chromite.lib import toolchain
Mike Frysingere652ba12019-09-08 00:57:43 -040040from chromite.utils import key_value_store
41
Brian Harringb938c782012-02-29 15:14:38 -080042
Mike Frysingerf744d032022-05-07 20:38:39 -040043# Which compression algos the SDK tarball uses. We've used xz since 2012.
44COMPRESSION_PREFERENCE = ('xz',)
Zdenek Behanfd0efe42012-04-13 04:36:40 +020045
Brian Harringb938c782012-02-29 15:14:38 -080046# TODO(zbehan): Remove the dependency on these, reimplement them in python
Manoj Guptab12f7302019-06-03 16:40:14 -070047ENTER_CHROOT = [
48 os.path.join(constants.SOURCE_ROOT, 'src/scripts/sdk_lib/enter_chroot.sh')
49]
Brian Harringb938c782012-02-29 15:14:38 -080050
Josh Triplett472a4182013-03-08 11:48:57 -080051# Proxy simulator configuration.
52PROXY_HOST_IP = '192.168.240.1'
53PROXY_PORT = 8080
54PROXY_GUEST_IP = '192.168.240.2'
55PROXY_NETMASK = 30
56PROXY_VETH_PREFIX = 'veth'
57PROXY_CONNECT_PORTS = (80, 443, 9418)
58PROXY_APACHE_FALLBACK_USERS = ('www-data', 'apache', 'nobody')
59PROXY_APACHE_MPMS = ('event', 'worker', 'prefork')
60PROXY_APACHE_FALLBACK_PATH = ':'.join(
Manoj Guptab12f7302019-06-03 16:40:14 -070061 '/usr/lib/apache2/mpm-%s' % mpm for mpm in PROXY_APACHE_MPMS)
Josh Triplett472a4182013-03-08 11:48:57 -080062PROXY_APACHE_MODULE_GLOBS = ('/usr/lib*/apache2/modules', '/usr/lib*/apache2')
63
Josh Triplett9a495f62013-03-15 18:06:55 -070064# We need these tools to run. Very common tools (tar,..) are omitted.
Josh Triplette759b232013-03-08 13:03:43 -080065NEEDED_TOOLS = ('curl', 'xz')
Brian Harringb938c782012-02-29 15:14:38 -080066
Josh Triplett472a4182013-03-08 11:48:57 -080067# Tools needed for --proxy-sim only.
68PROXY_NEEDED_TOOLS = ('ip',)
Brian Harringb938c782012-02-29 15:14:38 -080069
Benjamin Gordon386b9eb2017-07-20 09:21:33 -060070# Tools needed when use_image is true (the default).
71IMAGE_NEEDED_TOOLS = ('losetup', 'lvchange', 'lvcreate', 'lvs', 'mke2fs',
Benjamin Gordoncfa9c162017-08-03 13:49:29 -060072 'pvscan', 'thin_check', 'vgchange', 'vgcreate', 'vgs')
Benjamin Gordon386b9eb2017-07-20 09:21:33 -060073
Benjamin Gordone3d5bd12017-11-16 15:42:28 -070074# As space is used inside the chroot, the empty space in chroot.img is
75# allocated. Deleting files inside the chroot doesn't automatically return the
76# used space to the OS. Over time, this tends to make the sparse chroot.img
77# less sparse even if the chroot contents don't currently need much space. We
78# can recover most of this unused space with fstrim, but that takes too much
79# time to run it every time. Instead, check the used space against the image
80# size after mounting the chroot and only call fstrim if it looks like we could
81# recover at least this many GiB.
82MAX_UNUSED_IMAGE_GBS = 20
83
Mike Frysingercc838832014-05-24 13:10:30 -040084
Brian Harring1790ac42012-09-23 08:53:33 -070085def GetArchStageTarballs(version):
Brian Harringb938c782012-02-29 15:14:38 -080086 """Returns the URL for a given arch/version"""
Mike Frysingerf744d032022-05-07 20:38:39 -040087 extension = {'xz': 'tar.xz'}
Manoj Guptab12f7302019-06-03 16:40:14 -070088 return [
89 toolchain.GetSdkURL(
90 suburl='cros-sdk-%s.%s' % (version, extension[compressor]))
91 for compressor in COMPRESSION_PREFERENCE
92 ]
Brian Harring1790ac42012-09-23 08:53:33 -070093
94
Mike Frysingerf744d032022-05-07 20:38:39 -040095def FetchRemoteTarballs(storage_dir, urls):
Mike Frysinger34db8692013-11-11 14:54:08 -050096 """Fetches a tarball given by url, and place it in |storage_dir|.
Zdenek Behanfd0efe42012-04-13 04:36:40 +020097
98 Args:
Mike Frysinger34db8692013-11-11 14:54:08 -050099 storage_dir: Path where to save the tarball.
Zdenek Behanfd0efe42012-04-13 04:36:40 +0200100 urls: List of URLs to try to download. Download will stop on first success.
101
102 Returns:
Mike Frysingerdaf57b82019-11-23 17:26:51 -0500103 Full path to the downloaded file.
Gilad Arnoldecc86fa2015-05-22 12:06:04 -0700104
105 Raises:
Mike Frysingerdaf57b82019-11-23 17:26:51 -0500106 ValueError: None of the URLs worked.
Zdenek Behanfd0efe42012-04-13 04:36:40 +0200107 """
Brian Harring1790ac42012-09-23 08:53:33 -0700108 # Note we track content length ourselves since certain versions of curl
109 # fail if asked to resume a complete file.
Brian Harring1790ac42012-09-23 08:53:33 -0700110 # https://sourceforge.net/tracker/?func=detail&atid=100976&aid=3482927&group_id=976
Mike Frysingerdaf57b82019-11-23 17:26:51 -0500111 status_re = re.compile(br'^HTTP/[0-9]+(\.[0-9]+)? 200')
Mike Frysinger27e21b72018-07-12 14:20:21 -0400112 # pylint: disable=undefined-loop-variable
Zdenek Behanfd0efe42012-04-13 04:36:40 +0200113 for url in urls:
Mike Frysingerf744d032022-05-07 20:38:39 -0400114 logging.notice('Downloading tarball %s ...', urls[0].rsplit('/', 1)[-1])
Mike Frysinger3dcacee2019-08-23 17:09:11 -0400115 parsed = urllib.parse.urlparse(url)
Brian Harring1790ac42012-09-23 08:53:33 -0700116 tarball_name = os.path.basename(parsed.path)
117 if parsed.scheme in ('', 'file'):
118 if os.path.exists(parsed.path):
119 return parsed.path
120 continue
121 content_length = 0
Ralph Nathan7070e6a2015-04-02 10:16:43 -0700122 logging.debug('Attempting download from %s', url)
Manoj Guptab12f7302019-06-03 16:40:14 -0700123 result = retry_util.RunCurl(['-I', url],
124 print_cmd=False,
125 debug_level=logging.NOTICE,
126 capture_output=True)
Brian Harring1790ac42012-09-23 08:53:33 -0700127 successful = False
128 for header in result.output.splitlines():
Brian Norrisd37e2f72016-08-22 16:09:24 -0700129 # We must walk the output to find the 200 code for use cases where
Brian Harring1790ac42012-09-23 08:53:33 -0700130 # a proxy is involved and may have pushed down the actual header.
Brian Norrisd37e2f72016-08-22 16:09:24 -0700131 if status_re.match(header):
Brian Harring1790ac42012-09-23 08:53:33 -0700132 successful = True
Mike Frysingerdaf57b82019-11-23 17:26:51 -0500133 elif header.lower().startswith(b'content-length:'):
134 content_length = int(header.split(b':', 1)[-1].strip())
Brian Harring1790ac42012-09-23 08:53:33 -0700135 if successful:
136 break
137 if successful:
Zdenek Behanfd0efe42012-04-13 04:36:40 +0200138 break
139 else:
Gilad Arnoldecc86fa2015-05-22 12:06:04 -0700140 raise ValueError('No valid URLs found!')
Zdenek Behanfd0efe42012-04-13 04:36:40 +0200141
Brian Harringae0a5322012-09-15 01:46:51 -0700142 tarball_dest = os.path.join(storage_dir, tarball_name)
Brian Harring1790ac42012-09-23 08:53:33 -0700143 current_size = 0
144 if os.path.exists(tarball_dest):
145 current_size = os.path.getsize(tarball_dest)
146 if current_size > content_length:
David James56e6c2c2012-10-24 23:54:41 -0700147 osutils.SafeUnlink(tarball_dest)
Brian Harring1790ac42012-09-23 08:53:33 -0700148 current_size = 0
Zdenek Behanb2fa72e2012-03-16 04:49:30 +0100149
Brian Harring1790ac42012-09-23 08:53:33 -0700150 if current_size < content_length:
David Jamesc93e6a4d2014-01-13 11:37:36 -0800151 retry_util.RunCurl(
Hidehiko Abee55af7f2017-05-01 18:38:04 +0900152 ['--fail', '-L', '-y', '30', '-C', '-', '--output', tarball_dest, url],
Manoj Guptab12f7302019-06-03 16:40:14 -0700153 print_cmd=False,
154 debug_level=logging.NOTICE)
Brian Harringb938c782012-02-29 15:14:38 -0800155
Brian Harring1790ac42012-09-23 08:53:33 -0700156 # Cleanup old tarballs now since we've successfull fetched; only cleanup
Gilad Arnoldecc86fa2015-05-22 12:06:04 -0700157 # the tarballs for our prefix, or unknown ones. This gets a bit tricky
158 # because we might have partial overlap between known prefixes.
Mike Frysingerf744d032022-05-07 20:38:39 -0400159 for p in Path(storage_dir).glob('cros-sdk-*'):
160 if p.name == tarball_name:
Brian Harring1790ac42012-09-23 08:53:33 -0700161 continue
Mike Frysingerf744d032022-05-07 20:38:39 -0400162 logging.info('Cleaning up old tarball: %s', p)
163 osutils.SafeUnlink(p)
Zdenek Behan9c644dd2012-04-05 06:24:02 +0200164
Brian Harringb938c782012-02-29 15:14:38 -0800165 return tarball_dest
166
167
Brian Harringae0a5322012-09-15 01:46:51 -0700168def EnterChroot(chroot_path, cache_dir, chrome_root, chrome_root_mount,
Joanna Wang1ec0c812021-11-17 17:41:27 -0800169 goma_dir, goma_client_json, reclient_dir, reproxy_cfg_file,
170 working_dir, additional_args):
Brian Harringb938c782012-02-29 15:14:38 -0800171 """Enters an existing SDK chroot"""
Mike Frysingere5456972013-06-13 00:07:23 -0400172 st = os.statvfs(os.path.join(chroot_path, 'usr', 'bin', 'sudo'))
Alex Klein875b30e2021-01-05 14:56:33 -0700173 if st.f_flag & os.ST_NOSUID:
Mike Frysingere5456972013-06-13 00:07:23 -0400174 cros_build_lib.Die('chroot cannot be in a nosuid mount')
175
Brian Harringae0a5322012-09-15 01:46:51 -0700176 cmd = ENTER_CHROOT + ['--chroot', chroot_path, '--cache_dir', cache_dir]
Brian Harringb938c782012-02-29 15:14:38 -0800177 if chrome_root:
178 cmd.extend(['--chrome_root', chrome_root])
179 if chrome_root_mount:
180 cmd.extend(['--chrome_root_mount', chrome_root_mount])
Hidehiko Abeb5daf2f2017-03-02 17:57:43 +0900181 if goma_dir:
182 cmd.extend(['--goma_dir', goma_dir])
183 if goma_client_json:
184 cmd.extend(['--goma_client_json', goma_client_json])
Joanna Wang1ec0c812021-11-17 17:41:27 -0800185 if reclient_dir:
186 cmd.extend(['--reclient_dir', reclient_dir])
187 if reproxy_cfg_file:
188 cmd.extend(['--reproxy_cfg_file', reproxy_cfg_file])
Yong Hong84ba9172018-02-07 01:37:42 +0800189 if working_dir is not None:
190 cmd.extend(['--working_dir', working_dir])
Don Garrett230d1b22015-03-09 16:21:19 -0700191
Mike Frysinger53ffaae2019-08-27 16:30:27 -0400192 if additional_args:
Brian Harringb938c782012-02-29 15:14:38 -0800193 cmd.append('--')
194 cmd.extend(additional_args)
Brian Harring7199e7d2012-03-23 04:10:08 -0700195
Ting-Yuan Huangf56d9af2017-06-19 16:08:32 -0700196 # ThinLTO opens lots of files at the same time.
Bob Haarman7c9f31b2020-10-12 19:08:51 +0000197 # Set rlimit and vm.max_map_count to accommodate this.
198 file_limit = 262144
199 soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
200 resource.setrlimit(resource.RLIMIT_NOFILE,
201 (max(soft, file_limit), max(hard, file_limit)))
202 max_map_count = int(open('/proc/sys/vm/max_map_count').read())
203 if max_map_count < file_limit:
204 logging.notice(
205 'Raising vm.max_map_count from %s to %s', max_map_count, file_limit)
206 open('/proc/sys/vm/max_map_count', 'w').write(f'{file_limit}\n')
Mike Frysingere1407f62021-10-30 01:56:40 -0400207 return cros_build_lib.dbg_run(cmd, check=False)
Brian Harringb938c782012-02-29 15:14:38 -0800208
209
Benjamin Gordonabb3e372017-08-09 10:21:05 -0600210def _ImageFileForChroot(chroot):
211 """Find the image file that should be associated with |chroot|.
212
213 This function does not check if the image exists; it simply returns the
214 filename that would be used.
215
216 Args:
217 chroot: Path to the chroot.
218
219 Returns:
220 Path to an image file that would be associated with chroot.
221 """
222 return chroot.rstrip('/') + '.img'
223
224
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600225def CreateChrootSnapshot(snapshot_name, chroot_vg, chroot_lv):
226 """Create a snapshot for the specified chroot VG/LV.
227
228 Args:
229 snapshot_name: The name of the new snapshot.
230 chroot_vg: The name of the VG containing the origin LV.
231 chroot_lv: The name of the origin LV.
232
233 Returns:
234 True if the snapshot was created, or False if a snapshot with the same
235 name already exists.
236
237 Raises:
238 SystemExit: The lvcreate command failed.
239 """
240 if snapshot_name in ListChrootSnapshots(chroot_vg, chroot_lv):
Manoj Guptab12f7302019-06-03 16:40:14 -0700241 logging.error(
242 'Cannot create snapshot %s: A volume with that name already '
243 'exists.', snapshot_name)
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600244 return False
245
Manoj Guptab12f7302019-06-03 16:40:14 -0700246 cmd = [
247 'lvcreate', '-s', '--name', snapshot_name,
248 '%s/%s' % (chroot_vg, chroot_lv)
249 ]
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600250 try:
251 logging.notice('Creating snapshot %s from %s in VG %s.', snapshot_name,
252 chroot_lv, chroot_vg)
Mike Frysinger3e8de442020-02-14 16:46:28 -0500253 cros_build_lib.dbg_run(cmd, capture_output=True)
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600254 return True
Mike Frysinger75634e32020-02-22 23:48:12 -0500255 except cros_build_lib.RunCommandError as e:
256 cros_build_lib.Die('Creating snapshot failed!\n%s', e)
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600257
258
259def DeleteChrootSnapshot(snapshot_name, chroot_vg, chroot_lv):
260 """Delete the named snapshot from the specified chroot VG.
261
262 If the requested snapshot is not found, nothing happens. The main chroot LV
263 and internal thinpool LV cannot be deleted with this function.
264
265 Args:
266 snapshot_name: The name of the snapshot to delete.
267 chroot_vg: The name of the VG containing the origin LV.
268 chroot_lv: The name of the origin LV.
269
270 Raises:
271 SystemExit: The lvremove command failed.
272 """
Benjamin Gordon74645232018-05-04 17:40:42 -0600273 if snapshot_name in (cros_sdk_lib.CHROOT_LV_NAME,
274 cros_sdk_lib.CHROOT_THINPOOL_NAME):
Manoj Guptab12f7302019-06-03 16:40:14 -0700275 logging.error(
276 'Cannot remove LV %s as a snapshot. Use cros_sdk --delete '
277 'if you want to remove the whole chroot.', snapshot_name)
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600278 return
279
280 if snapshot_name not in ListChrootSnapshots(chroot_vg, chroot_lv):
281 return
282
283 cmd = ['lvremove', '-f', '%s/%s' % (chroot_vg, snapshot_name)]
284 try:
285 logging.notice('Deleting snapshot %s in VG %s.', snapshot_name, chroot_vg)
Mike Frysinger3e8de442020-02-14 16:46:28 -0500286 cros_build_lib.dbg_run(cmd, capture_output=True)
Mike Frysinger75634e32020-02-22 23:48:12 -0500287 except cros_build_lib.RunCommandError as e:
288 cros_build_lib.Die('Deleting snapshot failed!\n%s', e)
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600289
290
291def RestoreChrootSnapshot(snapshot_name, chroot_vg, chroot_lv):
292 """Restore the chroot to an existing snapshot.
293
294 This is done by renaming the original |chroot_lv| LV to a temporary name,
295 renaming the snapshot named |snapshot_name| to |chroot_lv|, and deleting the
296 now unused LV. If an error occurs, attempts to rename the original snapshot
297 back to |chroot_lv| to leave the chroot unchanged.
298
299 The chroot must be unmounted before calling this function, and will be left
300 unmounted after this function returns.
301
302 Args:
303 snapshot_name: The name of the snapshot to restore. This snapshot will no
304 longer be accessible at its original name after this function finishes.
305 chroot_vg: The VG containing the chroot LV and snapshot LV.
306 chroot_lv: The name of the original chroot LV.
307
308 Returns:
309 True if the chroot was restored to the requested snapshot, or False if
310 the snapshot wasn't found or isn't valid.
311
312 Raises:
313 SystemExit: Any of the LVM commands failed.
314 """
315 valid_snapshots = ListChrootSnapshots(chroot_vg, chroot_lv)
Benjamin Gordon74645232018-05-04 17:40:42 -0600316 if (snapshot_name in (cros_sdk_lib.CHROOT_LV_NAME,
317 cros_sdk_lib.CHROOT_THINPOOL_NAME) or
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600318 snapshot_name not in valid_snapshots):
319 logging.error('Chroot cannot be restored to %s. Valid snapshots: %s',
320 snapshot_name, ', '.join(valid_snapshots))
321 return False
322
323 backup_chroot_name = 'chroot-bak-%d' % random.randint(0, 1000)
324 cmd = ['lvrename', chroot_vg, chroot_lv, backup_chroot_name]
325 try:
Mike Frysinger3e8de442020-02-14 16:46:28 -0500326 cros_build_lib.dbg_run(cmd, capture_output=True)
Mike Frysinger75634e32020-02-22 23:48:12 -0500327 except cros_build_lib.RunCommandError as e:
328 cros_build_lib.Die('Restoring snapshot failed!\n%s', e)
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600329
330 cmd = ['lvrename', chroot_vg, snapshot_name, chroot_lv]
331 try:
Mike Frysinger3e8de442020-02-14 16:46:28 -0500332 cros_build_lib.dbg_run(cmd, capture_output=True)
Mike Frysinger75634e32020-02-22 23:48:12 -0500333 except cros_build_lib.RunCommandError as e:
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600334 cmd = ['lvrename', chroot_vg, backup_chroot_name, chroot_lv]
335 try:
Mike Frysinger3e8de442020-02-14 16:46:28 -0500336 cros_build_lib.dbg_run(cmd, capture_output=True)
Mike Frysinger75634e32020-02-22 23:48:12 -0500337 except cros_build_lib.RunCommandError as e:
338 cros_build_lib.Die(
339 'Failed to rename %s to chroot and failed to restore %s back to '
340 'chroot!\n%s', snapshot_name, backup_chroot_name, e)
341 cros_build_lib.Die(
342 'Failed to rename %s to chroot! Original chroot LV has '
343 'been restored.\n%s', snapshot_name, e)
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600344
345 # Some versions of LVM set snapshots to be skipped at auto-activate time.
346 # Other versions don't have this flag at all. We run lvchange to try
347 # disabling auto-skip and activating the volume, but ignore errors. Versions
348 # that don't have the flag should be auto-activated.
349 chroot_lv_path = '%s/%s' % (chroot_vg, chroot_lv)
350 cmd = ['lvchange', '-kn', chroot_lv_path]
Mike Frysinger45602c72019-09-22 02:15:11 -0400351 cros_build_lib.run(
Mike Frysingerf5a3b2d2019-12-12 14:36:17 -0500352 cmd, print_cmd=False, capture_output=True, check=False)
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600353
354 # Activate the LV in case the lvchange above was needed. Activating an LV
355 # that is already active shouldn't do anything, so this is safe to run even if
356 # the -kn wasn't needed.
357 cmd = ['lvchange', '-ay', chroot_lv_path]
Mike Frysinger3e8de442020-02-14 16:46:28 -0500358 cros_build_lib.dbg_run(cmd, capture_output=True)
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600359
360 cmd = ['lvremove', '-f', '%s/%s' % (chroot_vg, backup_chroot_name)]
361 try:
Mike Frysinger3e8de442020-02-14 16:46:28 -0500362 cros_build_lib.dbg_run(cmd, capture_output=True)
Mike Frysinger75634e32020-02-22 23:48:12 -0500363 except cros_build_lib.RunCommandError as e:
364 cros_build_lib.Die('Failed to remove backup LV %s/%s!\n%s',
365 chroot_vg, backup_chroot_name, e)
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600366
367 return True
368
369
370def ListChrootSnapshots(chroot_vg, chroot_lv):
371 """Return all snapshots in |chroot_vg| regardless of origin volume.
372
373 Args:
374 chroot_vg: The name of the VG containing the chroot.
375 chroot_lv: The name of the chroot LV.
376
377 Returns:
378 A (possibly-empty) list of snapshot LVs found in |chroot_vg|.
379
380 Raises:
381 SystemExit: The lvs command failed.
382 """
383 if not chroot_vg or not chroot_lv:
384 return []
385
Manoj Guptab12f7302019-06-03 16:40:14 -0700386 cmd = [
387 'lvs', '-o', 'lv_name,pool_lv,lv_attr', '-O', 'lv_name', '--noheadings',
388 '--separator', '\t', chroot_vg
389 ]
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600390 try:
Mike Frysinger45602c72019-09-22 02:15:11 -0400391 result = cros_build_lib.run(
Chris McDonaldffdf5aa2020-04-07 16:28:45 -0600392 cmd, print_cmd=False, stdout=True, encoding='utf-8')
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600393 except cros_build_lib.RunCommandError:
394 raise SystemExit('Running %r failed!' % cmd)
395
396 # Once the thin origin volume has been deleted, there's no way to tell a
397 # snapshot apart from any other volume. Since this VG is created and managed
398 # by cros_sdk, we'll assume that all volumes that share the same thin pool are
399 # valid snapshots.
400 snapshots = []
401 snapshot_attrs = re.compile(r'^V.....t.{2,}') # Matches a thin volume.
402 for line in result.output.splitlines():
403 lv_name, pool_lv, lv_attr = line.lstrip().split('\t')
Manoj Guptab12f7302019-06-03 16:40:14 -0700404 if (lv_name == chroot_lv or lv_name == cros_sdk_lib.CHROOT_THINPOOL_NAME or
Benjamin Gordon74645232018-05-04 17:40:42 -0600405 pool_lv != cros_sdk_lib.CHROOT_THINPOOL_NAME or
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600406 not snapshot_attrs.match(lv_attr)):
407 continue
408 snapshots.append(lv_name)
409 return snapshots
410
411
David James56e6c2c2012-10-24 23:54:41 -0700412def _SudoCommand():
413 """Get the 'sudo' command, along with all needed environment variables."""
414
Cindy Lin81093092021-12-09 20:40:57 +0000415 # Pass in the ENVIRONMENT_ALLOWLIST and ENV_PASSTHRU variables so that
Mike Frysinger2bda4d12020-07-14 11:15:49 -0400416 # scripts in the chroot know what variables to pass through.
David James56e6c2c2012-10-24 23:54:41 -0700417 cmd = ['sudo']
Cindy Lin81093092021-12-09 20:40:57 +0000418 for key in constants.CHROOT_ENVIRONMENT_ALLOWLIST + constants.ENV_PASSTHRU:
David James56e6c2c2012-10-24 23:54:41 -0700419 value = os.environ.get(key)
420 if value is not None:
421 cmd += ['%s=%s' % (key, value)]
422
Mike Frysinger2bda4d12020-07-14 11:15:49 -0400423 # We keep PATH not for the chroot but for the re-exec & for programs we might
424 # run before we chroot into the SDK. The process that enters the SDK itself
425 # will take care of initializing PATH to the right value then. But we can't
426 # override the system's default PATH for root as that will hide /sbin.
427 cmd += ['CHROMEOS_SUDO_PATH=%s' % os.environ.get('PATH', '')]
428
David James56e6c2c2012-10-24 23:54:41 -0700429 # Pass in the path to the depot_tools so that users can access them from
430 # within the chroot.
Mike Frysinger08e75f12014-08-13 01:30:09 -0400431 cmd += ['DEPOT_TOOLS=%s' % constants.DEPOT_TOOLS_DIR]
Mike Frysinger749251e2014-01-29 05:04:27 -0500432
David James56e6c2c2012-10-24 23:54:41 -0700433 return cmd
434
435
Josh Triplett472a4182013-03-08 11:48:57 -0800436def _ReportMissing(missing):
437 """Report missing utilities, then exit.
438
439 Args:
440 missing: List of missing utilities, as returned by
441 osutils.FindMissingBinaries. If non-empty, will not return.
442 """
443
444 if missing:
445 raise SystemExit(
446 'The tool(s) %s were not found.\n'
447 'Please install the appropriate package in your host.\n'
448 'Example(ubuntu):\n'
Manoj Guptab12f7302019-06-03 16:40:14 -0700449 ' sudo apt-get install <packagename>' % ', '.join(missing))
Josh Triplett472a4182013-03-08 11:48:57 -0800450
451
452def _ProxySimSetup(options):
453 """Set up proxy simulator, and return only in the child environment.
454
455 TODO: Ideally, this should support multiple concurrent invocations of
456 cros_sdk --proxy-sim; currently, such invocations will conflict with each
457 other due to the veth device names and IP addresses. Either this code would
458 need to generate fresh, unused names for all of these before forking, or it
459 would need to support multiple concurrent cros_sdk invocations sharing one
460 proxy and allowing it to exit when unused (without counting on any local
461 service-management infrastructure on the host).
462 """
463
464 may_need_mpm = False
465 apache_bin = osutils.Which('apache2')
466 if apache_bin is None:
467 apache_bin = osutils.Which('apache2', PROXY_APACHE_FALLBACK_PATH)
468 if apache_bin is None:
469 _ReportMissing(('apache2',))
470 else:
471 may_need_mpm = True
472
473 # Module names and .so names included for ease of grepping.
474 apache_modules = [('proxy_module', 'mod_proxy.so'),
475 ('proxy_connect_module', 'mod_proxy_connect.so'),
476 ('proxy_http_module', 'mod_proxy_http.so'),
477 ('proxy_ftp_module', 'mod_proxy_ftp.so')]
478
479 # Find the apache module directory, and make sure it has the modules we need.
480 module_dirs = {}
481 for g in PROXY_APACHE_MODULE_GLOBS:
Mike Frysinger336f6b02020-05-09 00:03:28 -0400482 for _, so in apache_modules:
Josh Triplett472a4182013-03-08 11:48:57 -0800483 for f in glob.glob(os.path.join(g, so)):
484 module_dirs.setdefault(os.path.dirname(f), []).append(so)
Mike Frysinger0bdbc102019-06-13 15:27:29 -0400485 for apache_module_path, modules_found in module_dirs.items():
Josh Triplett472a4182013-03-08 11:48:57 -0800486 if len(modules_found) == len(apache_modules):
487 break
488 else:
489 # Appease cros lint, which doesn't understand that this else block will not
490 # fall through to the subsequent code which relies on apache_module_path.
491 apache_module_path = None
492 raise SystemExit(
493 'Could not find apache module path containing all required modules: %s'
Mike Frysingerd6e2df02014-11-26 02:55:04 -0500494 % ', '.join(so for mod, so in apache_modules))
Josh Triplett472a4182013-03-08 11:48:57 -0800495
496 def check_add_module(name):
497 so = 'mod_%s.so' % name
498 if os.access(os.path.join(apache_module_path, so), os.F_OK):
499 mod = '%s_module' % name
500 apache_modules.append((mod, so))
501 return True
502 return False
503
504 check_add_module('authz_core')
505 if may_need_mpm:
506 for mpm in PROXY_APACHE_MPMS:
507 if check_add_module('mpm_%s' % mpm):
508 break
509
510 veth_host = '%s-host' % PROXY_VETH_PREFIX
511 veth_guest = '%s-guest' % PROXY_VETH_PREFIX
512
Mike Frysinger77bf4af2016-02-26 17:13:15 -0500513 # Set up locks to sync the net namespace setup. We need the child to create
514 # the net ns first, and then have the parent assign the guest end of the veth
515 # interface to the child's new network namespace & bring up the proxy. Only
516 # then can the child move forward and rely on the network being up.
517 ns_create_lock = locking.PipeLock()
518 ns_setup_lock = locking.PipeLock()
Josh Triplett472a4182013-03-08 11:48:57 -0800519
520 pid = os.fork()
521 if not pid:
Mike Frysinger77bf4af2016-02-26 17:13:15 -0500522 # Create our new isolated net namespace.
Josh Triplett472a4182013-03-08 11:48:57 -0800523 namespaces.Unshare(namespaces.CLONE_NEWNET)
Mike Frysinger77bf4af2016-02-26 17:13:15 -0500524
525 # Signal the parent the ns is ready to be configured.
526 ns_create_lock.Post()
527 del ns_create_lock
528
529 # Wait for the parent to finish setting up the ns/proxy.
530 ns_setup_lock.Wait()
531 del ns_setup_lock
Josh Triplett472a4182013-03-08 11:48:57 -0800532
533 # Set up child side of the network.
534 commands = (
Mike Frysingerd6e2df02014-11-26 02:55:04 -0500535 ('ip', 'link', 'set', 'up', 'lo'),
Manoj Guptab12f7302019-06-03 16:40:14 -0700536 ('ip', 'address', 'add', '%s/%u' % (PROXY_GUEST_IP, PROXY_NETMASK),
Mike Frysingerd6e2df02014-11-26 02:55:04 -0500537 'dev', veth_guest),
538 ('ip', 'link', 'set', veth_guest, 'up'),
Josh Triplett472a4182013-03-08 11:48:57 -0800539 )
540 try:
541 for cmd in commands:
Mike Frysinger3e8de442020-02-14 16:46:28 -0500542 cros_build_lib.dbg_run(cmd)
Mike Frysinger75634e32020-02-22 23:48:12 -0500543 except cros_build_lib.RunCommandError as e:
544 cros_build_lib.Die('Proxy setup failed!\n%s', e)
Josh Triplett472a4182013-03-08 11:48:57 -0800545
546 proxy_url = 'http://%s:%u' % (PROXY_HOST_IP, PROXY_PORT)
547 for proto in ('http', 'https', 'ftp'):
548 os.environ[proto + '_proxy'] = proxy_url
549 for v in ('all_proxy', 'RSYNC_PROXY', 'no_proxy'):
550 os.environ.pop(v, None)
551 return
552
Josh Triplett472a4182013-03-08 11:48:57 -0800553 # Set up parent side of the network.
554 uid = int(os.environ.get('SUDO_UID', '0'))
555 gid = int(os.environ.get('SUDO_GID', '0'))
556 if uid == 0 or gid == 0:
557 for username in PROXY_APACHE_FALLBACK_USERS:
558 try:
559 pwnam = pwd.getpwnam(username)
560 uid, gid = pwnam.pw_uid, pwnam.pw_gid
561 break
562 except KeyError:
563 continue
564 if uid == 0 or gid == 0:
565 raise SystemExit('Could not find a non-root user to run Apache as')
566
567 chroot_parent, chroot_base = os.path.split(options.chroot)
568 pid_file = os.path.join(chroot_parent, '.%s-apache-proxy.pid' % chroot_base)
569 log_file = os.path.join(chroot_parent, '.%s-apache-proxy.log' % chroot_base)
570
Mike Frysinger77bf4af2016-02-26 17:13:15 -0500571 # Wait for the child to create the net ns.
572 ns_create_lock.Wait()
573 del ns_create_lock
574
Josh Triplett472a4182013-03-08 11:48:57 -0800575 apache_directives = [
Mike Frysingerd6e2df02014-11-26 02:55:04 -0500576 'User #%u' % uid,
577 'Group #%u' % gid,
578 'PidFile %s' % pid_file,
579 'ErrorLog %s' % log_file,
580 'Listen %s:%u' % (PROXY_HOST_IP, PROXY_PORT),
581 'ServerName %s' % PROXY_HOST_IP,
582 'ProxyRequests On',
Mike Frysinger66ce4132019-07-17 22:52:52 -0400583 'AllowCONNECT %s' % ' '.join(str(x) for x in PROXY_CONNECT_PORTS),
Josh Triplett472a4182013-03-08 11:48:57 -0800584 ] + [
Mike Frysingerd6e2df02014-11-26 02:55:04 -0500585 'LoadModule %s %s' % (mod, os.path.join(apache_module_path, so))
586 for (mod, so) in apache_modules
Josh Triplett472a4182013-03-08 11:48:57 -0800587 ]
588 commands = (
Manoj Guptab12f7302019-06-03 16:40:14 -0700589 ('ip', 'link', 'add', 'name', veth_host, 'type', 'veth', 'peer', 'name',
590 veth_guest),
591 ('ip', 'address', 'add', '%s/%u' % (PROXY_HOST_IP, PROXY_NETMASK), 'dev',
592 veth_host),
Mike Frysingerd6e2df02014-11-26 02:55:04 -0500593 ('ip', 'link', 'set', veth_host, 'up'),
594 ([apache_bin, '-f', '/dev/null'] +
595 [arg for d in apache_directives for arg in ('-C', d)]),
596 ('ip', 'link', 'set', veth_guest, 'netns', str(pid)),
Josh Triplett472a4182013-03-08 11:48:57 -0800597 )
Manoj Guptab12f7302019-06-03 16:40:14 -0700598 cmd = None # Make cros lint happy.
Josh Triplett472a4182013-03-08 11:48:57 -0800599 try:
600 for cmd in commands:
Mike Frysinger3e8de442020-02-14 16:46:28 -0500601 cros_build_lib.dbg_run(cmd)
Mike Frysinger75634e32020-02-22 23:48:12 -0500602 except cros_build_lib.RunCommandError as e:
Josh Triplett472a4182013-03-08 11:48:57 -0800603 # Clean up existing interfaces, if any.
604 cmd_cleanup = ('ip', 'link', 'del', veth_host)
605 try:
Mike Frysinger45602c72019-09-22 02:15:11 -0400606 cros_build_lib.run(cmd_cleanup, print_cmd=False)
Josh Triplett472a4182013-03-08 11:48:57 -0800607 except cros_build_lib.RunCommandError:
Ralph Nathan59900422015-03-24 10:41:17 -0700608 logging.error('running %r failed', cmd_cleanup)
Mike Frysinger75634e32020-02-22 23:48:12 -0500609 cros_build_lib.Die('Proxy network setup failed!\n%s', e)
Mike Frysinger77bf4af2016-02-26 17:13:15 -0500610
611 # Signal the child that the net ns/proxy is fully configured now.
612 ns_setup_lock.Post()
613 del ns_setup_lock
Josh Triplett472a4182013-03-08 11:48:57 -0800614
Mike Frysingere2d8f0d2014-11-01 13:09:26 -0400615 process_util.ExitAsStatus(os.waitpid(pid, 0)[1])
Josh Triplett472a4182013-03-08 11:48:57 -0800616
617
Mike Frysingera78a56e2012-11-20 06:02:30 -0500618def _ReExecuteIfNeeded(argv):
David James56e6c2c2012-10-24 23:54:41 -0700619 """Re-execute cros_sdk as root.
620
621 Also unshare the mount namespace so as to ensure that processes outside
622 the chroot can't mess with our mounts.
623 """
Ram Chandrasekar69751282022-02-25 21:07:36 +0000624 if osutils.IsNonRootUser():
Mike Frysinger1f113512020-07-29 03:36:57 -0400625 # Make sure to preserve the active Python executable in case the version
626 # we're running as is not the default one found via the (new) $PATH.
627 cmd = _SudoCommand() + ['--'] + [sys.executable] + argv
Mike Frysinger3e8de442020-02-14 16:46:28 -0500628 logging.debug('Reexecing self via sudo:\n%s', cros_build_lib.CmdToStr(cmd))
Mike Frysingera78a56e2012-11-20 06:02:30 -0500629 os.execvp(cmd[0], cmd)
David James56e6c2c2012-10-24 23:54:41 -0700630
631
Mike Frysinger34db8692013-11-11 14:54:08 -0500632def _CreateParser(sdk_latest_version, bootstrap_latest_version):
633 """Generate and return the parser with all the options."""
Mike Frysinger2f95cfc2015-06-04 04:00:26 -0400634 usage = ('usage: %(prog)s [options] '
635 '[VAR1=val1 ... VAR2=val2] [--] [command [args]]')
Manoj Guptab12f7302019-06-03 16:40:14 -0700636 parser = commandline.ArgumentParser(
637 usage=usage, description=__doc__, caching=True)
Brian Harring218e13c2012-10-10 16:21:26 -0700638
Mike Frysinger34db8692013-11-11 14:54:08 -0500639 # Global options.
Mike Frysinger648ba2d2013-01-08 14:19:34 -0500640 default_chroot = os.path.join(constants.SOURCE_ROOT,
641 constants.DEFAULT_CHROOT_DIR)
Mike Frysinger2f95cfc2015-06-04 04:00:26 -0400642 parser.add_argument(
Manoj Guptab12f7302019-06-03 16:40:14 -0700643 '--chroot',
644 dest='chroot',
645 default=default_chroot,
646 type='path',
Brian Harring218e13c2012-10-10 16:21:26 -0700647 help=('SDK chroot dir name [%s]' % constants.DEFAULT_CHROOT_DIR))
Manoj Guptab12f7302019-06-03 16:40:14 -0700648 parser.add_argument(
649 '--nouse-image',
650 dest='use_image',
651 action='store_false',
Benjamin Gordon87b068a2020-11-02 11:22:16 -0700652 default=False,
Manoj Guptab12f7302019-06-03 16:40:14 -0700653 help='Do not mount the chroot on a loopback image; '
654 'instead, create it directly in a directory.')
Benjamin Gordonacbaac22020-09-25 12:59:33 -0600655 parser.add_argument(
656 '--use-image',
657 dest='use_image',
658 action='store_true',
Benjamin Gordon87b068a2020-11-02 11:22:16 -0700659 default=False,
Benjamin Gordonacbaac22020-09-25 12:59:33 -0600660 help='Mount the chroot on a loopback image '
661 'instead of creating it directly in a directory.')
Brian Harringb938c782012-02-29 15:14:38 -0800662
Manoj Guptab12f7302019-06-03 16:40:14 -0700663 parser.add_argument(
Alex Klein5e4b1bc2019-07-02 12:27:06 -0600664 '--chrome-root',
Manoj Guptab12f7302019-06-03 16:40:14 -0700665 '--chrome_root',
666 type='path',
667 help='Mount this chrome root into the SDK chroot')
668 parser.add_argument(
669 '--chrome_root_mount',
670 type='path',
671 help='Mount chrome into this path inside SDK chroot')
672 parser.add_argument(
673 '--nousepkg',
674 action='store_true',
675 default=False,
676 help='Do not use binary packages when creating a chroot.')
677 parser.add_argument(
678 '-u',
679 '--url',
680 dest='sdk_url',
681 help='Use sdk tarball located at this url. Use file:// '
682 'for local files.')
683 parser.add_argument(
Manoj Guptab12f7302019-06-03 16:40:14 -0700684 '--sdk-version',
685 help=('Use this sdk version. For prebuilt, current is %r'
686 ', for bootstrapping it is %r.' % (sdk_latest_version,
687 bootstrap_latest_version)))
688 parser.add_argument(
689 '--goma_dir',
690 type='path',
691 help='Goma installed directory to mount into the chroot.')
692 parser.add_argument(
693 '--goma_client_json',
694 type='path',
695 help='Service account json file to use goma on bot. '
696 'Mounted into the chroot.')
Joanna Wang1ec0c812021-11-17 17:41:27 -0800697 parser.add_argument(
698 '--reclient-dir',
699 type='path',
700 help='Reclient installed directory to mount into the chroot.')
701 parser.add_argument(
702 '--reproxy-cfg-file',
703 type='path',
704 help="Config file for re-client's reproxy used for remoteexec.")
Sergey Frolov6038f612022-06-13 14:07:21 -0600705 parser.add_argument(
706 '--skip-chroot-upgrade',
707 dest='chroot_upgrade',
708 action='store_false',
709 default=True,
710 help='Skip automatic SDK and toolchain upgrade when entering the chroot. '
711 'Never guaranteed to work, especially as ToT moves forward.')
Yong Hong84ba9172018-02-07 01:37:42 +0800712
713 # Use type=str instead of type='path' to prevent the given path from being
714 # transfered to absolute path automatically.
Manoj Guptab12f7302019-06-03 16:40:14 -0700715 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 Hong84ba9172018-02-07 01:37:42 +0800722
Mike Frysinger2f95cfc2015-06-04 04:00:26 -0400723 parser.add_argument('commands', nargs=argparse.REMAINDER)
Mike Frysinger34db8692013-11-11 14:54:08 -0500724
725 # Commands.
Mike Frysinger2f95cfc2015-06-04 04:00:26 -0400726 group = parser.add_argument_group('Commands')
727 group.add_argument(
Manoj Guptab12f7302019-06-03 16:40:14 -0700728 '--enter',
729 action='store_true',
730 default=False,
Mike Frysinger34db8692013-11-11 14:54:08 -0500731 help='Enter the SDK chroot. Implies --create.')
Mike Frysinger2f95cfc2015-06-04 04:00:26 -0400732 group.add_argument(
Manoj Guptab12f7302019-06-03 16:40:14 -0700733 '--create',
734 action='store_true',
735 default=False,
Mike Frysinger34db8692013-11-11 14:54:08 -0500736 help='Create the chroot only if it does not already exist. '
737 'Implies --download.')
Mike Frysinger2f95cfc2015-06-04 04:00:26 -0400738 group.add_argument(
Manoj Guptab12f7302019-06-03 16:40:14 -0700739 '--bootstrap',
740 action='store_true',
741 default=False,
Mike Frysinger34db8692013-11-11 14:54:08 -0500742 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 Frysinger2f95cfc2015-06-04 04:00:26 -0400748 group.add_argument(
Manoj Guptab12f7302019-06-03 16:40:14 -0700749 '-r',
750 '--replace',
751 action='store_true',
752 default=False,
Mike Frysinger34db8692013-11-11 14:54:08 -0500753 help='Replace an existing SDK chroot. Basically an alias '
754 'for --delete --create.')
Mike Frysinger2f95cfc2015-06-04 04:00:26 -0400755 group.add_argument(
Manoj Guptab12f7302019-06-03 16:40:14 -0700756 '--delete',
757 action='store_true',
758 default=False,
Mike Frysinger34db8692013-11-11 14:54:08 -0500759 help='Delete the current SDK chroot if it exists.')
Mike Frysinger2f95cfc2015-06-04 04:00:26 -0400760 group.add_argument(
Michael Mortensene979a4d2020-06-24 13:09:42 -0600761 '--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 Guptab12f7302019-06-03 16:40:14 -0700767 '--unmount',
768 action='store_true',
769 default=False,
Benjamin Gordon64a3f8d2018-06-08 10:34:39 -0600770 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 Guptab12f7302019-06-03 16:40:14 -0700778 '--download',
779 action='store_true',
780 default=False,
Mike Frysinger34db8692013-11-11 14:54:08 -0500781 help='Download the sdk.')
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600782 group.add_argument(
Manoj Guptab12f7302019-06-03 16:40:14 -0700783 '--snapshot-create',
784 metavar='SNAPSHOT_NAME',
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600785 help='Create a snapshot of the chroot. Requires that the chroot was '
Manoj Guptab12f7302019-06-03 16:40:14 -0700786 'created without the --nouse-image option.')
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600787 group.add_argument(
Manoj Guptab12f7302019-06-03 16:40:14 -0700788 '--snapshot-restore',
789 metavar='SNAPSHOT_NAME',
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600790 help='Restore the chroot to a previously created snapshot.')
791 group.add_argument(
Manoj Guptab12f7302019-06-03 16:40:14 -0700792 '--snapshot-delete',
793 metavar='SNAPSHOT_NAME',
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600794 help='Delete a previously created snapshot. Deleting a snapshot that '
Manoj Guptab12f7302019-06-03 16:40:14 -0700795 'does not exist is not an error.')
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600796 group.add_argument(
Manoj Guptab12f7302019-06-03 16:40:14 -0700797 '--snapshot-list',
798 action='store_true',
799 default=False,
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600800 help='List existing snapshots of the chroot and exit.')
Mike Frysinger34db8692013-11-11 14:54:08 -0500801 commands = group
802
Mike Frysinger80dfce92014-04-21 10:58:53 -0400803 # Namespace options.
Mike Frysinger2f95cfc2015-06-04 04:00:26 -0400804 group = parser.add_argument_group('Namespaces')
Manoj Guptab12f7302019-06-03 16:40:14 -0700805 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 Frysinger79024a32021-04-05 03:28:35 -0400811 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 Frysinger80dfce92014-04-21 10:58:53 -0400822
Mike Frysinger34db8692013-11-11 14:54:08 -0500823 # Internal options.
Mike Frysinger2f95cfc2015-06-04 04:00:26 -0400824 group = parser.add_argument_group(
Mike Frysinger34db8692013-11-11 14:54:08 -0500825 'Internal Chromium OS Build Team Options',
826 'Caution: these are for meant for the Chromium OS build team only')
Manoj Guptab12f7302019-06-03 16:40:14 -0700827 group.add_argument(
828 '--buildbot-log-version',
829 default=False,
830 action='store_true',
831 help='Log SDK version for buildbot consumption')
Mike Frysinger34db8692013-11-11 14:54:08 -0500832
Mike Frysinger2f95cfc2015-06-04 04:00:26 -0400833 return parser, commands
Mike Frysinger34db8692013-11-11 14:54:08 -0500834
835
836def main(argv):
Greg Edelston97f4e302020-03-13 14:01:23 -0600837 # Turn on strict sudo checks.
838 cros_build_lib.STRICT_SUDO = True
Mike Frysingere652ba12019-09-08 00:57:43 -0400839 conf = key_value_store.LoadFile(
Mike Frysinger34db8692013-11-11 14:54:08 -0500840 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 Gupta01927c12019-05-13 17:33:14 -0700843 bootstrap_frozen_version = conf.get('BOOTSTRAP_FROZEN_VERSION', '<unknown>')
Manoj Gupta55a63092019-06-13 11:47:13 -0700844
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 Frysinger34db8692013-11-11 14:54:08 -0500850 parser, commands = _CreateParser(sdk_latest_version, bootstrap_latest_version)
Mike Frysinger2f95cfc2015-06-04 04:00:26 -0400851 options = parser.parse_args(argv)
852 chroot_command = options.commands
Brian Harringb938c782012-02-29 15:14:38 -0800853
Sloan Johnsonc59e9262022-06-10 20:51:53 +0000854 # Some basic checks first, before we ask for sudo credentials.
Mike Frysinger8fd67dc2012-12-03 23:51:18 -0500855 cros_build_lib.AssertOutsideChroot()
Brian Harringb938c782012-02-29 15:14:38 -0800856
Brian Harring1790ac42012-09-23 08:53:33 -0700857 host = os.uname()[4]
Brian Harring1790ac42012-09-23 08:53:33 -0700858 if host != 'x86_64':
Benjamin Gordon040a1162017-06-29 13:44:47 -0600859 cros_build_lib.Die(
Brian Harring1790ac42012-09-23 08:53:33 -0700860 "cros_sdk is currently only supported on x86_64; you're running"
Mike Frysinger80de5012019-08-01 14:10:53 -0400861 ' %s. Please find a x86_64 machine.' % (host,))
Brian Harring1790ac42012-09-23 08:53:33 -0700862
Mike Frysinger2bda4d12020-07-14 11:15:49 -0400863 # 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 Triplett472a4182013-03-08 11:48:57 -0800868 _ReportMissing(osutils.FindMissingBinaries(NEEDED_TOOLS))
869 if options.proxy_sim:
870 _ReportMissing(osutils.FindMissingBinaries(PROXY_NEEDED_TOOLS))
Benjamin Gordonabb3e372017-08-09 10:21:05 -0600871 missing_image_tools = osutils.FindMissingBinaries(IMAGE_NEEDED_TOOLS)
Brian Harringb938c782012-02-29 15:14:38 -0800872
Benjamin Gordon040a1162017-06-29 13:44:47 -0600873 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 Frysingerdcad4e02018-08-03 16:20:02 -0400880 ' https://dev.chromium.org/chromium-os/developer-guide')
Benjamin Gordon040a1162017-06-29 13:44:47 -0600881
Manoj Guptab12f7302019-06-03 16:40:14 -0700882 any_snapshot_operation = (
883 options.snapshot_create or options.snapshot_restore or
884 options.snapshot_delete or options.snapshot_list)
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600885
Manoj Guptab12f7302019-06-03 16:40:14 -0700886 if (options.snapshot_delete and
887 options.snapshot_delete == options.snapshot_restore):
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600888 parser.error('Cannot --snapshot_delete the same snapshot you are '
889 'restoring with --snapshot_restore.')
890
David James471532c2013-01-21 10:23:31 -0800891 _ReExecuteIfNeeded([sys.argv[0]] + argv)
892
Benjamin Gordonabb3e372017-08-09 10:21:05 -0600893 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 Harring218e13c2012-10-10 16:21:26 -0700897 # Expand out the aliases...
898 if options.replace:
899 options.delete = options.create = True
Brian Harringb938c782012-02-29 15:14:38 -0800900
Brian Harring218e13c2012-10-10 16:21:26 -0700901 if options.bootstrap:
902 options.create = True
Brian Harringb938c782012-02-29 15:14:38 -0800903
Brian Harring218e13c2012-10-10 16:21:26 -0700904 # If a command is not given, default to enter.
Mike Frysinger2f95cfc2015-06-04 04:00:26 -0400905 # 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 Guptab12f7302019-06-03 16:40:14 -0700908 options.enter |= not any(
909 getattr(options, x.dest) for x in commands._group_actions)
Mike Frysinger2f95cfc2015-06-04 04:00:26 -0400910 # pylint: enable=protected-access
Brian Harring218e13c2012-10-10 16:21:26 -0700911 options.enter |= bool(chroot_command)
912
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600913 if (options.delete and not options.create and
914 (options.enter or any_snapshot_operation)):
Mike Frysinger80de5012019-08-01 14:10:53 -0400915 parser.error('Trying to enter or snapshot the chroot when --delete '
916 'was specified makes no sense.')
Brian Harring218e13c2012-10-10 16:21:26 -0700917
Benjamin Gordon64a3f8d2018-06-08 10:34:39 -0600918 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 Hong84ba9172018-02-07 01:37:42 +0800922 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 Gordon87b068a2020-11-02 11:22:16 -0700925 # 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 Gordon7b44bef2018-06-08 08:13:59 -0600928 chroot_exists = cros_sdk_lib.IsChrootReady(options.chroot)
Benjamin Gordon87b068a2020-11-02 11:22:16 -0700929 img_path = _ImageFileForChroot(options.chroot)
Benjamin Gordon832a4412020-12-08 10:39:16 -0700930 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 Gordon87b068a2020-11-02 11:22:16 -0700943
Benjamin Gordon9bce7032020-11-19 09:58:44 -0700944 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 Gordon87b068a2020-11-02 11:22:16 -0700951 # Discern if we need to create the chroot.
Benjamin Gordonabb3e372017-08-09 10:21:05 -0600952 if (options.use_image and not chroot_exists and not options.delete and
Benjamin Gordon64a3f8d2018-06-08 10:34:39 -0600953 not options.unmount and not missing_image_tools and
Benjamin Gordon87b068a2020-11-02 11:22:16 -0700954 os.path.exists(img_path)):
Benjamin Gordonabb3e372017-08-09 10:21:05 -0600955 # Try to re-mount an existing image in case the user has rebooted.
Mike Frysingerbf47cce2021-01-20 13:46:30 -0500956 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 Harring218e13c2012-10-10 16:21:26 -0700963
964 # Finally, flip create if necessary.
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600965 if options.enter or options.snapshot_create:
Brian Harring218e13c2012-10-10 16:21:26 -0700966 options.create |= not chroot_exists
Brian Harringb938c782012-02-29 15:14:38 -0800967
Benjamin Gordon7b44bef2018-06-08 08:13:59 -0600968 # Make sure we will download if we plan to create.
969 options.download |= options.create
970
Benjamin Gordon2d7bf582017-07-12 10:11:26 -0600971 # 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 Gordon386b9eb2017-07-20 09:21:33 -0600976 if options.delete:
Mike Frysingerbf47cce2021-01-20 13:46:30 -0500977 # 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 Gordonabb3e372017-08-09 10:21:05 -0600986 else:
Mike Frysingerbf47cce2021-01-20 13:46:30 -0500987 logging.warning(
988 'cros_sdk was invoked with force option, continuing.')
Mike Frysingerf6fe6d02021-06-16 20:26:35 -0400989 logging.notice('Deleting chroot.')
990 cros_sdk_lib.CleanupChrootMount(options.chroot, delete=True)
Benjamin Gordonabb3e372017-08-09 10:21:05 -0600991
Benjamin Gordon64a3f8d2018-06-08 10:34:39 -0600992 # 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 Mortensenbf296fb2020-06-18 18:21:54 -0600997 # 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 Mortensen1a176922020-07-14 20:53:35 -06001004 logging.warning(
1005 'Continuing with CleanupChroot(%s), which will umount the tree.',
1006 options.chroot)
Michael Mortensenbf296fb2020-06-18 18:21:54 -06001007 # 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 Frysingerf6fe6d02021-06-16 20:26:35 -04001011 cros_sdk_lib.CleanupChrootMount(options.chroot, delete=False)
Benjamin Gordon64a3f8d2018-06-08 10:34:39 -06001012 sys.exit(0)
1013
Benjamin Gordon2d7bf582017-07-12 10:11:26 -06001014 # Make sure the main chroot mount is visible. Contents will be filled in
1015 # below if needed.
Benjamin Gordonabb3e372017-08-09 10:21:05 -06001016 if options.create and options.use_image:
1017 if missing_image_tools:
Mike Frysinger80de5012019-08-01 14:10:53 -04001018 raise SystemExit("""The tool(s) %s were not found.
Benjamin Gordonabb3e372017-08-09 10:21:05 -06001019Please make sure the lvm2 and thin-provisioning-tools packages
1020are installed on your host.
1021Example(ubuntu):
1022 sudo apt-get install lvm2 thin-provisioning-tools
1023
1024If you want to run without lvm2, pass --nouse-image (chroot
Mike Frysinger80de5012019-08-01 14:10:53 -04001025snapshots will be unavailable).""" % ', '.join(missing_image_tools))
Benjamin Gordon2d7bf582017-07-12 10:11:26 -06001026
Benjamin Gordonabb3e372017-08-09 10:21:05 -06001027 logging.debug('Making sure chroot image is mounted.')
Mike Frysingerbf47cce2021-01-20 13:46:30 -05001028 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 Gordon386b9eb2017-07-20 09:21:33 -06001035
Benjamin Gordon2d7bf582017-07-12 10:11:26 -06001036 # Snapshot operations will always need the VG/LV, but other actions won't.
1037 if any_snapshot_operation:
Mike Frysingerbf47cce2021-01-20 13:46:30 -05001038 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 Gordon2d7bf582017-07-12 10:11:26 -06001042
Mike Frysingerbf47cce2021-01-20 13:46:30 -05001043 # 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 Gordon2d7bf582017-07-12 10:11:26 -06001060
Mike Frysingerbf47cce2021-01-20 13:46:30 -05001061 # 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 Gordon2d7bf582017-07-12 10:11:26 -06001070
Mike Frysingerbf47cce2021-01-20 13:46:30 -05001071 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 Gordon2d7bf582017-07-12 10:11:26 -06001076
Benjamin Gordone3d5bd12017-11-16 15:42:28 -07001077 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 Guptab12f7302019-06-03 16:40:14 -07001084 mount_used_bytes = mount_stat.f_frsize * (
1085 mount_stat.f_blocks - mount_stat.f_bfree)
Benjamin Gordone3d5bd12017-11-16 15:42:28 -07001086
Mike Frysinger93e8ffa2019-07-03 20:24:18 -04001087 extra_gbs = (img_used_bytes - mount_used_bytes) // 2**30
Benjamin Gordone3d5bd12017-11-16 15:42:28 -07001088 if extra_gbs > MAX_UNUSED_IMAGE_GBS:
1089 logging.notice('%s is using %s GiB more than needed. Running '
Sergey Frolov1cb46ec2020-12-09 21:46:16 -07001090 '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 Gordone3d5bd12017-11-16 15:42:28 -07001103
Benjamin Gordon2d7bf582017-07-12 10:11:26 -06001104 # Enter a new set of namespaces. Everything after here cannot directly affect
1105 # the hosts's mounts or alter LVM volumes.
Mike Frysinger79024a32021-04-05 03:28:35 -04001106 namespaces.SimpleUnshare(net=options.ns_net, pid=options.ns_pid)
Benjamin Gordon386b9eb2017-07-20 09:21:33 -06001107
Benjamin Gordon2d7bf582017-07-12 10:11:26 -06001108 if options.snapshot_list:
1109 for snap in ListChrootSnapshots(chroot_vg, chroot_lv):
1110 print(snap)
1111 sys.exit(0)
1112
Brian Harringb938c782012-02-29 15:14:38 -08001113 if not options.sdk_version:
Manoj Guptab12f7302019-06-03 16:40:14 -07001114 sdk_version = (
1115 bootstrap_latest_version if options.bootstrap else sdk_latest_version)
Brian Harringb938c782012-02-29 15:14:38 -08001116 else:
1117 sdk_version = options.sdk_version
Mike Frysinger34db8692013-11-11 14:54:08 -05001118 if options.buildbot_log_version:
Chris McDonaldb55b7032021-06-17 16:41:32 -06001119 cbuildbot_alerts.PrintBuildbotStepText(sdk_version)
Brian Harringb938c782012-02-29 15:14:38 -08001120
Gilad Arnoldecc86fa2015-05-22 12:06:04 -07001121 # Based on selections, determine the tarball to fetch.
Yong Hong4e29b622018-02-05 14:31:10 +08001122 if options.download:
1123 if options.sdk_url:
1124 urls = [options.sdk_url]
Yong Hong4e29b622018-02-05 14:31:10 +08001125 else:
1126 urls = GetArchStageTarballs(sdk_version)
Brian Harring1790ac42012-09-23 08:53:33 -07001127
Mike Frysingerbf47cce2021-01-20 13:46:30 -05001128 with locking.FileLock(lock_path, 'chroot lock') as lock:
1129 if options.proxy_sim:
1130 _ProxySimSetup(options)
Josh Triplett472a4182013-03-08 11:48:57 -08001131
Mike Frysingerbf47cce2021-01-20 13:46:30 -05001132 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 Harringae0a5322012-09-15 01:46:51 -07001135
Mike Frysingerbf47cce2021-01-20 13:46:30 -05001136 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 Harringae0a5322012-09-15 01:46:51 -07001160
Mike Frysingerbf47cce2021-01-20 13:46:30 -05001161 if options.download:
1162 lock.write_lock()
Mike Frysingerf744d032022-05-07 20:38:39 -04001163 sdk_tarball = FetchRemoteTarballs(sdk_cache, urls)
Brian Harring218e13c2012-10-10 16:21:26 -07001164
Mike Frysinger65b7b242021-06-17 21:11:25 -04001165 mounted = False
Mike Frysingerbf47cce2021-01-20 13:46:30 -05001166 if options.create:
1167 lock.write_lock()
1168 # Recheck if the chroot is set up here before creating to make sure we
1169 # account for whatever the various delete/unmount/remount steps above
1170 # have done.
1171 if cros_sdk_lib.IsChrootReady(options.chroot):
1172 logging.debug('Chroot already exists. Skipping creation.')
1173 else:
Mike Frysinger23b5cf52021-06-16 23:18:00 -04001174 cros_sdk_lib.CreateChroot(
1175 Path(options.chroot),
1176 Path(sdk_tarball),
1177 Path(options.cache_dir),
Sergey Frolov6038f612022-06-13 14:07:21 -06001178 usepkg=not options.bootstrap and not options.nousepkg,
1179 chroot_upgrade=options.chroot_upgrade)
Mike Frysinger65b7b242021-06-17 21:11:25 -04001180 mounted = True
Brian Harring1790ac42012-09-23 08:53:33 -07001181
Mike Frysingerbf47cce2021-01-20 13:46:30 -05001182 if options.enter:
1183 lock.read_lock()
Mike Frysinger65b7b242021-06-17 21:11:25 -04001184 if not mounted:
1185 cros_sdk_lib.MountChrootPaths(options.chroot)
Mike Frysingere1407f62021-10-30 01:56:40 -04001186 ret = EnterChroot(options.chroot, options.cache_dir, options.chrome_root,
1187 options.chrome_root_mount, options.goma_dir,
Joanna Wang1ec0c812021-11-17 17:41:27 -08001188 options.goma_client_json, options.reclient_dir,
1189 options.reproxy_cfg_file, options.working_dir,
Mike Frysingere1407f62021-10-30 01:56:40 -04001190 chroot_command)
1191 sys.exit(ret.returncode)