Skip to contentSkip to conversation

Make all linux guests to use cdrom instead of floppy to hold answer file

PATCH New Lucas Meneghel Rodrigues 1 participant 3 messages Sep 21, 2010 KVM

lore
Share & tools
lore
#0 lore raw

cover Make all linux guests to use cdrom instead of floppy to hold answer file

This patchset contains a lot of much needed cleanups for the unattended install setup code. As the final user visible result, it is now possible to use both floppy as cdrom to hold the kickstart/autoyast file (cdrom default).

Lucas Meneghel Rodrigues (2):
KVM test: Cleaning up scripts/unattended.py
KVM test: Make linux guests to use cdrom to hold answer file

 client/tests/kvm/scripts/unattended.py |  649 +++++++++++++++++---------------
 client/tests/kvm/tests_base.cfg.sample |  304 ++++++++++-----
 2 files changed, 554 insertions(+), 399 deletions(-)
Signature · 1 line
1.7.2.3
#1 lore raw

PATCH 1/2 KVM test: Cleaning up scripts/unattended.py

Over the months, scripts/unattended.py received a lot of expansions, which made code difficult to read and understand. This patch gives some much needed cleanup to the code:

* Configuration key variables are directly translated to class attributes, making up for shorter, cleaner code
* All non callable, non-private attributes that are not empty are listed during setup(), allowing better debug and no need to manually print the values of the attributes anymore
* Make proper escaping of windows paths. Now users won't have to escape windows paths on virtio configuration by themselves.
* Removed functions that were not essential to the code's functionality

Also, fixed the configuration files to use all windows virtio paths unescaped.

---
 client/tests/kvm/scripts/unattended.py |  314 +++++++++++++-------------------
 client/tests/kvm/tests_base.cfg.sample |  114 +++++++++---
 2 files changed, 211 insertions(+), 217 deletions(-)
client/tests/kvm/scripts/unattended.py +125 -189
index a0f96e7..4c219cc 100755
@@ -3,49 +3,9 @@
 Simple script to setup unattended installs on KVM guests.
 """
 # -*- coding: utf-8 -*-
-import os, sys, shutil, tempfile, re, ConfigParser, glob
+import os, sys, shutil, tempfile, re, ConfigParser, glob, inspect
 import common
 
-KNOWN_PACKAGE_MANAGERS = ['rpm', 'dpkg']
-
-
-def command(cmd):
-    """
-    Find a given command on the user's PATH.
-
-    @param cmd: Command you wish to find (ie, 'ls').
-    @return: Full path to the command.
-    @raise ValueError: In case the command are not found on PATH.
-    """
-    for dir in os.environ['PATH'].split(':'):
-        file = os.path.join(dir, cmd)
-        if os.path.exists(file):
-            return file
-    raise ValueError('Missing command: %s' % cmd)
-
-
-def package_that_owns(file):
-    """
-    Inquiry the main package manager which package owns file.
-
-    @param file: Path to a given file
-    """
-    support_info = {}
-    for package_manager in KNOWN_PACKAGE_MANAGERS:
-        try:
-            command(package_manager)
-            support_info[package_manager] = True
-        except:
-            support_info[package_manager] = False
-
-    if support_info['rpm']:
-        return str(os.popen('rpm -qf %s' % file).read())
-    elif support_info['dpkg']:
-        return str(os.popen('dpkg -S %s' % file).read())
-    else:
-        return ('Did not find a known package manager to inquire about '
-                'file %s' % file)
-
 
 class SetupError(Exception):
     """
@@ -71,80 +31,60 @@ class UnattendedInstall(object):
         self.deps_dir = os.path.join(kvm_test_dir, 'deps')
         self.unattended_dir = os.path.join(kvm_test_dir, 'unattended')
 
-        tftp_root = os.environ.get('KVM_TEST_tftp', '')
-        if tftp_root:
-            self.tftp_root = os.path.join(kvm_test_dir, tftp_root)
-            if not os.path.isdir(self.tftp_root):
-                os.makedirs(self.tftp_root)
-        else:
-            self.tftp_root = tftp_root
-
-        self.kernel_args = os.environ.get('KVM_TEST_kernel_args', '')
-        self.finish_program = os.environ.get('KVM_TEST_finish_program', '')
-        cdrom_iso = os.environ.get('KVM_TEST_cdrom_cd1')
-        self.unattended_file = os.environ.get('KVM_TEST_unattended_file')
-
-        install_virtio = os.environ.get('KVM_TEST_install_virtio', 'no')
-        if install_virtio == 'yes':
-            self.install_virtio = True
-        else:
-            self.install_virtio = False
-
-        virtio_floppy = os.environ.get('KVM_TEST_virtio_floppy', '')
-        self.virtio_floppy = None
-        self.virtio_floppy_mount = tempfile.mkdtemp(prefix='floppy_',
-                                                    dir='/tmp')
-        if self.install_virtio:
-            if virtio_floppy and os.path.isfile(virtio_floppy):
-                print package_that_owns(virtio_floppy)
-                self.virtio_floppy = virtio_floppy
-            elif virtio_floppy and not os.path.isfile(virtio_floppy):
-                print ('Virtio floppy path %s was not found. Please verify' %
-                       virtio_floppy)
-
-        vni = os.environ.get('KVM_TEST_virtio_network_installer', '')
-        self.virtio_network_installer = None
-        if self.install_virtio and vni:
-            self.virtio_network_installer = vni
-
-        voi = os.environ.get('KVM_TEST_virtio_oemsetup_id', '')
-        self.virtio_oemsetup_identifier = None
-        if self.install_virtio and voi:
-            self.virtio_oemsetup_identifier = voi
-
-        vsp = os.environ.get('KVM_TEST_virtio_storage_path', '')
-        self.virtio_storage_path = None
-        if self.install_virtio and vsp:
-            self.virtio_storage_path = vsp
-
-        self.qemu_img_bin = os.environ.get('KVM_TEST_qemu_img_binary')
-        if not os.path.isabs(self.qemu_img_bin):
-            self.qemu_img_bin = os.path.join(kvm_test_dir, self.qemu_img_bin)
-        self.cdrom_iso = os.path.join(kvm_test_dir, cdrom_iso)
+        attributes = ['kernel_args', 'finish_program', 'cdrom_cd1',
+                      'unattended_file', 'medium', 'url', 'kernel', 'initrd',
+                      'nfs_server', 'nfs_dir', 'pxe_dir', 'pxe_image',
+                      'pxe_initrd', 'install_virtio', 'tftp', 'qemu_img_binary',
+                      'floppy']
+        for a in attributes:
+            self._setattr(a)
+
+        if self.install_virtio == 'yes':
+            v_attributes = ['virtio_floppy', 'virtio_storage_path',
+                            'virtio_network_path', 'virtio_oemsetup_id',
+                            'virtio_network_installer']
+            for va in v_attributes:
+                self._setattr(va)
+            self.virtio_floppy_mount = tempfile.mkdtemp(prefix='virtio_floppy_',
+                                                        dir='/tmp')
+
+        if self.tftp:
+            self.tftp = os.path.join(kvm_test_dir, self.tftp)
+            if not os.path.isdir(self.tftp):
+                os.makedirs(self.tftp)
+
+        if not os.path.isabs(self.qemu_img_binary):
+            self.qemu_img_binary = os.path.join(kvm_test_dir,
+                                                self.qemu_img_binary)
+
+        self.cdrom_cd1 = os.path.join(kvm_test_dir, self.cdrom_cd1)
         self.floppy_mount = tempfile.mkdtemp(prefix='floppy_', dir='/tmp')
         self.cdrom_mount = tempfile.mkdtemp(prefix='cdrom_', dir='/tmp')
