blob: 51b11645f65941ad6e1da4217b0d3ee755943e04 [file] [log] [blame]
# Copyright 2015 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""This tool can be used to set up a base container for test. For example,
python lxc.py -s -p /tmp/container
This command will download and setup base container in directory /tmp/container.
After that command finishes, you can run lxc command to work with the base
container, e.g.,
lxc-start -P /tmp/container -n base -d
lxc-attach -P /tmp/container -n base
"""
import argparse
import logging
import common
from autotest_lib.client.bin import utils
from autotest_lib.site_utils import lxc
from autotest_lib.site_utils.lxc import base_image
def parse_options():
"""Parse command line inputs.
@raise argparse.ArgumentError: If command line arguments are invalid.
"""
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--setup', action='store_true',
default=False,
help='Set up base container.')
parser.add_argument('-p',
'--path',
type=str,
help='Directory to store the container.',
default=lxc.DEFAULT_BASE_CONTAINER_PATH)
parser.add_argument('-f', '--force_delete', action='store_true',
default=False,
help=('Force to delete existing containers and rebuild '
'base containers.'))
parser.add_argument('-n', '--name', type=str,
help='Name of the base container.',
default=lxc.BASE)
options = parser.parse_args()
if not options.setup and not options.force_delete:
raise argparse.ArgumentError(
'Use --setup to setup a base container, or --force_delete to '
'delete all containers in given path.')
return options
def main():
"""main script."""
# Force to run the setup as superuser.
# TODO(dshi): crbug.com/459344 Set remove this enforcement when test
# container can be unprivileged container.
if utils.sudo_require_password():
logging.warning('SSP requires root privilege to run commands, please '
'grant root access to this process.')
utils.run('sudo true')
options = parse_options()
image = base_image.BaseImage(options.path, lxc.BASE)
if options.setup:
image.setup(name=options.name, force_delete=options.force_delete)
elif options.force_delete:
image.cleanup()
if __name__ == '__main__':
main()