Removed checkResult method from cygwin.py.
authorBarthaM@N3SIM1218.D03.arc.local
Fri, 22 Aug 2014 09:35:02 +0100
changeset 218327f282364b9
parent 217 4162648fb167
child 219 9480e5ba1a82
Removed checkResult method from cygwin.py.
Created additional tests and force removal of certificate image.
Modified the updateTemplate method.
Fixed VM startup wait method.
OpenSecurity/bin/cygwin.py
OpenSecurity/bin/vmmanager.pyw
     1.1 --- a/OpenSecurity/bin/cygwin.py	Thu Aug 14 09:51:11 2014 +0100
     1.2 +++ b/OpenSecurity/bin/cygwin.py	Fri Aug 22 09:35:02 2014 +0100
     1.3 @@ -153,7 +153,7 @@
     1.4          result, res_stdout, res_stderr = None, None, None
     1.5          try:
     1.6              # quote the executable otherwise we run into troubles
     1.7 -            # when the path contains spaces and additonal arguments
     1.8 +            # when the path contains spaces and additional arguments
     1.9              # are presented as well.
    1.10              # special: invoking bash as login shell here with
    1.11              # an unquoted command does not execute /etc/profile
    1.12 @@ -166,14 +166,15 @@
    1.13              res_stdout, res_stderr = process.communicate()
    1.14              result = process.returncode
    1.15              logger.debug('Finished: ' + program + ' ' + ''.join(arguments))
    1.16 -            #result = process.wait()
    1.17 -            #res_stdout = process.stdout.read();
    1.18 -            #res_stderr = process.stderr.read();
    1.19  
    1.20          except Exception as ex:
    1.21              res_stderr = ''.join(str(ex.args))
    1.22              result = 1 
    1.23 -            
    1.24 +    
    1.25 +        if result != 0:
    1.26 +            logger.error('Command failed:' + ''.join(res_stderr))
    1.27 +            raise OpenSecurityException('Command failed:' + ''.join(res_stderr))
    1.28 +        
    1.29          return result, res_stdout, res_stderr
    1.30      
    1.31      @staticmethod
    1.32 @@ -182,12 +183,17 @@
    1.33          result = None
    1.34          while retry < try_count:
    1.35              if Cygwin.executeLock.acquire(True):
    1.36 -                result = Cygwin.execute(Cygwin.vbox_man, command, wait_return, window)
    1.37 -                Cygwin.executeLock.release()
    1.38 -                if result[0] == 0:
    1.39 +                try:
    1.40 +                    result = Cygwin.execute(Cygwin.vbox_man, command, wait_return, window)
    1.41 +                except Exception as ex:
    1.42 +                    Cygwin.executeLock.release()
    1.43 +                    if (retry+1) == try_count:
    1.44 +                        raise ex
    1.45 +                else:
    1.46 +                    Cygwin.executeLock.release()
    1.47                      return result
    1.48 -                retry+=1
    1.49 -        return result
    1.50 +            retry+=1
    1.51 +        raise OpenSecurityException('Command max retry reached: ' + ''.join(command))
    1.52  
    1.53  
    1.54      @staticmethod
    1.55 @@ -206,6 +212,8 @@
    1.56      # executes command over ssh on guest vm
    1.57      @staticmethod
    1.58      def sshExecute(command, address, user_name, certificate, wait_return=True, window = False):
    1.59 +        if command == None or address == None or user_name == None or certificate == None:
    1.60 +            raise OpenSecurityException('Invalid parameter value')
    1.61          command = ' -v -o StrictHostKeyChecking=no -i "' + certificate + '" ' + user_name + '@' + address + ' ' + command        
    1.62          return Cygwin.execute(Cygwin.cygwin_ssh, command, wait_return, window)
    1.63  
    1.64 @@ -248,9 +256,9 @@
    1.65      
    1.66      @staticmethod
    1.67      def checkResult(result):
    1.68 -        if result[0] != 0:
    1.69 -            logger.error('Command failed:' + ''.join(result[2]))
    1.70 -            raise OpenSecurityException('Command failed:' + ''.join(result[2]))
    1.71 +        #if result[0] != 0:
    1.72 +        #    logger.error('Command failed:' + ''.join(result[2]))
    1.73 +        #    raise OpenSecurityException('Command failed:' + ''.join(result[2]))
    1.74          return result
    1.75                  
    1.76  # start
     2.1 --- a/OpenSecurity/bin/vmmanager.pyw	Thu Aug 14 09:51:11 2014 +0100
     2.2 +++ b/OpenSecurity/bin/vmmanager.pyw	Fri Aug 22 09:35:02 2014 +0100
     2.3 @@ -163,7 +163,7 @@
     2.4      # return hosty system properties
     2.5      @staticmethod
     2.6      def getSystemProperties():
     2.7 -        result = Cygwin.checkResult(Cygwin.vboxExecute('list systemproperties'))
     2.8 +        result = Cygwin.vboxExecute('list systemproperties')
     2.9          if result[1]=='':
    2.10              return None
    2.11          props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result[1].strip().splitlines()))
    2.12 @@ -267,13 +267,13 @@
    2.13  
    2.14      # list all existing VMs registered with VBox
    2.15      def listVM(self):
    2.16 -        result = Cygwin.checkResult(Cygwin.vboxExecute('list vms'))[1]
    2.17 +        result = Cygwin.vboxExecute('list vms')[1]
    2.18          vms = list(k.strip().strip('"') for k,_ in (line.split(' ') for line in result.splitlines()))
    2.19          return vms
    2.20      
    2.21      # list running VMs
    2.22      def listRunningVMS(self):
    2.23 -        result = Cygwin.checkResult(Cygwin.vboxExecute('list runningvms'))[1]
    2.24 +        result = Cygwin.vboxExecute('list runningvms')[1]
    2.25          vms = list(k.strip().strip('"') for k,_ in (line.split(' ') for line in result.splitlines()))
    2.26          return vms
    2.27      
    2.28 @@ -334,7 +334,7 @@
    2.29      # return the RSDs connected to the host
    2.30      @staticmethod
    2.31      def getExistingRSDs():
    2.32 -        results = Cygwin.checkResult(Cygwin.vboxExecute('list usbhost'))[1]
    2.33 +        results = Cygwin.vboxExecute('list usbhost')[1]
    2.34          results = results.split('Host USB Devices:')[1].strip()
    2.35          
    2.36          items = list( "UUID:"+result for result in results.split('UUID:') if result != '')
    2.37 @@ -392,13 +392,13 @@
    2.38      
    2.39      # attach removable storage device to VM by provision of filter
    2.40      def attachRSD(self, vm_name, rsd_filter):
    2.41 -        #return Cygwin.checkResult(Cygwin.vboxExecute('usbfilter add 0 --target ' + vm_name + ' --name OpenSecurityRSD --vendorid ' + rsd_filter.vendorid + ' --productid ' + rsd_filter.productid + ' --revision ' + rsd_filter.revision + ' --serialnumber ' + rsd_filter.serial))
    2.42 -        return Cygwin.checkResult(Cygwin.vboxExecute('controlvm ' + vm_name + ' usbattach ' + rsd_filter.uuid ))
    2.43 +        #return Cygwin.vboxExecute('usbfilter add 0 --target ' + vm_name + ' --name OpenSecurityRSD --vendorid ' + rsd_filter.vendorid + ' --productid ' + rsd_filter.productid + ' --revision ' + rsd_filter.revision + ' --serialnumber ' + rsd_filter.serial)
    2.44 +        return Cygwin.vboxExecute('controlvm ' + vm_name + ' usbattach ' + rsd_filter.uuid )
    2.45      
    2.46      # detach removable storage from VM by 
    2.47      def detachRSD(self, vm_name, rsd_filter):
    2.48 -        #return Cygwin.checkResult(Cygwin.vboxExecute('usbfilter remove 0 --target ' + vm_name))
    2.49 -        return Cygwin.checkResult(Cygwin.vboxExecute('controlvm ' + vm_name + ' usbdetach ' + rsd_filter.uuid ))
    2.50 +        #return Cygwin.vboxExecute('usbfilter remove 0 --target ' + vm_name)
    2.51 +        return Cygwin.vboxExecute('controlvm ' + vm_name + ' usbdetach ' + rsd_filter.uuid )
    2.52          
    2.53      # configures hostonly networking and DHCP server. requires admin rights
    2.54      def configureHostNetworking(self):
    2.55 @@ -408,10 +408,10 @@
    2.56          #Cygwin.vboxExecute(cmd)
    2.57          #cmd = 'vboxmanage hostonlyif create'
    2.58          #Cygwin.vboxExecute(cmd)
    2.59 -        Cygwin.checkResult(Cygwin.vboxExecute('hostonlyif ipconfig \"VirtualBox Host-Only Ethernet Adapter\" --ip 192.168.56.1 --netmask 255.255.255.0'))
    2.60 +        Cygwin.vboxExecute('hostonlyif ipconfig \"VirtualBox Host-Only Ethernet Adapter\" --ip 192.168.56.1 --netmask 255.255.255.0')
    2.61          #cmd = 'vboxmanage dhcpserver add'
    2.62          #Cygwin.vboxExecute(cmd)
    2.63 -        Cygwin.checkResult(Cygwin.vboxExecute('dhcpserver modify --ifname \"VirtualBox Host-Only Ethernet Adapter\" --ip 192.168.56.100 --netmask 255.255.255.0 --lowerip 192.168.56.101 --upperip 192.168.56.200'))
    2.64 +        Cygwin.vboxExecute('dhcpserver modify --ifname \"VirtualBox Host-Only Ethernet Adapter\" --ip 192.168.56.100 --netmask 255.255.255.0 --lowerip 192.168.56.101 --upperip 192.168.56.200')
    2.65      
    2.66      def isSDVMExisting(self, vm_name):
    2.67          sdvms = self.listSDVM()
    2.68 @@ -423,10 +423,10 @@
    2.69              return
    2.70          #remove eventually existing SDVM folder
    2.71          machineFolder = Cygwin.cygPath(VMManager.machineFolder)
    2.72 -        Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '\\\"'))
    2.73 -        Cygwin.checkResult(Cygwin.vboxExecute('createvm --name ' + vm_name + ' --ostype Debian --register'))
    2.74 -        Cygwin.checkResult(Cygwin.vboxExecute('modifyvm ' + vm_name + ' --memory 768 --vram 10 --cpus 1 --usb on --usbehci on --nic1 hostonly --hostonlyadapter1 \"' + self.hostonlyIF['Name'] + '\" --nic2 nat'))
    2.75 -        Cygwin.checkResult(Cygwin.vboxExecute('storagectl ' + vm_name + ' --name SATA --add sata --portcount 2'))
    2.76 +        Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '\\\"')
    2.77 +        Cygwin.vboxExecute('createvm --name ' + vm_name + ' --ostype Debian --register')
    2.78 +        Cygwin.vboxExecute('modifyvm ' + vm_name + ' --memory 768 --vram 10 --cpus 1 --usb on --usbehci on --nic1 hostonly --hostonlyadapter1 \"' + self.hostonlyIF['Name'] + '\" --nic2 nat')
    2.79 +        Cygwin.vboxExecute('storagectl ' + vm_name + ' --name SATA --add sata --portcount 2')
    2.80  
    2.81      #create new SecurityDVM with automatically generated name from template (thread safe)        
    2.82      def newSDVM(self):
    2.83 @@ -436,10 +436,10 @@
    2.84          return vm_name
    2.85      
    2.86      # attach storage image to controller
    2.87 -    def storageAttach(self, vm_name):
    2.88 +    def attachStorage(self, vm_name):
    2.89          if self.isStorageAttached(vm_name):
    2.90 -            self.storageDetach(vm_name)
    2.91 -        Cygwin.checkResult(Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium \"'+ VMManager.machineFolder + '\SecurityDVM\SecurityDVM.vmdk\"'))
    2.92 +            self.detachStorage(vm_name)
    2.93 +        Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium \"'+ VMManager.machineFolder + '\SecurityDVM\SecurityDVM.vmdk\"')
    2.94      
    2.95      # return true if storage is attached 
    2.96      def isStorageAttached(self, vm_name):
    2.97 @@ -447,12 +447,12 @@
    2.98          return (info['SATA-0-0']!='none')
    2.99      
   2.100      # detach storage from controller
   2.101 -    def storageDetach(self, vm_name):
   2.102 +    def detachStorage(self, vm_name):
   2.103          if self.isStorageAttached(vm_name):
   2.104 -            Cygwin.checkResult(Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium none'))
   2.105 +            Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium none')
   2.106      
   2.107      def changeStorageType(self, filename, storage_type):
   2.108 -        Cygwin.checkResult(Cygwin.vboxExecute('modifyhd \"' + filename + '\" --type ' + storage_type))
   2.109 +        Cygwin.vboxExecute('modifyhd \"' + filename + '\" --type ' + storage_type)
   2.110                  
   2.111      # list storage snaphots for VM
   2.112      def updateTemplate(self):
   2.113 @@ -462,29 +462,29 @@
   2.114          self.waitShutdown(self.vmRootName)
   2.115          
   2.116          # check for updates
   2.117 -        self.genCertificateISO(self.vmRootName)
   2.118 -        self.attachCertificateISO(self.vmRootName)
   2.119 +        self.genCertificate(self.vmRootName)
   2.120 +        self.attachCertificate(self.vmRootName)
   2.121          
   2.122          #templateUUID = self.getVMInfo(self.vmRootName)["SATA-ImageUUID-0-0"] #TODO: // verify value
   2.123          templateUUID = self.getTemplateUUID()
   2.124          
   2.125 -        self.storageDetach(self.vmRootName)
   2.126 +        self.detachStorage(self.vmRootName)
   2.127          self.removeSnapshots(templateUUID)
   2.128          
   2.129          template_storage = VMManager.machineFolder + '\\' + self.vmRootName + '\\' + self.vmRootName + '.vmdk'
   2.130          #TODO:// modify to take vm name as argument
   2.131          self.changeStorageType(template_storage,'normal')
   2.132 -        self.storageAttach(self.vmRootName)
   2.133 +        self.attachStorage(self.vmRootName)
   2.134          self.startVM(self.vmRootName)
   2.135 -        self.waitStartup(self.vmRootName, timeout_ms = 30000)
   2.136 +        self.waitStartup(self.vmRootName)
   2.137          
   2.138          tmp_ip = self.getHostOnlyIP(self.vmRootName)
   2.139          tmp_machine_folder = Cygwin.cygPath(VMManager.machineFolder)
   2.140 -        Cygwin.checkResult(Cygwin.sshExecute('"sudo apt-get -y update"', tmp_ip, 'osecuser', tmp_machine_folder + '/' + self.vmRootName + '/dvm_key'))
   2.141 -        Cygwin.checkResult(Cygwin.sshExecute('"sudo apt-get -y upgrade"', tmp_ip, 'osecuser', tmp_machine_folder + '/' + self.vmRootName + '/dvm_key'))
   2.142 +        Cygwin.sshExecute('"sudo apt-get -y update"', tmp_ip, 'osecuser', tmp_machine_folder + '/' + self.vmRootName + '/dvm_key')
   2.143 +        Cygwin.sshExecute('"sudo apt-get -y upgrade"', tmp_ip, 'osecuser', tmp_machine_folder + '/' + self.vmRootName + '/dvm_key')
   2.144          
   2.145          #check if reboot is required
   2.146 -        result = Cygwin.checkResult(Cygwin.sshExecute('"if [ -f /var/run/reboot-required ]; then echo \\\"Yes\\\"; fi"', tmp_ip, 'osecuser', tmp_machine_folder + '/' + self.vmRootName + '/dvm_key'))
   2.147 +        result = Cygwin.sshExecute('"if [ -f /var/run/reboot-required ]; then echo \\\"Yes\\\"; fi"', tmp_ip, 'osecuser', tmp_machine_folder + '/' + self.vmRootName + '/dvm_key')
   2.148          if "Yes" in result[1]:
   2.149              self.stopVM(self.vmRootName)
   2.150              self.waitShutdown(self.vmRootName)
   2.151 @@ -494,9 +494,9 @@
   2.152          #self.hibernateVM(self.vmRootName)
   2.153          self.stopVM(self.vmRootName)
   2.154          self.waitShutdown(self.vmRootName)
   2.155 -        self.storageDetach(self.vmRootName)
   2.156 +        self.detachStorage(self.vmRootName)
   2.157          self.changeStorageType(template_storage,'immutable')
   2.158 -        self.storageAttach(self.vmRootName)
   2.159 +        self.attachStorage(self.vmRootName)
   2.160          
   2.161          #self.start()
   2.162  
   2.163 @@ -504,7 +504,7 @@
   2.164      #"SATA-ImageUUID-0-0"="d0af827d-f13a-49be-8ac1-df20b13bda83"
   2.165      @staticmethod    
   2.166      def getDiskImages():
   2.167 -        results = Cygwin.checkResult(Cygwin.vboxExecute('list hdds'))[1]
   2.168 +        results = Cygwin.vboxExecute('list hdds')[1]
   2.169          results = results.replace('Parent UUID', 'Parent')
   2.170          items = list( "UUID:"+result for result in results.split('UUID:') if result != '')
   2.171          
   2.172 @@ -540,26 +540,26 @@
   2.173                  
   2.174      def removeImage(self, imageUUID):
   2.175          logger.debug('removing snapshot ' + imageUUID)
   2.176 -        Cygwin.checkResult(Cygwin.vboxExecute('closemedium disk {' + imageUUID + '} --delete'))#[1]
   2.177 +        Cygwin.vboxExecute('closemedium disk {' + imageUUID + '} --delete')
   2.178          # parse result 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
   2.179      
   2.180      #remove VM from the system. should be used on VMs returned by listSDVMs    
   2.181      def removeVM(self, vm_name):
   2.182          logger.info('Removing ' + vm_name)
   2.183          
   2.184 -        Cygwin.checkResult(Cygwin.vboxExecute('unregistervm ' + vm_name + ' --delete'))
   2.185 +        Cygwin.vboxExecute('unregistervm ' + vm_name + ' --delete')
   2.186          #TODO:// try to close medium if still existing
   2.187 -        #Cygwin.checkResult(Cygwin.vboxExecute('closemedium disk {' + hdd['UUID'] + '} --delete'))#[1]
   2.188 +        #Cygwin.vboxExecute('closemedium disk {' + hdd['UUID'] + '} --delete')
   2.189          self.removeVMFolder(vm_name)
   2.190      
   2.191      def removeVMFolder(self, vm_name):
   2.192          machineFolder = Cygwin.cygPath(VMManager.machineFolder)
   2.193 -        Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '\\\"'))
   2.194 +        Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '\\\"')
   2.195      
   2.196      # start VM
   2.197      def startVM(self, vm_name):
   2.198          logger.info('Starting ' +  vm_name)
   2.199 -        #TODO: modify to use Cygwin.checkResult() of make it retry 3 times
   2.200 +        Cygwin.vboxExecute('guestproperty set ' + vm_name + ' SDVMStarted False')
   2.201          result = Cygwin.vboxExecute('startvm ' + vm_name + ' --type headless' )
   2.202          while 'successfully started' not in result[1]:
   2.203              logger.error("Failed to start SDVM: " + vm_name + " retrying")
   2.204 @@ -575,19 +575,23 @@
   2.205      # stop VM
   2.206      def stopVM(self, vm_name):
   2.207          logger.info('Sending shutdown signal to ' + vm_name)
   2.208 -        Cygwin.checkResult(Cygwin.sshExecute( '"sudo shutdown -h now"', self.getHostOnlyIP(vm_name), 'osecuser', Cygwin.cygPath(VMManager.machineFolder) + '/' + vm_name + '/dvm_key' ))
   2.209 +        Cygwin.sshExecute( '"sudo shutdown -h now"', self.getHostOnlyIP(vm_name), 'osecuser', Cygwin.cygPath(VMManager.machineFolder) + '/' + vm_name + '/dvm_key' )
   2.210 +        Cygwin.vboxExecute('guestproperty set ' + vm_name + ' SDVMStarted False')
   2.211      
   2.212      # stop VM
   2.213      def hibernateVM(self, vm_name):
   2.214          logger.info('Sending hibernate-disk signal to ' + vm_name)
   2.215 -        Cygwin.checkResult(Cygwin.sshBackgroundExecute( '"sudo hibernate-disk"', self.getHostOnlyIP(vm_name), 'osecuser', Cygwin.cygPath(VMManager.machineFolder) + '/' + vm_name + '/dvm_key', wait_return=False))
   2.216 +        Cygwin.sshBackgroundExecute( '"sudo hibernate-disk"', self.getHostOnlyIP(vm_name), 'osecuser', Cygwin.cygPath(VMManager.machineFolder) + '/' + vm_name + '/dvm_key', wait_return=False)
   2.217 +        Cygwin.vboxExecute('guestproperty set ' + vm_name + ' SDVMStarted False')
   2.218              
   2.219      # poweroff VM
   2.220      def poweroffVM(self, vm_name):
   2.221          if not self.isVMRunning(vm_name):
   2.222              return
   2.223          logger.info('Powering off ' + vm_name)
   2.224 -        return Cygwin.checkResult(Cygwin.vboxExecute('controlvm ' + vm_name + ' poweroff'))
   2.225 +        Cygwin.vboxExecute('controlvm ' + vm_name + ' poweroff')
   2.226 +        Cygwin.vboxExecute('guestproperty set ' + vm_name + ' SDVMStarted False')
   2.227 +    
   2.228      
   2.229      # return the hostOnly IP for a running guest or the host    
   2.230      def getHostOnlyIP(self, vm_name):
   2.231 @@ -596,7 +600,7 @@
   2.232              return VMManager.hostonlyIF['IPAddress']
   2.233          else:
   2.234              logger.info('Getting hostOnly IP address ' + vm_name)
   2.235 -            result = Cygwin.checkResult(Cygwin.vboxExecute('guestproperty get ' + vm_name + ' /VirtualBox/GuestInfo/Net/0/V4/IP'))
   2.236 +            result = Cygwin.vboxExecute('guestproperty get ' + vm_name + ' /VirtualBox/GuestInfo/Net/0/V4/IP')
   2.237              if result=='':
   2.238                  return None
   2.239              result = result[1]
   2.240 @@ -606,43 +610,54 @@
   2.241          
   2.242      # return the description set for an existing VM
   2.243      def getVMInfo(self, vm_name):
   2.244 -        results = Cygwin.checkResult(Cygwin.vboxExecute('showvminfo ' + vm_name + ' --machinereadable'))[1]
   2.245 +        results = Cygwin.vboxExecute('showvminfo ' + vm_name + ' --machinereadable')[1]
   2.246          props = dict((k.strip().strip('"'),v.strip().strip('"')) for k,v in (line.split('=', 1) for line in results.splitlines()))
   2.247          return props
   2.248      
   2.249      #generates ISO containing authorized_keys for use with guest VM
   2.250 -    def genCertificateISO(self, vm_name):
   2.251 +    def genCertificate(self, vm_name):
   2.252          machineFolder = Cygwin.cygPath(VMManager.machineFolder)
   2.253          # remove .ssh folder if exists
   2.254 -        Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"'))
   2.255 +        Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"')
   2.256          # remove .ssh folder if exists
   2.257 -        Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '/dvm_key\\\"'))
   2.258 +        Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '/dvm_key\\\"')
   2.259          # create .ssh folder in vm_name
   2.260 -        Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/mkdir -p \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"'))
   2.261 +        Cygwin.bashExecute('/usr/bin/mkdir -p \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"')
   2.262          # generate dvm_key pair in vm_name / .ssh     
   2.263 -        Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/ssh-keygen -q -t rsa -N \\\"\\\" -C \\\"' + vm_name + '\\\" -f \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key\\\"'))
   2.264 +        Cygwin.bashExecute('/usr/bin/ssh-keygen -q -t rsa -N \\\"\\\" -C \\\"' + vm_name + '\\\" -f \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key\\\"')
   2.265          # move out private key
   2.266 -        Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/mv \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key\\\" \\\"' + machineFolder + '/' + vm_name + '\\\"'))
   2.267 +        Cygwin.bashExecute('/usr/bin/mv \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key\\\" \\\"' + machineFolder + '/' + vm_name + '\\\"')
   2.268          # set permissions for private key
   2.269 -        Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/chmod 500 \\\"' + machineFolder + '/' + vm_name + '/dvm_key\\\"'))
   2.270 +        Cygwin.bashExecute('/usr/bin/chmod 500 \\\"' + machineFolder + '/' + vm_name + '/dvm_key\\\"')
   2.271          # rename public key to authorized_keys
   2.272 -        Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/mv \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key.pub\\\" \\\"' + machineFolder + '/' + vm_name + '/.ssh/authorized_keys\\\"'))
   2.273 +        Cygwin.bashExecute('/usr/bin/mv \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key.pub\\\" \\\"' + machineFolder + '/' + vm_name + '/.ssh/authorized_keys\\\"')
   2.274          # set permissions for authorized_keys
   2.275 -        Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/chmod 500 \\\"' + machineFolder + '/' + vm_name + '/.ssh/authorized_keys\\\"'))
   2.276 +        Cygwin.bashExecute('/usr/bin/chmod 500 \\\"' + machineFolder + '/' + vm_name + '/.ssh/authorized_keys\\\"')
   2.277          # generate iso image with .ssh/authorized keys
   2.278 -        Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/genisoimage -J -R -o \\\"' + machineFolder + '/' + vm_name + '/'+ vm_name + '.iso\\\" \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"'))
   2.279 +        Cygwin.bashExecute('/usr/bin/genisoimage -J -R -o \\\"' + machineFolder + '/' + vm_name + '/'+ vm_name + '.iso\\\" \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"')
   2.280      
   2.281      # attaches generated ssh public cert to guest vm
   2.282 -    def attachCertificateISO(self, vm_name):
   2.283 -        result = Cygwin.checkResult(Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 1 --device 0 --type dvddrive --mtype readonly --medium \"' + VMManager.machineFolder + '\\' + vm_name + '\\'+ vm_name + '.iso\"'))
   2.284 -        return result
   2.285 +    def attachCertificate(self, vm_name):
   2.286 +        if self.isCertificateAttached(vm_name):
   2.287 +            self.detachCertificate(vm_name)
   2.288 +        Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 1 --device 0 --type dvddrive --mtype readonly --medium \"' + VMManager.machineFolder + '\\' + vm_name + '\\'+ vm_name + '.iso\"')
   2.289      
   2.290 +    # return true if storage is attached 
   2.291 +    def isCertificateAttached(self, vm_name):
   2.292 +        info = self.getVMInfo(vm_name)
   2.293 +        return (info['SATA-1-0']!='none')
   2.294 +    
   2.295 +    # detach storage from controller
   2.296 +    def detachCertificate(self, vm_name):
   2.297 +        if self.isCertificateAttached(vm_name):
   2.298 +            Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 1 --device 0 --type hdd --medium none')
   2.299 +            
   2.300      # wait for machine to come up
   2.301 -    def waitStartup(self, vm_name, timeout_ms = 1000):
   2.302 -        #Cygwin.checkResult(Cygwin.vboxExecute('guestproperty wait ' + vm_name + ' SDVMStarted --timeout ' + str(timeout_ms) + ' --fail-on-timeout', try_count = 60))
   2.303 +    def waitStartup(self, vm_name):
   2.304 +        #Cygwin.vboxExecute('guestproperty wait ' + vm_name + ' SDVMStarted --timeout ' + str(timeout_ms) + ' --fail-on-timeout', try_count = 60)
   2.305          started = False
   2.306          while not started:
   2.307 -            result = Cygwin.checkResult(Cygwin.vboxExecute('guestproperty get ' + vm_name + ' SDVMStarted'))[1]
   2.308 +            result = Cygwin.vboxExecute('guestproperty get ' + vm_name + ' SDVMStarted')[1]
   2.309              if "Value: True" in result:
   2.310                  started = True
   2.311              else:
   2.312 @@ -657,7 +672,7 @@
   2.313      
   2.314      #Small function to check if the mentioned location is a directory
   2.315      def isDirectory(self, path):
   2.316 -        result = Cygwin.checkResult(Cygwin.cmdExecute('dir ' + path + ' | FIND ".."'))
   2.317 +        result = Cygwin.cmdExecute('dir ' + path + ' | FIND ".."')
   2.318          return string.find(result[1], 'DIR',)
   2.319      
   2.320      def genNetworkDrive(self):
   2.321 @@ -798,7 +813,7 @@
   2.322              else:
   2.323                  browser = '\\\"/usr/bin/chromium\\\"'
   2.324              self.vmm.browsingManager.started.wait() 
   2.325 -            result = Cygwin.checkResult(Cygwin.sshExecuteX11(browser, self.vmm.browsingManager.ip_addr, 'osecuser', Cygwin.cygPath(self.vmm.getMachineFolder()) + '/' + self.vmm.browsingManager.vm_name + '/dvm_key'))
   2.326 +            result = Cygwin.sshExecuteX11(browser, self.vmm.browsingManager.ip_addr, 'osecuser', Cygwin.cygPath(self.vmm.getMachineFolder()) + '/' + self.vmm.browsingManager.vm_name + '/dvm_key')
   2.327              self.vmm.backupFile('/home/osecuser/.config/chromium', self.vmm.browsingManager.appDataDir + '/OpenSecurity/')
   2.328          except:
   2.329              logger.info("BrowsingHandler closing. Restarting browsing SDVM.")
   2.330 @@ -849,13 +864,13 @@
   2.331              
   2.332              try:
   2.333                  self.vm_name = self.vmm.newSDVM()
   2.334 -                self.vmm.storageAttach(self.vm_name)
   2.335 -                self.vmm.genCertificateISO(self.vm_name)
   2.336 -                self.vmm.attachCertificateISO(self.vm_name)
   2.337 +                self.vmm.attachStorage(self.vm_name)
   2.338 +                self.vmm.genCertificate(self.vm_name)
   2.339 +                self.vmm.attachCertificate(self.vm_name)
   2.340                  
   2.341                  self.vmm.startVM(self.vm_name)
   2.342                  
   2.343 -                self.ip_addr = self.vmm.waitStartup(self.vm_name, timeout_ms=30000)
   2.344 +                self.ip_addr = self.vmm.waitStartup(self.vm_name)
   2.345                  if self.ip_addr == None:
   2.346                      logger.error("Failed to get ip address")
   2.347                      continue
   2.348 @@ -893,11 +908,11 @@
   2.349                  self.appDataDir = Cygwin.cygPath(path)
   2.350                  logger.info("Restoring browser settings in AppData dir " + self.appDataDir)
   2.351                  # create OpenSecurity settings dir on local machine user home /AppData/Roaming 
   2.352 -                Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/mkdir -p \\\"' + self.appDataDir + '/OpenSecurity\\\"'))
   2.353 +                Cygwin.bashExecute('/usr/bin/mkdir -p \\\"' + self.appDataDir + '/OpenSecurity\\\"')
   2.354                  # create chromium settings dir on local machine if not existing
   2.355 -                Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/mkdir -p \\\"' + self.appDataDir + '/OpenSecurity/chromium\\\"'))
   2.356 +                Cygwin.bashExecute('/usr/bin/mkdir -p \\\"' + self.appDataDir + '/OpenSecurity/chromium\\\"')
   2.357                  # create chromium settings dir on remote machine if not existing
   2.358 -                Cygwin.checkResult(Cygwin.sshExecute('"mkdir -p \\\"/home/osecuser/.config\\\""', self.ip_addr, 'osecuser', Cygwin.cygPath(self.vmm.getMachineFolder()) + '/' + self.vm_name + '/dvm_key'))
   2.359 +                Cygwin.sshExecute('"mkdir -p \\\"/home/osecuser/.config\\\""', self.ip_addr, 'osecuser', Cygwin.cygPath(self.vmm.getMachineFolder()) + '/' + self.vm_name + '/dvm_key')
   2.360                  #restore settings on vm
   2.361                  self.vmm.restoreFile(self.appDataDir + '/OpenSecurity/chromium', '/home/osecuser/.config/')
   2.362                  self.started.set()
   2.363 @@ -963,9 +978,9 @@
   2.364                  showTrayMessage('Mounting device...', 7000)
   2.365                  if (self.attachedRSDs and False) or (new_device not in self.attachedRSDs.values()):
   2.366                      new_sdvm = self.vmm.newSDVM()
   2.367 -                    self.vmm.storageAttach(new_sdvm)
   2.368 +                    self.vmm.attachStorage(new_sdvm)
   2.369                      self.vmm.startVM(new_sdvm)
   2.370 -                    new_ip = self.vmm.waitStartup(new_sdvm, timeout_ms=30000)
   2.371 +                    new_ip = self.vmm.waitStartup(new_sdvm)
   2.372                      if new_ip == None:
   2.373                          logger.error("Error getting IP address of SDVM. Cleaning up.")
   2.374                          self.vmm.poweroffVM(new_sdvm)