def connect(self, logger=None): ''' Gets LPAR console using mkvterm :param logger: string, name of the logger to use other than default log :raises: `CommandFailed` :returns: object, LPAR console using mkvterm ''' if self.state == ConsoleState.CONNECTED: return self.pty self.util.clear_state(self) # clear when coming in DISCONNECTED log.info("De-activating the console") self.deactivate_lpar_console() log.debug("#HMC Console CONNECT") command = "sshpass -p %s ssh -p 22 -l %s %s -o PubkeyAuthentication=no"\ " -o afstokenpassing=no -q -o 'UserKnownHostsFile=/dev/null'"\ " -o 'StrictHostKeyChecking=no'" try: self.pty = Spawn(command % (self.passwd, self.user, self.hmc_ip)) log.info("Opening the LPAR console") time.sleep(STALLTIME) self.pty.send('\r') self.pty.sendline("mkvterm -m %s -p %s" % (self.mg_system, self.lpar_name)) self.pty.send('\r') time.sleep(STALLTIME) i = self.pty.expect(["Open Completed.", pexpect.TIMEOUT], timeout=60) self.pty.logfile = sys.stdout if logger: self.pty.logfile_read = OpTestLogger.FileLikeLogger(logger) else: self.pty.logfile_read = OpTestLogger.FileLikeLogger(log) if i == 0: time.sleep(STALLTIME) self.state = ConsoleState.CONNECTED self.pty.setwinsize(1000, 1000) else: raise OpTestError("Check the lpar activate command") except Exception as exp: self.state = ConsoleState.DISCONNECTED raise CommandFailed( 'OPexpect.spawn', 'OPexpect.spawn encountered a problem: ' + str(exp), -1) if self.delaybeforesend: self.pty.delaybeforesend = self.delaybeforesend if not self.pty.isalive(): raise CommandFailed("mkvterm", self.pty.read(), self.pty.status) return self.pty
def connect(self): if self.state == ConsoleState.CONNECTED: return self.pty else: self.util.clear_state(self) # clear when coming in DISCONNECTED log.debug("#Mambo Console CONNECT") if not os.access(self.mambo_initial_run_script, os.R_OK | os.W_OK): raise ParameterCheck( message="Check that the file exists with" " R/W permissions mambo-initial-run-script={}".format( self.mambo_initial_run_script)) cmd = ("%s" % (self.mambo_binary) + " -e" + " -f {}".format(self.mambo_initial_run_script)) spawn_env = os.environ if self.skiboot: spawn_env['SKIBOOT'] = self.skiboot if self.kernel: spawn_env['SKIBOOT_ZIMAGE'] = self.kernel if self.initramfs: if not os.access(self.initramfs, os.R_OK | os.W_OK): raise ParameterCheck(message="Check that the file exists with" " R/W permissions flash-initramfs={}". format(self.initramfs)) spawn_env['SKIBOOT_INITRD'] = self.initramfs if self.mambo_autorun: spawn_env['SKIBOOT_AUTORUN'] = str(self.mambo_autorun) log.debug("OpTestMambo cmd={} mambo spawn_env={}".format( cmd, spawn_env)) try: self.pty = OPexpect.spawn(cmd, logfile=self.logfile, env=spawn_env) except Exception as e: self.state = ConsoleState.DISCONNECTED raise CommandFailed( 'OPexpect.spawn', 'OPexpect.spawn encountered a problem: ' + str(e), -1) self.state = ConsoleState.CONNECTED self.pty.setwinsize(1000, 1000) if self.delaybeforesend: self.pty.delaybeforesend = self.delaybeforesend if self.system.SUDO_set != 1 or self.system.LOGIN_set != 1 or self.system.PS1_set != 1: self.util.setup_term(self.system, self.pty, None, self.system.block_setup_term) time.sleep(0.2) if not self.pty.isalive(): raise CommandFailed(cmd, self.pty.read(), self.pty.status) return self.pty
def connect(self): if self.state == ConsoleState.CONNECTED: return self.sol else: self.util.clear_state(self) # clear when coming in DISCONNECTED log.debug("#Qemu Console CONNECT") cmd = ("%s" % (self.qemu_binary) + " -machine powernv -m 4G" + " -nographic -nodefaults" ) if self.pnor: cmd = cmd + " -drive file={},format=raw,if=mtd".format(self.pnor) if self.skiboot: cmd = cmd + " -bios %s" % (self.skiboot) if self.kernel: cmd = cmd + " -kernel %s" % (self.kernel) if self.initramfs is not None: cmd = cmd + " -initrd %s" % (self.initramfs) if self.hda is not None: # Put the disk on the first PHB cmd = (cmd + " -drive file={},id=disk01,if=none".format(self.hda) + " -device virtio-blk-pci,drive=disk01,id=virtio01,bus=pcie.0,addr=0" ) if self.cdrom is not None: # Put the CDROM on the second PHB cmd = (cmd + " -drive file={},id=cdrom01,if=none,media=cdrom".format(self.cdrom) + " -device virtio-blk-pci,drive=cdrom01,id=virtio02,bus=pcie.1,addr=0" ) # typical host ip=10.0.2.2 and typical skiroot 10.0.2.15 # use skiroot as the source, no sshd in skiroot cmd = cmd + " -nic user,model=virtio-net-pci" cmd = cmd + " -device ipmi-bmc-sim,id=bmc0 -device isa-ipmi-bt,bmc=bmc0,irq=10" cmd = cmd + " -serial none -device isa-serial,chardev=s1 -chardev stdio,id=s1,signal=off" print cmd try: solChild = OPexpect.spawn(cmd,logfile=self.logfile) except Exception as e: self.state = ConsoleState.DISCONNECTED raise CommandFailed('OPexpect.spawn', 'OPexpect.spawn encountered a problem: ' + str(e), -1) self.state = ConsoleState.CONNECTED solChild.setwinsize(1000,1000) self.sol = solChild self.console = solChild if self.delaybeforesend: self.sol.delaybeforesend = self.delaybeforesend if self.system.SUDO_set != 1 or self.system.LOGIN_set != 1 or self.system.PS1_set != 1: self.util.setup_term(self.system, self.sol, None, self.system.block_setup_term) time.sleep(0.2) return solChild
def vterm_run_command(self, console, cmd, timeout=60): if console is None: console = self.get_console() try: console.send(cmd) console.send('\r') output = console.before console.sendline("echo $?") console.send('\r') # Time to accumulate both outputs time.sleep(STALLTIME) rc = console.expect([self.expect_prompt], timeout=timeout) if rc != 0: exitcode = int(console.before) log.debug("# LAST COMMAND EXIT CODE %d (%s)" % (exitcode, repr(console.before))) except pexpect.TIMEOUT as e: log.debug(e) log.debug("# TIMEOUT waiting for command to finish.") log.debug("# Attempting to control-c") try: console.sendcontrol('c') rc = console.expect([self.expect_prompt], 10) if rc == 0: raise CommandFailed(cmd, "TIMEOUT", -1) except pexpect.TIMEOUT: log.error("# Timeout trying to kill timed-out command.") log.error( "# Failing current command and attempting to continue") self.deactivate_lpar_console() raise CommandFailed("console", "timeout", -1) raise e if rc == 0: res = console.before res = res.split(cmd) return res[-1].splitlines()[1:-2] else: res = output res = res.splitlines() if exitcode != 0: raise CommandFailed(cmd, res, exitcode) return res
def connect(self): if self.state == ConsoleState.CONNECTED: return self.sol else: self.util.clear_state(self) # clear when coming in DISCONNECTED print "#Qemu Console CONNECT" cmd = ("%s" % (self.qemu_binary) + " -machine powernv -m 4G" + " -nographic -nodefaults" + " -bios %s" % (self.skiboot) + " -kernel %s" % (self.kernel)) if self.initramfs is not None: cmd = cmd + " -initrd %s" % (self.initramfs) if self.hda is not None: cmd = cmd + " -hda %s" % (self.hda) if self.cdrom is not None: cmd = cmd + " -cdrom %s" % (self.cdrom) # typical host ip=10.0.2.2 and typical skiroot 10.0.2.15 # use skiroot as the source, no sshd in skiroot cmd = cmd + " -nic user,model=virtio-net-pci" cmd = cmd + " -device ipmi-bmc-sim,id=bmc0 -device isa-ipmi-bt,bmc=bmc0,irq=10" cmd = cmd + " -serial none -device isa-serial,chardev=s1 -chardev stdio,id=s1,signal=off" print cmd try: solChild = OPexpect.spawn(cmd, logfile=self.logfile) except Exception as e: self.state = ConsoleState.DISCONNECTED raise CommandFailed('OPexpect.spawn', 'OPexpect.spawn encountered a problem', -1) self.state = ConsoleState.CONNECTED solChild.setwinsize(1000, 1000) self.sol = solChild self.console = solChild solChild.logfile_read = self.logfile if self.delaybeforesend: self.sol.delaybeforesend = self.delaybeforesend if self.system.SUDO_set != 1 or self.system.LOGIN_set != 1 or self.system.PS1_set != 1: self.util.setup_term(self.system, self.sol, None, self.system.block_setup_term) time.sleep(0.2) return solChild
def run_command(self, command, timeout=60): console = self.get_console() console.sendline(command) console.expect("\n") # from us rc = console.expect(["\[console-pexpect\]#$",pexpect.TIMEOUT], timeout) output = console.before console.sendline("echo $?") console.expect("\n") # from us rc = console.expect(["\[console-pexpect\]#$",pexpect.TIMEOUT], timeout) exitcode = int(console.before) if rc == 0: res = output res = res.splitlines() if exitcode != 0: raise CommandFailed(command, res, exitcode) return res else: res = console.before res = res.split(command) return res[-1].splitlines()
def connect(self): if self.state == ConsoleState.CONNECTED: return self.pty else: self.util.clear_state(self) # clear when coming in DISCONNECTED log.debug("#Qemu Console CONNECT") cmd = ("%s" % (self.qemu_binary) + " -machine powernv -m 4G" + " -nographic -nodefaults") if self.pnor: cmd = cmd + " -drive file={},format=raw,if=mtd".format(self.pnor) if self.skiboot: cmd = cmd + " -bios %s" % (self.skiboot) if self.kernel: cmd = cmd + " -kernel %s" % (self.kernel) if self.initramfs is not None: cmd = cmd + " -initrd %s" % (self.initramfs) if self.hda is not None: # Put the disk on the first PHB cmd = ( cmd + " -drive file={},id=disk01,if=none".format(self.hda) + " -device virtio-blk-pci,drive=disk01,id=virtio01,bus=pcie.0,addr=0" ) if self.cdrom is not None: # Put the CDROM on the second PHB cmd = ( cmd + " -drive file={},id=cdrom01,if=none,media=cdrom".format( self.cdrom) + " -device virtio-blk-pci,drive=cdrom01,id=virtio02,bus=pcie.1,addr=0" ) # typical host ip=10.0.2.2 and typical skiroot 10.0.2.15 # use skiroot as the source, no sshd in skiroot fru_path = os.path.join(OpTestConfiguration.conf.basedir, "test_binaries", "qemu_fru") cmd = cmd + " -nic user,model=virtio-net-pci" cmd = cmd + " -device ipmi-bmc-sim,id=bmc0,frudatafile=" + fru_path + " -device isa-ipmi-bt,bmc=bmc0,irq=10" cmd = cmd + " -serial none -device isa-serial,chardev=s1 -chardev stdio,id=s1,signal=off" print(cmd) try: self.pty = OPexpect.spawn(cmd, logfile=self.logfile) except Exception as e: self.state = ConsoleState.DISCONNECTED raise CommandFailed( 'OPexpect.spawn', 'OPexpect.spawn encountered a problem: ' + str(e), -1) self.state = ConsoleState.CONNECTED self.pty.setwinsize(1000, 1000) if self.delaybeforesend: self.pty.delaybeforesend = self.delaybeforesend if self.system.SUDO_set != 1 or self.system.LOGIN_set != 1 or self.system.PS1_set != 1: self.util.setup_term(self.system, self.pty, None, self.system.block_setup_term) # Wait a moment for isalive() to read a correct value and then check # if the command has already exited. If it has then QEMU has most # likely encountered an error and there's no point proceeding. time.sleep(0.2) if not self.pty.isalive(): raise CommandFailed(cmd, self.pty.read(), self.pty.status) return self.pty
def connect(self): if self.state == ConsoleState.CONNECTED: return self.pty else: self.util.clear_state(self) # clear when coming in DISCONNECTED log.debug("#Qemu Console CONNECT") cmd = ("%s" % (self.qemu_binary) + " -machine powernv -m 4G" + " -nographic -nodefaults" ) if self.pnor: cmd = cmd + " -drive file={},format=raw,if=mtd".format(self.pnor) if self.skiboot: skibootdir = os.path.dirname(self.skiboot) skibootfile = os.path.basename(self.skiboot) if skibootfile: cmd = cmd + " -bios %s" % (skibootfile) if skibootdir: cmd = cmd + " -L %s" % (skibootdir) if self.kernel: cmd = cmd + " -kernel %s" % (self.kernel) if self.initramfs is not None: cmd = cmd + " -initrd %s" % (self.initramfs) # So in the powernv(9) QEMU model we have 6 PHBs with one slot free each. # We can add a pcie bridge to each of these, and each bridge has 31 # slots.. if you see where I'm going.. cmd = (cmd + " -device pcie-pci-bridge,id=pcie.6,bus=pcie.0,addr=0x0" + " -device pcie-pci-bridge,id=pcie.7,bus=pcie.1,addr=0x0" + " -device pcie-pci-bridge,id=pcie.8,bus=pcie.2,addr=0x0" + " -device pcie-pci-bridge,id=pcie.9,bus=pcie.3,addr=0x0" + " -device pcie-pci-bridge,id=pcie.10,bus=pcie.4,addr=0x0" + " -device pcie-pci-bridge,id=pcie.11,bus=pcie.5,addr=0x0" ) # Put the NIC in slot 2 of the second PHB (1st is reserved for later) cmd = (cmd + " -netdev user,id=u1 -device e1000e,netdev=u1,mac={},bus=pcie.7,addr=2" .format(self.mac_str) ) prefilled_slots = 1 if self.cdrom is not None: # Put the CDROM in slot 3 of the second PHB cmd = (cmd + " -drive file={},id=cdrom01,if=none,media=cdrom".format(self.cdrom) + " -device virtio-blk-pci,drive=cdrom01,id=virtio02,bus=pcie.7,addr=3" ) prefilled_slots += 1 bridges = [] bridges.append({'bus': 3, 'n_devices': 0, 'bridged': False}) bridges.append({'bus': 4, 'n_devices': 0, 'bridged': False}) bridges.append({'bus': 5, 'n_devices': 0, 'bridged': False}) bridges.append({'bus': 6, 'n_devices': 0, 'bridged': False}) bridges.append( {'bus': 7, 'n_devices': prefilled_slots, 'bridged': False}) bridges.append({'bus': 8, 'n_devices': 0, 'bridged': False}) bridges.append({'bus': 9, 'n_devices': 0, 'bridged': False}) bridges.append({'bus': 10, 'n_devices': 0, 'bridged': False}) bridges.append({'bus': 11, 'n_devices': 0, 'bridged': False}) # For any amount of disks we have, start finding spots for them in the PHBs if self.disks: diskid = 0 bid = 0 for disk in self.disks: bridge = bridges[bid] if bridge['n_devices'] >= 30: # This bridge is full if bid == len(bridges) - 1: # All bridges full, find one to extend if [x for x in bridges if x['bridged'] == False] == []: # We messed up and filled up all our slots raise OpTestError("Oops! We ran out of slots!") for i in range(0, bid): if not bridges[i]['bridged']: # We can add a bridge here parent = bridges[i]['bus'] new = bridges[-1]['bus'] + 1 print(("Adding new bridge {} on bridge {}".format( new, parent))) bridges.append( {'bus': new, 'n_devices': 0, 'bridged': False}) cmd = cmd + \ " -device pcie-pci-bridge,id=pcie.{},bus=pcie.{},addr=0x1".format( new, parent) bid = bid + 1 bridges[i]['bridged'] = True bridge = bridges[bid] break else: # Just move to the next one, subsequent bridge should # always have slots bid = bid + 1 bridge = bridges[bid] if bridge['n_devices'] >= 30: raise OpTestError("Lost track of our PCI bridges!") # Got a bridge, let's go! # Valid bridge slots are 1..31, but keep 1 free for more bridges addr = 2 + bridge['n_devices'] print(("Adding disk {} on bus {} at address {}".format( diskid, bridge['bus'], addr))) cmd = cmd + \ " -drive file={},id=disk{},if=none".format( disk.name, diskid) cmd = cmd + " -device virtio-blk-pci,drive=disk{},id=virtio{},bus=pcie.{},addr={}".format( diskid, diskid, bridge['bus'], hex(addr)) diskid += 1 bridge['n_devices'] += 1 # typical host ip=10.0.2.2 and typical skiroot 10.0.2.15 # use skiroot as the source, no sshd in skiroot fru_path = os.path.join( OpTestConfiguration.conf.basedir, "test_binaries", "qemu_fru") cmd = cmd + " -device ipmi-bmc-sim,id=bmc0,frudatafile=" + \ fru_path + " -device isa-ipmi-bt,bmc=bmc0,irq=10" cmd = cmd + " -serial none -device isa-serial,chardev=s1 -chardev stdio,id=s1,signal=off" print(cmd) try: self.pty = OPexpect.spawn(cmd, logfile=self.logfile) except Exception as e: self.state = ConsoleState.DISCONNECTED raise CommandFailed('OPexpect.spawn', 'OPexpect.spawn encountered a problem: ' + str(e), -1) self.state = ConsoleState.CONNECTED self.pty.setwinsize(1000, 1000) if self.delaybeforesend: self.pty.delaybeforesend = self.delaybeforesend if self.system.SUDO_set != 1 or self.system.LOGIN_set != 1 or self.system.PS1_set != 1: self.util.setup_term(self.system, self.pty, None, self.system.block_setup_term) # Wait a moment for isalive() to read a correct value and then check # if the command has already exited. If it has then QEMU has most # likely encountered an error and there's no point proceeding. time.sleep(0.2) if not self.pty.isalive(): raise CommandFailed(cmd, self.pty.read(), self.pty.status) return self.pty