diff --git a/guest/computer-linux.py b/guest/computer-linux.py index 18da6b459d3ffc4687c119e41637f6577f0c03dc..136fd867a0388f0fd4ba416fde233ff3d73893f3 100644 --- a/guest/computer-linux.py +++ b/guest/computer-linux.py @@ -17,6 +17,9 @@ if not re.fullmatch(r'snowglobe-computer-request-[0-9a-f]{32}', unit): raise ValueError('Invalid computer task.') lease = '/run/snowglobe-computer-request' if prepare or stop: + if prepare: + subprocess.run(['systemctl', 'show', 'systemd-user-sessions.service', '-p', 'ActiveState', '--value'], + check=True, capture_output=True, timeout=3) with os.fdopen(os.open(lease, os.O_CREAT | os.O_RDWR | os.O_NOFOLLOW, 0o600), 'r+') as current: fcntl.flock(current, fcntl.LOCK_EX) if prepare or current.read() == unit: diff --git a/tools/computer-test.py b/tools/computer-test.py index 6cebf6777dc9b6cce9c51a83cff135e817dea318..6ad5fda2aab8161f9870b789b4253530702149b6 100644 --- a/tools/computer-test.py +++ b/tools/computer-test.py @@ -5,6 +5,7 @@ import base64 import asyncio import time import json +import io import tempfile import threading import unittest @@ -419,6 +420,16 @@ class GuestCancellation(unittest.TestCase): self.assertNotIn('[ComputerProcess]::Stop', fallback.call_args.args[1]) self.assertNotIn('$t.Stop(0)', fallback.call_args.args[1]) + def test_linux_prepare_does_not_claim_a_request_before_systemd_is_accessible(self): + source = (Path(__file__).resolve().parent.parent / 'guest/computer-linux.py').read_text() + request = io.StringIO(json.dumps({'_prepare':'snowglobe-computer-request-' + 'a' * 32})) + denied = computer.subprocess.CalledProcessError(1, 'systemctl', stderr=b'Access denied') + with patch.object(computer.sys, 'stdin', request), patch.object(computer.subprocess, 'run', side_effect=denied), \ + patch.object(computer.os, 'open') as claim: + with self.assertRaises(computer.subprocess.CalledProcessError): + exec(compile(source, '', 'exec'), {'__name__':'__main__'}) + claim.assert_not_called() + def test_linux_stops_the_exact_request_unit_with_a_fresh_cleanup_budget(self): units = [] def execute(name, executable, arguments, data=None, **kwargs): diff --git a/tools/vms.py b/tools/vms.py index 4fe156f0dc10a0d94410b95910c4461a3fb89cbb..d973628c87f5d53c0a4a7931e31639762b48b84d 100644 --- a/tools/vms.py +++ b/tools/vms.py @@ -7,6 +7,7 @@ import os from pathlib import Path import re import secrets +import shlex import shutil import signal import stat @@ -695,7 +696,9 @@ def linux_seed(directory, username, os_name, serial="ttyS0"): fedora = "fedora" in os_name.lower() nixos = "nixos" in os_name.lower() account_tool = "/run/current-system/sw/bin/" if nixos else "" - rename = f'''set -eu + gdm_config = "/etc/gdm/custom.conf" if fedora or nixos else "/etc/gdm3/custom.conf" + gdm_content = f"[daemon]\nAutomaticLoginEnable=true\nAutomaticLogin={username}\n" + personalize = f'''set -eu old=$({account_tool}getent passwd 1000 | cut -d: -f1) test -n "$old" if [ "$old" != {username} ]; then @@ -708,10 +711,13 @@ if [ "$old" != {username} ]; then if [ "$({account_tool}getent group 1000 | cut -d: -f1)" = "$old" ]; then {account_tool}groupmod -n {username} "$old"; fi fi ''' - gdm_config = "/etc/gdm/custom.conf" if fedora or nixos else "/etc/gdm3/custom.conf" - files = [ - {"path": gdm_config, "content": f"[daemon]\nAutomaticLoginEnable=true\nAutomaticLogin={username}\n"}, - ] + if fedora: + personalize += "printf '%s' " + shlex.quote(gdm_content) + " > " + shlex.quote(gdm_config) + "\n" + personalize += '''for service in accounts-daemon.service display-manager.service; do + if systemctl is-active --quiet "$service"; then systemctl restart "$service"; fi +done +''' + files = [] if fedora else [{"path": gdm_config, "content": gdm_content}] if not nixos: files.insert(0, {"path": "/etc/ssh/sshd_config.d/00-snowglobe.conf", "permissions": "0600", "content": "PasswordAuthentication yes\nPermitRootLogin yes\n"}) else: @@ -729,7 +735,8 @@ fi computer_setup = [["/run/current-system/sw/bin/bash", "-lc", "nix-env --profile /var/lib/snowglobe/computer -if /var/lib/snowglobe/computer.nix"]] config = { "users": [], "disable_root": False, "ssh_pwauth": True, "ssh_deletekeys": True, - "bootcmd": ([["rm", "-f", gdm_config]] if nixos else []) + [["sh", "-c", rename]], + "bootcmd": ([["rm", "-f", gdm_config]] if nixos else []) + [ + (["cloud-init-per", "instance", "snowglobe-desktop"] if fedora else []) + ["sh", "-c", personalize]], "chpasswd": {"expire": False, "users": [{"name": user, "password": hashed, "type": "hash"} for user in [username, "root"]]}, "write_files": files, "runcmd": [["systemctl", "enable", "--now", "sshd" if fedora or nixos else "ssh"], ["systemctl", "enable", "--now", f"serial-getty@{serial}.service"]] + computer_setup,