Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2021 The ChromiumOS Authors |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -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. |
| 4 | |
| 5 | """Library containing functions to install an image on a Chromium OS device.""" |
| 6 | |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 7 | import abc |
Jae Hoon Kim | 04a0806 | 2023-03-15 06:06:03 +0000 | [diff] [blame] | 8 | import datetime |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 9 | import enum |
Daichi Hirono | c1a8fd3 | 2022-01-07 22:17:51 +0900 | [diff] [blame] | 10 | from io import BytesIO |
Chris McDonald | 14ac61d | 2021-07-21 11:49:56 -0600 | [diff] [blame] | 11 | import logging |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 12 | import os |
| 13 | import re |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 14 | import tempfile |
| 15 | import threading |
Amin Hassani | 5597056 | 2021-02-22 20:49:13 -0800 | [diff] [blame] | 16 | import time |
Daichi Hirono | c1a8fd3 | 2022-01-07 22:17:51 +0900 | [diff] [blame] | 17 | from typing import Dict, List, Tuple, Union |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 18 | |
Amin Hassani | 5597056 | 2021-02-22 20:49:13 -0800 | [diff] [blame] | 19 | from chromite.cli import command |
Amin Hassani | cf8f004 | 2021-03-12 10:42:13 -0800 | [diff] [blame] | 20 | from chromite.cli import flash |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 21 | from chromite.lib import cgpt |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 22 | from chromite.lib import constants |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 23 | from chromite.lib import cros_build_lib |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 24 | from chromite.lib import gs |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 25 | from chromite.lib import image_lib |
Amin Hassani | 5597056 | 2021-02-22 20:49:13 -0800 | [diff] [blame] | 26 | from chromite.lib import operation |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 27 | from chromite.lib import osutils |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 28 | from chromite.lib import parallel |
| 29 | from chromite.lib import remote_access |
| 30 | from chromite.lib import retry_util |
Amin Hassani | 7440308 | 2021-02-22 11:40:09 -0800 | [diff] [blame] | 31 | from chromite.lib import stateful_updater |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 32 | from chromite.lib.paygen import partition_lib |
Amin Hassani | 7440308 | 2021-02-22 11:40:09 -0800 | [diff] [blame] | 33 | from chromite.lib.paygen import paygen_stateful_payload_lib |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 34 | from chromite.lib.xbuddy import devserver_constants |
| 35 | from chromite.lib.xbuddy import xbuddy |
Alex Klein | 18ef121 | 2021-10-14 12:49:02 -0600 | [diff] [blame] | 36 | from chromite.utils import timer |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 37 | |
| 38 | |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 39 | class Error(Exception): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 40 | """Thrown when there is a general Chromium OS-specific flash error.""" |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 41 | |
| 42 | |
| 43 | class ImageType(enum.Enum): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 44 | """Type of the image that is used for flashing the device.""" |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 45 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 46 | # The full image on disk (e.g. chromiumos_test_image.bin). |
| 47 | FULL = 0 |
| 48 | # The remote directory path |
| 49 | # (e.g gs://chromeos-image-archive/eve-release/R90-x.x.x) |
| 50 | REMOTE_DIRECTORY = 1 |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 51 | |
| 52 | |
| 53 | class Partition(enum.Enum): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 54 | """An enum for partition types like kernel and rootfs.""" |
| 55 | |
| 56 | KERNEL = 0 |
| 57 | ROOTFS = 1 |
| 58 | MINIOS = 2 |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 59 | |
| 60 | |
Alex Klein | 074f94f | 2023-06-22 10:32:06 -0600 | [diff] [blame^] | 61 | class DeviceImager: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 62 | """A class to flash a Chromium OS device. |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 63 | |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame] | 64 | This utility uses parallelism as much as possible to achieve its goal as |
| 65 | fast as possible. For example, it uses parallel compressors, parallel |
| 66 | transfers, and simultaneous pipes. |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 67 | """ |
| 68 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 69 | # The parameters of the kernel and rootfs's two main partitions. |
| 70 | A = {Partition.KERNEL: 2, Partition.ROOTFS: 3} |
| 71 | B = {Partition.KERNEL: 4, Partition.ROOTFS: 5} |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 72 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 73 | MINIOS_A = {Partition.MINIOS: 9} |
| 74 | MINIOS_B = {Partition.MINIOS: 10} |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 75 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 76 | def __init__( |
| 77 | self, |
| 78 | device, |
| 79 | image: str, |
| 80 | board: str = None, |
| 81 | version: str = None, |
| 82 | no_rootfs_update: bool = False, |
| 83 | no_stateful_update: bool = False, |
| 84 | no_minios_update: bool = False, |
| 85 | no_reboot: bool = False, |
| 86 | disable_verification: bool = False, |
| 87 | clobber_stateful: bool = False, |
| 88 | clear_tpm_owner: bool = False, |
| 89 | delta: bool = False, |
Jae Hoon Kim | 04a0806 | 2023-03-15 06:06:03 +0000 | [diff] [blame] | 90 | reboot_timeout: datetime.timedelta = None, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 91 | ): |
| 92 | """Initialize DeviceImager for flashing a Chromium OS device. |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 93 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 94 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 95 | device: The ChromiumOSDevice to be updated. |
| 96 | image: The target image path (can be xBuddy path). |
| 97 | board: Board to use. |
| 98 | version: Image version to use. |
| 99 | no_rootfs_update: Whether to do rootfs partition update. |
| 100 | no_stateful_update: Whether to do stateful partition update. |
| 101 | no_minios_update: Whether to do minios partition update. |
| 102 | no_reboot: Whether to reboot device after update, default True. |
| 103 | disable_verification: Whether to disable rootfs verification on the |
| 104 | device. |
| 105 | clobber_stateful: Whether to do a clean stateful partition. |
| 106 | clear_tpm_owner: If true, it will clear the TPM owner on reboot. |
| 107 | delta: Whether to use delta compression when transferring image |
| 108 | bytes. |
Jae Hoon Kim | 04a0806 | 2023-03-15 06:06:03 +0000 | [diff] [blame] | 109 | reboot_timeout: The timeout for reboot. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 110 | """ |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 111 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 112 | self._device = device |
| 113 | self._image = image |
| 114 | self._board = board |
| 115 | self._version = version |
| 116 | self._no_rootfs_update = no_rootfs_update |
| 117 | self._no_stateful_update = no_stateful_update |
| 118 | self._no_minios_update = no_minios_update |
| 119 | self._no_reboot = no_reboot |
| 120 | self._disable_verification = disable_verification |
| 121 | self._clobber_stateful = clobber_stateful |
| 122 | self._clear_tpm_owner = clear_tpm_owner |
Jae Hoon Kim | 04a0806 | 2023-03-15 06:06:03 +0000 | [diff] [blame] | 123 | self._reboot_timeout = reboot_timeout |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 124 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 125 | self._image_type = None |
| 126 | self._inactive_state = None |
| 127 | self._delta = delta |
Daichi Hirono | 28831b3b | 2022-04-07 12:41:11 +0900 | [diff] [blame] | 128 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 129 | def Run(self): |
| 130 | """Update the device with image of specific version.""" |
| 131 | self._LocateImage() |
| 132 | logging.notice( |
| 133 | "Preparing to update the remote device %s with image %s", |
| 134 | self._device.hostname, |
| 135 | self._image, |
| 136 | ) |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 137 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 138 | try: |
| 139 | if command.UseProgressBar(): |
| 140 | op = DeviceImagerOperation() |
| 141 | op.Run(self._Run) |
| 142 | else: |
| 143 | self._Run() |
| 144 | except Exception as e: |
| 145 | raise Error(f"DeviceImager Failed with error: {e}") |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 146 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 147 | # DeviceImagerOperation will look for this log. |
| 148 | logging.info("DeviceImager completed.") |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 149 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 150 | def _Run(self): |
| 151 | """Runs the various operations to install the image on device.""" |
| 152 | # TODO(b/228389041): Switch to delta compression if self._delta is True |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 153 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 154 | self._InstallPartitions() |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 155 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 156 | if self._clear_tpm_owner: |
| 157 | self._device.ClearTpmOwner() |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 158 | |
Daichi Hirono | 1d45ed5 | 2023-01-20 17:30:26 +0900 | [diff] [blame] | 159 | if self._disable_verification: |
| 160 | # DisableRootfsVerification internally invokes Reboot(). |
Jae Hoon Kim | 04a0806 | 2023-03-15 06:06:03 +0000 | [diff] [blame] | 161 | self._device.DisableRootfsVerification( |
| 162 | timeout_sec=self._reboot_timeout.total_seconds() |
| 163 | ) |
Daichi Hirono | 1d45ed5 | 2023-01-20 17:30:26 +0900 | [diff] [blame] | 164 | self._VerifyBootExpectations() |
| 165 | elif not self._no_reboot: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 166 | self._Reboot() |
| 167 | self._VerifyBootExpectations() |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 168 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 169 | def _LocateImage(self): |
| 170 | """Locates the path to the final image(s) that need to be installed. |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 171 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 172 | If the paths is local, the image should be the Chromium OS GPT image |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame] | 173 | (e.g. chromiumos_test_image.bin). If the path is remote, it should be |
| 174 | the remote directory where we can find the quick-provision and stateful |
| 175 | update files (e.g. gs://chromeos-image-archive/eve-release/R90-x.x.x). |
Amin Hassani | cf8f004 | 2021-03-12 10:42:13 -0800 | [diff] [blame] | 176 | |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame] | 177 | NOTE: At this point there is no caching involved. Hence we always |
| 178 | download the partition payloads or extract them from the Chromium OS |
| 179 | image. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 180 | """ |
| 181 | if os.path.isfile(self._image): |
| 182 | self._image_type = ImageType.FULL |
| 183 | return |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 184 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 185 | # TODO(b/172212406): We could potentially also allow this by searching |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame] | 186 | # through the directory to see whether we have quick-provision and |
| 187 | # stateful payloads. This only makes sense when a user has their |
| 188 | # workstation at home and doesn't want to incur the bandwidth cost of |
| 189 | # downloading the same image multiple times. For that, they can simply |
| 190 | # download the GPT image image first and flash that instead. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 191 | if os.path.isdir(self._image): |
| 192 | raise ValueError( |
| 193 | f"{self._image}: input must be a disk image, not a directory." |
| 194 | ) |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 195 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 196 | if gs.PathIsGs(self._image): |
| 197 | # TODO(b/172212406): Check whether it is a directory. If it wasn't a |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame] | 198 | # directory download the image into some temp location and use it |
| 199 | # instead. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 200 | self._image_type = ImageType.REMOTE_DIRECTORY |
| 201 | return |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 202 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 203 | # Assuming it is an xBuddy path. |
| 204 | board = cros_build_lib.GetBoard( |
| 205 | device_board=self._device.board or flash.GetDefaultBoard(), |
| 206 | override_board=self._board, |
| 207 | force=True, |
| 208 | ) |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 209 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 210 | xb = xbuddy.XBuddy(board=board, version=self._version) |
| 211 | build_id, local_file = xb.Translate([self._image]) |
| 212 | if build_id is None: |
| 213 | raise Error(f"{self._image}: unable to find matching xBuddy path.") |
| 214 | logging.info("XBuddy path translated to build ID %s", build_id) |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 215 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 216 | if local_file: |
| 217 | self._image = local_file |
| 218 | self._image_type = ImageType.FULL |
| 219 | return |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 220 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 221 | self._image = f"{devserver_constants.GS_IMAGE_DIR}/{build_id}" |
| 222 | self._image_type = ImageType.REMOTE_DIRECTORY |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 223 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 224 | def _SplitDevPath(self, path: str) -> Tuple[str, int]: |
| 225 | """Splits the given /dev/x path into prefix and the dev number. |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 226 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 227 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 228 | path: The path to a block dev device. |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 229 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 230 | Returns: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 231 | A tuple of representing the prefix and the index of the dev path. |
| 232 | e.g.: '/dev/mmcblk0p1' -> ['/dev/mmcblk0p', 1] |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 233 | """ |
| 234 | match = re.search(r"(.*)([0-9]+)$", path) |
| 235 | if match is None: |
| 236 | raise Error(f"{path}: Could not parse root dev path.") |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 237 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 238 | return match.group(1), int(match.group(2)) |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 239 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 240 | def _GetKernelState(self, root_num: int) -> Tuple[Dict, Dict]: |
| 241 | """Returns the kernel state. |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 242 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 243 | Returns: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 244 | A tuple of two dictionaries: The current active kernel state and the |
| 245 | inactive kernel state. (Look at A and B constants in this class.) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 246 | """ |
| 247 | if root_num == self.A[Partition.ROOTFS]: |
| 248 | return self.A, self.B |
| 249 | elif root_num == self.B[Partition.ROOTFS]: |
| 250 | return self.B, self.A |
| 251 | else: |
| 252 | raise Error(f"Invalid root partition number {root_num}") |
Amin Hassani | d684e98 | 2021-02-26 11:10:58 -0800 | [diff] [blame] | 253 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 254 | def _GetMiniOSState(self, minios_num: int) -> Tuple[Dict, Dict]: |
| 255 | """Returns the miniOS state. |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 256 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 257 | Returns: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 258 | A tuple of dictionaries: The current active miniOS state and the |
| 259 | inactive miniOS state. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 260 | """ |
| 261 | if minios_num == self.MINIOS_A[Partition.MINIOS]: |
| 262 | return self.MINIOS_A, self.MINIOS_B |
| 263 | elif minios_num == self.MINIOS_B[Partition.MINIOS]: |
| 264 | return self.MINIOS_B, self.MINIOS_A |
| 265 | else: |
| 266 | raise Error(f"Invalid minios partition number {minios_num}") |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 267 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 268 | def _InstallPartitions(self): |
| 269 | """The main method that installs the partitions of a Chrome OS device. |
Amin Hassani | 7440308 | 2021-02-22 11:40:09 -0800 | [diff] [blame] | 270 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 271 | It uses parallelism to install the partitions as fast as possible. |
| 272 | """ |
| 273 | prefix, root_num = self._SplitDevPath(self._device.root_dev) |
| 274 | active_state, self._inactive_state = self._GetKernelState(root_num) |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 275 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 276 | updaters = [] |
| 277 | if not self._no_rootfs_update: |
| 278 | current_root = prefix + str(active_state[Partition.ROOTFS]) |
| 279 | target_root = prefix + str(self._inactive_state[Partition.ROOTFS]) |
| 280 | updaters.append( |
| 281 | RootfsUpdater( |
| 282 | current_root, |
| 283 | self._device, |
| 284 | self._image, |
| 285 | self._image_type, |
| 286 | target_root, |
| 287 | ) |
| 288 | ) |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 289 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 290 | target_kernel = prefix + str(self._inactive_state[Partition.KERNEL]) |
| 291 | updaters.append( |
| 292 | KernelUpdater( |
| 293 | self._device, self._image, self._image_type, target_kernel |
| 294 | ) |
| 295 | ) |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 296 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 297 | if not self._no_stateful_update: |
| 298 | updaters.append( |
| 299 | StatefulUpdater( |
| 300 | self._clobber_stateful, |
| 301 | self._device, |
| 302 | self._image, |
| 303 | self._image_type, |
| 304 | None, |
| 305 | ) |
| 306 | ) |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 307 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 308 | if not self._no_minios_update: |
| 309 | minios_priority = self._device.run( |
| 310 | ["crossystem", constants.MINIOS_PRIORITY] |
| 311 | ).stdout |
| 312 | if minios_priority not in ["A", "B"]: |
| 313 | logging.warning( |
| 314 | "Skipping miniOS flash due to missing priority." |
| 315 | ) |
| 316 | else: |
| 317 | # Reference disk_layout_v3 for partition numbering. |
| 318 | _, inactive_minios_state = self._GetMiniOSState( |
| 319 | 9 if minios_priority == "A" else 10 |
| 320 | ) |
| 321 | target_minios = prefix + str( |
| 322 | inactive_minios_state[Partition.MINIOS] |
| 323 | ) |
| 324 | minios_updater = MiniOSUpdater( |
| 325 | self._device, self._image, self._image_type, target_minios |
| 326 | ) |
| 327 | updaters.append(minios_updater) |
Amin Hassani | 92f6c4a | 2021-02-20 17:36:09 -0800 | [diff] [blame] | 328 | |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame] | 329 | # Retry the partitions updates that failed, in case a transient error |
| 330 | # (like SSH drop, etc) caused the error. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 331 | num_retries = 1 |
| 332 | try: |
| 333 | retry_util.RetryException( |
| 334 | Error, |
| 335 | num_retries, |
| 336 | parallel.RunParallelSteps, |
| 337 | (x.Run for x in updaters if not x.IsFinished()), |
| 338 | halt_on_error=True, |
| 339 | ) |
| 340 | except Exception: |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame] | 341 | # If one of the partitions failed to be installed, revert all |
| 342 | # partitions. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 343 | parallel.RunParallelSteps(x.Revert for x in updaters) |
| 344 | raise |
| 345 | |
| 346 | def _Reboot(self): |
| 347 | """Reboots the device.""" |
| 348 | try: |
Jae Hoon Kim | 04a0806 | 2023-03-15 06:06:03 +0000 | [diff] [blame] | 349 | self._device.Reboot( |
| 350 | timeout_sec=self._reboot_timeout.total_seconds() |
| 351 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 352 | except remote_access.RebootError: |
| 353 | raise Error( |
| 354 | "Could not recover from reboot. Once example reason" |
| 355 | " could be the image provided was a non-test image" |
| 356 | " or the system failed to boot after the update." |
| 357 | ) |
| 358 | except Exception as e: |
| 359 | raise Error(f"Failed to reboot to the device with error: {e}") |
| 360 | |
| 361 | def _VerifyBootExpectations(self): |
| 362 | """Verify that we fully booted into the expected kernel state.""" |
| 363 | # Discover the newly active kernel. |
| 364 | _, root_num = self._SplitDevPath(self._device.root_dev) |
| 365 | active_state, _ = self._GetKernelState(root_num) |
| 366 | |
| 367 | # If this happens, we should rollback. |
| 368 | if active_state != self._inactive_state: |
| 369 | raise Error("The expected kernel state after update is invalid.") |
| 370 | |
| 371 | logging.info("Verified boot expectations.") |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 372 | |
| 373 | |
| 374 | class ReaderBase(threading.Thread): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 375 | """The base class for reading different inputs and writing into output. |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 376 | |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame] | 377 | This class extends threading.Thread, so it will be run on its own thread. |
| 378 | Also it can be used as a context manager. Internally, it opens necessary |
| 379 | files for writing to and reading from. This class cannot be instantiated, it |
| 380 | needs to be sub-classed first to provide necessary function implementations. |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 381 | """ |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 382 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 383 | def __init__(self, use_named_pipes: bool = False): |
| 384 | """Initializes the class. |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 385 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 386 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 387 | use_named_pipes: Whether to use a named pipe or anonymous file |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 388 | descriptors. |
| 389 | """ |
| 390 | super().__init__() |
| 391 | self._use_named_pipes = use_named_pipes |
| 392 | self._pipe_target = None |
| 393 | self._pipe_source = None |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 394 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 395 | def __del__(self): |
| 396 | """Destructor. |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 397 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 398 | Make sure to clean up any named pipes we might have created. |
| 399 | """ |
| 400 | if self._use_named_pipes: |
| 401 | osutils.SafeUnlink(self._pipe_target) |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 402 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 403 | def __enter__(self): |
| 404 | """Enters the context manager""" |
| 405 | if self._use_named_pipes: |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame] | 406 | # There is no need for the temp file, we only need its path. So the |
| 407 | # named pipe is created after this temp file is deleted. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 408 | with tempfile.NamedTemporaryFile( |
| 409 | prefix="chromite-device-imager" |
| 410 | ) as fp: |
| 411 | self._pipe_target = self._pipe_source = fp.name |
| 412 | os.mkfifo(self._pipe_target) |
| 413 | else: |
| 414 | self._pipe_target, self._pipe_source = os.pipe() |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 415 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 416 | self.start() |
| 417 | return self |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 418 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 419 | def __exit__(self, *args, **kwargs): |
| 420 | """Exits the context manager.""" |
| 421 | self.join() |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 422 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 423 | def _Source(self): |
| 424 | """Returns the source pipe to write data into. |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 425 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 426 | Sub-classes can use this function to determine where to write their data |
| 427 | into. |
| 428 | """ |
| 429 | return self._pipe_source |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 430 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 431 | def _CloseSource(self): |
| 432 | """Closes the source pipe. |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 433 | |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame] | 434 | Sub-classes should use this function to close the pipe after they are |
| 435 | done writing into it. Failure to do so may result reader of the data to |
| 436 | hang indefinitely. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 437 | """ |
| 438 | if not self._use_named_pipes: |
| 439 | os.close(self._pipe_source) |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 440 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 441 | def Target(self): |
| 442 | """Returns the target pipe to read data from. |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 443 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 444 | Users of this class can use this path to read data from. |
| 445 | """ |
| 446 | return self._pipe_target |
| 447 | |
| 448 | def CloseTarget(self): |
| 449 | """Closes the target pipe. |
| 450 | |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame] | 451 | Users of this class should use this function to close the pipe after |
| 452 | they are done reading from it. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 453 | """ |
| 454 | if self._use_named_pipes: |
| 455 | os.remove(self._pipe_target) |
| 456 | else: |
| 457 | os.close(self._pipe_target) |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 458 | |
| 459 | |
| 460 | class PartialFileReader(ReaderBase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 461 | """A class to read specific offset and length from a file and compress it. |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 462 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 463 | This class can be used to read from specific location and length in a file |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame] | 464 | (e.g. A partition in a GPT image). Then it compresses the input and writes |
| 465 | it out (to a pipe). Look at the base class for more information. |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 466 | """ |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 467 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 468 | # The offset of different partitions in a Chromium OS image does not always |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame] | 469 | # align to larger values like 4096. It seems that 512 is the maximum value |
| 470 | # to be divisible by partition offsets. This size should not be increased |
| 471 | # just for 'performance reasons'. Since we are doing everything in parallel, |
| 472 | # in practice there is not much difference between this and larger block |
| 473 | # sizes as parallelism hides the possible extra latency provided by smaller |
| 474 | # block sizes. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 475 | _BLOCK_SIZE = 512 |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 476 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 477 | def __init__( |
| 478 | self, |
| 479 | image: str, |
| 480 | offset: int, |
| 481 | length: int, |
| 482 | compression_command: List[str], |
| 483 | ): |
| 484 | """Initializes the class. |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 485 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 486 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 487 | image: The path to an image (local or remote directory). |
| 488 | offset: The offset (in bytes) to read from the image. |
| 489 | length: The length (in bytes) to read from the image. |
| 490 | compression_command: The command to compress transferred bytes. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 491 | """ |
| 492 | super().__init__() |
| 493 | |
| 494 | self._image = image |
| 495 | self._offset = offset |
| 496 | self._length = length |
| 497 | self._compression_command = compression_command |
| 498 | |
| 499 | def run(self): |
| 500 | """Runs the reading and compression.""" |
Mike Frysinger | 906119e | 2022-12-27 18:10:23 -0500 | [diff] [blame] | 501 | data = osutils.ReadFile( |
| 502 | self._image, mode="rb", size=self._length, seek=self._offset |
| 503 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 504 | try: |
Mike Frysinger | 906119e | 2022-12-27 18:10:23 -0500 | [diff] [blame] | 505 | cros_build_lib.run( |
| 506 | self._compression_command, input=data, stdout=self._Source() |
| 507 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 508 | finally: |
| 509 | self._CloseSource() |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 510 | |
| 511 | |
Amin Hassani | 0fe49ae | 2021-02-21 23:41:58 -0800 | [diff] [blame] | 512 | class GsFileCopier(ReaderBase): |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame] | 513 | """A class to download gzip compressed file from GS bucket into a pipe.""" |
Amin Hassani | 0fe49ae | 2021-02-21 23:41:58 -0800 | [diff] [blame] | 514 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 515 | def __init__(self, image: str): |
| 516 | """Initializes the class. |
Amin Hassani | 0fe49ae | 2021-02-21 23:41:58 -0800 | [diff] [blame] | 517 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 518 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 519 | image: The path to an image (local or remote directory). |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 520 | """ |
| 521 | super().__init__(use_named_pipes=True) |
| 522 | self._image = image |
Amin Hassani | 0fe49ae | 2021-02-21 23:41:58 -0800 | [diff] [blame] | 523 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 524 | def run(self): |
| 525 | """Runs the download and write into the output pipe.""" |
| 526 | try: |
| 527 | gs.GSContext().Copy(self._image, self._Source()) |
| 528 | finally: |
| 529 | self._CloseSource() |
Amin Hassani | 0fe49ae | 2021-02-21 23:41:58 -0800 | [diff] [blame] | 530 | |
| 531 | |
Alex Klein | 074f94f | 2023-06-22 10:32:06 -0600 | [diff] [blame^] | 532 | class PartitionUpdaterBase: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 533 | """A base abstract class to use for installing an image into a partition. |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 534 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 535 | Sub-classes should implement the abstract methods to provide the core |
| 536 | functionality. |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 537 | """ |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 538 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 539 | def __init__(self, device, image: str, image_type, target: str): |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame] | 540 | """Initializes this base class with the most commonly needed values. |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 541 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 542 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 543 | device: The ChromiumOSDevice to be updated. |
| 544 | image: The target image path for the partition update. |
| 545 | image_type: The type of the image (ImageType). |
| 546 | target: The target path (e.g. block dev) to install the update. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 547 | """ |
| 548 | self._device = device |
| 549 | self._image = image |
| 550 | self._image_type = image_type |
| 551 | self._target = target |
| 552 | self._finished = False |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 553 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 554 | def Run(self): |
| 555 | """The main function that does the partition update job.""" |
| 556 | with timer.Timer() as t: |
| 557 | try: |
| 558 | self._Run() |
| 559 | finally: |
| 560 | self._finished = True |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 561 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 562 | logging.debug("Completed %s in %s", self.__class__.__name__, t) |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 563 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 564 | @abc.abstractmethod |
| 565 | def _Run(self): |
| 566 | """The method that need to be implemented by sub-classes.""" |
| 567 | raise NotImplementedError("Sub-classes need to implement this.") |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 568 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 569 | def IsFinished(self): |
| 570 | """Returns whether the partition update has been successful.""" |
| 571 | return self._finished |
| 572 | |
| 573 | @abc.abstractmethod |
| 574 | def Revert(self): |
| 575 | """Reverts the partition update. |
| 576 | |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame] | 577 | Subclasses need to implement this function to provide revert capability. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 578 | """ |
| 579 | raise NotImplementedError("Sub-classes need to implement this.") |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 580 | |
| 581 | |
| 582 | class RawPartitionUpdater(PartitionUpdaterBase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 583 | """A class to update a raw partition on a Chromium OS device.""" |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 584 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 585 | def _Run(self): |
| 586 | """The function that does the job of kernel partition update.""" |
| 587 | if self._image_type == ImageType.FULL: |
| 588 | self._CopyPartitionFromImage(self._GetPartitionName()) |
| 589 | elif self._image_type == ImageType.REMOTE_DIRECTORY: |
| 590 | self._RedirectPartition(self._GetRemotePartitionName()) |
| 591 | else: |
| 592 | raise ValueError(f"Invalid image type {self._image_type}") |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 593 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 594 | def _GetPartitionName(self): |
| 595 | """Returns the name of the partition in a Chromium OS GPT layout. |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 596 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 597 | Subclasses should override this function to return correct name. |
| 598 | """ |
| 599 | raise NotImplementedError("Subclasses need to implement this.") |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 600 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 601 | def _CopyPartitionFromImage(self, part_name: str): |
| 602 | """Updates the device's partition from a local Chromium OS image. |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 603 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 604 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 605 | part_name: The name of the partition in the source image that needs |
| 606 | to be extracted. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 607 | """ |
| 608 | offset, length = self._GetPartLocation(part_name) |
| 609 | offset, length = self._OptimizePartLocation(offset, length) |
| 610 | compressor, decompressor = self._GetCompressionAndDecompression() |
Daichi Hirono | c1a8fd3 | 2022-01-07 22:17:51 +0900 | [diff] [blame] | 611 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 612 | with PartialFileReader( |
| 613 | self._image, offset, length, compressor |
| 614 | ) as generator: |
| 615 | try: |
| 616 | self._WriteToTarget(generator.Target(), decompressor) |
| 617 | finally: |
| 618 | generator.CloseTarget() |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 619 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 620 | def _GetCompressionAndDecompression(self) -> Tuple[List[str], List[str]]: |
| 621 | """Returns compression / decompression commands.""" |
Daichi Hirono | c1a8fd3 | 2022-01-07 22:17:51 +0900 | [diff] [blame] | 622 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 623 | return ( |
Mike Frysinger | 6630601 | 2022-04-22 15:23:13 -0400 | [diff] [blame] | 624 | [ |
| 625 | cros_build_lib.FindCompressor( |
| 626 | cros_build_lib.CompressionType.GZIP |
| 627 | ) |
| 628 | ], |
| 629 | self._device.GetDecompressor(cros_build_lib.CompressionType.GZIP), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 630 | ) |
Daichi Hirono | c1a8fd3 | 2022-01-07 22:17:51 +0900 | [diff] [blame] | 631 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 632 | def _WriteToTarget( |
| 633 | self, source: Union[int, BytesIO], decompress_command: List[str] |
| 634 | ) -> None: |
| 635 | """Writes bytes source to the target device on DUT. |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 636 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 637 | Returns: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 638 | A string command to run on a device to read data from stdin, |
| 639 | uncompress it and write it to the target partition. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 640 | """ |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 641 | cmd = " ".join( |
| 642 | [ |
| 643 | *decompress_command, |
| 644 | "|", |
| 645 | "dd", |
| 646 | "bs=1M", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 647 | f"of={self._target}", |
| 648 | ] |
| 649 | ) |
| 650 | self._device.run(cmd, input=source, shell=True) |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 651 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 652 | def _GetPartLocation(self, part_name: str): |
| 653 | """Extracts the location and size of the raw partition from the image. |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 654 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 655 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 656 | part_name: The name of the partition in the source image that needs |
| 657 | to be extracted. |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 658 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 659 | Returns: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 660 | A tuple of offset and length (in bytes) from the image. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 661 | """ |
| 662 | try: |
| 663 | parts = image_lib.GetImageDiskPartitionInfo(self._image) |
| 664 | part_info = [p for p in parts if p.name == part_name][0] |
| 665 | except IndexError: |
| 666 | raise Error(f"No partition named {part_name} found.") |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 667 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 668 | return int(part_info.start), int(part_info.size) |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 669 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 670 | def _GetRemotePartitionName(self): |
| 671 | """Returns the name of the quick-provision partition file. |
Amin Hassani | 0fe49ae | 2021-02-21 23:41:58 -0800 | [diff] [blame] | 672 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 673 | Subclasses should override this function to return correct name. |
| 674 | """ |
| 675 | raise NotImplementedError("Subclasses need to implement this.") |
Amin Hassani | 0fe49ae | 2021-02-21 23:41:58 -0800 | [diff] [blame] | 676 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 677 | def _OptimizePartLocation(self, offset: int, length: int): |
| 678 | """Optimizes the offset and length of the partition. |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 679 | |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame] | 680 | Subclasses can override this to provide better offset/length than what |
| 681 | is defined in the PGT partition layout. |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 682 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 683 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 684 | offset: The offset (in bytes) of the partition in the image. |
| 685 | length: The length (in bytes) of the partition. |
Amin Hassani | d4b3ff8 | 2021-02-20 23:05:14 -0800 | [diff] [blame] | 686 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 687 | Returns: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 688 | A tuple of offset and length (in bytes) from the image. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 689 | """ |
| 690 | return offset, length |
Amin Hassani | d684e98 | 2021-02-26 11:10:58 -0800 | [diff] [blame] | 691 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 692 | def _RedirectPartition(self, file_name: str): |
| 693 | """Downloads the partition from a remote path and writes it into target. |
Amin Hassani | 0fe49ae | 2021-02-21 23:41:58 -0800 | [diff] [blame] | 694 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 695 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 696 | file_name: The file name in the remote directory self._image. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 697 | """ |
| 698 | image_path = os.path.join(self._image, file_name) |
| 699 | with GsFileCopier(image_path) as generator: |
| 700 | try: |
| 701 | with open(generator.Target(), "rb") as fp: |
| 702 | # Always use GZIP as remote quick provision images are gzip |
| 703 | # compressed only. |
| 704 | self._WriteToTarget( |
| 705 | fp, |
Mike Frysinger | 6630601 | 2022-04-22 15:23:13 -0400 | [diff] [blame] | 706 | self._device.GetDecompressor( |
| 707 | cros_build_lib.CompressionType.GZIP |
| 708 | ), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 709 | ) |
| 710 | finally: |
| 711 | generator.CloseTarget() |
Amin Hassani | 0fe49ae | 2021-02-21 23:41:58 -0800 | [diff] [blame] | 712 | |
Amin Hassani | d684e98 | 2021-02-26 11:10:58 -0800 | [diff] [blame] | 713 | |
| 714 | class KernelUpdater(RawPartitionUpdater): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 715 | """A class to update the kernel partition on a Chromium OS device.""" |
Amin Hassani | d684e98 | 2021-02-26 11:10:58 -0800 | [diff] [blame] | 716 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 717 | def _GetPartitionName(self): |
| 718 | """See RawPartitionUpdater._GetPartitionName().""" |
| 719 | return constants.PART_KERN_B |
Amin Hassani | d684e98 | 2021-02-26 11:10:58 -0800 | [diff] [blame] | 720 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 721 | def _GetRemotePartitionName(self): |
| 722 | """See RawPartitionUpdater._GetRemotePartitionName().""" |
| 723 | return constants.QUICK_PROVISION_PAYLOAD_KERNEL |
Amin Hassani | 0fe49ae | 2021-02-21 23:41:58 -0800 | [diff] [blame] | 724 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 725 | def Revert(self): |
| 726 | """Reverts the kernel partition update.""" |
| 727 | # There is nothing to do for reverting kernel partition. |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 728 | |
| 729 | |
| 730 | class RootfsUpdater(RawPartitionUpdater): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 731 | """A class to update the root partition on a Chromium OS device.""" |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 732 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 733 | def __init__(self, current_root: str, *args): |
| 734 | """Initializes the class. |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 735 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 736 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 737 | current_root: The current root device path. |
| 738 | *args: See PartitionUpdaterBase |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 739 | """ |
| 740 | super().__init__(*args) |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 741 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 742 | self._current_root = current_root |
| 743 | self._ran_postinst = False |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 744 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 745 | def _GetPartitionName(self): |
| 746 | """See RawPartitionUpdater._GetPartitionName().""" |
| 747 | return constants.PART_ROOT_A |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 748 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 749 | def _GetRemotePartitionName(self): |
| 750 | """See RawPartitionUpdater._GetRemotePartitionName().""" |
| 751 | return constants.QUICK_PROVISION_PAYLOAD_ROOTFS |
Amin Hassani | 0fe49ae | 2021-02-21 23:41:58 -0800 | [diff] [blame] | 752 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 753 | def _Run(self): |
| 754 | """The function that does the job of rootfs partition update.""" |
| 755 | with ProgressWatcher(self._device, self._target): |
| 756 | super()._Run() |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 757 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 758 | self._RunPostInst() |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 759 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 760 | def _OptimizePartLocation(self, offset: int, length: int): |
| 761 | """Optimizes the size of the root partition of the image. |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 762 | |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame] | 763 | Normally the file system does not occupy the entire partition. |
| 764 | Furthermore we don't need the verity hash tree at the end of the root |
| 765 | file system because postinst will recreate it. This function reads the |
| 766 | (approximate) superblock of the ext4 partition and extracts the actual |
| 767 | file system size in the root partition. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 768 | """ |
| 769 | superblock_size = 4096 * 2 |
| 770 | with open(self._image, "rb") as r: |
| 771 | r.seek(offset) |
| 772 | with tempfile.NamedTemporaryFile(delete=False) as fp: |
| 773 | fp.write(r.read(superblock_size)) |
| 774 | fp.close() |
| 775 | return offset, partition_lib.Ext2FileSystemSize(fp.name) |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 776 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 777 | def _RunPostInst(self, on_target: bool = True): |
| 778 | """Runs the postinst process in the root partition. |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 779 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 780 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 781 | on_target: If true the postinst is run on the target (inactive) |
| 782 | partition. This is used when doing normal updates. If false, the |
| 783 | postinst is run on the current (active) partition. This is used |
| 784 | when reverting an update. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 785 | """ |
| 786 | try: |
| 787 | postinst_dir = "/" |
| 788 | partition = self._current_root |
| 789 | if on_target: |
| 790 | postinst_dir = self._device.run( |
| 791 | ["mktemp", "-d", "-p", self._device.work_dir], |
| 792 | capture_output=True, |
| 793 | ).stdout.strip() |
| 794 | self._device.run( |
| 795 | ["mount", "-o", "ro", self._target, postinst_dir] |
| 796 | ) |
| 797 | partition = self._target |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 798 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 799 | self._ran_postinst = True |
| 800 | postinst = os.path.join(postinst_dir, "postinst") |
| 801 | result = self._device.run( |
| 802 | [postinst, partition], capture_output=True |
| 803 | ) |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 804 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 805 | logging.debug( |
| 806 | "Postinst result on %s: \n%s", postinst, result.stdout |
| 807 | ) |
| 808 | # DeviceImagerOperation will look for this log. |
| 809 | logging.info("Postinstall completed.") |
| 810 | finally: |
| 811 | if on_target: |
| 812 | self._device.run(["umount", postinst_dir]) |
Amin Hassani | 75c5f94 | 2021-02-20 23:56:53 -0800 | [diff] [blame] | 813 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 814 | def Revert(self): |
| 815 | """Reverts the root update install.""" |
| 816 | logging.info("Reverting the rootfs partition update.") |
| 817 | if self._ran_postinst: |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame] | 818 | # We don't have to do anything for revert if we haven't changed the |
| 819 | # kernel priorities yet. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 820 | self._RunPostInst(on_target=False) |
Amin Hassani | 7440308 | 2021-02-22 11:40:09 -0800 | [diff] [blame] | 821 | |
| 822 | |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 823 | class MiniOSUpdater(RawPartitionUpdater): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 824 | """A class to update the miniOS partition on a Chromium OS device.""" |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 825 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 826 | def __init__(self, *args): |
| 827 | """Initializes the class. |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 828 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 829 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 830 | *args: See PartitionUpdaterBase |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 831 | """ |
| 832 | super().__init__(*args) |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 833 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 834 | self._ran_postinst = False |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 835 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 836 | def _GetPartitionName(self): |
| 837 | """See RawPartitionUpdater._GetPartitionName().""" |
| 838 | return constants.PART_MINIOS_A |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 839 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 840 | def _GetRemotePartitionName(self): |
| 841 | """See RawPartitionUpdater._GetRemotePartitionName().""" |
| 842 | return constants.QUICK_PROVISION_PAYLOAD_MINIOS |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 843 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 844 | def _Run(self): |
| 845 | """The function that does the job of rootfs partition update.""" |
| 846 | if self._image_type == ImageType.FULL: |
| 847 | if self._MiniOSPartitionsExistInImage(): |
| 848 | logging.info("Updating miniOS partition from local.") |
| 849 | super()._Run() |
| 850 | else: |
| 851 | logging.warning( |
| 852 | "Not updating miniOS partition as it does not exist." |
| 853 | ) |
| 854 | return |
| 855 | elif self._image_type == ImageType.REMOTE_DIRECTORY: |
| 856 | if not gs.GSContext().Exists( |
| 857 | os.path.join( |
| 858 | self._image, constants.QUICK_PROVISION_PAYLOAD_MINIOS |
| 859 | ) |
| 860 | ): |
| 861 | logging.warning("Not updating miniOS, missing remote files.") |
| 862 | return |
| 863 | elif not self._MiniOSPartitionsExist(): |
| 864 | logging.warning("Not updating miniOS, missing partitions.") |
| 865 | return |
| 866 | else: |
| 867 | logging.info("Updating miniOS partition from remote.") |
| 868 | super()._Run() |
| 869 | else: |
| 870 | # Let super() handle this error. |
| 871 | super()._Run() |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 872 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 873 | self._RunPostInstall() |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 874 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 875 | def _RunPostInstall(self): |
| 876 | """The function will change the priority of the miniOS partitions.""" |
| 877 | self._FlipMiniOSPriority() |
| 878 | self._ran_postinst = True |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 879 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 880 | def Revert(self): |
| 881 | """Reverts the miniOS partition update.""" |
| 882 | if self._ran_postinst: |
| 883 | self._FlipMiniOSPriority() |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 884 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 885 | def _GetMiniOSPriority(self): |
| 886 | return self._device.run( |
| 887 | ["crossystem", constants.MINIOS_PRIORITY] |
| 888 | ).stdout |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 889 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 890 | def _SetMiniOSPriority(self, priority: str): |
| 891 | self._device.run( |
| 892 | ["crossystem", f"{constants.MINIOS_PRIORITY}={priority}"] |
| 893 | ) |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 894 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 895 | def _FlipMiniOSPriority(self): |
| 896 | inactive_minios_priority = ( |
| 897 | "B" if self._GetMiniOSPriority() == "A" else "A" |
| 898 | ) |
| 899 | logging.info("Setting miniOS priority to %s", inactive_minios_priority) |
| 900 | self._SetMiniOSPriority(inactive_minios_priority) |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 901 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 902 | def _MiniOSPartitionsExistInImage(self): |
| 903 | """Checks if miniOS partition exists in the image.""" |
| 904 | d = cgpt.Disk.FromImage(self._image) |
| 905 | try: |
| 906 | d.GetPartitionByTypeGuid(cgpt.MINIOS_TYPE_GUID) |
| 907 | return True |
| 908 | except KeyError: |
| 909 | return False |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 910 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 911 | def _MiniOSPartitionsExist(self): |
| 912 | """Checks if the device has miniOS partitions.""" |
| 913 | run = lambda x: self._device.run(x).stdout.strip() |
| 914 | device_drive = run(["rootdev", "-s", "-d"]) |
| 915 | cmd = ["cgpt", "show", "-t", device_drive, "-i"] |
| 916 | return all( |
| 917 | (run(cmd + [p]) == cgpt.MINIOS_TYPE_GUID) for p in ("9", "10") |
| 918 | ) |
Jae Hoon Kim | b88b796 | 2021-10-18 11:08:38 -0700 | [diff] [blame] | 919 | |
Jae Hoon Kim | cc723e0 | 2021-08-16 21:03:21 +0000 | [diff] [blame] | 920 | |
Amin Hassani | 7440308 | 2021-02-22 11:40:09 -0800 | [diff] [blame] | 921 | class StatefulPayloadGenerator(ReaderBase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 922 | """A class for generating a stateful update payload in a separate thread.""" |
Amin Hassani | 7440308 | 2021-02-22 11:40:09 -0800 | [diff] [blame] | 923 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 924 | def __init__(self, image: str): |
| 925 | """Initializes that class. |
Amin Hassani | 7440308 | 2021-02-22 11:40:09 -0800 | [diff] [blame] | 926 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 927 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 928 | image: The path to a local Chromium OS image. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 929 | """ |
| 930 | super().__init__() |
| 931 | self._image = image |
| 932 | |
| 933 | def run(self): |
| 934 | """Generates the stateful update and writes it into the output pipe.""" |
| 935 | try: |
| 936 | paygen_stateful_payload_lib.GenerateStatefulPayload( |
| 937 | self._image, self._Source() |
| 938 | ) |
| 939 | finally: |
| 940 | self._CloseSource() |
Amin Hassani | 7440308 | 2021-02-22 11:40:09 -0800 | [diff] [blame] | 941 | |
| 942 | |
| 943 | class StatefulUpdater(PartitionUpdaterBase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 944 | """A class to update the stateful partition on a device.""" |
Amin Hassani | 7440308 | 2021-02-22 11:40:09 -0800 | [diff] [blame] | 945 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 946 | def __init__(self, clobber_stateful: bool, *args): |
| 947 | """Initializes the class |
Amin Hassani | 7440308 | 2021-02-22 11:40:09 -0800 | [diff] [blame] | 948 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 949 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 950 | clobber_stateful: Whether to clobber the stateful or not. |
| 951 | *args: Look at PartitionUpdaterBase. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 952 | """ |
| 953 | super().__init__(*args) |
| 954 | self._clobber_stateful = clobber_stateful |
Amin Hassani | 7440308 | 2021-02-22 11:40:09 -0800 | [diff] [blame] | 955 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 956 | def _Run(self): |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame] | 957 | """Read/Download the stateful updates and write it into the device.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 958 | if self._image_type == ImageType.FULL: |
| 959 | generator_cls = StatefulPayloadGenerator |
| 960 | elif self._image_type == ImageType.REMOTE_DIRECTORY: |
| 961 | generator_cls = GsFileCopier |
| 962 | self._image = os.path.join( |
| 963 | self._image, paygen_stateful_payload_lib.STATEFUL_FILE |
| 964 | ) |
| 965 | else: |
| 966 | raise ValueError(f"Invalid image type {self._image_type}") |
Amin Hassani | 7440308 | 2021-02-22 11:40:09 -0800 | [diff] [blame] | 967 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 968 | with generator_cls(self._image) as generator: |
| 969 | try: |
| 970 | updater = stateful_updater.StatefulUpdater(self._device) |
| 971 | updater.Update( |
| 972 | generator.Target(), |
| 973 | is_payload_on_device=False, |
| 974 | update_type=( |
| 975 | stateful_updater.StatefulUpdater.UPDATE_TYPE_CLOBBER |
| 976 | if self._clobber_stateful |
| 977 | else None |
| 978 | ), |
| 979 | ) |
| 980 | finally: |
| 981 | generator.CloseTarget() |
| 982 | |
| 983 | def Revert(self): |
| 984 | """Reverts the stateful partition update.""" |
| 985 | logging.info("Reverting the stateful update.") |
| 986 | stateful_updater.StatefulUpdater(self._device).Reset() |
Amin Hassani | 5597056 | 2021-02-22 20:49:13 -0800 | [diff] [blame] | 987 | |
| 988 | |
| 989 | class ProgressWatcher(threading.Thread): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 990 | """A class used for watching the progress of rootfs update.""" |
Amin Hassani | 5597056 | 2021-02-22 20:49:13 -0800 | [diff] [blame] | 991 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 992 | def __init__(self, device, target_root: str): |
| 993 | """Initializes the class. |
Amin Hassani | 5597056 | 2021-02-22 20:49:13 -0800 | [diff] [blame] | 994 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 995 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 996 | device: The ChromiumOSDevice to be updated. |
| 997 | target_root: The target root partition to monitor the progress of. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 998 | """ |
| 999 | super().__init__() |
Amin Hassani | 5597056 | 2021-02-22 20:49:13 -0800 | [diff] [blame] | 1000 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1001 | self._device = device |
| 1002 | self._target_root = target_root |
| 1003 | self._exit = False |
Amin Hassani | 5597056 | 2021-02-22 20:49:13 -0800 | [diff] [blame] | 1004 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1005 | def __enter__(self): |
| 1006 | """Starts the thread.""" |
| 1007 | self.start() |
| 1008 | return self |
Amin Hassani | 5597056 | 2021-02-22 20:49:13 -0800 | [diff] [blame] | 1009 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1010 | def __exit__(self, *args, **kwargs): |
| 1011 | """Exists the thread.""" |
| 1012 | self._exit = True |
| 1013 | self.join() |
Amin Hassani | 5597056 | 2021-02-22 20:49:13 -0800 | [diff] [blame] | 1014 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1015 | def _ShouldExit(self): |
| 1016 | return self._exit |
Amin Hassani | 5597056 | 2021-02-22 20:49:13 -0800 | [diff] [blame] | 1017 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1018 | def run(self): |
| 1019 | """Monitors the progress of the target root partitions' update. |
Amin Hassani | 5597056 | 2021-02-22 20:49:13 -0800 | [diff] [blame] | 1020 | |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame] | 1021 | This is done by periodically, reading the fd position of the process |
| 1022 | that is writing into the target partition and reporting it back. Then |
| 1023 | the position is divided by the size of the block device to report |
| 1024 | approximate progress. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1025 | """ |
| 1026 | cmd = ["blockdev", "--getsize64", self._target_root] |
Amin Hassani | 5597056 | 2021-02-22 20:49:13 -0800 | [diff] [blame] | 1027 | output = self._device.run(cmd, capture_output=True).stdout.strip() |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1028 | if output is None: |
| 1029 | raise Error(f"Cannot get the block device size from {output}.") |
| 1030 | dev_size = int(output) |
| 1031 | |
| 1032 | # Using lsof to find out which process is writing to the target rootfs. |
| 1033 | cmd = ["lsof", "-t", self._target_root] |
Brian Norris | 7ceb0fe | 2022-11-10 17:46:31 -0800 | [diff] [blame] | 1034 | while True: |
| 1035 | if self._ShouldExit(): |
| 1036 | return |
| 1037 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1038 | try: |
| 1039 | pid = self._device.run(cmd, capture_output=True).stdout.strip() |
| 1040 | if pid: |
| 1041 | break |
| 1042 | except cros_build_lib.RunCommandError: |
| 1043 | continue |
| 1044 | finally: |
| 1045 | time.sleep(1) |
| 1046 | |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame] | 1047 | # Now that we know which process is writing to it, we can look the |
| 1048 | # fdinfo of stdout of that process to get its offset. We're assuming |
| 1049 | # there will be no seek, which is correct. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1050 | cmd = ["cat", f"/proc/{pid}/fdinfo/1"] |
| 1051 | while not self._ShouldExit(): |
| 1052 | try: |
| 1053 | output = self._device.run( |
| 1054 | cmd, capture_output=True |
| 1055 | ).stdout.strip() |
| 1056 | m = re.search(r"^pos:\s*(\d+)$", output, flags=re.M) |
| 1057 | if m: |
| 1058 | offset = int(m.group(1)) |
| 1059 | # DeviceImagerOperation will look for this log. |
| 1060 | logging.info("RootFS progress: %f", offset / dev_size) |
| 1061 | except cros_build_lib.RunCommandError: |
| 1062 | continue |
| 1063 | finally: |
| 1064 | time.sleep(1) |
Amin Hassani | 5597056 | 2021-02-22 20:49:13 -0800 | [diff] [blame] | 1065 | |
| 1066 | |
| 1067 | class DeviceImagerOperation(operation.ProgressBarOperation): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1068 | """A class to provide a progress bar for DeviceImager operation.""" |
Amin Hassani | 5597056 | 2021-02-22 20:49:13 -0800 | [diff] [blame] | 1069 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1070 | def __init__(self): |
| 1071 | """Initializes the class.""" |
| 1072 | super().__init__() |
Amin Hassani | 5597056 | 2021-02-22 20:49:13 -0800 | [diff] [blame] | 1073 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1074 | self._progress = 0.0 |
Amin Hassani | 5597056 | 2021-02-22 20:49:13 -0800 | [diff] [blame] | 1075 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1076 | def ParseOutput(self, output=None): |
| 1077 | """Override function to parse the output and provide progress. |
Amin Hassani | 5597056 | 2021-02-22 20:49:13 -0800 | [diff] [blame] | 1078 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1079 | Args: |
Alex Klein | 53cc3bf | 2022-10-13 08:50:01 -0600 | [diff] [blame] | 1080 | output: The stderr or stdout. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1081 | """ |
| 1082 | output = self._stdout.read() |
| 1083 | match = re.findall(r"RootFS progress: (\d+(?:\.\d+)?)", output) |
| 1084 | if match: |
| 1085 | progress = float(match[0]) |
| 1086 | self._progress = max(self._progress, progress) |
Amin Hassani | 5597056 | 2021-02-22 20:49:13 -0800 | [diff] [blame] | 1087 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1088 | # If postinstall completes, move half of the remaining progress. |
| 1089 | if re.findall(r"Postinstall completed", output): |
| 1090 | self._progress += (1.0 - self._progress) / 2 |
Amin Hassani | 5597056 | 2021-02-22 20:49:13 -0800 | [diff] [blame] | 1091 | |
Alex Klein | 975e86c | 2023-01-23 16:49:10 -0700 | [diff] [blame] | 1092 | # While waiting for reboot, each time, move half of the remaining |
| 1093 | # progress. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1094 | if re.findall(r"Unable to get new boot_id", output): |
| 1095 | self._progress += (1.0 - self._progress) / 2 |
Amin Hassani | 5597056 | 2021-02-22 20:49:13 -0800 | [diff] [blame] | 1096 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1097 | if re.findall(r"DeviceImager completed.", output): |
| 1098 | self._progress = 1.0 |
Amin Hassani | 5597056 | 2021-02-22 20:49:13 -0800 | [diff] [blame] | 1099 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1100 | self.ProgressBar(self._progress) |