|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright VyOS maintainers and contributors <maintainers@vyos.io> |
| 4 | +# |
| 5 | +# This program is free software; you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License version 2 or later as |
| 7 | +# published by the Free Software Foundation. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +from sys import exit |
| 18 | +from pathlib import Path |
| 19 | + |
| 20 | +from vyos.config import Config |
| 21 | +from vyos.template import render |
| 22 | +from vyos.utils.process import call, run |
| 23 | +from vyos import ConfigError |
| 24 | +from vyos import airbag |
| 25 | + |
| 26 | +airbag.enable() |
| 27 | + |
| 28 | +watchdog_config_dir = Path('/run/systemd/system.conf.d') |
| 29 | +watchdog_config_file = Path(watchdog_config_dir / 'watchdog.conf') |
| 30 | +modules_load_directory = Path('/run/modules-load.d') |
| 31 | +modules_load_file = Path(modules_load_directory / 'watchdog.conf') |
| 32 | +WATCHDOG_DEV = Path('/dev/watchdog0') |
| 33 | + |
| 34 | + |
| 35 | +def get_config(config=None): |
| 36 | + if config: |
| 37 | + conf = config |
| 38 | + else: |
| 39 | + conf = Config() |
| 40 | + base = ['system', 'watchdog'] |
| 41 | + |
| 42 | + if not conf.exists(base): |
| 43 | + return None |
| 44 | + |
| 45 | + watchdog = conf.get_config_dict( |
| 46 | + base, key_mangling=('-', '_'), get_first_key=True, with_recursive_defaults=True |
| 47 | + ) |
| 48 | + |
| 49 | + return watchdog |
| 50 | + |
| 51 | + |
| 52 | +def verify(watchdog): |
| 53 | + if watchdog is None: |
| 54 | + return None |
| 55 | + |
| 56 | + module = watchdog.get('module') |
| 57 | + device_exists = WATCHDOG_DEV.exists() |
| 58 | + |
| 59 | + # Require a usable watchdog: either device already present or a module provided |
| 60 | + if not module and not device_exists: |
| 61 | + raise ConfigError( |
| 62 | + "No watchdog device found at /dev/watchdog0 and no module configured. " |
| 63 | + "Either configure 'system watchdog module <name>' or ensure the kernel auto-loads a watchdog driver." |
| 64 | + ) |
| 65 | + |
| 66 | + # If a module is provided, verify it resolves via modprobe (dry-run) |
| 67 | + if module: |
| 68 | + # Dry-run modprobe (-n) in quiet mode (-q) verifies availability without loading |
| 69 | + rc = run(f'modprobe -n -q {module}') |
| 70 | + if rc != 0: |
| 71 | + raise ConfigError( |
| 72 | + f"Watchdog module '{module}' was not found or cannot be loaded" |
| 73 | + ) |
| 74 | + |
| 75 | + return None |
| 76 | + |
| 77 | + |
| 78 | +def generate(watchdog): |
| 79 | + # If watchdog node removed entirely, clean up everything |
| 80 | + if watchdog is None: |
| 81 | + watchdog_config_file.unlink(missing_ok=True) |
| 82 | + modules_load_file.unlink(missing_ok=True) |
| 83 | + return None |
| 84 | + |
| 85 | + # Persist kernel module autoload on boot if specified (even if not enabled) |
| 86 | + module = watchdog.get('module') |
| 87 | + if module: |
| 88 | + try: |
| 89 | + modules_load_directory.mkdir(exist_ok=True) |
| 90 | + modules_load_file.write_text(f"{module}\n") |
| 91 | + except Exception as e: |
| 92 | + print(f"Warning: Failed writing modules-load configuration: {e}") |
| 93 | + else: |
| 94 | + # If module option removed, drop persisted autoload file |
| 95 | + modules_load_file.unlink(missing_ok=True) |
| 96 | + |
| 97 | + # Try to load kernel module if specified and /dev/watchdog0 is missing |
| 98 | + if not WATCHDOG_DEV.exists(): |
| 99 | + if module: |
| 100 | + # Try to load the module using vyos call wrapper for logging/airbag integration |
| 101 | + try: |
| 102 | + call(f'modprobe {module}') |
| 103 | + except Exception as e: |
| 104 | + print(f"Warning: Could not load watchdog module '{module}': {e}") |
| 105 | + # Re-check for device |
| 106 | + if not WATCHDOG_DEV.exists(): |
| 107 | + print( |
| 108 | + "Warning: /dev/watchdog0 not found. Systemd watchdog will not be enabled." |
| 109 | + ) |
| 110 | + watchdog_config_file.unlink(missing_ok=True) |
| 111 | + return None |
| 112 | + |
| 113 | + # Ensure the directory exists |
| 114 | + watchdog_config_dir.mkdir(parents=True, exist_ok=True) |
| 115 | + |
| 116 | + # Pass through configured time values directly as seconds |
| 117 | + render(str(watchdog_config_file), 'system/watchdog.conf.j2', watchdog) |
| 118 | + |
| 119 | + return None |
| 120 | + |
| 121 | + |
| 122 | +def apply(watchdog): |
| 123 | + # Reload systemd daemon to apply/unload the watchdog configuration |
| 124 | + # The watchdog settings take immediate effect after systemd is reloaded |
| 125 | + call('systemctl daemon-reload') |
| 126 | + |
| 127 | + return None |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == '__main__': |
| 131 | + try: |
| 132 | + c = get_config() |
| 133 | + verify(c) |
| 134 | + generate(c) |
| 135 | + apply(c) |
| 136 | + except ConfigError as e: |
| 137 | + print(e) |
| 138 | + exit(1) |
0 commit comments