blob: 94761540271f5d483b37c8433ea13033286763b0 [file] [log] [blame]
Tudor Brindus0086f7a2018-07-23 16:33:13 -07001# -*- 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
8from __future__ import print_function
9
10import os
11
12from chromite.lib import constants
13from chromite.lib import commandline
14from chromite.lib import cros_build_lib
15from chromite.lib import cros_logging as logging
16from chromite.lib import osutils
17
18
19_STATEFUL_FILE = 'stateful.tgz'
20
21
22# TODO(tbrindus): move to paygen.
23def 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
35 parts = cros_build_lib.GetImageDiskPartitionInfo(image_path)
36 stateful_num = parts[constants.PART_STATE].number
37
38 # Mount the image to pull out the important directories.
39 with osutils.TempDir() as stateful_mnt, \
40 osutils.MountImageContext(image_path, stateful_mnt, (stateful_num,)) as _:
41 stateful_dir = os.path.join(stateful_mnt, 'dir-%s' % constants.PART_STATE)
42
43 try:
44 logging.info('Tarring up /usr/local and /var!')
45 cros_build_lib.SudoRunCommand([
46 'tar',
47 '-czf',
48 output_gz,
49 '--directory=%s' % stateful_dir,
50 '--hard-dereference',
51 '--transform=s,^dev_image,dev_image_new,',
52 '--transform=s,^var_overlay,var_new,',
53 'dev_image',
54 'var_overlay',
55 ])
56 except:
57 logging.error('Failed to create stateful update file')
58 raise
59
60 logging.info('Successfully generated %s', output_gz)
61
62
63def ParseArguments(argv):
64 """Returns a namespace for the CLI arguments."""
65 parser = commandline.ArgumentParser(description=__doc__)
66 parser.add_argument('-i', '--image_path', type='path', required=True,
67 help='The image to generate the stateful update for.')
68 parser.add_argument('-o', '--output_dir', type='path', required=True,
69 help='The path to the directory to output the stateful'
70 'update file.')
71 opts = parser.parse_args(argv)
72 opts.Freeze()
73
74 return opts
75
76
77def main(argv):
78 opts = ParseArguments(argv)
79
80 GenerateStatefulPayload(opts.image_path, opts.output_dir)