From patchwork Thu Feb 11 19:25:04 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas Meneghel Rodrigues X-Patchwork-Id: 78797 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.3/8.14.3) with ESMTP id o1BJPFZG009274 for ; Thu, 11 Feb 2010 19:25:16 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756835Ab0BKTZN (ORCPT ); Thu, 11 Feb 2010 14:25:13 -0500 Received: from mx1.redhat.com ([209.132.183.28]:33194 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756714Ab0BKTZM (ORCPT ); Thu, 11 Feb 2010 14:25:12 -0500 Received: from int-mx08.intmail.prod.int.phx2.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o1BJP9pr018976 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 11 Feb 2010 14:25:09 -0500 Received: from localhost.localdomain (vpn-11-113.rdu.redhat.com [10.11.11.113]) by int-mx08.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o1BJP6CJ008927; Thu, 11 Feb 2010 14:25:07 -0500 From: Lucas Meneghel Rodrigues To: autotest@test.kernel.org Cc: kvm@vger.kernel.org, Lucas Meneghel Rodrigues Subject: [PATCH] KVM test: Make unattended script to clean up on failures Date: Thu, 11 Feb 2010 17:25:04 -0200 Message-Id: <1265916304-4400-1-git-send-email-lmr@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.21 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Thu, 11 Feb 2010 19:25:16 +0000 (UTC) diff --git a/client/tests/kvm/scripts/unattended.py b/client/tests/kvm/scripts/unattended.py index 87a8973..63c01b1 100755 --- a/client/tests/kvm/scripts/unattended.py +++ b/client/tests/kvm/scripts/unattended.py @@ -73,7 +73,8 @@ class UnattendedInstall(object): if os.path.exists(self.floppy_img): os.remove(self.floppy_img) - 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_bin, + self.floppy_img) if os.system(c_cmd): raise SetupError('Could not create floppy image.') @@ -81,48 +82,51 @@ class UnattendedInstall(object): if os.system(f_cmd): raise SetupError('Error formatting floppy image.') - m_cmd = 'mount -o loop %s %s' % (self.floppy_img, self.floppy_mount) - if os.system(m_cmd): - raise SetupError('Could not mount floppy image.') - - 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) - elif self.unattended_file.endswith('.ks'): - dest_fname = 'ks.cfg' - elif self.unattended_file.endswith('.xml'): - dest_fname = "autounattend.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 - 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") - - # Write the unattended file contents to 'dest' - open(dest, 'w').write(unattended_contents) + try: + m_cmd = 'mount -o loop %s %s' % (self.floppy_img, self.floppy_mount) + if os.system(m_cmd): + raise SetupError('Could not mount floppy image.') + + 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) + elif self.unattended_file.endswith('.ks'): + dest_fname = 'ks.cfg' + elif self.unattended_file.endswith('.xml'): + dest_fname = "autounattend.xml" - 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) - u_cmd = 'umount %s' % self.floppy_mount - if os.system(u_cmd): - raise SetupError('Could not unmount floppy at %s.' % - self.floppy_mount) + # Replace KVM_TEST_CDKEY (in the unattended file) with the cdkey + # 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") + + # 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) + + 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) os.chmod(self.floppy_img, 0755) @@ -155,34 +159,40 @@ class UnattendedInstall(object): pxe_dest = os.path.join(self.tftp_root, 'pxelinux.0') shutil.copyfile(pxe_file, pxe_dest) - m_cmd = 'mount -t iso9660 -v -o loop,ro %s %s' % (self.cdrom_iso, - self.cdrom_mount) - if os.system(m_cmd): - raise SetupError('Could not mount CD image %s.' % self.cdrom_iso) - - p = os.path.join('images', 'pxeboot') - pxe_dir = os.path.join(self.cdrom_mount, p) - pxe_image = os.path.join(pxe_dir, 'vmlinuz') - pxe_initrd = os.path.join(pxe_dir, 'initrd.img') - - if not os.path.isdir(pxe_dir): - raise SetupError('The ISO image does not have a %s dir. The script ' - 'assumes that the cd has a %s dir where to search ' - 'for the vmlinuz image.' % (p, p)) - - if not os.path.isfile(pxe_image) or not os.path.isfile(pxe_initrd): - raise SetupError('The location %s is lacking either a vmlinuz or a ' - 'initrd.img file. Cannot find a PXE image to ' - 'proceed.' % pxe_dir) - - tftp_image = os.path.join(self.tftp_root, 'vmlinuz') - tftp_initrd = os.path.join(self.tftp_root, 'initrd.img') - shutil.copyfile(pxe_image, tftp_image) - shutil.copyfile(pxe_initrd, tftp_initrd) - - u_cmd = 'umount %s' % self.cdrom_mount - if os.system(u_cmd): - raise SetupError('Could not unmount CD at %s.' % self.cdrom_mount) + try: + m_cmd = 'mount -t iso9660 -v -o loop,ro %s %s' % (self.cdrom_iso, + self.cdrom_mount) + if os.system(m_cmd): + raise SetupError('Could not mount CD image %s.' % + self.cdrom_iso) + + p = os.path.join('images', 'pxeboot') + pxe_dir = os.path.join(self.cdrom_mount, p) + pxe_image = os.path.join(pxe_dir, 'vmlinuz') + pxe_initrd = os.path.join(pxe_dir, 'initrd.img') + + if not os.path.isdir(pxe_dir): + raise SetupError('The ISO image does not have a %s dir. The ' + 'script assumes that the cd has a %s dir ' + 'where to search for the vmlinuz image.' % + (p, p)) + + if not os.path.isfile(pxe_image) or not os.path.isfile(pxe_initrd): + raise SetupError('The location %s is lacking either a vmlinuz ' + 'or a initrd.img file. Cannot find a PXE ' + 'image to proceed.' % pxe_dir) + + tftp_image = os.path.join(self.tftp_root, 'vmlinuz') + tftp_initrd = os.path.join(self.tftp_root, 'initrd.img') + shutil.copyfile(pxe_image, tftp_image) + shutil.copyfile(pxe_initrd, tftp_initrd) + + finally: + u_cmd = 'umount %s' % self.cdrom_mount + if os.system(u_cmd): + raise SetupError('Could not unmount CD at %s.' % + self.cdrom_mount) + self.cleanup(self.cdrom_mount) pxe_config_dir = os.path.join(self.tftp_root, 'pxelinux.cfg') if not os.path.isdir(pxe_config_dir): @@ -202,18 +212,19 @@ class UnattendedInstall(object): print "PXE boot successfuly set" - def cleanup(self): + + def cleanup(self, mount): """ - Clean up previously used mount points. + Clean up a previously used mountpoint. + + @param mount: Mountpoint to be cleaned up. """ - print "Cleaning up unused mount points" - for mount in [self.floppy_mount, self.cdrom_mount]: - if os.path.isdir(mount): - if os.path.ismount(mount): - print "Path %s is still mounted, please verify" % mount - else: - print "Removing mount point %s" % mount - os.rmdir(mount) + if os.path.isdir(mount): + if os.path.ismount(mount): + print "Path %s is still mounted, please verify" % mount + else: + print "Removing mount point %s" % mount + os.rmdir(mount) def setup(self): @@ -232,7 +243,6 @@ class UnattendedInstall(object): self.create_boot_floppy() if self.tftp_root: self.setup_pxe_boot() - self.cleanup() print "Unattended install setup finished successfuly"