1# Copyright 2015 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5"""This tool can be used to set up a base container for test. For example, 6 python lxc.py -s -p /tmp/container 7This command will download and setup base container in directory /tmp/container. 8After that command finishes, you can run lxc command to work with the base 9container, e.g., 10 lxc-start -P /tmp/container -n base -d 11 lxc-attach -P /tmp/container -n base 12""" 13 14import argparse 15import logging 16 17import common 18from autotest_lib.client.bin import utils 19from autotest_lib.site_utils import lxc 20from autotest_lib.site_utils.lxc import base_image 21 22 23def parse_options(): 24 """Parse command line inputs. 25 26 @raise argparse.ArgumentError: If command line arguments are invalid. 27 """ 28 parser = argparse.ArgumentParser() 29 parser.add_argument('-s', '--setup', action='store_true', 30 default=False, 31 help='Set up base container.') 32 parser.add_argument('-p', 33 '--path', 34 type=str, 35 help='Directory to store the container.', 36 default=lxc.DEFAULT_BASE_CONTAINER_PATH) 37 parser.add_argument('-f', '--force_delete', action='store_true', 38 default=False, 39 help=('Force to delete existing containers and rebuild ' 40 'base containers.')) 41 parser.add_argument('-n', '--name', type=str, 42 help='Name of the base container.', 43 default=lxc.BASE) 44 options = parser.parse_args() 45 if not options.setup and not options.force_delete: 46 raise argparse.ArgumentError( 47 'Use --setup to setup a base container, or --force_delete to ' 48 'delete all containers in given path.') 49 return options 50 51 52def main(): 53 """main script.""" 54 # Force to run the setup as superuser. 55 # TODO(dshi): crbug.com/459344 Set remove this enforcement when test 56 # container can be unprivileged container. 57 if utils.sudo_require_password(): 58 logging.warning('SSP requires root privilege to run commands, please ' 59 'grant root access to this process.') 60 utils.run('sudo true') 61 62 options = parse_options() 63 image = base_image.BaseImage(options.path, lxc.BASE) 64 if options.setup: 65 image.setup(name=options.name, force_delete=options.force_delete) 66 elif options.force_delete: 67 image.cleanup() 68 69 70if __name__ == '__main__': 71 main() 72