podman icon indicating copy to clipboard operation
podman copied to clipboard

Quadlet volume not recreated after being removed

Open matthijskooijman opened this issue 10 months ago • 9 comments

Issue Description

When using .volume files, these produce services with RemainAfterExit=Yes. This means a volume is created only once and when it is subsequently deleted (which I did because I had updated the volume options in the .volume file), it is not recreated by the quadlet service files (and instead created by podman run implicitly with default options).

Steps to reproduce the issue

❯ sudo tee /etc/containers/systemd/foo.container > /dev/null << EOF
[Container]
Image=docker.io/hello-world:latest
Volume=foo.volume:/foo
EOF

❯ sudo tee /etc/containers/systemd/foo.volume > /dev/null << EOF
[Volume]
User=1000                         
EOF                   

❯ sudo systemctl daemon-reload

❯ sudo systemctl start foo

❯ sudo ls -ld /var/lib/containers/storage/volumes/systemd-foo/_data
drwxr-xr-x 2 matthijs root 4096 Jan  5 18:27 /var/lib/containers/storage/volumes/systemd-foo/_data

❯ sudo podman volume rm systemd-foo
systemd-foo

❯ sudo systemctl start foo

❯ sudo ls -ld /var/lib/containers/storage/volumes/systemd-foo/_data
drwxr-xr-x 2 root root 4096 Jan  5 18:27 /var/lib/containers/storage/volumes/systemd-foo/_data

Describe the results you received

The volume is not recreated by the foo-volume.service with the right options (uid=1000). Instead, the container is started and podman implicitly creates the volume with the default options (uid=0).

This is also visible in the journal, the first time foo-volume.service is started, but the second time it is not:

❯ sudo journalctl -u foo.service -u foo-volume.service -e
Jan 05 18:27:14 zozo systemd[1]: Starting foo-volume.service...
Jan 05 18:27:14 zozo podman[55776]: 2026-01-05 18:27:14.331612086 +0100 CET m=+0.023455833 volume create systemd-foo
Jan 05 18:27:14 zozo foo-volume[55776]: systemd-foo
Jan 05 18:27:14 zozo systemd[1]: Finished foo-volume.service.
Jan 05 18:27:14 zozo systemd[1]: Starting foo.service...
Jan 05 18:27:14 zozo podman[55792]: 2026-01-05 18:27:14.370545545 +0100 CET m=+0.021431909 container create 59254861fda8aa05f5eaa939459f7ff4ae8d410b5fd9804702cd07fb8807de1f (image=docker.io/library/h>
Jan 05 18:27:14 zozo podman[55792]: 2026-01-05 18:27:14.357800554 +0100 CET m=+0.008686935 image pull 1b44b5a3e06a9aae883e7bf25e45c100be0bb81a0e01b32de604f3ac44711634 docker.io/hello-world:latest
Jan 05 18:27:14 zozo podman[55792]: 2026-01-05 18:27:14.46780586 +0100 CET m=+0.118692244 container init 59254861fda8aa05f5eaa939459f7ff4ae8d410b5fd9804702cd07fb8807de1f (image=docker.io/library/hell>
Jan 05 18:27:14 zozo podman[55792]: 2026-01-05 18:27:14.473270748 +0100 CET m=+0.124157116 container start 59254861fda8aa05f5eaa939459f7ff4ae8d410b5fd9804702cd07fb8807de1f (image=docker.io/library/he>
Jan 05 18:27:14 zozo systemd-foo[55914]: Hello from Docker!
Jan 05 18:27:14 zozo systemd[1]: Started foo.service.
Jan 05 18:27:14 zozo foo[55792]: 59254861fda8aa05f5eaa939459f7ff4ae8d410b5fd9804702cd07fb8807de1f
Jan 05 18:27:14 zozo podman[55945]: 2026-01-05 18:27:14.494566818 +0100 CET m=+0.013907817 container died 59254861fda8aa05f5eaa939459f7ff4ae8d410b5fd9804702cd07fb8807de1f (image=docker.io/library/hel>
Jan 05 18:27:14 zozo podman[55945]: 2026-01-05 18:27:14.892265994 +0100 CET m=+0.411607130 container remove 59254861fda8aa05f5eaa939459f7ff4ae8d410b5fd9804702cd07fb8807de1f (image=docker.io/library/h>
Jan 05 18:27:14 zozo systemd[1]: foo.service: Deactivated successfully.


Jan 05 18:27:28 zozo systemd[1]: Starting foo.service...
Jan 05 18:27:29 zozo podman[56286]: 2026-01-05 18:27:29.00155174 +0100 CET m=+0.026409568 volume create systemd-foo
Jan 05 18:27:29 zozo podman[56286]: 2026-01-05 18:27:29.010531364 +0100 CET m=+0.035389202 container create bed42283a7129114229d67cc03eb7a454328de82362c0c7db83ac3f1ead13552 (image=docker.io/library/h>
Jan 05 18:27:29 zozo podman[56286]: 2026-01-05 18:27:28.984573286 +0100 CET m=+0.009431165 image pull 1b44b5a3e06a9aae883e7bf25e45c100be0bb81a0e01b32de604f3ac44711634 docker.io/hello-world:latest
Jan 05 18:27:29 zozo podman[56286]: 2026-01-05 18:27:29.111775103 +0100 CET m=+0.136632946 container init bed42283a7129114229d67cc03eb7a454328de82362c0c7db83ac3f1ead13552 (image=docker.io/library/hel>
Jan 05 18:27:29 zozo podman[56286]: 2026-01-05 18:27:29.115609442 +0100 CET m=+0.140467272 container start bed42283a7129114229d67cc03eb7a454328de82362c0c7db83ac3f1ead13552 (image=docker.io/library/he>
Jan 05 18:27:29 zozo systemd[1]: Started foo.service.
Jan 05 18:27:29 zozo systemd-foo[56407]: 
Jan 05 18:27:29 zozo systemd-foo[56407]: Hello from Docker!
Jan 05 18:27:29 zozo foo[56286]: bed42283a7129114229d67cc03eb7a454328de82362c0c7db83ac3f1ead13552
Jan 05 18:27:29 zozo podman[56437]: 2026-01-05 18:27:29.132423223 +0100 CET m=+0.013521511 container died bed42283a7129114229d67cc03eb7a454328de82362c0c7db83ac3f1ead13552 (image=docker.io/library/hel>
Jan 05 18:27:29 zozo podman[56437]: 2026-01-05 18:27:29.526874745 +0100 CET m=+0.407973013 container remove bed42283a7129114229d67cc03eb7a454328de82362c0c7db83ac3f1ead13552 (image=docker.io/library/h>
Jan 05 18:27:29 zozo systemd[1]: foo.service: Deactivated successfully.

Describe the results you expected

I would expect the volume to be recreated with the original options (or, which was my original case, possibly with updated options if they were changed in the meanwhile).

Suggested fix

The obvious fix for this issue would be to not set RemainAfterExit=yes for volume units. If I set that explicitly in the volume file, things behave as expected:

❯ sudo tee /etc/containers/systemd/foo.volume > /dev/null << EOF
[Volume]
User=1000
[Service]
RemainAfterExit=no
EOF

❯ sudo tee /etc/containers/systemd/foo.container > /dev/null << EOF
[Container]
Image=docker.io/hello-world:latest
Volume=foo.volume:/foo
EOF

❯ sudo systemctl daemon-reload

❯ sudo systemctl start foo

❯ sudo ls -ld /var/lib/containers/storage/volumes/systemd-foo/_data
drwxr-xr-x 2 matthijs root 4096 Jan  5 18:40 /var/lib/containers/storage/volumes/systemd-foo/_data

❯ sudo podman volume rm systemd-foo
systemd-foo

❯ sudo systemctl start foo

❯ sudo ls -ld /var/lib/containers/storage/volumes/systemd-foo/_data
drwxr-xr-x 2 matthijs root 4096 Jan  5 18:40 /var/lib/containers/storage/volumes/systemd-foo/_data

AFAICS that would involve changing true to false in this line: https://github.com/containers/podman/blob/ed5f4d6513d1736f93d5aaa5f7d415a5cc846c39/pkg/systemd/quadlet/quadlet.go#L1197

There might be unintended consequences - I just started using podman, so I cannot oversee the results of this change well :-)

podman info output

host:
  arch: amd64
  buildahVersion: 1.42.1
  cgroupControllers:
  - memory
  - pids
  cgroupManager: systemd
  cgroupVersion: v2
  conmon:
    package: conmon_2.1.12-4_amd64
    path: /usr/bin/conmon
    version: 'conmon version 2.1.12, commit: unknown'
  cpuUtilization:
    idlePercent: 97.72
    systemPercent: 0.49
    userPercent: 1.8
  cpus: 22
  databaseBackend: sqlite
  distribution:
    codename: plucky
    distribution: ubuntu
    version: "25.04"
  eventLogger: journald
  freeLocks: 2048
  hostname: zozo
  idMappings:
    gidmap:
    - container_id: 0
      host_id: 1000
      size: 1
    - container_id: 1
      host_id: 100000
      size: 65536
    uidmap:
    - container_id: 0
      host_id: 1000
      size: 1
    - container_id: 1
      host_id: 100000
      size: 65536
  kernel: 6.14.0-36-generic
  linkmode: dynamic
  logDriver: journald
  memFree: 2017853440
  memTotal: 32511954944
  networkBackend: netavark
  networkBackendInfo:
    backend: netavark
    dns:
      package: aardvark-dns_1.12.2-2_amd64
      path: /usr/lib/podman/aardvark-dns
      version: aardvark-dns 1.12.2
    package: netavark_1.12.1-9_amd64
    path: /usr/lib/podman/netavark
    version: netavark 1.12.1
  ociRuntime:
    name: runc
    package: runc_1.3.3-0ubuntu1~25.04.3_amd64
    path: /usr/bin/runc
    version: |-
      runc version 1.3.3-0ubuntu1~25.04.3
      spec: 1.2.1
      go: go1.24.2
      libseccomp: 2.5.5
  os: linux
  pasta:
    executable: /usr/bin/pasta
    package: passt_0.0~git20250217.a1e48a0-1_amd64
    version: ""
  remoteSocket:
    exists: true
    path: /run/user/1000/podman/podman.sock
  rootlessNetworkCmd: pasta
  security:
    apparmorEnabled: false
    capabilities: CAP_CHOWN,CAP_DAC_OVERRIDE,CAP_FOWNER,CAP_FSETID,CAP_KILL,CAP_NET_BIND_SERVICE,CAP_SETFCAP,CAP_SETGID,CAP_SETPCAP,CAP_SETUID,CAP_SYS_CHROOT
    rootless: true
    seccompEnabled: true
    seccompProfilePath: /usr/share/containers/seccomp.json
    selinuxEnabled: false
  serviceIsRemote: false
  slirp4netns:
    executable: /usr/bin/slirp4netns
    package: slirp4netns_1.2.1-1build2_amd64
    version: |-
      slirp4netns version 1.2.1
      commit: 09e31e92fa3d2a1d3ca261adaeb012c8d75a8194
      libslirp: 4.8.0
      SLIRP_CONFIG_VERSION_MAX: 4
      libseccomp: 2.5.5
  swapFree: 27957260288
  swapTotal: 34359734272
  uptime: 531h 30m 56.00s (Approximately 22.12 days)
  variant: ""
plugins:
  authorization: null
  log:
  - k8s-file
  - none
  - passthrough
  - journald
  network:
  - bridge
  - macvlan
  - ipvlan
  volume:
  - local
registries: {}
store:
  configFile: /home/matthijs/.config/containers/storage.conf
  containerStore:
    number: 0
    paused: 0
    running: 0
    stopped: 0
  graphDriverName: overlay
  graphOptions: {}
  graphRoot: /home/matthijs/.local/share/containers/storage
  graphRootAllocated: 1055742447616
  graphRootUsed: 973944573952
  graphStatus:
    Backing Filesystem: extfs
    Native Overlay Diff: "true"
    Supports d_type: "true"
    Supports shifting: "false"
    Supports volatile: "true"
    Using metacopy: "false"
  imageCopyTmpDir: /var/tmp
  imageStore:
    number: 0
  runRoot: /run/user/1000/containers
  transientStore: false
  volumePath: /home/matthijs/.local/share/containers/storage/volumes
version:
  APIVersion: 5.7.0
  BuildOrigin: Debian
  Built: 1764725792
  BuiltTime: Wed Dec  3 02:36:32 2025
  GitCommit: ""
  GoVersion: go1.24.9
  Os: linux
  OsArch: linux/amd64
  Version: 5.7.0

Podman in a container

No

Privileged Or Rootless

Privileged

Upstream Latest Release

No (but one point release older, and looking at the code this did not change in git since then).

matthijskooijman avatar Jan 05 '26 17:01 matthijskooijman

What you described is by design.

The assumption is that volumes should persist across restarts. So, stopping the service does not delete the volume. As you pointed, if users wish to delete the volume, they have to do so manually via podman volume rm.

Also, as you mentioned, the systemd service is defined as RemainAfterExit=Yes. This to make the independent service not exit and to allow the dependency mechanism to work.

As a result of the two, when users manually delete the volume, they also need to manually stop the corresponding service. This to allow systemd to run it again when needed (which will create the volume correctly)

# First time starting the service
$ sudo systemctl start foo

# Manually deleting the volume
$ sudo podman volume rm systemd-foo
systemd-foo

# Manually stopping the volume's service
$ sudo systemctl stop foo-volume.service

# Since this container exists, you can just start the service, but in general, you should restart it.
$ sudo systemctl restart foo

ygalblum avatar Jan 05 '26 18:01 ygalblum

The assumption is that volumes should persist across restarts. So, stopping the service does not delete the volume. As you pointed, if users wish to delete the volume, they have to do so manually via podman volume rm.

Yes, that was clear to me (thinking on it, it might be more consistent if the volume would indeed be deleted on stop, but also utterly pointless, so this was never unclear to me).

Also, as you mentioned, the systemd service is defined as RemainAfterExit=Yes. This to make the independent service not exit and to allow the dependency mechanism to work.

I would argue that this is not allowing the dependency mechanism to work. I would say that the dependency on foo-volume.service should ensure that the volume exists when starting foo.service, which is not what is currently happening.

Also, there is now an inconsistency with rebooting: If I podman volume rm foo and then restart foo.service, it does not get recreated (by the quadlet config/service), but if I podman volume rm foo and then restart my system, it does get recreated as expected.

But maybe you mean there is another reason why RemainAfterExit=yes is needed and the volume service is not allowed to exit?

matthijskooijman avatar Jan 05 '26 18:01 matthijskooijman

The reason for RemainAfterExit=yes is the consistency of systemd, not of podman or any of its elements. From systemd documentation on oneshot units:

Behavior of oneshot is similar to exec; however, the service manager will consider the unit up after the main process exits. It will then start follow-up units. RemainAfterExit= is particularly useful for this type of service. Type=oneshot is the implied default if neither Type= nor ExecStart= are specified. Note that if this option is used without RemainAfterExit= the service will never enter "active" unit state, but will directly transition from "activating" to "deactivating" or "dead", since no process is configured that shall run continuously. In particular this means that after a service of this type ran (and which has RemainAfterExit= not set) it will not show up as started afterwards, but as dead.

I've done some more digging and it seems that both setting it or not doing so can create consistency issues. So, I'm not sure I want to change the behaviour.

Having said that, I'm looking into adding an option to instruct Podman not to create non-existing named volumes. This will at least cause the main service to fail instead of running incorrectly.

ygalblum avatar Jan 05 '26 20:01 ygalblum

I've done some more digging and it seems that both setting it or not doing so can create consistency issues. So, I'm not sure I want to change the behaviour.

Can you comment on what consistency issues these are? I cannot see them in the docs you posted.

In particular, AFAICS the volume services exist exclusively to ensure the volume exists when a depending container is started (since the volume unit does not do anything else, such as on stop). And the docs for Type=oneshot say:

       •   Behavior of oneshot is similar to simple; however, the service manager will consider the unit up after the main process exits. It will then start follow-up units.

So that should satisfy the goals: After the volume is created, the container can be started.

The docs then continue with:

RemainAfterExit= is particularly useful for this type of service. Type=oneshot is the implied default if neither Type= nor ExecStart= are specified. Note that if this option is used without RemainAfterExit= the service will never enter "active" unit state, but will directly transition from "activating" to "deactivating" or "dead", since no process is configured that shall run continuously. In particular this means that after a service of this type ran (and which has RemainAfterExit= not set) it will not show up as started afterwards, but as dead.

Which suggests that RemainAfterExit is useful to keep the service in the active state, which might be useful for actual services, or things that are not intended to be started a second time, but this does not seem needed to me in this case (and running it a second time is explicitly supported by adding --ignore).

Having said that, I'm looking into adding an option to instruct Podman not to create non-existing named volumes. This will at least cause the main service to fail instead of running incorrectly.

This sounds like a great idea. It does not solve the problem entirely, but at least prevents things from silently breaking, which helps a lot.

matthijskooijman avatar Jan 08 '26 11:01 matthijskooijman

A friendly reminder that this issue had no activity for 30 days.

github-actions[bot] avatar Feb 22 '26 00:02 github-actions[bot]

This sounds like a great idea. It does not solve the problem entirely, but at least prevents things from silently breaking, which helps a lot.

@matthijskooijman just letting you know that this was added here: https://github.com/containers/podman/pull/27867 and will part of the next release. I know it does not address the root of the issue, but it is a start.

ygalblum avatar Mar 02 '26 15:03 ygalblum

While investigating this behavior I initially considered opening a new issue, but then I found this one and thought it fits well, so I am adding my observations here.

I think there are actually two related aspects to the behavior described above.

  1. The .volume unit remains active (exited) after the dependent .container unit stops.
  2. Because of that, the .volume unit is not executed again, even if the underlying Podman volume was manually deleted.

Since .volume units behave like a oneshot ensure operation, systemd still considers the unit successful.

This leads to the following situation:

  • a .container unit starts and the referenced .volume unit creates the volume correctly
  • the .container unit is stopped again
  • the .volume unit remains active (exited)
  • the underlying Podman volume is deleted manually
  • the .container unit is started again

At that point the .volume unit is not executed again, so Podman implicitly recreates the missing volume with default settings instead of the options and possible labels defined in the .volume file.

This means the configuration defined in the .volume quadlet is silently bypassed.

My assumption was that the built-in --ignore flag of podman volume create should already cover this situation: if the volume exists nothing happens, and if it does not exist it should be created again with the defined options.

However, due to the .volume unit remaining active (exited), the creation step is never executed again.

@ygalblum You mentioned a fix in this issue. Does that fix address exactly this behavior?

flomickl avatar Mar 06 '26 08:03 flomickl

@flomickl you are correct in your analysis.

The change I introduced does not solve this issue but rather allows the user to instruct Podman to fail when the volume does not exist instead of creating one with default setting (by adding nocreate to the volume options). Once set, when the .container is started again (the last step in your sequence), Podman will not create the volume and instead fail to start the container. I know it does not solve the issue, but at least it blocks unintended behaviour.

@matthijskooijman suggested we revisit the decision to set RemainAfterExit=yes. Would one of you care to look into it and open a PR?

ygalblum avatar Mar 06 '26 14:03 ygalblum

@ygalblum I have submitted a PR for this in #29375 to disable RemainAfterExit=yes by default when converting Quadlet .volume files.

This allows systemd to re-execute volume creation when a dependent container service restarts after a volume has been removed or updated.

Mahajan-Sachin avatar Aug 02 '26 16:08 Mahajan-Sachin