Browse Source

scripts/qemu.py: support adding a console with the default serial device

The set_console() utility function either adds a device based on the
explicitly given device type, or adds a known good type of device
based on the machine type.

But, for a number of machine types, it may be impossible or
inconvenient to add the devices by means of "-device" command line
options, and then it may better to just use the "-serial" option and
let QEMU itself, based on the machine type, set the device
accordingly.

To achieve that, the behavior of set_console() now flags the intention
to add a console device on launch(), and if no explicit device type is
given the "-serial" option is going to be added to the QEMU command
line, instead of raising exceptions.

Based on testing with different machine types, the CONSOLE_DEV_TYPES
is not necessary anymore, so it's being removed, as is the logic to
use it.

Signed-off-by: Cleber Rosa <crosa@redhat.com>
Reviewed-by: Cornelia Huck <cohuck@redhat.com>
Message-Id: <2019031217.5134-13-crosa@redhat.com>
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
master
Cleber Rosa 3 months ago
parent
commit
123990adbf
1 changed files with 17 additions and 33 deletions
  1. 17
    33
      python/qemu/__init__.py

+ 17
- 33
python/qemu/__init__.py View File

@@ -41,17 +41,6 @@ def kvm_available(target_arch=None):
41 41
     return os.access("/dev/kvm", os.R_OK | os.W_OK)
42 42
 
43 43
 
44
-#: Maps machine types to the preferred console device types
45
-CONSOLE_DEV_TYPES = {
46
-    r'^clipper$': 'isa-serial',
47
-    r'^malta': 'isa-serial',
48
-    r'^(pc.*|q35.*|isapc)$': 'isa-serial',
49
-    r'^(40p|powernv|prep)$': 'isa-serial',
50
-    r'^pseries.*': 'spapr-vty',
51
-    r'^s390-ccw-virtio.*': 'sclpconsole',
52
-    }
53
-
54
-
55 44
 class QEMUMachineError(Exception):
56 45
     """
57 46
     Exception called when an error in QEMUMachine happens.
@@ -130,6 +119,7 @@ class QEMUMachine(object):
130 119
         self._temp_dir = None
131 120
         self._launched = False
132 121
         self._machine = None
122
+        self._console_set = False
133 123
         self._console_device_type = None
134 124
         self._console_address = None
135 125
         self._console_socket = None
@@ -248,13 +238,17 @@ class QEMUMachine(object):
248 238
                 '-display', 'none', '-vga', 'none']
249 239
         if self._machine is not None:
250 240
             args.extend(['-machine', self._machine])
251
-        if self._console_device_type is not None:
241
+        if self._console_set:
252 242
             self._console_address = os.path.join(self._temp_dir,
253 243
                                                  self._name + "-console.sock")
254 244
             chardev = ('socket,id=console,path=%s,server,nowait' %
255 245
                        self._console_address)
256
-            device = '%s,chardev=console' % self._console_device_type
257
-            args.extend(['-chardev', chardev, '-device', device])
246
+            args.extend(['-chardev', chardev])
247
+            if self._console_device_type is None:
248
+                args.extend(['-serial', 'chardev:console'])
249
+            else:
250
+                device = '%s,chardev=console' % self._console_device_type
251
+                args.extend(['-device', device])
258 252
         return args
259 253
 
260 254
     def _pre_launch(self):
@@ -480,30 +474,20 @@ class QEMUMachine(object):
480 474
         line.
481 475
 
482 476
         This is a convenience method that will either use the provided
483
-        device type, of if not given, it will used the device type set
484
-        on CONSOLE_DEV_TYPES.
477
+        device type, or default to a "-serial chardev:console" command
478
+        line argument.
485 479
 
486 480
         The actual setting of command line arguments will be be done at
487 481
         machine launch time, as it depends on the temporary directory
488 482
         to be created.
489 483
 
490
-        @param device_type: the device type, such as "isa-serial"
491
-        @raises: QEMUMachineAddDeviceError if the device type is not given
492
-                 and can not be determined.
493
-        """
494
-        if device_type is None:
495
-            if self._machine is None:
496
-                raise QEMUMachineAddDeviceError("Can not add a console device:"
497
-                                                " QEMU instance without a "
498
-                                                "defined machine type")
499
-            for regex, device in CONSOLE_DEV_TYPES.items():
500
-                if re.match(regex, self._machine):
501
-                    device_type = device
502
-                    break
503
-            if device_type is None:
504
-                raise QEMUMachineAddDeviceError("Can not add a console device:"
505
-                                                " no matching console device "
506
-                                                "type definition")
484
+        @param device_type: the device type, such as "isa-serial".  If
485
+                            None is given (the default value) a "-serial
486
+                            chardev:console" command line argument will
487
+                            be used instead, resorting to the machine's
488
+                            default device type.
489
+        """
490
+        self._console_set = True
507 491
         self._console_device_type = device_type
508 492
 
509 493
     @property

Loading…
Cancel
Save