def _cleanup(self): """Unregister test HD and VM if they are registered.""" # Do machine first to detach any HDs try: machine = VirtualMachine.find(self.testVMname) except: pass else: machine.eject() try: harddisk = HardDisk.find(self.testHDpath) except: pass else: harddisk.close() try: machine = VirtualMachine.find(self.cloneVMname) except Exception as e: pass else: machine.eject() machine.delete() try: clonedisk = HardDisk.find(self.cloneHDpath) except: pass else: clonedisk.close() try: os.remove(self.cloneHDpath) except: pass
def invoke(cls, args): """Invoke the command. Return exit code for program.""" if len(args) < 1: raise Exception("Missing source VM name argument") srcVM = VirtualMachine.find(args.pop(0)) if len(args) < 1: raise Exception("Missing target VM name argument") targetName = args.pop(0) message("Cloning %s to %s" % (srcVM, targetName)) cloneVM = srcVM.clone(targetName) # Now clone and attach disks disks = srcVM.getHardDrives() for disk in disks: # Generate new HD filename by prefixing new VM name. # Not the greatest, but not sure what the best way is. targetFilename = os.path.join(disk.dirname(), "%s-%s" % (targetName, disk.name)) # Todo: Need to resolve file already existing here. message("Cloning disk %s to %s (%d bytes)" % (disk, os.path.basename(targetFilename), disk.size)) progress = disk.clone(targetFilename, wait=False) show_progress(progress) cloneHD = HardDisk.find(targetFilename) message("Attaching %s to %s" % (cloneHD, cloneVM)) cloneVM.attachMedium(cloneHD) return 0
def invoke(cls, args): """Invoke the command. Return exit code for program.""" if len(args) < 1: raise Exception("Missing virtual machine argument") vm = VirtualMachine.find(args.pop(0)) if len(args) < 1: raise Exception("Missing target directory argument") targetDir = args.pop(0) verboseMsg("Backing up %s to %s" % (vm, targetDir)) if vm.isRunning(): verboseMsg("Pausing VM...") # Must wait until paused or will have race condition for lock # on disks. vm.pause(wait=True) atexit.register(vm.resume) # Todo: Backup settings file in some way. # Todo: Want to back up devices than hard drives? disks = vm.getHardDrives() for disk in disks: targetFilename = os.path.join(targetDir, disk.basename()) # Todo: Need to resolve file already existing here. verboseMsg("Backing up disk %s to %s (%d bytes)" % (disk, targetFilename, disk.size)) progress = disk.clone(targetFilename, wait=False) show_progress(progress) # Remove newly created clone from registry clone = HardDisk.find(targetFilename) clone.close()
def testClone(self): """Test Medium.clone()""" harddisk = HardDisk.open(self.testHDpath) harddisk.clone(self.cloneHDpath) clonedisk = HardDisk.find(self.cloneHDpath) self.assertEqual(harddisk.format, harddisk.format) self.assertEqual(harddisk.logicalSize, harddisk.logicalSize) self.assertNotEqual(harddisk.id, clonedisk.id)
def testFind(self): """Test HardDisk.find()""" self.assertEqual(False, HardDisk.isRegistered(self.testHDpath)) harddisk = HardDisk.open(self.testHDpath) self.assertEqual(True, HardDisk.isRegistered(self.testHDpath)) hd = HardDisk.find(self.testHDpath) self.assertEqual(harddisk.id, hd.id) harddisk.close() self.assertEqual(False, HardDisk.isRegistered(self.testHDpath))
def harddisk(cls, string): """Load a harddisk described by string. Will open disk if needed.""" # TODO: Should also support string being UUID if HardDisk.isRegistered(string): hd = HardDisk.find(string) else: hd = HardDisk.open(string) return hd
def invoke(cls, args): """Invoke the command. Return exit code for program.""" mode = "gui" # or "vrdp" if len(args) < 1: raise Exception("Missing virtual machine filename argument") vm = VirtualMachine.open(args.pop(0)) atexit.register(vm.eject) if not vm.isRegistered(): vm.register() for hd in args: hd = HardDisk.find(hd) vm.attachMedium(hd) verboseMsg("Starting VM in %s mode" % mode) vm.powerOn(type=mode) # Wait until machine is running or we have a race condition # where it still might be down when we call waitUntilDown() vm.waitUntilRunning() verboseMsg("VM started. Waiting until power down...") vm.waitUntilDown() verboseMsg("VM powered down.") # Let atexit clean up return 0