Repository: systemd/mkosi

Issue:
Write-lock error when generating qcow2 files
when generating the image with 
```
[Distribution]
Distribution=arch

[Output]
Format=disk
Bootable=yes
QCow2=yes
Output=image.qcow2
ExtraSearchPaths=/home/bschlueter/University/Github/systemd/build

[Packages]
Packages=
         openssh
         vim
         crictl
         critest
         kubeadm
         kubectl
         qemu-guest-agent
         socat
         kubelet
         ethtool
         containerd

[Validation]
Password=password

[Content]
Cache=.cache
Autologin=yes

[Host]
QemuHeadless=yes

```
mkosi fails with 
```
‣ Converting image file to qcow2…
qemu-img: /home/bschlueter/University/Github/mkosi-test/mkosi.images.tmpleott8_1/staging/image.qcow2: error while converting qcow2: Failed to get "write" lock
Is another process using the image [/home/bschlueter/University/Github/mkosi-test/mkosi.images.tmpleott8_1/staging/image.qcow2]?
Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/home/bschlueter/University/Github/mkosi/mkosi/__main__.py", line 45, in <module>
    main()
  File "/usr/lib/python3.10/contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "/home/bschlueter/University/Github/mkosi/mkosi/__main__.py", line 41, in main
    run_verb(a)
  File "/home/bschlueter/University/Github/mkosi/mkosi/__init__.py", line 4511, in run_verb
    build_stuff(config)
  File "/home/bschlueter/University/Github/mkosi/mkosi/__init__.py", line 3860, in build_stuff
    qcow2_output(state)
  File "/home/bschlueter/University/Github/mkosi/mkosi/__init__.py", line 1264, in qcow2_output
    run(["qemu-img", "convert", "-onocow=on", "-fraw", "-Oqcow2",
  File "/home/bschlueter/University/Github/mkosi/mkosi/backend.py", line 742, in run
    return subprocess.run(cmdline, check=check, stdout=stdout, stderr=stderr, env={**os.environ, **env}, **kwargs)
  File "/usr/lib/python3.10/subprocess.py", line 526, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['qemu-img', 'convert', '-onocow=on', '-fraw', '-Oqcow2', '/home/bschlueter/University/Github/mkosi-test/mkosi.images.tmpleott8_1/staging/image.qcow2', '/home/bschlueter/University/Github/mkosi-test/mkosi.images.tmpleott8_1/staging/image.qcow2']' returned non-zero exit status 1.
```
without specifying qcow2 it works.


Discussion:

Resolving patch:
diff --git a/mkosi/__init__.py b/mkosi/__init__.py
--- a/mkosi/__init__.py
+++ b/mkosi/__init__.py
@@ -1263,7 +1263,8 @@ def qcow2_output(state: MkosiState) -> None:
     with complete_step("Converting image file to qcow2…"):
         run(["qemu-img", "convert", "-onocow=on", "-fraw", "-Oqcow2",
              state.staging / state.config.output.name,
-             state.staging / state.config.output.name])
+             state.workspace / "qemu.img"])
+        os.rename(state.workspace / "qemu.img", state.staging / state.config.output.name)
 
 
 def copy_nspawn_settings(state: MkosiState) -> None:


Test patch: