From eebee532a0170387ddcc8f52df46570db2e73f0b Mon Sep 17 00:00:00 2001 From: Lee Garrett Date: Fri, 1 Sep 2023 14:35:15 +0200 Subject: [PATCH] Fix encoding errors on Windows guests (fixes: #156) On Windows guests the default encoding will be set to whatever default legacy encoding, not utf-8. This issue isn't apparent on English locale Windows guests, as there the encodings (ISO-8859-1, utf-8) happen to coincide. However, on e.g. German Windows guests this will cause an encoding error when reading any output from there. This patch ensures that the proper encoding is set on every command. --- plugins/connection/libvirt_qemu.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/plugins/connection/libvirt_qemu.py b/plugins/connection/libvirt_qemu.py index 9e5271f..c9ed52f 100644 --- a/plugins/connection/libvirt_qemu.py +++ b/plugins/connection/libvirt_qemu.py @@ -160,6 +160,10 @@ def exec_command(self, cmd, in_data=None, sudoable=True): # prompt that will not occur sudoable = False + # Make sure our first command is to set the console encoding to + # utf-8, this must be done via chcp to get utf-8 (65001) + cmd = ' '.join(["chcp.com", "65001", self._shell._SHELL_REDIRECT_ALLNULL, self._shell._SHELL_AND, cmd]) + # Generate powershell commands cmd_args_list = self._shell._encode_script(cmd, as_list=True, strict_mode=False, preserve_rc=False)