-        self.nfs_mount = tempfile.mkdtemp(prefix='nfs_', dir='/tmp')
-        floppy_name = os.environ['KVM_TEST_floppy']
-        self.floppy_img = os.path.join(kvm_test_dir, floppy_name)
-        floppy_dir = os.path.dirname(self.floppy_img)
-        if not os.path.isdir(floppy_dir):
-            os.makedirs(floppy_dir)
-
-        self.pxe_dir = os.environ.get('KVM_TEST_pxe_dir', '')
-        self.pxe_image = os.environ.get('KVM_TEST_pxe_image', '')
-        self.pxe_initrd = os.environ.get('KVM_TEST_pxe_initrd', '')
-
-        self.medium = os.environ.get('KVM_TEST_medium', '')
-        self.url = os.environ.get('KVM_TEST_url', '')
-        self.kernel = os.environ.get('KVM_TEST_kernel', '')
-        self.initrd = os.environ.get('KVM_TEST_initrd', '')
-        self.nfs_server = os.environ.get('KVM_TEST_nfs_server', '')
-        self.nfs_dir = os.environ.get('KVM_TEST_nfs_dir', '')
+        if self.medium == 'nfs':
+            self.nfs_mount = tempfile.mkdtemp(prefix='nfs_', dir='/tmp')
+
+        self.floppy = os.path.join(kvm_test_dir, self.floppy)
+        if not os.path.isdir(os.path.dirname(self.floppy)):
+            os.makedirs(os.path.dirname(self.floppy))
+
         self.image_path = kvm_test_dir
         self.kernel_path = os.path.join(self.image_path, self.kernel)
         self.initrd_path = os.path.join(self.image_path, self.initrd)
 
 
+    def _setattr(self, key):
+        """
+        Populate class attributes with contents of environment variables.
+
+        Example: KVM_TEST_medium will populate self.medium.
+
+        @param key: Name of the class attribute we desire to have.
+        """
+        env_name = 'KVM_TEST_%s' % key
+        value = os.environ.get(env_name, '')
+        setattr(self, key, value)
+
+
     def copy_virtio_drivers_floppy(self):
         """
         Copy the virtio drivers on the virtio floppy to the install floppy.
@@ -231,20 +171,20 @@ class UnattendedInstall(object):
         """
         print "Creating boot floppy"
 
-        if os.path.exists(self.floppy_img):
-            os.remove(self.floppy_img)
+        if os.path.exists(self.floppy):
+            os.remove(self.floppy)
 
-        c_cmd = '%s create -f raw %s 1440k' % (self.qemu_img_bin,
-                                               self.floppy_img)
+        c_cmd = '%s create -f raw %s 1440k' % (self.qemu_img_binary,
+                                               self.floppy)
         if os.system(c_cmd):
             raise SetupError('Could not create floppy image.')
 
-        f_cmd = 'mkfs.msdos -s 1 %s' % self.floppy_img
+        f_cmd = 'mkfs.msdos -s 1 %s' % self.floppy
         if os.system(f_cmd):
             raise SetupError('Error formatting floppy image.')
 
         try:
