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