blob: 38a0f362c1deaa380071b10e608406cb7da5048f [file] [log] [blame]
Amin Hassani92f6c4a2021-02-20 17:36:09 -08001# Copyright 2021 The Chromium OS Authors. All rights reserved.
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 Hassanid4b3ff82021-02-20 23:05:14 -08007import abc
Amin Hassani92f6c4a2021-02-20 17:36:09 -08008import enum
Daichi Hironoc1a8fd32022-01-07 22:17:51 +09009from io import BytesIO
Chris McDonald14ac61d2021-07-21 11:49:56 -060010import logging
Amin Hassani92f6c4a2021-02-20 17:36:09 -080011import os
12import re
Amin Hassanid4b3ff82021-02-20 23:05:14 -080013import tempfile
14import threading
Amin Hassani55970562021-02-22 20:49:13 -080015import time
Daichi Hironoc1a8fd32022-01-07 22:17:51 +090016from typing import Dict, List, Tuple, Union
Amin Hassani92f6c4a2021-02-20 17:36:09 -080017
Amin Hassani55970562021-02-22 20:49:13 -080018from chromite.cli import command
Amin Hassanicf8f0042021-03-12 10:42:13 -080019from chromite.cli import flash
Jae Hoon Kimcc723e02021-08-16 21:03:21 +000020from chromite.lib import cgpt
Amin Hassanid4b3ff82021-02-20 23:05:14 -080021from chromite.lib import constants
Amin Hassani92f6c4a2021-02-20 17:36:09 -080022from chromite.lib import cros_build_lib
Amin Hassani92f6c4a2021-02-20 17:36:09 -080023from chromite.lib import gs
Amin Hassanid4b3ff82021-02-20 23:05:14 -080024from chromite.lib import image_lib
Amin Hassani55970562021-02-22 20:49:13 -080025from chromite.lib import operation
Amin Hassanid4b3ff82021-02-20 23:05:14 -080026from chromite.lib import osutils
Amin Hassani92f6c4a2021-02-20 17:36:09 -080027from chromite.lib import parallel
28from chromite.lib import remote_access
29from chromite.lib import retry_util
Amin Hassani74403082021-02-22 11:40:09 -080030from chromite.lib import stateful_updater
Amin Hassani75c5f942021-02-20 23:56:53 -080031from chromite.lib.paygen import partition_lib
Amin Hassani74403082021-02-22 11:40:09 -080032from chromite.lib.paygen import paygen_stateful_payload_lib
Amin Hassani92f6c4a2021-02-20 17:36:09 -080033from chromite.lib.xbuddy import devserver_constants
34from chromite.lib.xbuddy import xbuddy
Alex Klein18ef1212021-10-14 12:49:02 -060035from chromite.utils import timer
Amin Hassani92f6c4a2021-02-20 17:36:09 -080036
37
Amin Hassani92f6c4a2021-02-20 17:36:09 -080038class Error(Exception):
Alex Klein1699fab2022-09-08 08:46:06 -060039 """Thrown when there is a general Chromium OS-specific flash error."""
Amin Hassani92f6c4a2021-02-20 17:36:09 -080040
41
42class ImageType(enum.Enum):
Alex Klein1699fab2022-09-08 08:46:06 -060043 """Type of the image that is used for flashing the device."""
Amin Hassani92f6c4a2021-02-20 17:36:09 -080044
Alex Klein1699fab2022-09-08 08:46:06 -060045 # 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 Hassani92f6c4a2021-02-20 17:36:09 -080050
51
52class Partition(enum.Enum):
Alex Klein1699fab2022-09-08 08:46:06 -060053 """An enum for partition types like kernel and rootfs."""
54
55 KERNEL = 0
56 ROOTFS = 1
57 MINIOS = 2
Amin Hassani92f6c4a2021-02-20 17:36:09 -080058
59
60class DeviceImager(object):
Alex Klein1699fab2022-09-08 08:46:06 -060061 """A class to flash a Chromium OS device.
Amin Hassani92f6c4a2021-02-20 17:36:09 -080062
Alex Klein1699fab2022-09-08 08:46:06 -060063 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 Hassani92f6c4a2021-02-20 17:36:09 -080066 """
67
Alex Klein1699fab2022-09-08 08:46:06 -060068 # 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 Hassani92f6c4a2021-02-20 17:36:09 -080071
Alex Klein1699fab2022-09-08 08:46:06 -060072 MINIOS_A = {Partition.MINIOS: 9}
73 MINIOS_B = {Partition.MINIOS: 10}
Amin Hassani92f6c4a2021-02-20 17:36:09 -080074
Alex Klein1699fab2022-09-08 08:46:06 -060075 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 Hassani92f6c4a2021-02-20 17:36:09 -080091
Alex Klein1699fab2022-09-08 08:46:06 -060092 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 Hassani92f6c4a2021-02-20 17:36:09 -0800107
Alex Klein1699fab2022-09-08 08:46:06 -0600108 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 Hassani92f6c4a2021-02-20 17:36:09 -0800119
Alex Klein1699fab2022-09-08 08:46:06 -0600120 self._image_type = None
121 self._inactive_state = None
122 self._delta = delta
Daichi Hirono28831b3b2022-04-07 12:41:11 +0900123
Alex Klein1699fab2022-09-08 08:46:06 -0600124 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 Hassani92f6c4a2021-02-20 17:36:09 -0800132
Alex Klein1699fab2022-09-08 08:46:06 -0600133 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 Hassani92f6c4a2021-02-20 17:36:09 -0800141
Alex Klein1699fab2022-09-08 08:46:06 -0600142 # DeviceImagerOperation will look for this log.
143 logging.info("DeviceImager completed.")
Amin Hassani92f6c4a2021-02-20 17:36:09 -0800144
Alex Klein1699fab2022-09-08 08:46:06 -0600145 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 Hassani92f6c4a2021-02-20 17:36:09 -0800148
Alex Klein1699fab2022-09-08 08:46:06 -0600149 self._InstallPartitions()
Amin Hassani92f6c4a2021-02-20 17:36:09 -0800150
Alex Klein1699fab2022-09-08 08:46:06 -0600151 if self._clear_tpm_owner:
152 self._device.ClearTpmOwner()
Amin Hassani92f6c4a2021-02-20 17:36:09 -0800153
Alex Klein1699fab2022-09-08 08:46:06 -0600154 if not self._no_reboot:
155 self._Reboot()
156 self._VerifyBootExpectations()
Amin Hassani92f6c4a2021-02-20 17:36:09 -0800157
Alex Klein1699fab2022-09-08 08:46:06 -0600158 if self._disable_verification:
159 self._device.DisableRootfsVerification()
Amin Hassani92f6c4a2021-02-20 17:36:09 -0800160
Alex Klein1699fab2022-09-08 08:46:06 -0600161 def _LocateImage(self):
162 """Locates the path to the final image(s) that need to be installed.
Amin Hassani92f6c4a2021-02-20 17:36:09 -0800163
Alex Klein1699fab2022-09-08 08:46:06 -0600164 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 Hassanicf8f0042021-03-12 10:42:13 -0800168
Alex Klein1699fab2022-09-08 08:46:06 -0600169 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 Hassani92f6c4a2021-02-20 17:36:09 -0800175
Alex Klein1699fab2022-09-08 08:46:06 -0600176 # 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 Hassani92f6c4a2021-02-20 17:36:09 -0800186
Alex Klein1699fab2022-09-08 08:46:06 -0600187 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 Hassani92f6c4a2021-02-20 17:36:09 -0800192
Alex Klein1699fab2022-09-08 08:46:06 -0600193 # 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 Hassani92f6c4a2021-02-20 17:36:09 -0800199
Alex Klein1699fab2022-09-08 08:46:06 -0600200 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 Hassani92f6c4a2021-02-20 17:36:09 -0800205
Alex Klein1699fab2022-09-08 08:46:06 -0600206 if local_file:
207 self._image = local_file
208 self._image_type = ImageType.FULL
209 return
Amin Hassani92f6c4a2021-02-20 17:36:09 -0800210
Alex Klein1699fab2022-09-08 08:46:06 -0600211 self._image = f"{devserver_constants.GS_IMAGE_DIR}/{build_id}"
212 self._image_type = ImageType.REMOTE_DIRECTORY
Amin Hassani92f6c4a2021-02-20 17:36:09 -0800213
Alex Klein1699fab2022-09-08 08:46:06 -0600214 def _SplitDevPath(self, path: str) -> Tuple[str, int]:
215 """Splits the given /dev/x path into prefix and the dev number.
Amin Hassani92f6c4a2021-02-20 17:36:09 -0800216
Alex Klein1699fab2022-09-08 08:46:06 -0600217 Args:
218 path: The path to a block dev device.
Amin Hassani92f6c4a2021-02-20 17:36:09 -0800219
Alex Klein1699fab2022-09-08 08:46:06 -0600220 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 Kimcc723e02021-08-16 21:03:21 +0000227
Alex Klein1699fab2022-09-08 08:46:06 -0600228 return match.group(1), int(match.group(2))
Jae Hoon Kimcc723e02021-08-16 21:03:21 +0000229
Alex Klein1699fab2022-09-08 08:46:06 -0600230 def _GetKernelState(self, root_num: int) -> Tuple[Dict, Dict]:
231 """Returns the kernel state.
Amin Hassani92f6c4a2021-02-20 17:36:09 -0800232
Alex Klein1699fab2022-09-08 08:46:06 -0600233 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 Hassanid684e982021-02-26 11:10:58 -0800243
Alex Klein1699fab2022-09-08 08:46:06 -0600244 def _GetMiniOSState(self, minios_num: int) -> Tuple[Dict, Dict]:
245 """Returns the miniOS state.
Amin Hassani75c5f942021-02-20 23:56:53 -0800246
Alex Klein1699fab2022-09-08 08:46:06 -0600247 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 Hassani92f6c4a2021-02-20 17:36:09 -0800257
Alex Klein1699fab2022-09-08 08:46:06 -0600258 def _InstallPartitions(self):
259 """The main method that installs the partitions of a Chrome OS device.
Amin Hassani74403082021-02-22 11:40:09 -0800260
Alex Klein1699fab2022-09-08 08:46:06 -0600261 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 Kimcc723e02021-08-16 21:03:21 +0000265
Alex Klein1699fab2022-09-08 08:46:06 -0600266 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 Hassani92f6c4a2021-02-20 17:36:09 -0800279
Alex Klein1699fab2022-09-08 08:46:06 -0600280 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 Hassani92f6c4a2021-02-20 17:36:09 -0800286
Alex Klein1699fab2022-09-08 08:46:06 -0600287 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 Hassani92f6c4a2021-02-20 17:36:09 -0800297
Alex Klein1699fab2022-09-08 08:46:06 -0600298 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 Hassani92f6c4a2021-02-20 17:36:09 -0800318
Alex Klein1699fab2022-09-08 08:46:06 -0600319 # 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 Hassanid4b3ff82021-02-20 23:05:14 -0800359
360
361class ReaderBase(threading.Thread):
Alex Klein1699fab2022-09-08 08:46:06 -0600362 """The base class for reading different inputs and writing into output.
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800363
Alex Klein1699fab2022-09-08 08:46:06 -0600364 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 Hassanid4b3ff82021-02-20 23:05:14 -0800368 """
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800369
Alex Klein1699fab2022-09-08 08:46:06 -0600370 def __init__(self, use_named_pipes: bool = False):
371 """Initializes the class.
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800372
Alex Klein1699fab2022-09-08 08:46:06 -0600373 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 Hassanid4b3ff82021-02-20 23:05:14 -0800381
Alex Klein1699fab2022-09-08 08:46:06 -0600382 def __del__(self):
383 """Destructor.
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800384
Alex Klein1699fab2022-09-08 08:46:06 -0600385 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 Hassanid4b3ff82021-02-20 23:05:14 -0800389
Alex Klein1699fab2022-09-08 08:46:06 -0600390 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 Hassanid4b3ff82021-02-20 23:05:14 -0800402
Alex Klein1699fab2022-09-08 08:46:06 -0600403 self.start()
404 return self
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800405
Alex Klein1699fab2022-09-08 08:46:06 -0600406 def __exit__(self, *args, **kwargs):
407 """Exits the context manager."""
408 self.join()
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800409
Alex Klein1699fab2022-09-08 08:46:06 -0600410 def _Source(self):
411 """Returns the source pipe to write data into.
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800412
Alex Klein1699fab2022-09-08 08:46:06 -0600413 Sub-classes can use this function to determine where to write their data
414 into.
415 """
416 return self._pipe_source
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800417
Alex Klein1699fab2022-09-08 08:46:06 -0600418 def _CloseSource(self):
419 """Closes the source pipe.
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800420
Alex Klein1699fab2022-09-08 08:46:06 -0600421 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 Hassanid4b3ff82021-02-20 23:05:14 -0800427
Alex Klein1699fab2022-09-08 08:46:06 -0600428 def Target(self):
429 """Returns the target pipe to read data from.
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800430
Alex Klein1699fab2022-09-08 08:46:06 -0600431 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 Hassanid4b3ff82021-02-20 23:05:14 -0800445
446
447class PartialFileReader(ReaderBase):
Alex Klein1699fab2022-09-08 08:46:06 -0600448 """A class to read specific offset and length from a file and compress it.
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800449
Alex Klein1699fab2022-09-08 08:46:06 -0600450 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 Hassanid4b3ff82021-02-20 23:05:14 -0800453 """
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800454
Alex Klein1699fab2022-09-08 08:46:06 -0600455 # 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 Hassanid4b3ff82021-02-20 23:05:14 -0800463
Alex Klein1699fab2022-09-08 08:46:06 -0600464 def __init__(
465 self,
466 image: str,
467 offset: int,
468 length: int,
469 compression_command: List[str],
470 ):
471 """Initializes the class.
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800472
Alex Klein1699fab2022-09-08 08:46:06 -0600473 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 Hassanid4b3ff82021-02-20 23:05:14 -0800503
504
Amin Hassani0fe49ae2021-02-21 23:41:58 -0800505class GsFileCopier(ReaderBase):
Alex Klein1699fab2022-09-08 08:46:06 -0600506 """A class for downloading gzip compressed file from GS bucket into a pipe."""
Amin Hassani0fe49ae2021-02-21 23:41:58 -0800507
Alex Klein1699fab2022-09-08 08:46:06 -0600508 def __init__(self, image: str):
509 """Initializes the class.
Amin Hassani0fe49ae2021-02-21 23:41:58 -0800510
Alex Klein1699fab2022-09-08 08:46:06 -0600511 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 Hassani0fe49ae2021-02-21 23:41:58 -0800516
Alex Klein1699fab2022-09-08 08:46:06 -0600517 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 Hassani0fe49ae2021-02-21 23:41:58 -0800523
524
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800525class PartitionUpdaterBase(object):
Alex Klein1699fab2022-09-08 08:46:06 -0600526 """A base abstract class to use for installing an image into a partition.
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800527
Alex Klein1699fab2022-09-08 08:46:06 -0600528 Sub-classes should implement the abstract methods to provide the core
529 functionality.
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800530 """
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800531
Alex Klein1699fab2022-09-08 08:46:06 -0600532 def __init__(self, device, image: str, image_type, target: str):
533 """Initializes this base class with values that most sub-classes will need.
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800534
Alex Klein1699fab2022-09-08 08:46:06 -0600535 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 Hassanid4b3ff82021-02-20 23:05:14 -0800546
Alex Klein1699fab2022-09-08 08:46:06 -0600547 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 Hassanid4b3ff82021-02-20 23:05:14 -0800554
Alex Klein1699fab2022-09-08 08:46:06 -0600555 logging.debug("Completed %s in %s", self.__class__.__name__, t)
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800556
Alex Klein1699fab2022-09-08 08:46:06 -0600557 @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 Hassanid4b3ff82021-02-20 23:05:14 -0800561
Alex Klein1699fab2022-09-08 08:46:06 -0600562 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 Hassanid4b3ff82021-02-20 23:05:14 -0800573
574
575class RawPartitionUpdater(PartitionUpdaterBase):
Alex Klein1699fab2022-09-08 08:46:06 -0600576 """A class to update a raw partition on a Chromium OS device."""
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800577
Alex Klein1699fab2022-09-08 08:46:06 -0600578 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 Hassanid4b3ff82021-02-20 23:05:14 -0800586
Alex Klein1699fab2022-09-08 08:46:06 -0600587 def _GetPartitionName(self):
588 """Returns the name of the partition in a Chromium OS GPT layout.
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800589
Alex Klein1699fab2022-09-08 08:46:06 -0600590 Subclasses should override this function to return correct name.
591 """
592 raise NotImplementedError("Subclasses need to implement this.")
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800593
Alex Klein1699fab2022-09-08 08:46:06 -0600594 def _CopyPartitionFromImage(self, part_name: str):
595 """Updates the device's partition from a local Chromium OS image.
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800596
Alex Klein1699fab2022-09-08 08:46:06 -0600597 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 Hironoc1a8fd32022-01-07 22:17:51 +0900604
Alex Klein1699fab2022-09-08 08:46:06 -0600605 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 Hassanid4b3ff82021-02-20 23:05:14 -0800612
Alex Klein1699fab2022-09-08 08:46:06 -0600613 def _GetCompressionAndDecompression(self) -> Tuple[List[str], List[str]]:
614 """Returns compression / decompression commands."""
Daichi Hironoc1a8fd32022-01-07 22:17:51 +0900615
Alex Klein1699fab2022-09-08 08:46:06 -0600616 return (
617 [cros_build_lib.FindCompressor(cros_build_lib.COMP_GZIP)],
618 self._device.GetDecompressor(cros_build_lib.COMP_GZIP),
619 )
Daichi Hironoc1a8fd32022-01-07 22:17:51 +0900620
Alex Klein1699fab2022-09-08 08:46:06 -0600621 def _WriteToTarget(
622 self, source: Union[int, BytesIO], decompress_command: List[str]
623 ) -> None:
624 """Writes bytes source to the target device on DUT.
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800625
Alex Klein1699fab2022-09-08 08:46:06 -0600626 Returns:
627 A string command to run on a device to read data from stdin, uncompress it
628 and write it to the target partition.
629 """
630 # Using oflag=direct to tell the OS not to cache the writes (faster).
631 cmd = " ".join(
632 [
633 *decompress_command,
634 "|",
635 "dd",
636 "bs=1M",
637 "oflag=direct",
638 f"of={self._target}",
639 ]
640 )
641 self._device.run(cmd, input=source, shell=True)
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800642
Alex Klein1699fab2022-09-08 08:46:06 -0600643 def _GetPartLocation(self, part_name: str):
644 """Extracts the location and size of the raw partition from the image.
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800645
Alex Klein1699fab2022-09-08 08:46:06 -0600646 Args:
647 part_name: The name of the partition in the source image that needs to be
648 extracted.
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800649
Alex Klein1699fab2022-09-08 08:46:06 -0600650 Returns:
651 A tuple of offset and length (in bytes) from the image.
652 """
653 try:
654 parts = image_lib.GetImageDiskPartitionInfo(self._image)
655 part_info = [p for p in parts if p.name == part_name][0]
656 except IndexError:
657 raise Error(f"No partition named {part_name} found.")
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800658
Alex Klein1699fab2022-09-08 08:46:06 -0600659 return int(part_info.start), int(part_info.size)
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800660
Alex Klein1699fab2022-09-08 08:46:06 -0600661 def _GetRemotePartitionName(self):
662 """Returns the name of the quick-provision partition file.
Amin Hassani0fe49ae2021-02-21 23:41:58 -0800663
Alex Klein1699fab2022-09-08 08:46:06 -0600664 Subclasses should override this function to return correct name.
665 """
666 raise NotImplementedError("Subclasses need to implement this.")
Amin Hassani0fe49ae2021-02-21 23:41:58 -0800667
Alex Klein1699fab2022-09-08 08:46:06 -0600668 def _OptimizePartLocation(self, offset: int, length: int):
669 """Optimizes the offset and length of the partition.
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800670
Alex Klein1699fab2022-09-08 08:46:06 -0600671 Subclasses can override this to provide better offset/length than what is
672 defined in the PGT partition layout.
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800673
Alex Klein1699fab2022-09-08 08:46:06 -0600674 Args:
675 offset: The offset (in bytes) of the partition in the image.
676 length: The length (in bytes) of the partition.
Amin Hassanid4b3ff82021-02-20 23:05:14 -0800677
Alex Klein1699fab2022-09-08 08:46:06 -0600678 Returns:
679 A tuple of offset and length (in bytes) from the image.
680 """
681 return offset, length
Amin Hassanid684e982021-02-26 11:10:58 -0800682
Alex Klein1699fab2022-09-08 08:46:06 -0600683 def _RedirectPartition(self, file_name: str):
684 """Downloads the partition from a remote path and writes it into target.
Amin Hassani0fe49ae2021-02-21 23:41:58 -0800685
Alex Klein1699fab2022-09-08 08:46:06 -0600686 Args:
687 file_name: The file name in the remote directory self._image.
688 """
689 image_path = os.path.join(self._image, file_name)
690 with GsFileCopier(image_path) as generator:
691 try:
692 with open(generator.Target(), "rb") as fp:
693 # Always use GZIP as remote quick provision images are gzip
694 # compressed only.
695 self._WriteToTarget(
696 fp,
697 self._device.GetDecompressor(cros_build_lib.COMP_GZIP),
698 )
699 finally:
700 generator.CloseTarget()
Amin Hassani0fe49ae2021-02-21 23:41:58 -0800701
Amin Hassanid684e982021-02-26 11:10:58 -0800702
703class KernelUpdater(RawPartitionUpdater):
Alex Klein1699fab2022-09-08 08:46:06 -0600704 """A class to update the kernel partition on a Chromium OS device."""
Amin Hassanid684e982021-02-26 11:10:58 -0800705
Alex Klein1699fab2022-09-08 08:46:06 -0600706 def _GetPartitionName(self):
707 """See RawPartitionUpdater._GetPartitionName()."""
708 return constants.PART_KERN_B
Amin Hassanid684e982021-02-26 11:10:58 -0800709
Alex Klein1699fab2022-09-08 08:46:06 -0600710 def _GetRemotePartitionName(self):
711 """See RawPartitionUpdater._GetRemotePartitionName()."""
712 return constants.QUICK_PROVISION_PAYLOAD_KERNEL
Amin Hassani0fe49ae2021-02-21 23:41:58 -0800713
Alex Klein1699fab2022-09-08 08:46:06 -0600714 def Revert(self):
715 """Reverts the kernel partition update."""
716 # There is nothing to do for reverting kernel partition.
Amin Hassani75c5f942021-02-20 23:56:53 -0800717
718
719class RootfsUpdater(RawPartitionUpdater):
Alex Klein1699fab2022-09-08 08:46:06 -0600720 """A class to update the root partition on a Chromium OS device."""
Amin Hassani75c5f942021-02-20 23:56:53 -0800721
Alex Klein1699fab2022-09-08 08:46:06 -0600722 def __init__(self, current_root: str, *args):
723 """Initializes the class.
Amin Hassani75c5f942021-02-20 23:56:53 -0800724
Alex Klein1699fab2022-09-08 08:46:06 -0600725 Args:
726 current_root: The current root device path.
727 *args: See PartitionUpdaterBase
728 """
729 super().__init__(*args)
Amin Hassani75c5f942021-02-20 23:56:53 -0800730
Alex Klein1699fab2022-09-08 08:46:06 -0600731 self._current_root = current_root
732 self._ran_postinst = False
Amin Hassani75c5f942021-02-20 23:56:53 -0800733
Alex Klein1699fab2022-09-08 08:46:06 -0600734 def _GetPartitionName(self):
735 """See RawPartitionUpdater._GetPartitionName()."""
736 return constants.PART_ROOT_A
Amin Hassani75c5f942021-02-20 23:56:53 -0800737
Alex Klein1699fab2022-09-08 08:46:06 -0600738 def _GetRemotePartitionName(self):
739 """See RawPartitionUpdater._GetRemotePartitionName()."""
740 return constants.QUICK_PROVISION_PAYLOAD_ROOTFS
Amin Hassani0fe49ae2021-02-21 23:41:58 -0800741
Alex Klein1699fab2022-09-08 08:46:06 -0600742 def _Run(self):
743 """The function that does the job of rootfs partition update."""
744 with ProgressWatcher(self._device, self._target):
745 super()._Run()
Amin Hassani75c5f942021-02-20 23:56:53 -0800746
Alex Klein1699fab2022-09-08 08:46:06 -0600747 self._RunPostInst()
Amin Hassani75c5f942021-02-20 23:56:53 -0800748
Alex Klein1699fab2022-09-08 08:46:06 -0600749 def _OptimizePartLocation(self, offset: int, length: int):
750 """Optimizes the size of the root partition of the image.
Amin Hassani75c5f942021-02-20 23:56:53 -0800751
Alex Klein1699fab2022-09-08 08:46:06 -0600752 Normally the file system does not occupy the entire partition. Furthermore
753 we don't need the verity hash tree at the end of the root file system
754 because postinst will recreate it. This function reads the (approximate)
755 superblock of the ext4 partition and extracts the actual file system size in
756 the root partition.
757 """
758 superblock_size = 4096 * 2
759 with open(self._image, "rb") as r:
760 r.seek(offset)
761 with tempfile.NamedTemporaryFile(delete=False) as fp:
762 fp.write(r.read(superblock_size))
763 fp.close()
764 return offset, partition_lib.Ext2FileSystemSize(fp.name)
Amin Hassani75c5f942021-02-20 23:56:53 -0800765
Alex Klein1699fab2022-09-08 08:46:06 -0600766 def _RunPostInst(self, on_target: bool = True):
767 """Runs the postinst process in the root partition.
Amin Hassani75c5f942021-02-20 23:56:53 -0800768
Alex Klein1699fab2022-09-08 08:46:06 -0600769 Args:
770 on_target: If true the postinst is run on the target (inactive)
771 partition. This is used when doing normal updates. If false, the
772 postinst is run on the current (active) partition. This is used when
773 reverting an update.
774 """
775 try:
776 postinst_dir = "/"
777 partition = self._current_root
778 if on_target:
779 postinst_dir = self._device.run(
780 ["mktemp", "-d", "-p", self._device.work_dir],
781 capture_output=True,
782 ).stdout.strip()
783 self._device.run(
784 ["mount", "-o", "ro", self._target, postinst_dir]
785 )
786 partition = self._target
Amin Hassani75c5f942021-02-20 23:56:53 -0800787
Alex Klein1699fab2022-09-08 08:46:06 -0600788 self._ran_postinst = True
789 postinst = os.path.join(postinst_dir, "postinst")
790 result = self._device.run(
791 [postinst, partition], capture_output=True
792 )
Amin Hassani75c5f942021-02-20 23:56:53 -0800793
Alex Klein1699fab2022-09-08 08:46:06 -0600794 logging.debug(
795 "Postinst result on %s: \n%s", postinst, result.stdout
796 )
797 # DeviceImagerOperation will look for this log.
798 logging.info("Postinstall completed.")
799 finally:
800 if on_target:
801 self._device.run(["umount", postinst_dir])
Amin Hassani75c5f942021-02-20 23:56:53 -0800802
Alex Klein1699fab2022-09-08 08:46:06 -0600803 def Revert(self):
804 """Reverts the root update install."""
805 logging.info("Reverting the rootfs partition update.")
806 if self._ran_postinst:
807 # We don't have to do anything for revert if we haven't changed the kernel
808 # priorities yet.
809 self._RunPostInst(on_target=False)
Amin Hassani74403082021-02-22 11:40:09 -0800810
811
Jae Hoon Kimcc723e02021-08-16 21:03:21 +0000812class MiniOSUpdater(RawPartitionUpdater):
Alex Klein1699fab2022-09-08 08:46:06 -0600813 """A class to update the miniOS partition on a Chromium OS device."""
Jae Hoon Kimcc723e02021-08-16 21:03:21 +0000814
Alex Klein1699fab2022-09-08 08:46:06 -0600815 def __init__(self, *args):
816 """Initializes the class.
Jae Hoon Kimcc723e02021-08-16 21:03:21 +0000817
Alex Klein1699fab2022-09-08 08:46:06 -0600818 Args:
819 *args: See PartitionUpdaterBase
820 """
821 super().__init__(*args)
Jae Hoon Kimcc723e02021-08-16 21:03:21 +0000822
Alex Klein1699fab2022-09-08 08:46:06 -0600823 self._ran_postinst = False
Jae Hoon Kimcc723e02021-08-16 21:03:21 +0000824
Alex Klein1699fab2022-09-08 08:46:06 -0600825 def _GetPartitionName(self):
826 """See RawPartitionUpdater._GetPartitionName()."""
827 return constants.PART_MINIOS_A
Jae Hoon Kimcc723e02021-08-16 21:03:21 +0000828
Alex Klein1699fab2022-09-08 08:46:06 -0600829 def _GetRemotePartitionName(self):
830 """See RawPartitionUpdater._GetRemotePartitionName()."""
831 return constants.QUICK_PROVISION_PAYLOAD_MINIOS
Jae Hoon Kimcc723e02021-08-16 21:03:21 +0000832
Alex Klein1699fab2022-09-08 08:46:06 -0600833 def _Run(self):
834 """The function that does the job of rootfs partition update."""
835 if self._image_type == ImageType.FULL:
836 if self._MiniOSPartitionsExistInImage():
837 logging.info("Updating miniOS partition from local.")
838 super()._Run()
839 else:
840 logging.warning(
841 "Not updating miniOS partition as it does not exist."
842 )
843 return
844 elif self._image_type == ImageType.REMOTE_DIRECTORY:
845 if not gs.GSContext().Exists(
846 os.path.join(
847 self._image, constants.QUICK_PROVISION_PAYLOAD_MINIOS
848 )
849 ):
850 logging.warning("Not updating miniOS, missing remote files.")
851 return
852 elif not self._MiniOSPartitionsExist():
853 logging.warning("Not updating miniOS, missing partitions.")
854 return
855 else:
856 logging.info("Updating miniOS partition from remote.")
857 super()._Run()
858 else:
859 # Let super() handle this error.
860 super()._Run()
Jae Hoon Kimcc723e02021-08-16 21:03:21 +0000861
Alex Klein1699fab2022-09-08 08:46:06 -0600862 self._RunPostInstall()
Jae Hoon Kimcc723e02021-08-16 21:03:21 +0000863
Alex Klein1699fab2022-09-08 08:46:06 -0600864 def _RunPostInstall(self):
865 """The function will change the priority of the miniOS partitions."""
866 self._FlipMiniOSPriority()
867 self._ran_postinst = True
Jae Hoon Kimcc723e02021-08-16 21:03:21 +0000868
Alex Klein1699fab2022-09-08 08:46:06 -0600869 def Revert(self):
870 """Reverts the miniOS partition update."""
871 if self._ran_postinst:
872 self._FlipMiniOSPriority()
Jae Hoon Kimcc723e02021-08-16 21:03:21 +0000873
Alex Klein1699fab2022-09-08 08:46:06 -0600874 def _GetMiniOSPriority(self):
875 return self._device.run(
876 ["crossystem", constants.MINIOS_PRIORITY]
877 ).stdout
Jae Hoon Kimcc723e02021-08-16 21:03:21 +0000878
Alex Klein1699fab2022-09-08 08:46:06 -0600879 def _SetMiniOSPriority(self, priority: str):
880 self._device.run(
881 ["crossystem", f"{constants.MINIOS_PRIORITY}={priority}"]
882 )
Jae Hoon Kimcc723e02021-08-16 21:03:21 +0000883
Alex Klein1699fab2022-09-08 08:46:06 -0600884 def _FlipMiniOSPriority(self):
885 inactive_minios_priority = (
886 "B" if self._GetMiniOSPriority() == "A" else "A"
887 )
888 logging.info("Setting miniOS priority to %s", inactive_minios_priority)
889 self._SetMiniOSPriority(inactive_minios_priority)
Jae Hoon Kimcc723e02021-08-16 21:03:21 +0000890
Alex Klein1699fab2022-09-08 08:46:06 -0600891 def _MiniOSPartitionsExistInImage(self):
892 """Checks if miniOS partition exists in the image."""
893 d = cgpt.Disk.FromImage(self._image)
894 try:
895 d.GetPartitionByTypeGuid(cgpt.MINIOS_TYPE_GUID)
896 return True
897 except KeyError:
898 return False
Jae Hoon Kimcc723e02021-08-16 21:03:21 +0000899
Alex Klein1699fab2022-09-08 08:46:06 -0600900 def _MiniOSPartitionsExist(self):
901 """Checks if the device has miniOS partitions."""
902 run = lambda x: self._device.run(x).stdout.strip()
903 device_drive = run(["rootdev", "-s", "-d"])
904 cmd = ["cgpt", "show", "-t", device_drive, "-i"]
905 return all(
906 (run(cmd + [p]) == cgpt.MINIOS_TYPE_GUID) for p in ("9", "10")
907 )
Jae Hoon Kimb88b7962021-10-18 11:08:38 -0700908
Jae Hoon Kimcc723e02021-08-16 21:03:21 +0000909
Amin Hassani74403082021-02-22 11:40:09 -0800910class StatefulPayloadGenerator(ReaderBase):
Alex Klein1699fab2022-09-08 08:46:06 -0600911 """A class for generating a stateful update payload in a separate thread."""
Amin Hassani74403082021-02-22 11:40:09 -0800912
Alex Klein1699fab2022-09-08 08:46:06 -0600913 def __init__(self, image: str):
914 """Initializes that class.
Amin Hassani74403082021-02-22 11:40:09 -0800915
Alex Klein1699fab2022-09-08 08:46:06 -0600916 Args:
917 image: The path to a local Chromium OS image.
918 """
919 super().__init__()
920 self._image = image
921
922 def run(self):
923 """Generates the stateful update and writes it into the output pipe."""
924 try:
925 paygen_stateful_payload_lib.GenerateStatefulPayload(
926 self._image, self._Source()
927 )
928 finally:
929 self._CloseSource()
Amin Hassani74403082021-02-22 11:40:09 -0800930
931
932class StatefulUpdater(PartitionUpdaterBase):
Alex Klein1699fab2022-09-08 08:46:06 -0600933 """A class to update the stateful partition on a device."""
Amin Hassani74403082021-02-22 11:40:09 -0800934
Alex Klein1699fab2022-09-08 08:46:06 -0600935 def __init__(self, clobber_stateful: bool, *args):
936 """Initializes the class
Amin Hassani74403082021-02-22 11:40:09 -0800937
Alex Klein1699fab2022-09-08 08:46:06 -0600938 Args:
939 clobber_stateful: Whether to clobber the stateful or not.
940 *args: Look at PartitionUpdaterBase.
941 """
942 super().__init__(*args)
943 self._clobber_stateful = clobber_stateful
Amin Hassani74403082021-02-22 11:40:09 -0800944
Alex Klein1699fab2022-09-08 08:46:06 -0600945 def _Run(self):
946 """Reads/Downloads the stateful updates and writes it into the device."""
947 if self._image_type == ImageType.FULL:
948 generator_cls = StatefulPayloadGenerator
949 elif self._image_type == ImageType.REMOTE_DIRECTORY:
950 generator_cls = GsFileCopier
951 self._image = os.path.join(
952 self._image, paygen_stateful_payload_lib.STATEFUL_FILE
953 )
954 else:
955 raise ValueError(f"Invalid image type {self._image_type}")
Amin Hassani74403082021-02-22 11:40:09 -0800956
Alex Klein1699fab2022-09-08 08:46:06 -0600957 with generator_cls(self._image) as generator:
958 try:
959 updater = stateful_updater.StatefulUpdater(self._device)
960 updater.Update(
961 generator.Target(),
962 is_payload_on_device=False,
963 update_type=(
964 stateful_updater.StatefulUpdater.UPDATE_TYPE_CLOBBER
965 if self._clobber_stateful
966 else None
967 ),
968 )
969 finally:
970 generator.CloseTarget()
971
972 def Revert(self):
973 """Reverts the stateful partition update."""
974 logging.info("Reverting the stateful update.")
975 stateful_updater.StatefulUpdater(self._device).Reset()
Amin Hassani55970562021-02-22 20:49:13 -0800976
977
978class ProgressWatcher(threading.Thread):
Alex Klein1699fab2022-09-08 08:46:06 -0600979 """A class used for watching the progress of rootfs update."""
Amin Hassani55970562021-02-22 20:49:13 -0800980
Alex Klein1699fab2022-09-08 08:46:06 -0600981 def __init__(self, device, target_root: str):
982 """Initializes the class.
Amin Hassani55970562021-02-22 20:49:13 -0800983
Alex Klein1699fab2022-09-08 08:46:06 -0600984 Args:
985 device: The ChromiumOSDevice to be updated.
986 target_root: The target root partition to monitor the progress of.
987 """
988 super().__init__()
Amin Hassani55970562021-02-22 20:49:13 -0800989
Alex Klein1699fab2022-09-08 08:46:06 -0600990 self._device = device
991 self._target_root = target_root
992 self._exit = False
Amin Hassani55970562021-02-22 20:49:13 -0800993
Alex Klein1699fab2022-09-08 08:46:06 -0600994 def __enter__(self):
995 """Starts the thread."""
996 self.start()
997 return self
Amin Hassani55970562021-02-22 20:49:13 -0800998
Alex Klein1699fab2022-09-08 08:46:06 -0600999 def __exit__(self, *args, **kwargs):
1000 """Exists the thread."""
1001 self._exit = True
1002 self.join()
Amin Hassani55970562021-02-22 20:49:13 -08001003
Alex Klein1699fab2022-09-08 08:46:06 -06001004 def _ShouldExit(self):
1005 return self._exit
Amin Hassani55970562021-02-22 20:49:13 -08001006
Alex Klein1699fab2022-09-08 08:46:06 -06001007 def run(self):
1008 """Monitors the progress of the target root partitions' update.
Amin Hassani55970562021-02-22 20:49:13 -08001009
Alex Klein1699fab2022-09-08 08:46:06 -06001010 This is done by periodically, reading the fd position of the process that is
1011 writing into the target partition and reporting it back. Then the position
1012 is divided by the size of the block device to report an approximate
1013 progress.
1014 """
1015 cmd = ["blockdev", "--getsize64", self._target_root]
Amin Hassani55970562021-02-22 20:49:13 -08001016 output = self._device.run(cmd, capture_output=True).stdout.strip()
Alex Klein1699fab2022-09-08 08:46:06 -06001017 if output is None:
1018 raise Error(f"Cannot get the block device size from {output}.")
1019 dev_size = int(output)
1020
1021 # Using lsof to find out which process is writing to the target rootfs.
1022 cmd = ["lsof", "-t", self._target_root]
1023 while not self._ShouldExit():
1024 try:
1025 pid = self._device.run(cmd, capture_output=True).stdout.strip()
1026 if pid:
1027 break
1028 except cros_build_lib.RunCommandError:
1029 continue
1030 finally:
1031 time.sleep(1)
1032
1033 # Now that we know which process is writing to it, we can look the fdinfo of
1034 # stdout of that process to get its offset. We're assuming there will be no
1035 # seek, which is correct.
1036 cmd = ["cat", f"/proc/{pid}/fdinfo/1"]
1037 while not self._ShouldExit():
1038 try:
1039 output = self._device.run(
1040 cmd, capture_output=True
1041 ).stdout.strip()
1042 m = re.search(r"^pos:\s*(\d+)$", output, flags=re.M)
1043 if m:
1044 offset = int(m.group(1))
1045 # DeviceImagerOperation will look for this log.
1046 logging.info("RootFS progress: %f", offset / dev_size)
1047 except cros_build_lib.RunCommandError:
1048 continue
1049 finally:
1050 time.sleep(1)
Amin Hassani55970562021-02-22 20:49:13 -08001051
1052
1053class DeviceImagerOperation(operation.ProgressBarOperation):
Alex Klein1699fab2022-09-08 08:46:06 -06001054 """A class to provide a progress bar for DeviceImager operation."""
Amin Hassani55970562021-02-22 20:49:13 -08001055
Alex Klein1699fab2022-09-08 08:46:06 -06001056 def __init__(self):
1057 """Initializes the class."""
1058 super().__init__()
Amin Hassani55970562021-02-22 20:49:13 -08001059
Alex Klein1699fab2022-09-08 08:46:06 -06001060 self._progress = 0.0
Amin Hassani55970562021-02-22 20:49:13 -08001061
Alex Klein1699fab2022-09-08 08:46:06 -06001062 def ParseOutput(self, output=None):
1063 """Override function to parse the output and provide progress.
Amin Hassani55970562021-02-22 20:49:13 -08001064
Alex Klein1699fab2022-09-08 08:46:06 -06001065 Args:
1066 output: The stderr or stdout.
1067 """
1068 output = self._stdout.read()
1069 match = re.findall(r"RootFS progress: (\d+(?:\.\d+)?)", output)
1070 if match:
1071 progress = float(match[0])
1072 self._progress = max(self._progress, progress)
Amin Hassani55970562021-02-22 20:49:13 -08001073
Alex Klein1699fab2022-09-08 08:46:06 -06001074 # If postinstall completes, move half of the remaining progress.
1075 if re.findall(r"Postinstall completed", output):
1076 self._progress += (1.0 - self._progress) / 2
Amin Hassani55970562021-02-22 20:49:13 -08001077
Alex Klein1699fab2022-09-08 08:46:06 -06001078 # While waiting for reboot, each time, move half of the remaining progress.
1079 if re.findall(r"Unable to get new boot_id", output):
1080 self._progress += (1.0 - self._progress) / 2
Amin Hassani55970562021-02-22 20:49:13 -08001081
Alex Klein1699fab2022-09-08 08:46:06 -06001082 if re.findall(r"DeviceImager completed.", output):
1083 self._progress = 1.0
Amin Hassani55970562021-02-22 20:49:13 -08001084
Alex Klein1699fab2022-09-08 08:46:06 -06001085 self.ProgressBar(self._progress)