diff --git a/src/conf_mode/system_console.py b/src/conf_mode/system_console.py index b1781879..d78d4e0f 100755 --- a/src/conf_mode/system_console.py +++ b/src/conf_mode/system_console.py @@ -19,7 +19,7 @@ import re from fileinput import input as replace_in_file from vyos.config import Config -from vyos.util import call +from vyos.util import cmd, call from vyos.template import render from vyos import ConfigError, airbag airbag.enable() @@ -71,9 +71,19 @@ def verify(console): def generate(console): base_dir = '/etc/systemd/system' # Remove all serial-getty configuration files in advance + connected = [] + for line in cmd('utmpdump /run/utmp').splitlines(): + user_term = line.split('] [')[4].strip() + if 'ttyS' not in user_term: + continue + connected.append(user_term) for root, dirs, files in os.walk(base_dir): for basename in files: if 'serial-getty' in basename: + device = basename.split('@', 1)[1].replace('.service', '') + if device in connected: + print(f'Warning: not restarting currently in use device {device}, please perform a manual restart later.') + continue call(f'systemctl stop {basename}') os.unlink(os.path.join(root, basename)) @@ -85,6 +95,8 @@ def generate(console): getty_wants_symlink = base_dir + f'/getty.target.wants/serial-getty@{device}.service' render(config_file, 'getty/serial-getty.service.tmpl', console['device'][device]) + if os.path.exists(getty_wants_symlink): + continue os.symlink(config_file, getty_wants_symlink) # GRUB @@ -129,6 +141,9 @@ def apply(console): # Only start console if it exists on the running system. If a user # detaches a USB serial console and reboots - it should not fail! if os.path.exists(f'/dev/{device}'): + ret = call(f'systemctl is-active --quiet serial-getty@{device}.service') + if not ret: + continue call(f'systemctl start serial-getty@{device}.service') return None