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