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.
master
Dustin 2015-07-11 19:46:55 -05:00
parent ef6b3ba8e8
commit aa97d4cd43
1 changed files with 24 additions and 0 deletions

View File

@ -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)