-            m_cmd = 'mount -o loop,rw %s %s' % (self.floppy_img,
+            m_cmd = 'mount -o loop,rw %s %s' % (self.floppy,
                                                 self.floppy_mount)
             if os.system(m_cmd):
                 raise SetupError('Could not mount floppy image.')
@@ -255,17 +195,17 @@ class UnattendedInstall(object):
                 setup_file_path = os.path.join(self.unattended_dir, setup_file)
                 setup_file_dest = os.path.join(self.floppy_mount, setup_file)
                 shutil.copyfile(setup_file_path, setup_file_dest)
-                if self.install_virtio:
+                if self.install_virtio == "yes":
                     self.setup_virtio_win2003()
 
             elif self.unattended_file.endswith('.ks'):
                 # Red Hat kickstart install
                 dest_fname = 'ks.cfg'
             elif self.unattended_file.endswith('.xml'):
-                if  self.tftp_root is '':
+                if not self.tftp:
                     # Windows unattended install
                     dest_fname = "autounattend.xml"
-                    if self.install_virtio:
+                    if self.install_virtio == "yes":
                         self.setup_virtio_win2008()
                 else:
                     # SUSE autoyast install
@@ -293,50 +233,54 @@ class UnattendedInstall(object):
             elif self.medium == "url":
                 content = "url --url %s" % self.url
             elif self.medium == "nfs":
-                content = "nfs --server=%s --dir=%s" % (self.nfs_server, self.nfs_dir)
+                content = "nfs --server=%s --dir=%s" % (self.nfs_server,
+                                                        self.nfs_dir)
             else:
                 raise SetupError("Unexpected installation medium %s" % self.url)
 
-            unattended_contents = re.sub(dummy_medium_re, content, unattended_contents)
-
-            dummy_path = "C:"
-
-            dummy_storage_re = r'\bKVM_TEST_STORAGE_DRIVER_PATH\b'
-            storage_driver = os.environ.get('KVM_TEST_virtio_storage_path', '')
-            if re.search(dummy_storage_re, unattended_contents):
-                if self.install_virtio:
-                    unattended_contents = re.sub(dummy_storage_re,
-                                                 storage_driver,
-                                                 unattended_contents)
-                else:
-                    unattended_contents = re.sub(dummy_storage_re,
-                                                 dummy_path,
-                                                 unattended_contents)
-
-            dummy_network_re = r'\bKVM_TEST_NETWORK_DRIVER_PATH\b'
-            network_driver = os.environ.get('KVM_TEST_virtio_network_path', '')
-            if re.search(dummy_network_re, unattended_contents):
-                if self.install_virtio:
-                    unattended_contents = re.sub(dummy_network_re,
-                                                 network_driver,
-                                                 unattended_contents)
-                else:
-                    unattended_contents = re.sub(dummy_network_re,
-                                                 dummy_path,
-                                                 unattended_contents)
-
-            dummy_nw_install_re = r'\bKVM_TEST_VIRTIO_NETWORK_INSTALLER\b'
-            nw_install = os.environ.get('KVM_TEST_virtio_network_installer',
-                                        'help')
-            if re.search(dummy_nw_install_re, unattended_contents):
-                if self.install_virtio:
-                    unattended_contents = re.sub(dummy_nw_install_re,
-                                                 nw_install,
-                                                 unattended_contents)
-                else:
-                    unattended_contents = re.sub(dummy_nw_install_re,
-                                                 'help',
-                                                 unattended_contents)
+            unattended_contents = re.sub(dummy_medium_re, content,
+                                         unattended_contents)
+
+            def replace_virtio_key(contents, dummy_re, env):
+                """
+                Replace a virtio dummy string with contents.
+
+                If install_virtio is not set, replace it with a dummy string.
+
+                @param contents: Contents of the unattended file
+                @param dummy_re: Regular expression used to search on the.
+                        unattended file contents.
+                @param env: Name of the environment variable.
+                """
+                dummy_path = "C:"
+                driver = os.environ.get(env, '')
+
+                if re.search(dummy_re, contents):
+                    if self.install_virtio == "yes":
+                        if driver.endswith("msi"):
+                            driver = 'msiexec /passive /package ' + driver
+                        else:
+                            try:
+                                # Let's escape windows style paths properly
+                                drive, path = driver.split(":")
+                                driver = drive + ":" + re.escape(path)
+                            except:
+                                pass
+                        contents = re.sub(dummy_re, driver, contents)
+                    else:
+                        contents = re.sub(dummy_re, dummy_path, contents)
+                return contents
+
+            vdict = {r'\bKVM_TEST_STORAGE_DRIVER_PATH\b':
+                     'KVM_TEST_virtio_storage_path',
+                     r'\bKVM_TEST_NETWORK_DRIVER_PATH\b':
+                     'KVM_TEST_virtio_network_path',
+                     r'\bKVM_TEST_VIRTIO_NETWORK_INSTALLER\b':
+                     'KVM_TEST_virtio_network_installer_path'}
+
+            for vkey in vdict:
+                unattended_contents = replace_virtio_key(unattended_contents,
+                                                         vkey, vdict[vkey])
 
             print
             print "Unattended install %s contents:" % dest_fname
@@ -356,7 +300,7 @@ class UnattendedInstall(object):
                                  self.floppy_mount)
             self.cleanup(self.floppy_mount)
 
-        os.chmod(self.floppy_img, 0755)
+        os.chmod(self.floppy, 0755)
 
         print "Boot floppy created successfuly"
 
@@ -369,7 +313,7 @@ class UnattendedInstall(object):
         initrd.img files from the CD to a directory that qemu will serve trough
         TFTP to the VM.
         """
-        print "Setting up PXE boot using TFTP root %s" % self.tftp_root
+        print "Setting up PXE boot using TFTP root %s" % self.tftp
 
         pxe_file = None
         pxe_paths = ['/usr/lib/syslinux/pxelinux.0',
@@ -384,15 +328,15 @@ class UnattendedInstall(object):
                              'sure pxelinux or equivalent package for your '
                              'distro is installed.')
 
-        pxe_dest = os.path.join(self.tftp_root, 'pxelinux.0')
+        pxe_dest = os.path.join(self.tftp, 'pxelinux.0')
         shutil.copyfile(pxe_file, pxe_dest)
 
         try:
-            m_cmd = 'mount -t iso9660 -v -o loop,ro %s %s' % (self.cdrom_iso,
+            m_cmd = 'mount -t iso9660 -v -o loop,ro %s %s' % (self.cdrom_cd1,
                                                               self.cdrom_mount)
             if os.system(m_cmd):
                 raise SetupError('Could not mount CD image %s.' %
-                                 self.cdrom_iso)
+                                 self.cdrom_cd1)
 
             pxe_dir = os.path.join(self.cdrom_mount, self.pxe_dir)
             pxe_image = os.path.join(pxe_dir, self.pxe_image)
@@ -409,8 +353,8 @@ class UnattendedInstall(object):
                                  'or a initrd.img file. Cannot find a PXE '
                                  'image to proceed.' % self.pxe_dir)
 
-            tftp_image = os.path.join(self.tftp_root, 'vmlinuz')
-            tftp_initrd = os.path.join(self.tftp_root, 'initrd.img')
+            tftp_image = os.path.join(self.tftp, 'vmlinuz')
+            tftp_initrd = os.path.join(self.tftp, 'initrd.img')
             shutil.copyfile(pxe_image, tftp_image)
             shutil.copyfile(pxe_initrd, tftp_initrd)
 
@@ -421,7 +365,7 @@ class UnattendedInstall(object):
                                  self.cdrom_mount)
             self.cleanup(self.cdrom_mount)
 
-        pxe_config_dir = os.path.join(self.tftp_root, 'pxelinux.cfg')
+        pxe_config_dir = os.path.join(self.tftp, 'pxelinux.cfg')
         if not os.path.isdir(pxe_config_dir):
             os.makedirs(pxe_config_dir)
         pxe_config_path = os.path.join(pxe_config_dir, 'default')
@@ -461,13 +405,15 @@ class UnattendedInstall(object):
 
         print "Downloading finish"
 
+
     def setup_nfs(self):
         """
         Copy the vmlinuz and initrd.img from nfs.
         """
         print "Copying the vmlinuz and initrd.img from nfs"
 
-        m_cmd = "mount %s:%s %s -o ro" % (self.nfs_server, self.nfs_dir, self.nfs_mount)
+        m_cmd = "mount %s:%s %s -o ro" % (self.nfs_server, self.nfs_dir,
+                                          self.nfs_mount)
         if os.system(m_cmd):
             raise SetupError('Could not mount nfs server.')
 
@@ -491,6 +437,7 @@ class UnattendedInstall(object):
                 raise SetupError("Could not unmont nfs at %s" % self.nfs_mount)
             self.cleanup(self.nfs_mount)
 
+
     def cleanup(self, mount):
         """
         Clean up a previously used mountpoint.
@@ -507,31 +454,20 @@ class UnattendedInstall(object):
 
     def setup(self):
         print "Starting unattended install setup"
+        print
 
         print "Variables set:"
-        print "    medium: " + str(self.medium)
-        print "    qemu_img_bin: " + str(self.qemu_img_bin)
-        print "    cdrom iso: " + str(self.cdrom_iso)
-        print "    unattended_file: " + str(self.unattended_file)
-        print "    kernel_args: " + str(self.kernel_args)
-        print "    tftp_root: " + str(self.tftp_root)
-        print "    floppy_mount: " + str(self.floppy_mount)
-        print "    floppy_img: " + str(self.floppy_img)
-        print "    finish_program: " + str(self.finish_program)
-        print "    pxe_dir: " + str(self.pxe_dir)
-        print "    pxe_image: " + str(self.pxe_image)
-        print "    pxe_initrd: " + str(self.pxe_initrd)
-        print "    url: " + str(self.url)
-        print "    kernel: " + str(self.kernel)
-        print "    initrd: " + str(self.initrd)
-        print "    nfs_server: " + str(self.nfs_server)
-        print "    nfs_dir: " + str(self.nfs_dir)
-        print "    nfs_mount: " + str(self.nfs_mount)
-
-        if self.unattended_file and self.floppy_img is not None:
+        for member in inspect.getmembers(self):
+            name, value = member
+            attribute = getattr(self, name)
+            if not (name.startswith("__") or callable(attribute) or not value):
+                print "    %s: %s" % (name, value)
+        print
+
+        if self.unattended_file and self.floppy is not None:
             self.create_boot_floppy()
         if self.medium == "cdrom":
-            if self.tftp_root:
+            if self.tftp:
                 self.setup_pxe_boot()
         elif self.medium == "url":
             self.setup_url()
@@ -539,7 +475,7 @@ class UnattendedInstall(object):
             self.setup_nfs()
         else:
             raise SetupError("Unexpected installation method %s" %
-                                   self.medium)
+                             self.medium)
         print "Unattended install setup finished successfuly"
 
 
client/tests/kvm/tests_base.cfg.sample +86 -28

214 lines not on this page · raw message

Signature · 1 line
1.7.2.3
#2 lore raw

PATCH 2/2 KVM test: Make linux guests to use cdrom to hold answer file

In reply to Lucas Meneghel Rodrigues

Some versions of linux might have a bug in the floppy driver that can harm testing, due to guest kernel panics. So let's move the answer file (kickstart or autoyast file) to a cdrom, and use it during unattended install of linux guests.

* It supports both kickstart and autoyast, all linux distros able to use unattended install were ported to use cdroms.
* The code is flexible enough to allow going back to floppy, it's just a matter of tweaking the configuration file
* More refactoring and cleaning up of scripts/unattended.py - Now all Floppy or CDROM image handling is encapsulated in proper classes, making the boot disk creation stage much cleaner and easier to understand.

---
 client/tests/kvm/scripts/unattended.py |  543 +++++++++++++++++++-------------
 client/tests/kvm/tests_base.cfg.sample |  190 +++++++----
 2 files changed, 447 insertions(+), 286 deletions(-)
client/tests/kvm/scripts/unattended.py +326 -217
index 4c219cc..e624f5d 100755
@@ -7,6 +7,17 @@ import os, sys, shutil, tempfile, re, ConfigParser, glob, inspect
 import common
 
 
+SCRIPT_DIR = os.path.dirname(sys.modules[__name__].__file__)
+KVM_TEST_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, ".."))
+try:
+    QEMU_IMG_BINARY = os.environ['KVM_TEST_qemu_img_binary']
+except KeyError:
+    QEMU_IMG_BINARY = os.path.join(KVM_TEST_DIR, QEMU_IMG_BINARY)
+if not os.path.exists(QEMU_IMG_BINARY):
+    raise SetupError('The qemu-img binary that is supposed to be used (%s) '
+                     'does not exist. Please verify your configuration')
+
+
 class SetupError(Exception):
     """
     Simple wrapper for the builtin Exception class.
@@ -14,123 +25,125 @@ class SetupError(Exception):
     pass
 
 
-class UnattendedInstall(object):
+def cleanup(dir):
     """
