# HG changeset patch # User BarthaM # Date 1386163810 -3600 # Node ID 616dca19f52ceed61cc6fb9aa66edc4b888ea6e1 # Parent 903480cebdfbc858571c25a7334846c4973c45f1 small changes in vmmanager.py diff -r 903480cebdfb -r 616dca19f52c server/vmmanager/vmmanager.py --- a/server/vmmanager/vmmanager.py Tue Dec 03 18:04:46 2013 +0100 +++ b/server/vmmanager/vmmanager.py Wed Dec 04 14:30:10 2013 +0100 @@ -59,6 +59,7 @@ print res_stderr return result, res_stdout, res_stderr + # return hosty system properties def getSystemProperties(self): cmd = 'VBoxManage list systemproperties' result = self.execute(cmd) @@ -66,44 +67,12 @@ return None props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result[1].strip().splitlines())) return props - + + # return the folder containing the guest VMs def getDefaultMachineFolder(self): return self.systemProperties["Default machine folder"] - def createVM(self, vm_name): - hostonly_if = self.getHostOnlyIFs() - cmd = 'VBoxManage createvm --name ' + vm_name, ' --ostype Debian --register' - self.execute(cmd) - cmd = 'VBoxManage modifyvm ' + vm_name + ' --memory 512 --vram 10 --cpus 1 --usb on --usbehci on --nic1 hostonly --hostonlyadapter1 ' + hostonly_if['Name'] + ' --nic2 nat' - self.execute(cmd) - cmd = 'VBoxManage storagectl ' + vm_name + ' --name contr1 --add sata --sataportcount 2' - self.execute(cmd) - cmd = 'VBoxManage storageattach ' + vm_name + ' --storagectl contr1 --port 0 --device 0 --type hdd --mtype normal --medium C:\Users\BarthaM\VirtualBox VMs\SecurityVM\SecurityVM.vdi' - self.execute(cmd) - return - - def attachRSD(self, vm_name, filter): - cmd = 'VBoxManage usbfilter add 0 --target ' + vm_name + ' --name OpenSecurityRSD --vendorid ' + filter.vendorid + ' --productid ' + filter.productid + ' --revision ' + filter.revision - print self.execute(cmd) - - def removeVM(self, vm_name): - print('removing ' + vm_name) - cmd = 'VBoxManage unregistervm', vm_name, '--delete' - print self.execute(cmd) - machineFolder = self.getDefaultMachineFolder() - cmd = self.cygwin_path+'bash.exe --login -c \"rm -rf ' + machineFolder + '\\' + vm_name + '*\"' - print self.execute(cmd) - - def startVM(self, vm_name): - print('starting ' + vm_name) - cmd = 'VBoxManage startvm ' + vm_name + ' --type headless' - print self.execute(cmd) - - def stopVM(self, vm_name): - print('stopping ' + vm_name) - cmd = 'VBoxManage controlvm ' + vm_name + ' poweroff' - print self.execute(cmd) - + #list the hostonly IFs exposed by the VBox host def getHostOnlyIFs(self): cmd = ['VBoxManage list hostonlyifs'] result = self.execute(cmd) @@ -112,15 +81,6 @@ props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result.strip().splitlines())) return props - def getHostOnlyIP(self, vm_name): - print('gettting hostOnly IP address ' + vm_name) - cmd = 'VBoxManage guestproperty get ' + vm_name + ' /VirtualBox/GuestInfo/Net/0/V4/IP' - result = self.execute(cmd) - if result=='': - return None - result = result[1] - return result[result.index(':')+1:].strip() - def listRSDS(self): cmd = 'VBoxManage list usbhost' results = self.execute(cmd) @@ -144,12 +104,14 @@ print filter return rsds + # list all existing VMs registered with VBox def listVM(self): cmd = 'VBoxManage list vms' result = self.execute(cmd) vms = list(k.strip().strip('"') for k,_ in (line.split(' ') for line in result.splitlines())) return vms + # list existing SDVMs def listSDVM(self): vms = self.listVM() svdms = [] @@ -158,6 +120,7 @@ svdms.append(vm) return svdms + # generate valid (not already existing SDVM name). necessary for creating a new VM def generateSDVMName(self): vms = self.listVM() for i in range(0,999): @@ -165,12 +128,90 @@ return self.vmRootName+str(i) return '' + # return the RSDs attached to all existing SDVMs + def getAttachedRSDs(self): + vms = self.listSDVM() + attached_devices = dict() + for vm in vms: + rsd_filter = self.getUSBFilter(vm) + if filter != None: + attached_devices[vm] = rsd_filter + return attached_devices + + # configures hostonly networking and DHCP server. requires admin rights + def configureHostNetworking(self): + #cmd = 'vboxmanage list hostonlyifs' + #self.execute(cmd) + #cmd = 'vboxmanage hostonlyif remove \"VirtualBox Host-Only Ethernet Adapter\"' + #self.execute(cmd) + #cmd = 'vboxmanage hostonlyif create' + #self.execute(cmd) + cmd = 'vboxmanage hostonlyif ipconfig \"VirtualBox Host-Only Ethernet Adapter\" --ip 192.168.56.1 --netmask 255.255.255.0' + self.execute(cmd) + #cmd = 'vboxmanage dhcpserver add' + #self.execute(cmd) + cmd = 'vboxmanage dhcpserver modify --ifname \"VirtualBox Host-Only Ethernet Adapter\" --ip 192.168.56.1 --netmask 255.255.255.0 --lowerip 192.168.56.100 --upperip 192.168.56.255' + self.execute(cmd) + + #create new virtual machine instance based on template vm named SecurityVM (\SecurityVM\SecurityVM.vdi) + def createVM(self, vm_name): + hostonly_if = self.getHostOnlyIFs() + machineFolder = self.getDefaultMachineFolder() + cmd = 'VBoxManage createvm --name ' + vm_name, ' --ostype Debian --register' + self.execute(cmd) + cmd = 'VBoxManage modifyvm ' + vm_name + ' --memory 512 --vram 10 --cpus 1 --usb on --usbehci on --nic1 hostonly --hostonlyadapter1 ' + hostonly_if['Name'] + ' --nic2 nat' + self.execute(cmd) + cmd = 'VBoxManage storagectl ' + vm_name + ' --name contr1 --add sata --sataportcount 2' + self.execute(cmd) + cmd = 'VBoxManage storageattach ' + vm_name + ' --storagectl contr1 --port 0 --device 0 --type hdd --mtype normal --medium '+ machineFolder + '\SecurityVM\SecurityVM.vdi' + self.execute(cmd) + return + + #remove VM from the system. should be used on VMs returned by listSDVMs + def removeVM(self, vm_name): + print('removing ' + vm_name) + cmd = 'VBoxManage unregistervm', vm_name, '--delete' + print self.execute(cmd) + machineFolder = self.getDefaultMachineFolder() + cmd = self.cygwin_path+'bash.exe --login -c \"rm -rf ' + machineFolder + '\\' + vm_name + '*\"' + print self.execute(cmd) + + # start VM + def startVM(self, vm_name): + print('starting ' + vm_name) + cmd = 'VBoxManage startvm ' + vm_name + ' --type headless' + print self.execute(cmd) + + # stop VM + def stopVM(self, vm_name): + print('stopping ' + vm_name) + cmd = 'VBoxManage controlvm ' + vm_name + ' poweroff' + print self.execute(cmd) + + # return the hostOnly IP for a running guest + def getHostOnlyIP(self, vm_name): + print('gettting hostOnly IP address ' + vm_name) + cmd = 'VBoxManage guestproperty get ' + vm_name + ' /VirtualBox/GuestInfo/Net/0/V4/IP' + result = self.execute(cmd) + if result=='': + return None + result = result[1] + return result[result.index(':')+1:].strip() + + # attach removable storage device to VM by provision of filter + def attachRSD(self, vm_name, rsd_filter): + cmd = 'VBoxManage usbfilter add 0 --target ' + vm_name + ' --name OpenSecurityRSD --vendorid ' + rsd_filter.vendorid + ' --productid ' + rsd_filter.productid + ' --revision ' + rsd_filter.revision + print self.execute(cmd) + + + # return the description set for an existing VM def getVMInfo(self, vm_name): cmd = 'VBoxManage showvminfo ' + vm_name + ' --machinereadable' results = self.execute(cmd) props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split('=', 1) for line in results.splitlines())) return props + # return the configured USB filter for an existing VM def getUSBFilter(self, vm_name): props = self.getVMInfo(vm_name) keys = set(['USBFilterVendorId1', 'USBFilterProductId1', 'USBFilterRevision1']) @@ -180,15 +221,6 @@ usb_filter = USBFilter(props['USBFilterVendorId1'], props['USBFilterProductId1'], props['USBFilterRevision1']) return usb_filter - def getAttachedRSDs(self): - vms = self.listSDVM() - attached_devices = dict() - for vm in vms: - filter = self.getUSBFilter(vm) - if filter != None: - attached_devices[vm] = filter - return attached_devices - #generates ISO containing authorized_keys for use with guest VM def genCertificateISO(self, vm_name): machineFolder = self.getDefaultMachineFolder() @@ -239,39 +271,23 @@ self.startVM(new_sdvm) # executes command over ssh on guest vm - def sshGuestExecute(self, vm_name, prog): + def sshGuestExecute(self, vm_name, prog, user_name='opensec'): # get vm ip address = self.getHostOnlyIP(vm_name) machineFolder = self.getDefaultMachineFolder() # run command - cmd = self.cygwin_path+'bash.exe --login -c \"ssh -i \\\"' + machineFolder + '\\' + vm_name + '\\dvm_key\\\" bartham@' + address + ' ' + prog + '\"' + cmd = self.cygwin_path+'bash.exe --login -c \"ssh -i \\\"' + machineFolder + '\\' + vm_name + '\\dvm_key\\\" ' + user_name + '@' + address + ' ' + prog + '\"' return self.execute(cmd) # executes command over ssh on guest vm with X forwarding - def sshGuestX11Execute(self, vm_name, prog): + def sshGuestX11Execute(self, vm_name, prog, user_name='opensec'): #TODO: verify if X server is running on user account #TODO: set DISPLAY accordingly address = self.getHostOnlyIP(vm_name) machineFolder = self.getDefaultMachineFolder() # run command - cmd = self.cygwin_path+'bash.exe --login -c \"DISPLAY=:0 ssh -Y -i \\\"' + machineFolder + '\\' + vm_name + '\\dvm_key\\\" bartham@' + address + ' ' + prog + '\"' + cmd = self.cygwin_path+'bash.exe --login -c \"DISPLAY=:0 ssh -Y -i \\\"' + machineFolder + '\\' + vm_name + '\\dvm_key\\\" ' + user_name + '@' + address + ' ' + prog + '\"' return self.execute(cmd) - - # configures hostonly networking and DHCP server - # requires admin rights - def configureHostNetworking(self): - #cmd = 'vboxmanage list hostonlyifs' - #self.execute(cmd) - #cmd = 'vboxmanage hostonlyif remove \"VirtualBox Host-Only Ethernet Adapter\"' - #self.execute(cmd) - #cmd = 'vboxmanage hostonlyif create' - #self.execute(cmd) - cmd = 'vboxmanage hostonlyif ipconfig \"VirtualBox Host-Only Ethernet Adapter\" --ip 192.168.56.1 --netmask 255.255.255.0' - self.execute(cmd) - #cmd = 'vboxmanage dhcpserver add' - #self.execute(cmd) - cmd = 'vboxmanage dhcpserver modify --ifname \"VirtualBox Host-Only Ethernet Adapter\" --ip 192.168.56.1 --netmask 255.255.255.0 --lowerip 192.168.56.100 --upperip 192.168.56.255' - self.execute(cmd) # executes NET USE and connects to samba share on guestos def netUse(self, vm_name):