From ce0eb6694e3fb2510035501539c7acc92a0f174e Mon Sep 17 00:00:00 2001
From: Guido Trotter <ultrotter@google.com>
Date: Thu, 29 Oct 2009 16:58:50 -0400
Subject: [PATCH] kvm console: use socat raw mode with escape

If this is enabled at configure time, we pass in different parameters to
the socat console, making it a lot more manageable.

Signed-off-by: Guido Trotter <ultrotter@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 lib/constants.py         |  2 ++
 lib/hypervisor/hv_kvm.py | 23 ++++++++++++++---------
 2 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/lib/constants.py b/lib/constants.py
index eeba79b55..35cf2adac 100644
--- a/lib/constants.py
+++ b/lib/constants.py
@@ -165,6 +165,8 @@ XEN_INITRD = _autoconf.XEN_INITRD
 
 KVM_PATH = _autoconf.KVM_PATH
 SOCAT_PATH = _autoconf.SOCAT_PATH
+SOCAT_ESCAPE = _autoconf.SOCAT_ESCAPE
+SOCAT_ESCAPE_CODE = "0x1d"
 KVM_MIGRATION_PORT = _autoconf.KVM_MIGRATION_PORT
 
 VALUE_DEFAULT = "default"
diff --git a/lib/hypervisor/hv_kvm.py b/lib/hypervisor/hv_kvm.py
index aada45bc7..4b09edd12 100644
--- a/lib/hypervisor/hv_kvm.py
+++ b/lib/hypervisor/hv_kvm.py
@@ -114,6 +114,18 @@ class KVMHypervisor(hv_base.BaseHypervisor):
     """
     return '%s/%s.serial' % (cls._CTRL_DIR, instance_name)
 
+  @staticmethod
+  def _SocatUnixConsoleParams():
+    """Returns the correct parameters for socat
+
+    If we have a new-enough socat we can use raw mode with an escape character.
+
+    """
+    if constants.SOCAT_ESCAPE:
+      return "raw,echo=0,escape=%s" % constants.SOCAT_ESCAPE_CODE
+    else:
+      return "echo=0,icanon=0"
+
   @classmethod
   def _InstanceKVMRuntime(cls, instance_name):
     """Returns the instance KVM runtime filename
@@ -678,15 +690,8 @@ class KVMHypervisor(hv_base.BaseHypervisor):
 
     """
     if hvparams[constants.HV_SERIAL_CONSOLE]:
-      # FIXME: The socat shell is not perfect. In particular the way we start
-      # it ctrl+c will close it, rather than being passed to the other end.
-      # On the other hand if we pass the option 'raw' (or ignbrk=1) there
-      # will be no way of exiting socat (except killing it from another shell)
-      # and ctrl+c doesn't work anyway, printing ^C rather than being
-      # interpreted by kvm. For now we'll leave it this way, which at least
-      # allows a minimal interaction and changes on the machine.
-      shell_command = ("%s STDIO,echo=0,icanon=0 UNIX-CONNECT:%s" %
-                       (constants.SOCAT_PATH,
+      shell_command = ("%s STDIO,%s UNIX-CONNECT:%s" %
+                       (constants.SOCAT_PATH, cls._SocatUnixConsoleParams(),
                         utils.ShellQuote(cls._InstanceSerial(instance.name))))
     else:
       shell_command = "echo 'No serial shell for instance %s'" % instance.name
-- 
GitLab