blob: c48fb85a1ebde26163040533c0db7518d2d2ad08 [file] [log] [blame]
# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""This module is responsible for generate a stateful update payload."""
import logging
import optparse
import os
import sys
import subprocess
import tempfile
STATEFUL_FILE = 'stateful.tgz'
def GenerateStatefulPayload(image_path, output_directory, logger):
"""Generates a stateful update payload given a full path to an image.
image_path: Full path to the image.
output_directory: Path to the directory to leave the resulting output.
logger: logging instance.
"""'Generating stateful update file.')
from_dir = os.path.dirname(image_path)
image = os.path.basename(image_path)
output_gz = os.path.join(output_directory, STATEFUL_FILE)
# TODO(zbehan): This is only used for That script also
# needs to move away from src/scripts.
crosutils_dir = '%s/src/scripts' % os.environ['CROS_WORKON_SRCROOT']
except KeyError:
# Wow, we're outside the chroot. There's no clean way to do this, but we
# need to find out where sourceroot is.
# The two places where we may be running is platform/dev/, or /usr/bin/.
# Coincidentally, both are just a ../../../ away from sourceroot. Other
# locations for this script will just not work, and long term, we want
# exactly one anyway. (/usr/bin/)
self_path = os.path.dirname(sys.argv[0])
crosutils_dir = os.path.realpath(
os.path.join(self_path, '../../..', 'src/scripts'))
# Temporary directories for this function.
rootfs_dir = tempfile.mkdtemp(suffix='rootfs', prefix='tmp')
stateful_dir = tempfile.mkdtemp(suffix='stateful', prefix='tmp')
# Mount the image to pull out the important directories.
# Only need stateful partition, but this saves us having to manage our
# own loopback device.
subprocess.check_call(['%s/' % crosutils_dir,
'--from=%s' % from_dir,
'--image=%s' % image,
'--rootfs_mountpt=%s' % rootfs_dir,
'--stateful_mountpt=%s' % stateful_dir,
])'Tarring up /usr/local and /var!')
'--directory=%s' % stateful_dir,
logger.error('Failed to create stateful update file')
# Unmount best effort regardless.['%s/' % crosutils_dir,
'--rootfs_mountpt=%s' % rootfs_dir,
'--stateful_mountpt=%s' % stateful_dir,
# Clean up our directories.
os.rmdir(stateful_dir)'Successfully generated %s' % output_gz)
def main():
logger = logging.getLogger(os.path.basename(__file__))
parser = optparse.OptionParser()
parser.add_option('-i', '--image_path',
help='The image to generate the stateful update for.')
parser.add_option('-o', '--output_dir',
help='The path to the directory to output the update file.')
options, unused_args = parser.parse_args()
if not options.image_path:
parser.error('Missing image for stateful payload generator')
if not options.output_dir:
parser.error('Missing output directory for the payload generator')
options.output_dir, logger)
if __name__ == '__main__':