1#!/usr/bin/python
2"""
3Program to help setup kvm test environment
4
5@copyright: Red Hat 2010
6"""
7
8import os, sys, logging, shutil, glob
9import common
10from autotest_lib.client.common_lib import logging_manager
11from autotest_lib.client.bin import utils
12from autotest_lib.client.virt import virt_utils
13
14
15def check_iso(url, destination, hash):
16    """
17    Verifies if ISO that can be find on url is on destination with right hash.
18
19    This function will verify the SHA1 hash of the ISO image. If the file
20    turns out to be missing or corrupted, let the user know we can download it.
21
22    @param url: URL where the ISO file can be found.
23    @param destination: Directory in local disk where we'd like the iso to be.
24    @param hash: SHA1 hash for the ISO image.
25    """
26    file_ok = False
27    if not destination:
28        os.makedirs(destination)
29    iso_path = os.path.join(destination, os.path.basename(url))
30    if not os.path.isfile(iso_path):
31        logging.warning("File %s not found", iso_path)
32        logging.warning("Expected SHA1 sum: %s", hash)
33        answer = utils.ask("Would you like to download it from %s?" % url)
34        if answer == 'y':
35            try:
36                utils.unmap_url_cache(destination, url, hash, method="sha1")
37                file_ok = True
38            except EnvironmentError, e:
39                logging.error(e)
40        else:
41            logging.warning("Missing file %s", iso_path)
42            logging.warning("Please download it or put an exsiting copy on the "
43                            "appropriate location")
44            return
45    else:
46        logging.info("Found %s", iso_path)
47        logging.info("Expected SHA1 sum: %s", hash)
48        answer = utils.ask("Would you like to check %s? It might take a while" %
49                           iso_path)
50        if answer == 'y':
51            try:
52                utils.unmap_url_cache(destination, url, hash, method="sha1")
53                file_ok = True
54            except EnvironmentError, e:
55                logging.error(e)
56        else:
57            logging.info("File %s present, but chose to not verify it",
58                         iso_path)
59            return
60
61    if file_ok:
62        logging.info("%s present, with proper checksum", iso_path)
63
64
65if __name__ == "__main__":
66    logging_manager.configure_logging(virt_utils.VirtLoggingConfig(),
67                                      verbose=True)
68    logging.info("KVM test config helper")
69
70    logging.info("")
71    logging.info("1 - Verifying directories (check if the directory structure "
72                 "expected by the default test config is there)")
73    base_dir = "/tmp/kvm_autotest_root"
74    sub_dir_list = ["images", "isos", "steps_data"]
75    for sub_dir in sub_dir_list:
76        sub_dir_path = os.path.join(base_dir, sub_dir)
77        if not os.path.isdir(sub_dir_path):
78            logging.debug("Creating %s", sub_dir_path)
79            os.makedirs(sub_dir_path)
80        else:
81            logging.debug("Dir %s exists, not creating" %
82                          sub_dir_path)
83    logging.info("")
84    logging.info("2 - Creating config files from samples (copy the default "
85                 "config samples to actual config files)")
86    kvm_test_dir = os.path.dirname(sys.modules[__name__].__file__)
87    kvm_test_dir = os.path.abspath(kvm_test_dir)
88    config_file_list = glob.glob(os.path.join(kvm_test_dir, "*.cfg.sample"))
89    for config_file in config_file_list:
90        src_file = config_file
91        dst_file = config_file.rstrip(".sample")
92        if not os.path.isfile(dst_file):
93            logging.debug("Creating config file %s from sample", dst_file)
94            shutil.copyfile(src_file, dst_file)
95        else:
96            logging.debug("Config file %s exists, not touching" % dst_file)
97
98    logging.info("")
99    logging.info("3 - Verifying iso (make sure we have the OS ISO needed for "
100                 "the default test set)")
101
102    iso_name = "Fedora-15-x86_64-DVD.iso"
103    fedora_dir = "pub/fedora/linux/releases/15/Fedora/x86_64/iso"
104    url = os.path.join("http://download.fedoraproject.org/", fedora_dir,
105                       iso_name)
106    hash = "61b3407f62bac22d3a3b2e919c7fc960116012d7"
107    destination = os.path.join(base_dir, 'isos', 'linux')
108    path = os.path.join(destination, iso_name)
109    check_iso(url, destination, hash)
110
111    logging.info("")
112    logging.info("4 - Verifying winutils.iso (make sure we have the utility "
113                 "ISO needed for Windows testing)")
114
115    logging.info("In order to run the KVM autotests in Windows guests, we "
116                 "provide you an ISO that this script can download")
117
118    url = "http://people.redhat.com/mrodrigu/kvm/winutils.iso"
119    hash = "02930224756510e383c44c49bffb760e35d6f892"
120    destination = os.path.join(base_dir, 'isos', 'windows')
121    path = os.path.join(destination, iso_name)
122    check_iso(url, destination, hash)
123
124    logging.info("")
125    logging.info("5 - Checking if qemu is installed (certify qemu and qemu-kvm "
126                 "are in the place the default config expects)")
127    qemu_default_paths = ['/usr/bin/qemu-kvm', '/usr/bin/qemu-img']
128    for qemu_path in qemu_default_paths:
129        if not os.path.isfile(qemu_path):
130            logging.warning("No %s found. You might need to install qemu-kvm.",
131                            qemu_path)
132        else:
133            logging.debug("%s present", qemu_path)
134    logging.info("If you wish to change qemu-kvm to qemu or other binary path, "
135                 "you will have to modify tests.cfg")
136
137    logging.info("")
138    logging.info("6 - Checking for the KVM module (make sure kvm is loaded "
139                 "to accelerate qemu-kvm)")
140    if not utils.module_is_loaded("kvm"):
141        logging.warning("KVM module is not loaded. You might want to load it")
142    else:
143        logging.debug("KVM module loaded")
144
145    logging.info("")
146    logging.info("7 - Verify needed packages to get started")
147    logging.info("Please take a look at the online documentation "
148                 "http://www.linux-kvm.org/page/KVM-Autotest/Client_Install "
149                 "(session 'Install Prerequisite packages')")
150
151    client_dir = os.path.abspath(os.path.join(kvm_test_dir, "..", ".."))
152    autotest_bin = os.path.join(client_dir, 'bin', 'autotest')
153    control_file = os.path.join(kvm_test_dir, 'control')
154
155    logging.info("")
156    logging.info("When you are done fixing eventual warnings found, "
157                 "you can run the kvm test using the command line AS ROOT:")
158    logging.info("%s %s", autotest_bin, control_file)
159    logging.info("Autotest prints the results dir, so you can look at DEBUG "
160                 "logs if something went wrong")
161    logging.info("You can also edit the test config files (see output of "
162                 "step 2 for a list)")
163