corrections in vmmanager
authormb
Tue, 10 Dec 2013 16:02:24 +0100
changeset 36cc6abcf9108b
parent 35 ba1ca3e5870b
child 37 6d7b4672414c
corrections in vmmanager
OpenSecurity/bin/vmmanager.py
     1.1 --- a/OpenSecurity/bin/vmmanager.py	Tue Dec 10 14:56:11 2013 +0100
     1.2 +++ b/OpenSecurity/bin/vmmanager.py	Tue Dec 10 16:02:24 2013 +0100
     1.3 @@ -11,6 +11,7 @@
     1.4  import re
     1.5  import _winreg
     1.6  from cygwin import Cygwin
     1.7 +import threading
     1.8  
     1.9  
    1.10  DEBUG = True
    1.11 @@ -212,10 +213,10 @@
    1.12      #remove VM from the system. should be used on VMs returned by listSDVMs    
    1.13      def removeVM(self, vm_name):
    1.14          print('removing ' + vm_name)
    1.15 -        cmd = 'VBoxManage unregistervm', vm_name, '--delete'
    1.16 +        cmd = 'VBoxManage unregistervm ' + vm_name + ' --delete'
    1.17          print self.execute(cmd)
    1.18          machineFolder = self.getDefaultMachineFolder()
    1.19 -        cmd = self.cygwin_path+'bash.exe --login -c \"rm -rf ' + machineFolder + '\\' + vm_name + '*\"'
    1.20 +        cmd = self.cygwin_path + 'bash.exe --login -c \"rm -rf ' + machineFolder + '\\' + vm_name + '*\"'
    1.21          print self.execute(cmd)
    1.22      
    1.23      # start VM
    1.24 @@ -293,24 +294,30 @@
    1.25          result = self.execute(cmd)
    1.26          return result
    1.27      
    1.28 +    handleDeviceChangeLock = threading.Lock()
    1.29 +    
    1.30      # handles device change events
    1.31      def handleDeviceChange(self):
    1.32 -        attached_devices = self.getAttachedRSDs()
    1.33 -        connected_devices = self.listRSDS()
    1.34 -        for vm_name in attached_devices.keys():
    1.35 -            if connected_devices and attached_devices[vm_name] not in connected_devices.values():
    1.36 -                # self.netUse(vm_name)
    1.37 -                self.stopVM(vm_name)
    1.38 -                self.removeVM(vm_name)
    1.39 -        
    1.40 -        attached_devices = self.getAttachedRSDs()
    1.41 -        for connected_device in connected_devices.values():
    1.42 -            if attached_devices or connected_device not in attached_devices.values():
    1.43 -                new_sdvm = self.generateSDVMName()
    1.44 -                self.createVM(new_sdvm)
    1.45 -                self.attachRSD(new_sdvm, connected_device)
    1.46 -                self.startVM(new_sdvm)
    1.47 -                self.netUse(new_sdvm)
    1.48 +        if VMManager.handleDeviceChangeLock.acquire(True):
    1.49 +            #destroy unused vms
    1.50 +            attached_devices = self.getAttachedRSDs()
    1.51 +            connected_devices = self.listRSDS()
    1.52 +            for vm_name in attached_devices.keys():
    1.53 +                if attached_devices[vm_name] not in connected_devices.values():
    1.54 +                    # self.netUnUse(vm_name)
    1.55 +                    self.stopVM(vm_name)
    1.56 +                    self.removeVM(vm_name)
    1.57 +            #create new vm for attached device if any
    1.58 +            attached_devices = self.getAttachedRSDs()
    1.59 +            for connected_device in connected_devices.values():
    1.60 +                if (attached_devices and False) or (connected_device not in attached_devices.values()):
    1.61 +                    new_sdvm = self.generateSDVMName()
    1.62 +                    self.createVM(new_sdvm)
    1.63 +                    self.attachRSD(new_sdvm, connected_device)
    1.64 +                    self.startVM(new_sdvm)
    1.65 +                    
    1.66 +                    #self.netUse(new_sdvm)
    1.67 +            VMManager.handleDeviceChangeLock.release()
    1.68      
    1.69      def handleBrowsingRequest(self):
    1.70          new_sdvm = self.generateSDVMName()