def unbase(self): """unbase the disk files to have no dependency""" debugmsg(2, "AnyVBoxEmuDevice::unbase()") r = send(self.p, 'vbox unbase %s' % self.name) r = send(self.p, 'vbox unbase %s' % self.name) return r
def clean(self): """clean the disk files for this VBox instance""" debugmsg(2, "AnyVBoxEmuDevice::clean()") r = send(self.p, 'vbox clean %s' % self.name) r = send(self.p, 'vbox clean %s' % self.name) return r
def delete(self): """delete the emulated device instance in Qemu""" try: send(self.p, 'qemu delete %s' % self.name) except: pass
def disconnect_from_dynamips(self, local_port): #delete the emulated device side of UDP connection debugmsg(2, "AnyEmuDevice::disconnect_from_dynamips(%s)" % str(local_port)) send(self.p, 'qemu delete_udp %s %i' % (self.name, local_port)) if self.nios.has_key(local_port): del self.nios[local_port]
def __init__(self, name, port=11525): debugmsg(2, "VBox::__init__(%s, %s)" % (unicode(name), str(port))) self.port = port self.host = name #connect to VBox Wrapper timeout = 60.0 self._type = 'vboxwrapper' if not NOSEND: try: self.s = socket.create_connection((self.host, self.port), timeout) except: raise DynamipsError, '101-Could not connect to vboxwrapper at %s:%i' % ( self.host, self.port) #version checking try: version = send(self, 'vboxwrapper version')[0][4:] except IndexError: # Probably because NOSEND is set version = 'N/A' try: # version formats are a.b.c-RCd (major, minor, sub) = version.split(' ')[0].split('.') try: release_candidate = version.split('-')[1] if release_candidate[:2] == 'RC': rcver = float('.' + release_candidate[2:]) except IndexError: rcver = .999 intver = int(major) * 10000 + int(minor) * 100 + int(sub) + rcver except: #print 'Warning: problem determing vboxwrapper server version on host: %s. Skipping version check' % self.host intver = 999999 if intver < INTVER: raise DynamipsVerError, '101-This version of Dynagen requires at least version %s of vboxwrapper. \n Server %s is runnning version %s.' % ( STRVER, self.host, version) self._version = version #vbox version checking try: version = send(self, 'vbox version')[0] except IndexError: # Probably because NOSEND is set version = 'N/A' self._vbox_version = version[4:] #all other needed variables self.name = name self.devices = [] self._baseconsole = 3501 self.udp = 20900 self.default_udp = self.udp self.starting_udp = self.udp self._workingdir = None self.configchange = False
def delete(self): """delete the virtualized device instance in VBox""" debugmsg(2, "AnyVBoxEmuDevice::delete()") try: send(self.p, 'vbox delete %s' % self.name) except: pass
def delete(self): """delete the emulated device instance in Qemu""" debugmsg(2, "AnyEmuDevice::delete()") try: send(self.p, 'qemu delete %s' % self.name) except: pass
def __init__(self, name, port=11525): debugmsg(2, "VBox::__init__(%s, %s)" % (unicode(name), str(port))) self.port = port self.host = name #connect to VBox Wrapper timeout = 60.0 self._type = 'vboxwrapper' if not NOSEND: try: self.s = socket.create_connection((self.host, self.port), timeout) except: raise DynamipsError, '101-Could not connect to vboxwrapper at %s:%i' % (self.host, self.port) #version checking try: version = send(self, 'vboxwrapper version')[0][4:] except IndexError: # Probably because NOSEND is set version = 'N/A' try: # version formats are a.b.c-RCd (major, minor, sub) = version.split(' ')[0].split('.') try: release_candidate = version.split('-')[1] if release_candidate[:2] == 'RC': rcver = float('.' + release_candidate[2:]) except IndexError: rcver = .999 intver = int(major) * 10000 + int(minor) * 100 + int(sub) + rcver except: #print 'Warning: problem determing vboxwrapper server version on host: %s. Skipping version check' % self.host intver = 999999 if intver < INTVER: raise DynamipsVerError, '101-This version of Dynagen requires at least version %s of vboxwrapper. \n Server %s is runnning version %s.' % (STRVER, self.host, version) self._version = version #vbox version checking try: version = send(self, 'vbox version')[0] except IndexError: # Probably because NOSEND is set version = 'N/A' self._vbox_version = version[4:] #all other needed variables self.name = name self.devices = [] self._baseconsole = 3501 self.udp = 20900 self.default_udp = self.udp self.starting_udp = self.udp self._workingdir = None self.configchange = False
def __init__(self, vbox, name): debugmsg( 2, "AnyVBoxEmuDevice::__init__(%s, %s)" % (unicode(vbox), unicode(name))) self.p = vbox #create a twin variable to self.p but with name self.dynamips to keep things working elsewhere self.dynamips = vbox self._instance = self._instance_count self._instance_count += 1 if name == None: self.name = 'vbox_emu' + str(self._instance) else: self.name = name self._image = None self._console = None self.state = 'stopped' self.defaults = { 'image': None, 'nics': 6, 'netcard': 'automatic', 'guestcontrol_user': None, 'guestcontrol_password': None, 'first_nic_managed': True, 'headless_mode': False, 'console_support': False, 'console_telnet_server': False, } self._nics = self.defaults['nics'] self._netcard = self.defaults['netcard'] self._capture = {} self._guestcontrol_user = self.defaults['guestcontrol_user'] self._guestcontrol_password = self.defaults['guestcontrol_password'] self._first_nic_managed = self.defaults['first_nic_managed'] self._headless_mode = self.defaults['headless_mode'] self._console_support = self.defaults['console_support'] self._console_telnet_server = self.defaults['console_telnet_server'] self.nios = {} for i in range(self._nics): self.nios[i] = None send(self.p, 'vbox create %s %s' % (self.vbox_dev_type, self.name)) self.p.devices.append(self) #set the console to VBox baseconsole self.track = tracker.portTracker() self._console = self.track.allocateTcpPort(self.p.host, self.p.baseconsole) send(self.p, 'vbox setattr %s console %i' % (self.name, self._console)) self.p.baseconsole += 1 self.starttime = int(time.time()) self.suspendtime = self.starttime self.stoptime = self.starttime self.waittime = 0
def delete(self): """delete the virtualized device instance in VBox""" debugmsg(2, "AnyVBoxEmuDevice::delete()") self.track.freeTcpPort(self.p.host, self.console) try: send(self.p, 'vbox delete %s' % self.name) except: pass
def stop(self): """stops the virtualized device instance in VBox""" debugmsg(2, "AnyVBoxEmuDevice::stop()") if self.state == 'stopped': raise DynamipsWarning, 'virtualized device %s is already stopped' % self.name r = send(self.p, 'vbox stop %s' % self.name) r = send(self.p, 'vbox stop %s' % self.name) self.state = 'stopped' return r
def reset(self): """resets the virtualized device instance in VBox""" debugmsg(2, "AnyVBoxEmuDevice::reset()") if self.state == 'stopped': raise DynamipsWarning, 'virtualized device %s is already stopped' % self.name r = send(self.p, 'vbox reset %s' % self.name) r = send(self.p, 'vbox reset %s' % self.name) self.state = 'running' return r
def _setkqemu(self, kqemu): """ Set the kqemu option to be used by this emulated device kqemu: (bool) kqemu activation """ if type(kqemu) != bool: raise DynamipsError, 'invalid kqemu option' send(self.p, 'qemu setattr %s kqemu %s' % (self.name, str(kqemu))) self._kqemu = kqemu
def _setkvm(self, kvm): """ Set the kvm option to be used by this emulated device kvm: (bool) kvm activation """ if type(kvm) != bool: raise DynamipsError, 'invalid kvm option' send(self.p, 'qemu setattr %s kvm %s' % (self.name, str(kvm))) self._kvm = kvm
def _setram(self, ram): """ Set amount of RAM allocated to this emulated device ram: (int) amount of RAM in MB """ if type(ram) != int or ram < 1: raise DynamipsError, 'invalid ram size' send(self.p, 'qemu setattr %s ram %i' % (self.name, ram)) self._ram = ram
def _setnetcard(self, netcard): """ Set the netcard to be used by this emulated device netcard: (str) netcard name """ if type(netcard) not in [str, unicode]: raise DynamipsError, 'invalid netcard' send(self.p, 'qemu setattr %s netcard %s' % (self.name, netcard)) self._netcard = netcard
def _setkey(self, key): """ Set the key for this fw key: key number of this fw """ if type(key) != str: raise DynamipsError, 'invalid key' #TODO verify key send(self.p, 'pemu set_key %s %s' % (self.name, key)) self._key = key
def _setqemuimgpath(self, qemuimgpath): """ Set the path to Qemu-img for this network qemuimgpath: (string) path """ if type(qemuimgpath) not in [str, unicode]: raise DynamipsError, 'invalid Qemu-img path' # send to qemuwrapper encased in quotes to protect spaces send(self, 'qemuwrapper qemu_img_path %s' % '"' + qemuimgpath + '"') self._qemuimgpath = qemuimgpath
def _setconsole(self, console): """ Set console port console: (int) TCP port of console """ if type(console) != int or console < 1 or console > 65535: raise DynamipsError, 'invalid console port' send(self.p, 'pemu set_con_tcp %s %i' % (self.name, console)) self._console = console
def _setserial(self, serial): """ Set the serial for this fw serial: serial number of this fw """ if type(serial) != str: raise DynamipsError, 'invalid serial' #TODO verify serial send(self.p, 'pemu set_serial %s %s' % (self.name, serial)) self._serial = serial
def _setram(self, ram): """ Set amount of RAM allocated to this fw ram: (int) amount of RAM in MB """ if type(ram) != int or ram < 1: raise DynamipsError, 'invalid ram size' send(self.p, 'pemu set_ram %s %i' % (self.name, ram)) self._ram = ram
def _setconsole(self, console): """ Set console port console: (int) TCP port of console """ if type(console) != int or console < 1 or console > 65535: raise DynamipsError, 'invalid console port' send(self.p, 'qemu setattr %s console %i' % (self.name, console)) self._console = console
def _setworkingdir(self, directory): """ Set the working directory for this network directory: (string) the directory """ if type(directory) not in [str, unicode]: raise DynamipsError, 'invalid directory' # send to qemuwrapper encased in quotes to protect spaces send(self, 'qemuwrapper working_dir %s' % '"' + directory + '"') self._workingdir = directory
def _setworkingdir(self, directory): """ Set the working directory for this network directory: (string) the directory """ if type(directory) not in [str, unicode]: raise DynamipsError, 'invalid directory' # send to pemuwrapper encased in quotes to protect spaces send(self, 'pemuwrapper working_dir %s' % '"' + directory + '"') self._workingdir = directory
def start(self): """starts the virtualized device instance in VBox""" debugmsg(2, "AnyVBoxEmuDevice::start()") if self.state == 'running': raise DynamipsWarning, 'virtualized device %s is already running' % self.name r = send(self.p, 'vbox start %s' % self.name) r = send(self.p, 'vbox start %s' % self.name) self.state = 'running' return r
def _setkvm(self, kvm): """ Set the kvm option to be used by this emulated device kvm: (bool) kvm activation """ debugmsg(2, "AnyEmuDevice::_setkvm(%s)" % str(kvm)) if type(kvm) != bool: raise DynamipsError, 'invalid kvm option' send(self.p, 'qemu setattr %s kvm %s' % (self.name, str(kvm))) self._kvm = kvm
def _setserial(self, serial): """ Set the serial for this fw serial: serial number of this fw """ if type(serial) not in [str, unicode]: raise DynamipsError, 'invalid serial' #TODO verify serial if serial: send(self.p, 'qemu setattr %s serial %s' % (self.name, serial)) self._serial = serial
def disconnect_from_emulated_device(self, local_port, remote_emulated_device, remote_port): # disconnect the local emulated device side of UDP connection send(self.p, 'qemu delete_udp %s %i' % (self.name, local_port)) if self.nios.has_key(local_port): del self.nios[local_port] # disconnect the remote emulated device side of UDP connection send(remote_emulated_device.p, 'qemu delete_udp %s %i' % (remote_emulated_device.name, remote_port)) if remote_emulated_device.nios.has_key(remote_port): del remote_emulated_device.nios[remote_port]
def suspend(self): """suspends the virtualized device instance in VBox""" debugmsg(2, "AnyVBoxEmuDevice::suspend()") if self.state == 'suspended': raise DynamipsWarning, 'virtualized device %s is already suspended' % self.name r = send(self.p, 'vbox suspend %s' % self.name) r = send(self.p, 'vbox suspend %s' % self.name) debugmsg(3, "AnyVBoxEmuDevice::suspend(), r = %s" % str(r)) self.state = 'suspended' return r
def _setconsole(self, console): """ Set console port console: (int) TCP port of console """ debugmsg(2, "AnyEmuDevice::_setconsole(%s)" % str(console)) if type(console) != int or console < 1 or console > 65535: raise DynamipsError, 'invalid console port' send(self.p, 'qemu setattr %s console %i' % (self.name, console)) self._console = console
def _setram(self, ram): """ Set amount of RAM allocated to this emulated device ram: (int) amount of RAM in MB """ debugmsg(2, "AnyEmuDevice::_setram(%s)" % str(ram)) if type(ram) != int or ram < 1: raise DynamipsError, 'invalid ram size' send(self.p, 'qemu setattr %s ram %i' % (self.name, ram)) self._ram = ram
def _setqemuimgpath(self, qemuimgpath): """ Set the path to Qemu-img for this network qemuimgpath: (string) path """ debugmsg(2, "Qemu::_setqemuimgpath(%s)" % str(qemuimgpath)) if type(qemuimgpath) not in [str, unicode]: raise DynamipsError, 'invalid Qemu-img path' # send to qemuwrapper encased in quotes to protect spaces send(self, 'qemuwrapper qemu_img_path %s' % '"' + qemuimgpath + '"') self._qemuimgpath = qemuimgpath
def vboxexec(self, command): """sends GuestControl execute commands to the virtualized device instance in VBox""" debugmsg(2, "AnyVBoxEmuDevice::vboxexec(%s)" % str(command)) if self.state != 'running': raise DynamipsError, 'virtualized device %s is not running' % self.name return r = send(self.p, 'vbox exec %s %s' % (self.name, command)) r = send(self.p, 'vbox exec %s %s' % (self.name, command)) #print "ADEBUG: dynagen_vbox_lib.py: r[0][0:10] = %s" % r[0][0:10] return r
def _setkey(self, key): """ Set the key for this pix key: key number of this pix """ if type(key) not in [str, unicode]: raise DynamipsError, 'invalid key' #TODO verify key if key: send(self.p, 'qemu setattr %s key %s' % (self.name, key)) self._key = key
def _setqemupath(self, qemupath): """ Set the path to Qemu for this network qemupath: (string) path """ debugmsg(2, "Qemu::_setqemupath(%s)" % str(qemupath)) if type(qemupath) not in [str, unicode]: raise DynamipsError, 'invalid Qemu path' # send to qemuwrapper encased in quotes to protect spaces send(self, 'qemuwrapper qemu_path %s' % '"' + qemupath + '"') self._qemupath = qemupath
def _setoptions(self, options): """ Set the Qemu options for this emulated device options: Qemu options """ if type(options) not in [str, unicode]: raise DynamipsError, 'invalid options' #send the options enclosed in quotes to protect them send(self.p, 'qemu setattr %s options %s' % (self.name, '"' + options + '"')) self._options = options
def _setnetcard(self, netcard): """ Set the netcard to be used by this virtualized device netcard: (str) netcard name """ debugmsg(2, "AnyVBoxEmuDevice::_setnetcard(%s)" % str(netcard)) if type(netcard) not in [str, unicode]: raise DynamipsError, 'invalid netcard' send(self.p, 'vbox setattr %s netcard %s' % (self.name, netcard)) self._netcard = netcard
def _setkernel_cmdline(self, kernel_cmdline): """ Set the kernel command line for this emulated device kernel_cmdline: kernel command line """ if type(kernel_cmdline) not in [str, unicode]: raise DynamipsError, 'invalid kernel command line' #send the kernel command line enclosed in quotes to protect it send(self.p, 'qemu setattr %s kernel_cmdline %s' % (self.name, '"' + kernel_cmdline + '"')) self._kernel_cmdline = kernel_cmdline
def _setkey(self, key): """ Set the key for this fw key: key number of this fw """ if type(key) not in [str, unicode]: raise DynamipsError, 'invalid key' #TODO verify key if key: send(self.p, 'qemu setattr %s key %s' % (self.name, key)) self._key = key
def _setserial(self, serial): """ Set the serial for this pix serial: serial number of this pix """ if type(serial) not in [str, unicode]: raise DynamipsError, 'invalid serial' #TODO verify serial if serial: send(self.p, 'qemu setattr %s serial %s' % (self.name, serial)) self._serial = serial
def vboxexec(self, command): """sends GuestControl execute commands to the virtualized device instance in VBox""" debugmsg(2, "AnyVBoxEmuDevice::vboxexec(%s)" % unicode(command)) if self.state != 'running': raise DynamipsError, 'virtualized device %s is not running' % self.name return r = send(self.p, 'vbox exec %s %s' % (self.name, command)) r = send(self.p, 'vbox exec %s %s' % (self.name, command)) #print "ADEBUG: dynagen_vbox_lib.py: r[0][0:10] = %s" % r[0][0:10] return r
def _setoptions(self, options): """ Set the Qemu options for this emulated device options: Qemu options """ debugmsg(2, "AnyEmuDevice::_setoptions(%s)" % str(options)) if type(options) not in [str, unicode]: raise DynamipsError, 'invalid options' #send the options enclosed in quotes to protect them send(self.p, 'qemu setattr %s options %s' % (self.name, '"' + options + '"')) self._options = options
def _setinitrd(self, initrd): """ Set the initrd for this emulated device initrd: path to initrd file """ if type(initrd) not in [str, unicode]: raise DynamipsError, 'invalid initrd' # Can't verify existance of image because path is relative to backend #send the initrd filename enclosed in quotes to protect it send(self.p, 'qemu setattr %s initrd %s' % (self.name, '"' + initrd + '"')) self._initrd = initrd
def _setkernel(self, kernel): """ Set the kernel for this emulated device kernel: path to kernel file """ if type(kernel) not in [str, unicode]: raise DynamipsError, 'invalid kernel' # Can't verify existance of image because path is relative to backend #send the kernel filename enclosed in quotes to protect it send(self.p, 'qemu setattr %s kernel %s' % (self.name, '"' + kernel + '"')) self._kernel = kernel
def _setimage(self, image): """ Set the IOS image for this fw image: path to IOS image file """ if type(image) not in [str, unicode]: raise DynamipsError, 'invalid image' # Can't verify existance of image because path is relative to backend #send the image filename enclosed in quotes to protect it send(self.p, 'pemu set_image %s %s' % (self.name, '"' + image + '"')) self._image = image