From 2f4c951eeee5362ef8d9959f188b4f2ec9bb71d9 Mon Sep 17 00:00:00 2001 From: Stephen Shirley <diamond@google.com> Date: Fri, 24 Jun 2011 13:29:48 +0200 Subject: [PATCH] Adding a wrapper around connecting to kvm console The wrapper will connect to the console, and check in the background if the instance is paused, unpausing it as necessary. Signed-off-by: Stephen Shirley <diamond@google.com> Reviewed-by: Michael Hanselmann <hansmi@google.com> --- Makefile.am | 1 + lib/constants.py | 1 + lib/hypervisor/hv_kvm.py | 4 ++- test/ganeti.hypervisor.hv_kvm_unittest.py | 3 +- tools/kvm-console-wrapper | 42 +++++++++++++++++++++++ 5 files changed, 49 insertions(+), 2 deletions(-) create mode 100755 tools/kvm-console-wrapper diff --git a/Makefile.am b/Makefile.am index 04b1b3dac..3c9e9f69f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -490,6 +490,7 @@ dist_tools_PYTHON = \ dist_tools_SCRIPTS = \ $(dist_tools_PYTHON) \ + tools/kvm-console-wrapper \ tools/xm-console-wrapper pkglib_python_scripts = \ diff --git a/lib/constants.py b/lib/constants.py index b7a70599e..500827ed5 100644 --- a/lib/constants.py +++ b/lib/constants.py @@ -144,6 +144,7 @@ QUEUE_DIR = DATA_DIR + "/queue" DAEMON_UTIL = _autoconf.PKGLIBDIR + "/daemon-util" SETUP_SSH = _autoconf.TOOLSDIR + "/setup-ssh" KVM_IFUP = _autoconf.PKGLIBDIR + "/kvm-ifup" +KVM_CONSOLE_WRAPPER = _autoconf.PKGLIBDIR + "/tools/kvm-console-wrapper" XM_CONSOLE_WRAPPER = _autoconf.PKGLIBDIR + "/tools/xm-console-wrapper" ETC_HOSTS = "/etc/hosts" DEFAULT_FILE_STORAGE_DIR = _autoconf.FILE_STORAGE_DIR diff --git a/lib/hypervisor/hv_kvm.py b/lib/hypervisor/hv_kvm.py index 1840381a1..2e39a1c95 100644 --- a/lib/hypervisor/hv_kvm.py +++ b/lib/hypervisor/hv_kvm.py @@ -1146,7 +1146,9 @@ class KVMHypervisor(hv_base.BaseHypervisor): """ if hvparams[constants.HV_SERIAL_CONSOLE]: - cmd = [constants.SOCAT_PATH, + cmd = [constants.KVM_CONSOLE_WRAPPER, + constants.SOCAT_PATH, utils.ShellQuote(instance.name), + utils.ShellQuote(cls._InstanceMonitor(instance.name)), "STDIO,%s" % cls._SocatUnixConsoleParams(), "UNIX-CONNECT:%s" % cls._InstanceSerial(instance.name)] return objects.InstanceConsole(instance=instance.name, diff --git a/test/ganeti.hypervisor.hv_kvm_unittest.py b/test/ganeti.hypervisor.hv_kvm_unittest.py index 166d7dd14..d9e5e7377 100755 --- a/test/ganeti.hypervisor.hv_kvm_unittest.py +++ b/test/ganeti.hypervisor.hv_kvm_unittest.py @@ -49,7 +49,8 @@ class TestConsole(unittest.TestCase): cons = self._Test(instance, hvparams) self.assertEqual(cons.kind, constants.CONS_SSH) self.assertEqual(cons.host, instance.primary_node) - self.assertEqual(cons.command[0], constants.SOCAT_PATH) + self.assertEqual(cons.command[0], constants.KVM_CONSOLE_WRAPPER) + self.assertEqual(cons.command[1], constants.SOCAT_PATH) def testVnc(self): instance = objects.Instance(name="kvm.example.com", diff --git a/tools/kvm-console-wrapper b/tools/kvm-console-wrapper new file mode 100755 index 000000000..b0acd084e --- /dev/null +++ b/tools/kvm-console-wrapper @@ -0,0 +1,42 @@ +#!/bin/bash + +# Copyright (C) 2011 Google Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301, USA. + +SOCAT="$1" +INSTANCE="$2" +MONITOR="$3" +PARAMS="$4" +CONSOLE="$5" + +unpause() { + echo "info status" | + "$SOCAT" STDIO "UNIX-CONNECT:$MONITOR" 2>/dev/null | + grep -q '^VM status: paused' || return + # As there is no way to be sure when the main socat has actually connected to + # the instance console, sleep for a few seconds before unpausing the + # instance. This is a tradeoff between missing some console output if the + # node is overloaded and making the user wait everytime when the node isn't + # so busy. + sleep 3 + # Send \r\n after notice as terminal is in raw mode + printf "Instance $INSTANCE is paused, unpausing\r\n" + echo "c" | "$SOCAT" STDIO "UNIX-CONNECT:$MONITOR" &>/dev/null +} + +unpause & +exec "$SOCAT" "$PARAMS" "$CONSOLE" -- GitLab