• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2# Copyright 2007 Google Inc. Released under the GPL v2
3
4"""
5This module defines the Bootloader class.
6
7        Bootloader: a program to boot Kernels on a Host.
8"""
9
10import os, weakref
11from autotest_lib.client.common_lib import error, boottool
12from autotest_lib.server import utils
13
14BOOTTOOL_SRC = '../client/tools/boottool'  # Get it from autotest client
15
16
17class Bootloader(boottool.boottool):
18    """
19    This class gives access to a host's bootloader services.
20
21    It can be used to add a kernel to the list of kernels that can be
22    booted by a bootloader. It can also make sure that this kernel will
23    be the one chosen at next reboot.
24    """
25
26    def __init__(self, host):
27        super(Bootloader, self).__init__()
28        self._host = weakref.ref(host)
29        self._boottool_path = None
30
31
32    def set_default(self, index):
33        if self._host().job:
34            self._host().job.last_boot_tag = None
35        super(Bootloader, self).set_default(index)
36
37
38    def boot_once(self, title):
39        if self._host().job:
40            self._host().job.last_boot_tag = title
41
42        super(Bootloader, self).boot_once(title)
43
44
45    def _install_boottool(self):
46        if self._host() is None:
47            raise error.AutoservError(
48                "Host does not exist anymore")
49        tmpdir = self._host().get_tmp_dir()
50        self._host().send_file(os.path.abspath(os.path.join(
51                utils.get_server_dir(), BOOTTOOL_SRC)), tmpdir)
52        self._boottool_path= os.path.join(tmpdir,
53                os.path.basename(BOOTTOOL_SRC))
54
55
56    def _get_boottool_path(self):
57        if not self._boottool_path:
58            self._install_boottool()
59        return self._boottool_path
60
61
62    def _run_boottool(self, *options):
63        cmd = self._get_boottool_path()
64        # FIXME: add unsafe options strings sequence to host.run() parameters
65        for option in options:
66            cmd += ' "%s"' % utils.sh_escape(option)
67        return self._host().run(cmd).stdout
68