-    Creates a floppy disk image that will contain a config file for unattended
-    OS install. Optionally, sets up a PXE install server using qemu built in
-    TFTP and DHCP servers to install a particular operating system. The
-    parameters to the script are retrieved from environment variables.
+    If dir is a mountpoint, do what is possible to unmount it. Afterwards,
+    try to remove it.
+
+    @param dir: Directory to be cleaned up.
+    """
+    print "Cleaning up directory %s" % dir
+    if os.path.ismount(dir):
+        f_cmd = 'fuser -k %s' % dir
+        os.system(f_cmd)
+        u_cmd = 'umount %s' % dir
+        if os.system(u_cmd):
+            raise SetupError('Could not unmount %s' % dir)
+    if os.path.isdir(dir):
+        shutil.rmtree(dir)
+
+
+def clean_old_image(image):
+    """
+    Clean a leftover image file from previous processes. If it contains a
+    mounted file system, do the proper cleanup procedures.
+
+    @param image: Path to image to be cleaned up.
+    """
+    if os.path.exists(image):
+        mtab = open('/etc/mtab', 'r')
+        mtab_contents = mtab.read()
+        mtab.close()
+        if image in mtab_contents:
+            os.system('fuser -k %s' % image)
+            os.system('umount %s' % image)
+        os.remove(image)
+
+
+class Disk(object):
+    """
+    Abstract class for Disk objects, with the common methods implemented.
     """
     def __init__(self):
-        """
-        Gets params from environment variables and sets class attributes.
-        """
-        script_dir = os.path.dirname(sys.modules[__name__].__file__)
-        kvm_test_dir = os.path.abspath(os.path.join(script_dir, ".."))
-        images_dir = os.path.join(kvm_test_dir, 'images')
-        self.deps_dir = os.path.join(kvm_test_dir, 'deps')
-        self.unattended_dir = os.path.join(kvm_test_dir, 'unattended')
+        self.path = None
 
-        attributes = ['kernel_args', 'finish_program', 'cdrom_cd1',
-                      'unattended_file', 'medium', 'url', 'kernel', 'initrd',
-                      'nfs_server', 'nfs_dir', 'pxe_dir', 'pxe_image',
-                      'pxe_initrd', 'install_virtio', 'tftp', 'qemu_img_binary',
-                      'floppy']
-        for a in attributes:
-            self._setattr(a)
 
-        if self.install_virtio == 'yes':
-            v_attributes = ['virtio_floppy', 'virtio_storage_path',
-                            'virtio_network_path', 'virtio_oemsetup_id',
-                            'virtio_network_installer']
-            for va in v_attributes:
-                self._setattr(va)
-            self.virtio_floppy_mount = tempfile.mkdtemp(prefix='virtio_floppy_',
-                                                        dir='/tmp')
+    def setup_answer_file(self, filename, contents):
+        answer_file = open(os.path.join(self.mount, filename), 'w')
+        answer_file.write(contents)
+        answer_file.close()
 
-        if self.tftp:
-            self.tftp = os.path.join(kvm_test_dir, self.tftp)
-            if not os.path.isdir(self.tftp):
-                os.makedirs(self.tftp)
 
-        if not os.path.isabs(self.qemu_img_binary):
-            self.qemu_img_binary = os.path.join(kvm_test_dir,
-                                                self.qemu_img_binary)
+    def copy_to(self, src):
+        dst = os.path.join(self.mount, os.path.basename(src))
+        if os.path.isdir(src):
+            shutil.copytree(src, dst)
+        elif os.path.isfile(src):
+            shutil.copyfile(src, dst)
 
-        self.cdrom_cd1 = os.path.join(kvm_test_dir, self.cdrom_cd1)
-        self.floppy_mount = tempfile.mkdtemp(prefix='floppy_', dir='/tmp')
-        self.cdrom_mount = tempfile.mkdtemp(prefix='cdrom_', dir='/tmp')
-        if self.medium == 'nfs':
-            self.nfs_mount = tempfile.mkdtemp(prefix='nfs_', dir='/tmp')
 
-        self.floppy = os.path.join(kvm_test_dir, self.floppy)
-        if not os.path.isdir(os.path.dirname(self.floppy)):
-            os.makedirs(os.path.dirname(self.floppy))
+    def close(self):
+        os.chmod(self.path, 0755)
+        cleanup(self.mount)
+        print "Disk %s successfuly set" % self.path
 
-        self.image_path = kvm_test_dir
-        self.kernel_path = os.path.join(self.image_path, self.kernel)
-        self.initrd_path = os.path.join(self.image_path, self.initrd)
 
+class FloppyDisk(Disk):
+    """
+    Represents a 1.44 MB floppy disk. We can copy files to it, and setup it in
+    convenient ways.
+    """
+    def __init__(self, path):
+        print "Creating floppy unattended image %s" % path
 
-    def _setattr(self, key):
-        """
-        Populate class attributes with contents of environment variables.
+        self.mount = tempfile.mkdtemp(prefix='floppy_', dir='/tmp')
+        self.virtio_mount = None
+        self.path = path
+        clean_old_image(path)
+        if not os.path.isdir(os.path.dirname(path)):
+            os.makedirs(os.path.dirname(path))
 
-        Example: KVM_TEST_medium will populate self.medium.
+        try:
+            c_cmd = '%s create -f raw %s 1440k' % (QEMU_IMG_BINARY,
+                                                   path)
+            if os.system(c_cmd):
+                raise SetupError('Could not create floppy image.')
 
