Repository: systemd/mkosi

Issue:
Build fails when using `--ssh`
I just upgraded to 15.1 and I'm trying to get `--ssh` working again, but can't figure out how.

This is what I'm trying to use as the `build` command:

```
sudo mkosi -d ubuntu -r jammy -t disk -o ubuntu-jammy --ssh build
```

This is where the build stops after finishing package installations:

```
update-alternatives: using /usr/share/man/man7/bash-builtins.7.gz to provide /usr/share/man/man7/builtins.7.gz (builtins.7.gz) in auto mode                                                                                                                    
Processing triggers for libc-bin (2.35-0ubuntu3.1) ...                                                                                                                                                                                                         
Traceback (most recent call last):                                                                                                                                                                                                                             
  File "/usr/bin/mkosi", line 8, in <module>                                                                                                                                                                                                                   
    sys.exit(main())                                                                                                                                                                                                                                           
             ^^^^^^                                                                                                                                                                                                                                            
  File "/usr/lib/python3.11/contextlib.py", line 81, in inner                                                                                                                                                                                                  
    return func(*args, **kwds)                                                                                                                                                                                                                                 
           ^^^^^^^^^^^^^^^^^^^                                                                                                                                                                                                                                 
  File "/usr/lib/python3.11/site-packages/mkosi/__main__.py", line 49, in main                                                                                                                                                                                 
    run_verb(args, presets)                                                                                                                                                                                                                                    
  File "/usr/lib/python3.11/site-packages/mkosi/__init__.py", line 1817, in run_verb                                                                                                                                                                           
    build_image(args, config)                                                                                                                                                                                                                                  
  File "/usr/lib/python3.11/site-packages/mkosi/__init__.py", line 1398, in build_image                                                                                                                                                                        
    configure_ssh(state)                                                                                                                                                                                                                                       
  File "/usr/lib/python3.11/site-packages/mkosi/__init__.py", line 1061, in configure_ssh                                                                                                                                                                      
    (presetdir / "80-mkosi-ssh.preset").write_text("enable ssh.socket\n")                                                                                                                                                                                      
  File "/usr/lib/python3.11/pathlib.py", line 1078, in write_text                                                                                                                                                                                              
    with self.open(mode='w', encoding=encoding, errors=errors, newline=newline) as f:                                                                                                                                                                          
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                                                
  File "/usr/lib/python3.11/pathlib.py", line 1044, in open                                                                                                                                                                                                    
    return io.open(self, mode, buffering, encoding, errors, newline)                                                                                                                                                                                           
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                                                           
FileNotFoundError: [Errno 2] No such file or directory: '/var/tmp/mkosi15/.mkosi-tmpf52hgkfw/root/usr/lib/systemd/system-preset/80-mkosi-ssh.preset' 
```


Discussion:

Resolving patch:
diff --git a/mkosi/__init__.py b/mkosi/__init__.py
--- a/mkosi/__init__.py
+++ b/mkosi/__init__.py
@@ -1101,8 +1101,12 @@ def configure_ssh(state: MkosiState) -> None:
     if not state.config.ssh:
         return
 
+    unitdir = state.root / "usr/lib/systemd/system"
+    with umask(~0o755):
+        unitdir.mkdir(parents=True, exist_ok=True)
+
     with umask(~0o644):
-        (state.root / "usr/lib/systemd/system/ssh.socket").write_text(
+        (unitdir / "ssh.socket").write_text(
             textwrap.dedent(
                 """\
                 [Unit]
@@ -1120,7 +1124,7 @@ def configure_ssh(state: MkosiState) -> None:
             )
         )
 
-        (state.root / "usr/lib/systemd/system/ssh@.service").write_text(
+        (unitdir / "ssh@.service").write_text(
             textwrap.dedent(
                 """\
                 [Unit]
@@ -1140,8 +1144,11 @@ def configure_ssh(state: MkosiState) -> None:
             )
         )
 
-        presetdir = state.root / "usr/lib/systemd/system-preset"
-        (presetdir / "80-mkosi-ssh.preset").write_text("enable ssh.socket\n")
+    preset = state.root / "usr/lib/systemd/system-preset/80-mkosi-ssh.preset"
+    with umask(~0o755):
+        preset.parent.mkdir(parents=True, exist_ok=True)
+    with umask(~0o644):
+        preset.write_text("enable ssh.socket\n")
 
 
 def configure_initrd(state: MkosiState) -> None:


Test patch: