Dan Shi | 767dced | 2015-02-01 00:21:07 -0800 | [diff] [blame] | 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 | |
Ben Kwa | 966db08 | 2017-06-05 14:17:23 -0700 | [diff] [blame] | 5 | """This tool can be used to set up a base container for test. For example, |
Dan Shi | 767dced | 2015-02-01 00:21:07 -0800 | [diff] [blame] | 6 | python lxc.py -s -p /tmp/container |
| 7 | This command will download and setup base container in directory /tmp/container. |
| 8 | After that command finishes, you can run lxc command to work with the base |
| 9 | container, e.g., |
| 10 | lxc-start -P /tmp/container -n base -d |
| 11 | lxc-attach -P /tmp/container -n base |
| 12 | """ |
| 13 | |
Dan Shi | 767dced | 2015-02-01 00:21:07 -0800 | [diff] [blame] | 14 | import argparse |
| 15 | import logging |
Dan Shi | 767dced | 2015-02-01 00:21:07 -0800 | [diff] [blame] | 16 | |
| 17 | import common |
| 18 | from autotest_lib.client.bin import utils |
Ben Kwa | 966db08 | 2017-06-05 14:17:23 -0700 | [diff] [blame] | 19 | from autotest_lib.site_utils import lxc |
Prathmesh Prabhu | f37bd46 | 2020-07-10 14:37:45 -0700 | [diff] [blame] | 20 | from autotest_lib.site_utils.lxc import base_image |
Dan Shi | 767dced | 2015-02-01 00:21:07 -0800 | [diff] [blame] | 21 | |
| 22 | |
| 23 | def 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', '--path', type=str, |
| 33 | help='Directory to store the container.', |
Ben Kwa | 966db08 | 2017-06-05 14:17:23 -0700 | [diff] [blame] | 34 | default=lxc.DEFAULT_CONTAINER_PATH) |
Dan Shi | 767dced | 2015-02-01 00:21:07 -0800 | [diff] [blame] | 35 | parser.add_argument('-f', '--force_delete', action='store_true', |
| 36 | default=False, |
| 37 | help=('Force to delete existing containers and rebuild ' |
| 38 | 'base containers.')) |
Dan Shi | 2a0f61e | 2016-06-21 14:47:21 -0700 | [diff] [blame] | 39 | parser.add_argument('-n', '--name', type=str, |
| 40 | help='Name of the base container.', |
Ben Kwa | 966db08 | 2017-06-05 14:17:23 -0700 | [diff] [blame] | 41 | default=lxc.BASE) |
Dan Shi | 767dced | 2015-02-01 00:21:07 -0800 | [diff] [blame] | 42 | options = parser.parse_args() |
| 43 | if not options.setup and not options.force_delete: |
| 44 | raise argparse.ArgumentError( |
Prathmesh Prabhu | f37bd46 | 2020-07-10 14:37:45 -0700 | [diff] [blame] | 45 | 'Use --setup to setup a base container, or --force_delete to ' |
| 46 | 'delete all containers in given path.') |
Dan Shi | 767dced | 2015-02-01 00:21:07 -0800 | [diff] [blame] | 47 | return options |
| 48 | |
| 49 | |
| 50 | def main(): |
| 51 | """main script.""" |
Dan Shi | ca3be48 | 2015-05-05 23:23:53 -0700 | [diff] [blame] | 52 | # Force to run the setup as superuser. |
| 53 | # TODO(dshi): crbug.com/459344 Set remove this enforcement when test |
| 54 | # container can be unprivileged container. |
| 55 | if utils.sudo_require_password(): |
| 56 | logging.warn('SSP requires root privilege to run commands, please ' |
| 57 | 'grant root access to this process.') |
| 58 | utils.run('sudo true') |
| 59 | |
Dan Shi | 767dced | 2015-02-01 00:21:07 -0800 | [diff] [blame] | 60 | options = parse_options() |
Prathmesh Prabhu | 59d62d7 | 2020-07-10 16:08:13 -0700 | [diff] [blame] | 61 | image = base_image.BaseImage(options.path, lxc.BASE) |
Dan Shi | 767dced | 2015-02-01 00:21:07 -0800 | [diff] [blame] | 62 | if options.setup: |
Ben Kwa | d48cbcb | 2017-08-25 08:59:21 -0700 | [diff] [blame] | 63 | image.setup(name=options.name, force_delete=options.force_delete) |
Dan Shi | 767dced | 2015-02-01 00:21:07 -0800 | [diff] [blame] | 64 | elif options.force_delete: |
Ben Kwa | d48cbcb | 2017-08-25 08:59:21 -0700 | [diff] [blame] | 65 | image.cleanup() |
Dan Shi | 767dced | 2015-02-01 00:21:07 -0800 | [diff] [blame] | 66 | |
| 67 | |
| 68 | if __name__ == '__main__': |
| 69 | main() |