-        @param key: Name of the class attribute we desire to have.
-        """
-        env_name = 'KVM_TEST_%s' % key
-        value = os.environ.get(env_name, '')
-        setattr(self, key, value)
+            f_cmd = 'mkfs.msdos -s 1 %s' % path
+            if os.system(f_cmd):
+                raise SetupError('Error formatting floppy image.')
+
+            m_cmd = 'mount -o loop,rw %s %s' % (path, self.mount)
+            if os.system(m_cmd):
+                raise SetupError('Could not mount floppy image.')
+        except:
+            cleanup(self.mount)
 
 
-    def copy_virtio_drivers_floppy(self):
+    def _copy_virtio_drivers(self, virtio_floppy):
         """
         Copy the virtio drivers on the virtio floppy to the install floppy.
 
         1) Mount the floppy containing the viostor drivers
         2) Copy its contents to the root of the install floppy
         """
-        m_cmd = 'mount -o loop %s %s' % (self.virtio_floppy,
-                                         self.virtio_floppy_mount)
+        virtio_mount = tempfile.mkdtemp(prefix='virtio_floppy_', dir='/tmp')
+
+        m_cmd = 'mount -o loop %s %s' % (virtio_floppy, virtio_mount)
         pwd = os.getcwd()
         try:
             if os.system(m_cmd):
                 raise SetupError('Could not mount virtio floppy driver')
-            os.chdir(self.virtio_floppy_mount)
+            os.chdir(virtio_mount)
             path_list = glob.glob('*')
             for path in path_list:
-                src = os.path.join(self.virtio_floppy_mount, path)
-                dst = os.path.join(self.floppy_mount, path)
-                if os.path.isdir(path):
-                    shutil.copytree(src, dst)
-                elif os.path.isfile(path):
-                    shutil.copyfile(src, dst)
+                self.copy_to(path)
         finally:
             os.chdir(pwd)
-            u_cmd = 'umount %s' % self.virtio_floppy_mount
-            if os.system(u_cmd):
-                raise SetupError('Could not unmount virtio floppy at %s' %
-                                 self.virtio_floppy_mount)
-            self.cleanup(self.virtio_floppy_mount)
-
-
-    def setup_virtio_win2008(self):
-        """
-        Setup the install floppy with the virtio storage drivers, win2008 style.
-
-        Win2008, Vista and 7 require people to point out the path to the drivers
-        on the unattended file, so we just need to copy the drivers to the
-        driver floppy disk.
-        Process:
-
-        1) Copy the virtio drivers on the virtio floppy to the install floppy
-        """
-        self.copy_virtio_drivers_floppy()
+            cleanup(virtio_mount)
 
 
-    def setup_virtio_win2003(self):
+    def setup_virtio_win2003(self, virtio_floppy):
         """
         Setup the install floppy with the virtio storage drivers, win2003 style.
 
@@ -144,8 +157,8 @@ class UnattendedInstall(object):
            executed on the config parser object
         4) Re-write the config file to the disk
         """
-        self.copy_virtio_drivers_floppy()
-        txtsetup_oem = os.path.join(self.floppy_mount, 'txtsetup.oem')
+        self._copy_virtio_drivers(virtio_floppy)
+        txtsetup_oem = os.path.join(self.mount, 'txtsetup.oem')
         if not os.path.isfile(txtsetup_oem):
             raise SetupError('File txtsetup.oem not found on the install '
                              'floppy. Please verify if your floppy virtio '
@@ -163,146 +176,237 @@ class UnattendedInstall(object):
             fp.close()
 
 
-    def create_boot_floppy(self):
+    def setup_virtio_win2008(self, virtio_floppy):
         """
-        Prepares a boot floppy by creating a floppy image file, mounting it and
-        copying an answer file (kickstarts for RH based distros, answer files
-        for windows) to it. After that the image is umounted.
+        Setup the install floppy with the virtio storage drivers, win2008 style.
+
+        Win2008, Vista and 7 require people to point out the path to the drivers
+        on the unattended file, so we just need to copy the drivers to the
+        driver floppy disk.
+        Process:
+
+        1) Copy the virtio drivers on the virtio floppy to the install floppy
         """
-        print "Creating boot floppy"
+        self._copy_virtio_drivers(virtio_floppy)
 
-        if os.path.exists(self.floppy):
-            os.remove(self.floppy)
 
-        c_cmd = '%s create -f raw %s 1440k' % (self.qemu_img_binary,
-                                               self.floppy)
-        if os.system(c_cmd):
-            raise SetupError('Could not create floppy image.')
+class CdromDisk(Disk):
+    """
+    Represents a CDROM disk that we can master according to our needs.
+    """
+    def __init__(self, path):
+        print "Creating ISO unattended image %s" % path
+        self.mount = tempfile.mkdtemp(prefix='cdrom_unattended', dir='/tmp')
+        self.path = path
+        clean_old_image(path)
+        if not os.path.isdir(os.path.dirname(path)):
+            os.makedirs(os.path.dirname(path))
 
-        f_cmd = 'mkfs.msdos -s 1 %s' % self.floppy
-        if os.system(f_cmd):
-            raise SetupError('Error formatting floppy image.')
 
-        try:
-            m_cmd = 'mount -o loop,rw %s %s' % (self.floppy,
-                                                self.floppy_mount)
-            if os.system(m_cmd):
-                raise SetupError('Could not mount floppy image.')
+    def close(self):
+        g_cmd = ('genisoimage -o %s -max-iso9660-filenames '
+                 '-relaxed-filenames -D --input-charset iso8859-1 '
+                 '%s' % (self.path, self.mount))
+        if os.system(g_cmd):
+            raise SetupError("Could not generate iso with answer file")
 
-            if self.unattended_file.endswith('.sif'):
-                dest_fname = 'winnt.sif'
-                setup_file = 'winnt.bat'
-                setup_file_path = os.path.join(self.unattended_dir, setup_file)
-                setup_file_dest = os.path.join(self.floppy_mount, setup_file)
-                shutil.copyfile(setup_file_path, setup_file_dest)
+        os.chmod(self.path, 0755)
+        cleanup(self.mount)
+        print "Disk %s successfuly set" % self.path
+
+
+class UnattendedInstall(object):
+    """
+    Creates a floppy disk image that will contain a config file for unattended
+    OS install. Optionally, sets up a PXE install server using qemu built in
+    TFTP and DHCP servers to install a particular operating system. The
+    parameters to the script are retrieved from environment variables.
+    """
+    def __init__(self):
+        """
+        Gets params from environment variables and sets class attributes.
+        """
+        images_dir = os.path.join(KVM_TEST_DIR, 'images')
+        self.deps_dir = os.path.join(KVM_TEST_DIR, 'deps')
+        self.unattended_dir = os.path.join(KVM_TEST_DIR, 'unattended')
+
+        attributes = ['kernel_args', 'finish_program', 'cdrom_cd1',
+                      'unattended_file', 'medium', 'url', 'kernel', 'initrd',
+                      'nfs_server', 'nfs_dir', 'pxe_dir', 'pxe_image',
+                      'pxe_initrd', 'install_virtio', 'tftp',
+                      'floppy', 'cdrom_unattended']
+        for a in attributes:
+            self._setattr(a)
+
+        if self.install_virtio == 'yes':
+            v_attributes = ['virtio_floppy', 'virtio_storage_path',
+                            'virtio_network_path', 'virtio_oemsetup_id',
+                            'virtio_network_installer']
+            for va in v_attributes:
+                self._setattr(va)
+
+        # Silly attribution just to calm pylint down...
+        self.tftp = self.tftp
+        if self.tftp:
+            self.tftp = os.path.join(KVM_TEST_DIR, self.tftp)
+            if not os.path.isdir(self.tftp):
+                os.makedirs(self.tftp)
+
+        self.cdrom_cd1 = os.path.join(KVM_TEST_DIR, self.cdrom_cd1)
+        self.cdrom_mount = tempfile.mkdtemp(prefix='cdrom_', dir='/tmp')
+        if self.medium == 'nfs':
+            self.nfs_mount = tempfile.mkdtemp(prefix='nfs_', dir='/tmp')
+
+        self.floppy = os.path.join(KVM_TEST_DIR, self.floppy)
+        if not os.path.isdir(os.path.dirname(self.floppy)):
+            os.makedirs(os.path.dirname(self.floppy))
+
+        self.image_path = KVM_TEST_DIR
+        self.kernel_path = os.path.join(self.image_path, self.kernel)
+        self.initrd_path = os.path.join(self.image_path, self.initrd)
+
+
+    def _setattr(self, key):
+        """
+        Populate class attributes with contents of environment variables.
+
+        Example: KVM_TEST_medium will populate self.medium.
+
+        @param key: Name of the class attribute we desire to have.
+        """
+        env_name = 'KVM_TEST_%s' % key
+        value = os.environ.get(env_name, '')
+        setattr(self, key, value)
+
+
+    def render_answer_file(self):
+        # Replace KVM_TEST_CDKEY (in the unattended file) with the cdkey
+        # provided for this test and replace the KVM_TEST_MEDIUM with
+        # the tree url or nfs address provided for this test.
+        unattended_contents = open(self.unattended_file).read()
+        dummy_cdkey_re = r'\bKVM_TEST_CDKEY\b'
+        real_cdkey = os.environ.get('KVM_TEST_cdkey')
+        if re.search(dummy_cdkey_re, unattended_contents):
+            if real_cdkey:
+                unattended_contents = re.sub(dummy_cdkey_re, real_cdkey,
+                                             unattended_contents)
+            else:
+                print ("WARNING: 'cdkey' required but not specified for "
+                       "this unattended installation")
+
+        dummy_medium_re = r'\bKVM_TEST_MEDIUM\b'
+        if self.medium == "cdrom":
+            content = "cdrom"
+        elif self.medium == "url":
+            content = "url --url %s" % self.url
+        elif self.medium == "nfs":
+            content = "nfs --server=%s --dir=%s" % (self.nfs_server,
+                                                    self.nfs_dir)
+        else:
+            raise SetupError("Unexpected installation medium %s" % self.url)
+
+        unattended_contents = re.sub(dummy_medium_re, content,
+                                     unattended_contents)
+
+        def replace_virtio_key(contents, dummy_re, env):
+            """
+            Replace a virtio dummy string with contents.
+
+            If install_virtio is not set, replace it with a dummy string.
+
+            @param contents: Contents of the unattended file
+            @param dummy_re: Regular expression used to search on the.
+                    unattended file contents.
+            @param env: Name of the environment variable.
+            """
+            dummy_path = "C:"
+            driver = os.environ.get(env, '')
+
+            if re.search(dummy_re, contents):
                 if self.install_virtio == "yes":
-                    self.setup_virtio_win2003()
-
-            elif self.unattended_file.endswith('.ks'):
-                # Red Hat kickstart install
-                dest_fname = 'ks.cfg'
-            elif self.unattended_file.endswith('.xml'):
-                if not self.tftp:
-                    # Windows unattended install
-                    dest_fname = "autounattend.xml"
-                    if self.install_virtio == "yes":
-                        self.setup_virtio_win2008()
-                else:
-                    # SUSE autoyast install
-                    dest_fname = "autoinst.xml"
-
-            dest = os.path.join(self.floppy_mount, dest_fname)
-
-            # Replace KVM_TEST_CDKEY (in the unattended file) with the cdkey
-            # provided for this test and replace the KVM_TEST_MEDIUM with
-            # the tree url or nfs address provided for this test.
-            unattended_contents = open(self.unattended_file).read()
-            dummy_cdkey_re = r'\bKVM_TEST_CDKEY\b'
-            real_cdkey = os.environ.get('KVM_TEST_cdkey')
-            if re.search(dummy_cdkey_re, unattended_contents):
-                if real_cdkey:
-                    unattended_contents = re.sub(dummy_cdkey_re, real_cdkey,
-                                                 unattended_contents)
+                    if driver.endswith("msi"):
+                        driver = 'msiexec /passive /package ' + driver
+                    else:
+                        try:
+                            # Let's escape windows style paths properly
+                            drive, path = driver.split(":")
+                            driver = drive + ":" + re.escape(path)
+                        except:
+                            pass
+                    contents = re.sub(dummy_re, driver, contents)
                 else:
-                    print ("WARNING: 'cdkey' required but not specified for "
-                           "this unattended installation")
-
-            dummy_medium_re = r'\bKVM_TEST_MEDIUM\b'
-            if self.medium == "cdrom":
-                content = "cdrom"
-            elif self.medium == "url":
-                content = "url --url %s" % self.url
-            elif self.medium == "nfs":
-                content = "nfs --server=%s --dir=%s" % (self.nfs_server,
-                                                        self.nfs_dir)
+                    contents = re.sub(dummy_re, dummy_path, contents)
+            return contents
+
+        vdict = {r'\bKVM_TEST_STORAGE_DRIVER_PATH\b':
+                 'KVM_TEST_virtio_storage_path',
+                 r'\bKVM_TEST_NETWORK_DRIVER_PATH\b':
+                 'KVM_TEST_virtio_network_path',
+                 r'\bKVM_TEST_VIRTIO_NETWORK_INSTALLER\b':
+                 'KVM_TEST_virtio_network_installer_path'}
+
+        for vkey in vdict:
+            unattended_contents = replace_virtio_key(unattended_contents,
+                                                     vkey, vdict[vkey])
+
+        print "Unattended install contents:"
+        print unattended_contents
+        return unattended_contents
+
+
+    def setup_boot_disk(self):
+        answer_contents = self.render_answer_file()
+
+        if self.unattended_file.endswith('.sif'):
+            dest_fname = 'winnt.sif'
+            setup_file = 'winnt.bat'
+            boot_disk = FloppyDisk(self.floppy)
+            boot_disk.setup_answer_file(dest_fname, answer_contents)
+            setup_file_path = os.path.join(self.unattended_dir, setup_file)
+            boot_disk.copy_to(setup_file_path)
+            if self.install_virtio == "yes":
+                boot_disk.setup_virtio_win2003(self.virtio_floppy)
+            boot_disk.copy_to(self.finish_program)
+
+        elif self.unattended_file.endswith('.ks'):
+            # Red Hat kickstart install
+            dest_fname = 'ks.cfg'
+            if self.cdrom_unattended:
+                boot_disk = CdromDisk(self.cdrom_unattended)
+            elif self.floppy:
+                boot_disk = FloppyDisk(self.floppy)
             else:
-                raise SetupError("Unexpected installation medium %s" % self.url)
-
-            unattended_contents = re.sub(dummy_medium_re, content,
-                                         unattended_contents)
-
-            def replace_virtio_key(contents, dummy_re, env):
-                """
-                Replace a virtio dummy string with contents.
-
-                If install_virtio is not set, replace it with a dummy string.
-
-                @param contents: Contents of the unattended file
-                @param dummy_re: Regular expression used to search on the.
-                        unattended file contents.
-                @param env: Name of the environment variable.
-                """
-                dummy_path = "C:"
-                driver = os.environ.get(env, '')
-
-                if re.search(dummy_re, contents):
-                    if self.install_virtio == "yes":
-                        if driver.endswith("msi"):
-                            driver = 'msiexec /passive /package ' + driver
-                        else:
-                            try:
-                                # Let's escape windows style paths properly
-                                drive, path = driver.split(":")
-                                driver = drive + ":" + re.escape(path)
-                            except:
-                                pass
-                        contents = re.sub(dummy_re, driver, contents)
-                    else:
-                        contents = re.sub(dummy_re, dummy_path, contents)
-                return contents
-
-            vdict = {r'\bKVM_TEST_STORAGE_DRIVER_PATH\b':
-                     'KVM_TEST_virtio_storage_path',
-                     r'\bKVM_TEST_NETWORK_DRIVER_PATH\b':
-                     'KVM_TEST_virtio_network_path',
-                     r'\bKVM_TEST_VIRTIO_NETWORK_INSTALLER\b':
-                     'KVM_TEST_virtio_network_installer_path'}
-
-            for vkey in vdict:
-                unattended_contents = replace_virtio_key(unattended_contents,
-                                                         vkey, vdict[vkey])
-
-            print
-            print "Unattended install %s contents:" % dest_fname
-            print unattended_contents
-            # Write the unattended file contents to 'dest'
-            open(dest, 'w').write(unattended_contents)
-
-            if self.finish_program:
-                dest_fname = os.path.basename(self.finish_program)
-                dest = os.path.join(self.floppy_mount, dest_fname)
-                shutil.copyfile(self.finish_program, dest)
+                raise SetupError("Neither cdrom_unattended nor floppy set "
+                                 "on the config file, please verify")
+            boot_disk.setup_answer_file(dest_fname, answer_contents)
 
-        finally:
-            u_cmd = 'umount %s' % self.floppy_mount
-            if os.system(u_cmd):
-                raise SetupError('Could not unmount floppy at %s.' %
-                                 self.floppy_mount)
-            self.cleanup(self.floppy_mount)
+        elif self.unattended_file.endswith('.xml'):
+            if self.tftp:
+                # SUSE autoyast install
+                dest_fname = "autoinst.xml"
+                if self.cdrom_unattended:
+                    boot_disk = CdromDisk(self.cdrom_unattended)
+                elif self.floppy:
+                    boot_disk = FloppyDisk(self.floppy)
+                else:
+                    raise SetupError("Neither cdrom_unattended nor floppy set "
+                                     "on the config file, please verify")
+                boot_disk.setup_answer_file(dest_fname, answer_contents)
+
+            else:
+                # Windows unattended install
+                dest_fname = "autounattend.xml"
+                boot_disk = FloppyDisk(self.floppy)
+                boot_disk.setup_answer_file(dest_fname, answer_contents)
+                if self.install_virtio == "yes":
+                    boot_disk.setup_virtio_win2008(self.virtio_floppy)
+                boot_disk.copy_to(self.finish_program)
 
-        os.chmod(self.floppy, 0755)
+        else:
+            raise SetupError('Unknown answer file %s' %
+                             self.unattended_file)
 
-        print "Boot floppy created successfuly"
+        boot_disk.close()
 
 
     def setup_pxe_boot(self):
@@ -385,7 +489,7 @@ class UnattendedInstall(object):
 
     def setup_url(self):
         """
-        Download the vmlinuz and initrd.img from URL
+        Download the vmlinuz and initrd.img from URL.
         """
         print "Downloading the vmlinuz and initrd.img"
         os.chdir(self.image_path)
@@ -453,6 +557,11 @@ class UnattendedInstall(object):
 
 
     def setup(self):
+        """
+        Configure the environment for unattended install.
+
+        Uses an appropriate strategy according to each install model.
+        """
         print "Starting unattended install setup"
         print
 
@@ -464,8 +573,8 @@ class UnattendedInstall(object):
                 print "    %s: %s" % (name, value)
         print
 
-        if self.unattended_file and self.floppy is not None:
-            self.create_boot_floppy()
+        if self.unattended_file and (self.floppy or self.cdrom_unattended):
+            self.setup_boot_disk()
         if self.medium == "cdrom":
             if self.tftp:
                 self.setup_pxe_boot()
client/tests/kvm/tests_base.cfg.sample +121 -69

542 lines not on this page · raw message

Signature · 1 line
1.7.2.3