Tudor Brindus | 0086f7a | 2018-07-23 16:33:13 -0700 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """This module is responsible for generating a stateful update payload.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import os |
| 11 | |
| 12 | from chromite.lib import constants |
| 13 | from chromite.lib import commandline |
| 14 | from chromite.lib import cros_build_lib |
| 15 | from chromite.lib import cros_logging as logging |
| 16 | from chromite.lib import osutils |
| 17 | |
| 18 | |
| 19 | _STATEFUL_FILE = 'stateful.tgz' |
| 20 | |
| 21 | |
| 22 | # TODO(tbrindus): move to paygen. |
| 23 | def GenerateStatefulPayload(image_path, output_directory): |
| 24 | """Generates a stateful update payload given a full path to an image. |
| 25 | |
| 26 | Args: |
| 27 | image_path: Full path to the image. |
| 28 | output_directory: Path to the directory to leave the resulting output. |
| 29 | logging: logging instance. |
| 30 | """ |
| 31 | logging.info('Generating stateful update file.') |
| 32 | |
| 33 | output_gz = os.path.join(output_directory, _STATEFUL_FILE) |
| 34 | |
Tudor Brindus | 0086f7a | 2018-07-23 16:33:13 -0700 | [diff] [blame] | 35 | # Mount the image to pull out the important directories. |
| 36 | with osutils.TempDir() as stateful_mnt, \ |
LaMont Jones | ea6dda6 | 2018-12-04 16:11:45 -0700 | [diff] [blame] | 37 | osutils.MountImageContext(image_path, stateful_mnt, |
| 38 | (constants.PART_STATE,)) as _: |
Tudor Brindus | 0086f7a | 2018-07-23 16:33:13 -0700 | [diff] [blame] | 39 | stateful_dir = os.path.join(stateful_mnt, 'dir-%s' % constants.PART_STATE) |
| 40 | |
| 41 | try: |
| 42 | logging.info('Tarring up /usr/local and /var!') |
| 43 | cros_build_lib.SudoRunCommand([ |
| 44 | 'tar', |
| 45 | '-czf', |
| 46 | output_gz, |
| 47 | '--directory=%s' % stateful_dir, |
| 48 | '--hard-dereference', |
| 49 | '--transform=s,^dev_image,dev_image_new,', |
| 50 | '--transform=s,^var_overlay,var_new,', |
| 51 | 'dev_image', |
| 52 | 'var_overlay', |
| 53 | ]) |
| 54 | except: |
| 55 | logging.error('Failed to create stateful update file') |
| 56 | raise |
| 57 | |
| 58 | logging.info('Successfully generated %s', output_gz) |
| 59 | |
| 60 | |
| 61 | def ParseArguments(argv): |
| 62 | """Returns a namespace for the CLI arguments.""" |
| 63 | parser = commandline.ArgumentParser(description=__doc__) |
| 64 | parser.add_argument('-i', '--image_path', type='path', required=True, |
| 65 | help='The image to generate the stateful update for.') |
| 66 | parser.add_argument('-o', '--output_dir', type='path', required=True, |
| 67 | help='The path to the directory to output the stateful' |
| 68 | 'update file.') |
| 69 | opts = parser.parse_args(argv) |
| 70 | opts.Freeze() |
| 71 | |
| 72 | return opts |
| 73 | |
| 74 | |
| 75 | def main(argv): |
| 76 | opts = ParseArguments(argv) |
| 77 | |
| 78 | GenerateStatefulPayload(opts.image_path, opts.output_dir) |