OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/python2.6 | |
2 | |
3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | |
4 # Use of this source code is governed by a BSD-style license that can be | |
5 # found in the LICENSE file. | |
6 | |
7 """This module is responsible for generate a stateful update payload.""" | |
8 | |
9 import logging | |
10 import optparse | |
11 import os | |
12 import subprocess | |
13 import tempfile | |
14 | |
15 STATEFUL_FILE = 'stateful.tgz' | |
16 | |
17 | |
18 def GenerateStatefulPayload(image_path, output_directory, logger): | |
19 """Generates a stateful update payload given a full path to an image. | |
20 | |
21 Args: | |
22 image_path: Path to the image. | |
23 output_directory: Path to the directory to leave the resulting output. | |
24 logger: logging instance. | |
25 """ | |
26 logger.info('Generating stateful update file.') | |
27 from_dir = os.path.dirname(image_path) | |
28 image = os.path.basename(image_path) | |
29 output_gz = os.path.join(output_directory, STATEFUL_FILE) | |
30 crosutils_dir = os.path.dirname(__file__) | |
31 | |
32 # Temporary directories for this function. | |
33 rootfs_dir = tempfile.mkdtemp(suffix='rootfs', prefix='tmp') | |
34 stateful_dir = tempfile.mkdtemp(suffix='stateful', prefix='tmp') | |
35 | |
36 # Mount the image to pull out the important directories. | |
37 try: | |
38 # Only need stateful partition, but this saves us having to manage our | |
39 # own loopback device. | |
40 subprocess.check_call(['%s/mount_gpt_image.sh' % crosutils_dir, | |
DaleCurtis
2010/11/10 22:41:18
Will this take in stateful.image or just chromiumo
sosa
2010/11/10 23:31:30
This requires the image. This is probably the bes
| |
41 '--from=%s' % from_dir, | |
42 '--image=%s' % image, | |
43 '--read_only', | |
44 '--rootfs_mountpt=%s' % rootfs_dir, | |
45 '--stateful_mountpt=%s' % stateful_dir, | |
46 ]) | |
47 logger.info('Tarring up /usr/local and /var!') | |
48 subprocess.check_call(['sudo', | |
DaleCurtis
2010/11/10 22:41:18
Why /var?
sosa
2010/11/10 23:31:30
Gmerge / portage
On 2010/11/10 22:41:18, dalec wr
| |
49 'tar', | |
50 '-czf', | |
51 output_gz, | |
52 '--directory=%s' % stateful_dir, | |
53 '--transform=s,^dev_image,dev_image_new,', | |
54 '--transform=s,^var,var_new,', | |
55 'dev_image', | |
56 'var', | |
57 ]) | |
58 except: | |
59 logger.error('Failed to create stateful update file') | |
60 raise | |
61 finally: | |
62 # Unmount best effort regardless. | |
63 subprocess.call(['%s/mount_gpt_image.sh' % crosutils_dir, | |
64 '--unmount', | |
65 '--rootfs_mountpt=%s' % rootfs_dir, | |
66 '--stateful_mountpt=%s' % stateful_dir, | |
67 ]) | |
68 # Clean up our directories. | |
69 os.rmdir(rootfs_dir) | |
70 os.rmdir(stateful_dir) | |
71 | |
72 logger.info('Successfully generated %s' % output_gz) | |
73 | |
74 | |
75 def main(): | |
76 logging.basicConfig(level=logging.INFO) | |
77 logger = logging.getLogger(os.path.basename(__file__)) | |
78 parser = optparse.OptionParser() | |
79 parser.add_option('-i', '--image_path', | |
DaleCurtis
2010/11/10 22:41:18
Nit. Sync option names with cros_generate_update_p
sosa
2010/11/10 23:31:30
I'd like the ability to change the type of the pay
DaleCurtis
2010/11/11 00:03:46
Can you elaborate more? I don't understand how let
| |
80 help='The image to generate the stateful update for.') | |
81 parser.add_option('-o', '--output_dir', | |
82 help='The path to the directory to output the update file.') | |
83 parser.set_usage(parser.format_help()) | |
84 (options, unused_args) = parser.parse_args() | |
DaleCurtis
2010/11/10 22:41:18
Extra parens.
sosa
2010/11/10 23:31:30
Done.
| |
85 if not options.image_path: | |
86 parser.error('Missing image for stateful payload generator') | |
87 if not options.output_dir: | |
88 parser.error('Missing output directory for the payload generator') | |
89 | |
90 GenerateStatefulPayload(options.image_path, options.output_dir, logger) | |
91 | |
92 | |
93 if __name__ == '__main__': | |
94 main() | |
OLD | NEW |