From aa97d4cd43ae8bd072118de6b48676f5d4cfe8e8 Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Sat, 11 Jul 2015 19:46:55 -0500 Subject: [PATCH] configure: Add function to enable serial console The `enable_console_login` function modifies `/etc/inittab` to enable logins on a serial device. For virtio serial devices, a getty is configured to launch on `hvc0`; for other devices, it is configured for `ttyS0` (the default). When using the `configure.configure` function to automatically configure a virtual machine, `enable_console_login` is called if a console device is specified in the VM definition. --- src/mkvm/configure.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/mkvm/configure.py b/src/mkvm/configure.py index d7ec84f..c52cd69 100644 --- a/src/mkvm/configure.py +++ b/src/mkvm/configure.py @@ -1,6 +1,7 @@ import glob import logging import os +import re import subprocess @@ -53,6 +54,27 @@ def set_hostname(mountpoint, hostname): f.write(line) +def enable_console_login(mountpoint, driver): + path = os.path.join(mountpoint, 'etc/inittab') + with open(path) as f: + lines = f.readlines() + regex = re.compile(r'^#?s0') + entry = 's0:{runlevels}:{action}:{process}' + with open(path, 'w') as f: + for line in lines: + if regex.match(line): + runlevels, action, process = line.split(':', 3)[1:] + if driver == 'virtio': + process = process.replace('ttyS0', 'hvc0') + f.write(entry.format( + runlevels=runlevels, + action=action, + process=process, + )) + else: + f.write(line) + + def run_custom_script(mountpoint, script): cmd = ['chroot', mountpoint, '/bin/sh'] env = CUSTOM_SCRIPT_ENV.copy() @@ -74,6 +96,8 @@ def configure(vm): set_timezone(vm.mountpoint, vm.timezone) if vm.fqdn: set_hostname(vm.mountpoint, vm.fqdn) + if vm.console: + enable_console_login(vm.mountpoint, vm.console) if vm.customize: run_custom_script(vm.mountpoint, vm.customize)