-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
system: op-mode: T3334: allow delayed getty restart when configuring …
…serial ports * Relocated service control to op-mode command "restart serial-console" * Checking for logged-in serial sessions that may be affected by getty reconfig * Warning the user when changes are committed and serial sessions are active, otherwise restart services as normal. No prompts issued during commit, all config gen/commit steps still occur except for the service restarts (everything remains consistent) * To apply committed changes, user will need to run "restart serial-console" to complete the process or reboot the whole router
- Loading branch information
Showing
3 changed files
with
98 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0"?> | ||
<interfaceDefinition> | ||
<node name="restart"> | ||
<children> | ||
<node name="serial-console"> | ||
<properties> | ||
<help>Restart serial console service</help> | ||
</properties> | ||
<command>${vyos_op_scripts_dir}/restart_serial.py</command> | ||
</node> | ||
</children> | ||
</node> | ||
</interfaceDefinition> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#!/usr/bin/env python3 | ||
# | ||
# Copyright (C) 2024 VyOS maintainers and contributors | ||
# | ||
# This program is free software; you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License version 2 or later as | ||
# published by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import sys, os, re, json | ||
|
||
from vyos.base import Warning | ||
from vyos.utils.io import ask_yes_no | ||
from vyos.utils.process import cmd | ||
|
||
GLOB_GETTY_UNITS = 'serial-getty@*.service' | ||
RE_GETTY_DEVICES = re.compile(r'.+@(.+).service$') | ||
|
||
SD_UNIT_PATH = '/run/systemd/system' | ||
UTMP_PATH = '/run/utmp' | ||
|
||
def get_serial_units(): | ||
# Since we cannot depend on the current config for decommissioned ports, | ||
# we just grab everything that systemd knows about. | ||
tmp = cmd(f'sudo systemctl list-units {GLOB_GETTY_UNITS} --all --output json --no-pager') | ||
getty_units = json.loads(tmp) | ||
|
||
for sdunit in getty_units: | ||
m = RE_GETTY_DEVICES.search(sdunit['unit']) | ||
if m is None: | ||
Warning(f'Serial console unit name "{sdunit["unit"]}" is malformed and cannot be checked for activity!') | ||
else: | ||
sdunit['device'] = m.group(1) | ||
|
||
return getty_units | ||
|
||
def get_connected_ports(units): | ||
connected = [] | ||
|
||
ports = [ x['device'] for x in units if 'device' in x ] | ||
for line in cmd(f'utmpdump {UTMP_PATH}').splitlines(): | ||
row = line.split('] [') | ||
user_name = row[3].strip() | ||
user_term = row[4].strip() | ||
if user_name and user_name != 'LOGIN' and user_term in ports: | ||
connected.append(user_term) | ||
|
||
return connected | ||
|
||
if __name__ == '__main__': | ||
no_prompt = '--quiet' in sys.argv # don't need a full argparse for this. | ||
units = get_serial_units() | ||
connected = get_connected_ports(units) | ||
|
||
if connected: | ||
print('There are user sessions connected via serial console that will be terminated\n' \ | ||
'when serial console settings are changed.\n') # extra newline is deliberate. | ||
|
||
if no_prompt: | ||
# This flag is used by conf_mode/system_console.py to reset things, if there's | ||
# a problem, the user should issue a manual restart for serial-getty. | ||
print('Please ensure all settings are committed and saved before issuing a\n' \ | ||
'"restart serial-console" command to apply new configuration.') | ||
sys.exit(0) | ||
|
||
if not ask_yes_no('Any uncommitted changes from these sessions will be lost and in-progress actions\n' \ | ||
'may be left in an inconsistent state. Continue?'): | ||
sys.exit(1) | ||
|
||
for unit in units: | ||
unit_name = unit['unit'] | ||
if os.path.exists(os.path.join(SD_UNIT_PATH, unit_name)): | ||
cmd(f'sudo systemctl restart {unit_name}') | ||
else: | ||
# Deleted stubs don't need to be restarted, just shut them down. | ||
cmd(f'sudo systemctl stop {unit_name}') |