blob: 8595163efbe7e99dd4b48ccb60af69a1594a3075 [file] [log] [blame]
Dan Shi767dced2015-02-01 00:21:07 -08001# 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
Ben Kwa966db082017-06-05 14:17:23 -07005"""This tool can be used to set up a base container for test. For example,
Dan Shi767dced2015-02-01 00:21:07 -08006 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
Dan Shi767dced2015-02-01 00:21:07 -080014import argparse
15import logging
Dan Shi767dced2015-02-01 00:21:07 -080016
17import common
18from autotest_lib.client.bin import utils
Ben Kwa966db082017-06-05 14:17:23 -070019from autotest_lib.site_utils import lxc
Dan Shi767dced2015-02-01 00:21:07 -080020
21
22def parse_options():
23 """Parse command line inputs.
24
25 @raise argparse.ArgumentError: If command line arguments are invalid.
26 """
27 parser = argparse.ArgumentParser()
28 parser.add_argument('-s', '--setup', action='store_true',
29 default=False,
30 help='Set up base container.')
31 parser.add_argument('-p', '--path', type=str,
32 help='Directory to store the container.',
Ben Kwa966db082017-06-05 14:17:23 -070033 default=lxc.DEFAULT_CONTAINER_PATH)
Dan Shi767dced2015-02-01 00:21:07 -080034 parser.add_argument('-f', '--force_delete', action='store_true',
35 default=False,
36 help=('Force to delete existing containers and rebuild '
37 'base containers.'))
Dan Shi2a0f61e2016-06-21 14:47:21 -070038 parser.add_argument('-n', '--name', type=str,
39 help='Name of the base container.',
Ben Kwa966db082017-06-05 14:17:23 -070040 default=lxc.BASE)
Dan Shi767dced2015-02-01 00:21:07 -080041 options = parser.parse_args()
42 if not options.setup and not options.force_delete:
43 raise argparse.ArgumentError(
44 'Use --setup to setup a base container, or --force_delete to '
45 'delete all containers in given path.')
46 return options
47
48
49def main():
50 """main script."""
Dan Shica3be482015-05-05 23:23:53 -070051 # Force to run the setup as superuser.
52 # TODO(dshi): crbug.com/459344 Set remove this enforcement when test
53 # container can be unprivileged container.
54 if utils.sudo_require_password():
55 logging.warn('SSP requires root privilege to run commands, please '
56 'grant root access to this process.')
57 utils.run('sudo true')
58
Dan Shi767dced2015-02-01 00:21:07 -080059 options = parse_options()
Ben Kwad48cbcb2017-08-25 08:59:21 -070060 image = lxc.BaseImage(container_path=options.path)
Dan Shi767dced2015-02-01 00:21:07 -080061 if options.setup:
Ben Kwad48cbcb2017-08-25 08:59:21 -070062 image.setup(name=options.name, force_delete=options.force_delete)
Dan Shi767dced2015-02-01 00:21:07 -080063 elif options.force_delete:
Ben Kwad48cbcb2017-08-25 08:59:21 -070064 image.cleanup()
Dan Shi767dced2015-02-01 00:21:07 -080065
66
67if __name__ == '__main__':
68 main()