def createDeviceModel(self, restore = False): if self.device_model is None: return if self.pid: return # Execute device model. #todo: Error handling args = self.getDeviceModelArgs(restore) env = dict(os.environ) if self.display: env['DISPLAY'] = self.display if self.xauthority: env['XAUTHORITY'] = self.xauthority unique_id = "%i-%i" % (self.vm.getDomid(), time.time()) sentinel_path = sentinel_path_prefix + unique_id sentinel_path_fifo = sentinel_path + '.fifo' os.mkfifo(sentinel_path_fifo, 0600) sentinel_write = file(sentinel_path_fifo, 'r+') self._openSentinel(sentinel_path_fifo) self.vm.storeDom("image/device-model-fifo", sentinel_path_fifo) xstransact.Mkdir("/local/domain/0/device-model/%i" % self.vm.getDomid()) xstransact.SetPermissions("/local/domain/0/device-model/%i" % self.vm.getDomid(), { 'dom': self.vm.getDomid(), 'read': True, 'write': True }) log.info("spawning device models: %s %s", self.device_model, args) # keep track of pid and spawned options to kill it later self.logfile = "/var/log/xen/qemu-dm-%s.log" % str(self.vm.info['name_label']) # rotate log logfile_mode = os.O_WRONLY|os.O_CREAT|os.O_APPEND logrotate_count = XendOptions.instance().get_qemu_dm_logrotate_count() if logrotate_count > 0: logfile_mode |= os.O_TRUNC if os.path.exists("%s.%d" % (self.logfile, logrotate_count)): os.unlink("%s.%d" % (self.logfile, logrotate_count)) for n in range(logrotate_count - 1, 0, -1): if os.path.exists("%s.%d" % (self.logfile, n)): os.rename("%s.%d" % (self.logfile, n), "%s.%d" % (self.logfile, (n + 1))) if os.path.exists(self.logfile): os.rename(self.logfile, self.logfile + ".1") null = os.open("/dev/null", os.O_RDONLY) logfd = os.open(self.logfile, logfile_mode, 0666) sys.stderr.flush() contract = osdep.prefork("%s:%d" % (self.vm.getName(), self.vm.getDomid())) pid = os.fork() if pid == 0: #child try: osdep.postfork(contract) os.dup2(null, 0) os.dup2(logfd, 1) os.dup2(logfd, 2) oshelp.close_fds((sentinel_write.fileno(),)) try: os.execve(self.device_model, args, env) except Exception, e: print >>sys.stderr, ( 'failed to set up fds or execute dm %s: %s' % (self.device_model, utils.exception_string(e))) os._exit(126) except: os._exit(127) else: osdep.postfork(contract, abandon=True) self.pid = pid os.close(null) os.close(logfd) sentinel_write.close() self.vm.storeDom("image/device-model-pid", self.pid) log.info("device model pid: %d", self.pid) # we would very much prefer not to have a thread here and instead # have a callback but sadly we don't have Twisted in xend self.sentinel_thread = thread.start_new_thread(self._sentinel_watch,()) if self.device_model.find('stubdom-dm') > -1 : from xen.xend import XendDomain domains = XendDomain.instance() domains.domains_lock.release() count = 0 while True: orig_state = xstransact.Read("/local/domain/0/device-model/%i/state" % self.vm.getDomid()) # This can occur right after start-up if orig_state != None: break log.debug('createDeviceModel %i: orig_state is None, retrying' % self.vm.getDomid()) time.sleep(0.1) count += 1 if count > 100: break domains.domains_lock.acquire()
if ramdisk: args.append("--ramdisk=%s" % ramdisk) if kernel_args: args.append("--args=%s" % kernel_args) if quiet: args.append("-q") args.append("--output=%s" % fifo) if blargs: args.extend(shlex.split(blargs)) args.append(disk) try: log.debug("Launching bootloader as %s." % str(args)) env = os.environ.copy() env['TERM'] = 'vt100' oshelp.close_fds() os.execvpe(args[0], args, env) except OSError, e: print e pass os._exit(1) # record that this domain is bootloading dom.bootloader_pid = child # On Solaris, the master pty side does not have terminal semantics, # so don't try to set any attributes, as it will fail. if os.uname()[0] != 'SunOS': tty.setraw(m2) fcntl.fcntl(m2, fcntl.F_SETFL, os.O_NDELAY)
if ramdisk: args.append("--ramdisk=%s" % ramdisk) if kernel_args: args.append("--args=%s" % kernel_args) if quiet: args.append("-q") args.append("--output=%s" % fifo) if blargs: args.extend(shlex.split(blargs)) args.append(disk) try: log.debug("Launching bootloader as %s." % str(args)) env = os.environ.copy() env['TERM'] = 'vt100' oshelp.close_fds() os.execvpe(args[0], args, env) except OSError, e: print e pass os._exit(1) # record that this domain is bootloading dom.bootloader_pid = child # On Solaris, the master pty side does not have terminal semantics, # so don't try to set any attributes, as it will fail. if os.uname()[0] != 'SunOS': tty.setraw(m2); fcntl.fcntl(m2, fcntl.F_SETFL, os.O_NDELAY);
def createDeviceModel(self, restore=False): if self.device_model is None: return if self.pid: return # Execute device model. #todo: Error handling args = self.getDeviceModelArgs(restore) env = dict(os.environ) if self.display: env['DISPLAY'] = self.display if self.xauthority: env['XAUTHORITY'] = self.xauthority unique_id = "%i-%i" % (self.vm.getDomid(), time.time()) sentinel_path = sentinel_path_prefix + unique_id sentinel_path_fifo = sentinel_path + '.fifo' os.mkfifo(sentinel_path_fifo, 0600) sentinel_write = file(sentinel_path_fifo, 'r+') self._openSentinel(sentinel_path_fifo) self.vm.storeDom("image/device-model-fifo", sentinel_path_fifo) xstransact.Mkdir("/local/domain/0/device-model/%i" % self.vm.getDomid()) xstransact.SetPermissions( "/local/domain/0/device-model/%i" % self.vm.getDomid(), { 'dom': self.vm.getDomid(), 'read': True, 'write': True }) log.info("spawning device models: %s %s", self.device_model, args) # keep track of pid and spawned options to kill it later self.logfile = "/var/log/xen/qemu-dm-%s.log" % str( self.vm.info['name_label']) # rotate log logfile_mode = os.O_WRONLY | os.O_CREAT | os.O_APPEND logrotate_count = XendOptions.instance().get_qemu_dm_logrotate_count() if logrotate_count > 0: logfile_mode |= os.O_TRUNC if os.path.exists("%s.%d" % (self.logfile, logrotate_count)): os.unlink("%s.%d" % (self.logfile, logrotate_count)) for n in range(logrotate_count - 1, 0, -1): if os.path.exists("%s.%d" % (self.logfile, n)): os.rename("%s.%d" % (self.logfile, n), "%s.%d" % (self.logfile, (n + 1))) if os.path.exists(self.logfile): os.rename(self.logfile, self.logfile + ".1") null = os.open("/dev/null", os.O_RDONLY) logfd = os.open(self.logfile, logfile_mode, 0666) sys.stderr.flush() contract = osdep.prefork("%s:%d" % (self.vm.getName(), self.vm.getDomid())) pid = os.fork() if pid == 0: #child try: osdep.postfork(contract) os.dup2(null, 0) os.dup2(logfd, 1) os.dup2(logfd, 2) oshelp.close_fds((sentinel_write.fileno(), )) try: os.execve(self.device_model, args, env) except Exception, e: print >> sys.stderr, ( 'failed to set up fds or execute dm %s: %s' % (self.device_model, utils.exception_string(e))) os._exit(126) except: os._exit(127) else: osdep.postfork(contract, abandon=True) self.pid = pid os.close(null) os.close(logfd) sentinel_write.close() self.vm.storeDom("image/device-model-pid", self.pid) log.info("device model pid: %d", self.pid) # we would very much prefer not to have a thread here and instead # have a callback but sadly we don't have Twisted in xend self.sentinel_thread = thread.start_new_thread(self._sentinel_watch, ()) if self.device_model.find('stubdom-dm') > -1: from xen.xend import XendDomain domains = XendDomain.instance() domains.domains_lock.release() count = 0 while True: orig_state = xstransact.Read( "/local/domain/0/device-model/%i/state" % self.vm.getDomid()) # This can occur right after start-up if orig_state != None: break log.debug( 'createDeviceModel %i: orig_state is None, retrying' % self.vm.getDomid()) time.sleep(0.1) count += 1 if count > 100: break domains.domains_lock.acquire()