Repository: systemd/mkosi

Issue:
Is it possible to create system extensions based on Arch Linux
I have followed the documentation to the best of my abilities, however it seems not to be possible. When using `BaseTrees=…` and `Overlay=true`, pacman fails because it cannot lock its database. This is the reported error message:

```
‣ Building extension image
Create subvolume '/home/x/mkosi-test/.mkosi-tmp_eq6cbps/root'
‣  Mounting base trees…
‣   Installing extra packages for Arch
:: Synchronizing package databases...
error: failed to synchronize all databases (unable to lock database)
‣ "bwrap --dev-bind / / --chdir /home/x/mkosi-test --tmpfs /home/x/mkosi-test/.mkosi-tmp_eq6cbps/root/run --tmpfs /home/x/mkosi-test/.mkosi-tmp_eq6cbps/root/tmp --bind /home/x/mkosi-test/.mkosi-tmp_eq6cbps /home/x/mkosi-test/.mkosi-tmp_eq6cbps/root/var/tmp --proc /home/x/mkosi-test/.mkosi-tmp_eq6cbps/root/proc --dev /home/x/mkosi-test/.mkosi-tmp_eq6cbps/root/dev --ro-bind /sys /home/x/mkosi-test/.mkosi-tmp_eq6cbps/root/sys --unsetenv TMPDIR --ro-bind /home/x/mkosi-test/.mkosi-tmp_eq6cbps/root/etc/machine-id /home/x/mkosi-test/.mkosi-tmp_eq6cbps/root/etc/machine-id --bind /home/x/mkosi-test/.mkosi-tmp_eq6cbps/root/etc/passwd /etc/passwd --bind /home/x/mkosi-test/.mkosi-tmp_eq6cbps/root/etc/group /etc/group --bind /home/x/mkosi-test/.mkosi-tmp_eq6cbps/root/etc/shadow /etc/shadow --bind /x/mkosi-test/.mkosi-tmp_eq6cbps/root/etc/gshadow /etc/gshadow sh -c 'chmod 1777 /home/x/mkosi-test/.mkosi-tmp_eq6cbps/root/tmp /home/x/mkosi-test/.mkosi-tmp_eq6cbps/root/var/tmp /home/x/mkosi-test/.mkosi-tmp_eq6cbps/root/dev/shm && mkdir /home/x/mkosi-test/.mkosi-tmp_eq6cbps/root/run/host && echo mkosi >/home/x/mkosi-test/.mkosi-tmp_eq6cbps/root/run/host/container-manager && exec $0 "$@"' pacman --config /home/x/mkosi-test/.mkosi-tmp_eq6cbps/pkgmngr/etc/pacman.conf --root /home/x/mkosi-test/.mkosi-tmp_eq6cbps/root --logfile=/dev/null --cachedir '/home/x/mkosi-test/mkosi.cache/arch~rolling' --gpgdir /etc/pacman.d/gnupg --hookdir /home/x/mkosi-test/.mkosi-tmp_eq6cbps/root/etc/pacman.d/hooks --arch x86_64 --color auto --noconfirm --needed -Sy base-devel helix" returned non-zero exit code 1.
```

Is there any known solution or workaround?

### Basic Info
Host OS: Arch Linux (up to date)
mkosi version: 16

### Small Example

Create a `mkosi` directory with two presets, `base` and `extension`. Create a `mkosi.conf` file in both, with the following contents.

`mkosi.presets/base/mkosi.conf`:
```
[Distribution]
Distribution=arch

[Output]
Format=directory
ImageId=base

[Content]
Packages=
        base
        linux
Bootable=false
```

`mkosi.presets/extension/mkosi.conf`:
```
[Preset]
Dependencies=base

[Distribution]
Distribution=arch

[Output]
Format=directory
ImageId=extension
Overlay=true

[Content]
BaseTrees=../../mkosi.output/base/
Packages=
        base-devel
        helix
Bootable=false
```


Discussion:
@TeFiLeDo Are your using btrfs as your filesystem? If so, can you try again with `--use-subvolumes no`?

Resolving patch:
diff --git a/mkosi/state.py b/mkosi/state.py
--- a/mkosi/state.py
+++ b/mkosi/state.py
@@ -16,7 +16,13 @@ def __init__(self, args: MkosiArgs, config: MkosiConfig, workspace: Path) -> Non
         self.workspace = workspace
 
         with umask(~0o755):
-            make_tree(self.config, self.root)
+            # Using a btrfs subvolume as the upperdir in an overlayfs results in EXDEV so make sure we create
+            # the root directory as a regular directory if the Overlay= option is enabled.
+            if config.overlay:
+                self.root.mkdir()
+            else:
+                make_tree(self.config, self.root)
+
         self.staging.mkdir()
         self.pkgmngr.mkdir()
         self.install_dir.mkdir(exist_ok=True)


Test patch: