| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
import sys |
| 4 |
import os |
| 5 |
from pathlib import Path |
| 6 |
from console_log.config import read_config |
| 7 |
|
| 8 |
CONFIG = "/etc/console-log.conf" |
| 9 |
TARGET = "multi-user.target" |
| 10 |
DEBUG_MODE = os.getenv("DEBUG") is not None |
| 11 |
|
| 12 |
|
| 13 |
# --- Logging Utility --- |
| 14 |
def log(msg): |
| 15 |
tag = "[log-display-generator] " |
| 16 |
if DEBUG_MODE: |
| 17 |
print(tag + msg) |
| 18 |
|
| 19 |
|
| 20 |
# --- String Escape --- |
| 21 |
def systemd_escape_path(path: str) -> str: |
| 22 |
""" |
| 23 |
Encode a filesystem path to a systemd unit-compatible string, |
| 24 |
mimicking `systemd-escape --path`. |
| 25 |
|
| 26 |
Rules: |
| 27 |
- Leading and trailing slashes are ignored. |
| 28 |
- '/' replaced with '-' |
| 29 |
- Alphanumeric, '_', and '-' stay as is. |
| 30 |
- Other chars escaped as \\xHH (hex) |
| 31 |
""" |
| 32 |
|
| 33 |
def escape_char(c): |
| 34 |
if c.isalnum() or c in ('_', '-'): |
| 35 |
return c |
| 36 |
return '\\x{:02x}'.format(ord(c)) |
| 37 |
|
| 38 |
# Strip leading/trailing slashes, split by '/' |
| 39 |
parts = path.strip('/').split('/') |
| 40 |
|
| 41 |
escaped_parts = [] |
| 42 |
for part in parts: |
| 43 |
escaped = ''.join(escape_char(c) for c in part) |
| 44 |
escaped_parts.append(escaped) |
| 45 |
|
| 46 |
return '-'.join(escaped_parts) |
| 47 |
|
| 48 |
|
| 49 |
# --- Unit Generation --- |
| 50 |
def write_logpager_unit(block, output_dir, target_name): |
| 51 |
tty = block["tty"] |
| 52 |
group = block.get("group", "adm") |
| 53 |
user = block.get("user", "Debian-console-log") |
| 54 |
maxfilesize = block.get("maxfilesize", "7000000") |
| 55 |
tty_path = f"/dev/tty{tty}" |
| 56 |
|
| 57 |
file = None |
| 58 |
for filename in block.get("file", []): |
| 59 |
log(f"check for {filename}") |
| 60 |
if os.path.isfile(filename): |
| 61 |
log(f"{filename} exists") |
| 62 |
file = filename |
| 63 |
break |
| 64 |
if file is None: |
| 65 |
return |
| 66 |
|
| 67 |
source_path = block.get("source_path", "") |
| 68 |
unit_name = f"console-log-{tty}-{systemd_escape_path(file)}.service" |
| 69 |
unit_content = f"""# generated by console-log.generator |
| 70 |
|
| 71 |
[Unit] |
| 72 |
Description=page {file} on tty{tty} |
| 73 |
After={target_name} |
| 74 |
SourcePath={source_path} |
| 75 |
|
| 76 |
[Service] |
| 77 |
Type=simple |
| 78 |
ExecStart=/usr/share/console-log/do_console_log --file {file} --maxfilesize {maxfilesize} |
| 79 |
StandardInput=tty |
| 80 |
StandardOutput=tty |
| 81 |
TTYPath={tty_path} |
| 82 |
Restart=always |
| 83 |
User={user} |
| 84 |
Group={group} |
| 85 |
MemoryMax={maxfilesize} |
| 86 |
TTYReset=yes |
| 87 |
TTYVHangup=yes |
| 88 |
TTYVTDisallocate=yes |
| 89 |
|
| 90 |
[Install] |
| 91 |
WantedBy={target_name} |
| 92 |
""" |
| 93 |
|
| 94 |
unit_path = Path(output_dir) / unit_name |
| 95 |
unit_path.write_text(unit_content) |
| 96 |
log(f"Generated unit {unit_name}: {unit_path}") |
| 97 |
|
| 98 |
return unit_name |
| 99 |
|
| 100 |
|
| 101 |
def write_chvt_unit(block, output_dir, target_name): |
| 102 |
log(f"write_chvt_unit: block: {block}") |
| 103 |
tty = block.get("tty", "") |
| 104 |
log(f"write_chvt_unit: tty: {tty}") |
| 105 |
unit_name = f"console-log-chvt-{tty}.service" |
| 106 |
log(f"write_chvt_unit: unit_name: {unit_name}") |
| 107 |
|
| 108 |
source_path = block.get("source_path", "") |
| 109 |
unit_content = f"""# generated by console-log.generator |
| 110 |
|
| 111 |
[Unit] |
| 112 |
Description=console-log - Switch to virtual console tty{tty} |
| 113 |
After={target_name} |
| 114 |
SourcePath={source_path} |
| 115 |
|
| 116 |
[Service] |
| 117 |
Type=oneshot |
| 118 |
ExecStart=/bin/chvt {tty} |
| 119 |
StandardOutput=journal |
| 120 |
StandardError=journal |
| 121 |
TTYPath=/dev/console |
| 122 |
|
| 123 |
[Install] |
| 124 |
WantedBy={target_name} |
| 125 |
""" |
| 126 |
|
| 127 |
unit_path = Path(output_dir) / unit_name |
| 128 |
unit_path.write_text(unit_content) |
| 129 |
log(f"Generated unit {unit_name}: {unit_path}") |
| 130 |
|
| 131 |
return unit_name |
| 132 |
|
| 133 |
|
| 134 |
def ensure_symlink(target_name, link_dir): |
| 135 |
""" |
| 136 |
Create a symlink in `link_dir` pointing to `../target_name`, |
| 137 |
if it doesn't already exist. Ensures the parent directory exists. |
| 138 |
""" |
| 139 |
os.makedirs(link_dir, exist_ok=True) |
| 140 |
log(f"makedirs {link_dir} ok") |
| 141 |
|
| 142 |
link_path = os.path.join(link_dir, target_name) |
| 143 |
symlink_target = f"../{target_name}" |
| 144 |
|
| 145 |
if not os.path.exists(link_path): |
| 146 |
log(f"symlink {symlink_target} to {link_path}") |
| 147 |
os.symlink(symlink_target, link_path) |
| 148 |
else: |
| 149 |
log(f"{link_path} already exists") |
| 150 |
|
| 151 |
|
| 152 |
# --- Entry Point --- |
| 153 |
def main(): |
| 154 |
if len(sys.argv) != 4: |
| 155 |
print( |
| 156 |
"Usage: generator <unitdir> <runtime-dir> <sysconf-dir>", |
| 157 |
file=sys.stderr |
| 158 |
) |
| 159 |
sys.exit(1) |
| 160 |
|
| 161 |
output_dir = sys.argv[1] |
| 162 |
runtime_dir = sys.argv[2] |
| 163 |
sysconf_dir = sys.argv[3] |
| 164 |
log(f"Generator started. Output dir: {output_dir}") |
| 165 |
if not Path(CONFIG).exists(): |
| 166 |
log(f"Config file not found: {CONFIG}") |
| 167 |
return |
| 168 |
link_target_dir = output_dir + "/" + TARGET + ".wants" |
| 169 |
|
| 170 |
blocks = read_config(CONFIG) |
| 171 |
for block in blocks: |
| 172 |
log(f"processing {block}") |
| 173 |
filename = write_logpager_unit(block, output_dir, TARGET) |
| 174 |
if filename: |
| 175 |
ensure_symlink(filename, link_target_dir) |
| 176 |
if block.get("chvt"): |
| 177 |
filename = write_chvt_unit(block, output_dir, TARGET) |
| 178 |
if filename: |
| 179 |
ensure_symlink(filename, link_target_dir) |
| 180 |
log("Generator completed.") |
| 181 |
|
| 182 |
|
| 183 |
if __name__ == "__main__": |
| 184 |
main() |