summaryrefslogtreecommitdiff
path: root/pkg/systemd/generate/containers.go
diff options
context:
space:
mode:
authorValentin Rothberg <rothberg@redhat.com>2020-06-15 15:32:43 +0200
committerValentin Rothberg <rothberg@redhat.com>2020-06-15 15:56:02 +0200
commit6118ab494884eea62c388fa1536349f05cdac9d3 (patch)
tree73e90cb653b6512f23d995d05de241932b3d1617 /pkg/systemd/generate/containers.go
parentfe488b5f11836a021bcef6217aeeea41b1321217 (diff)
downloadpodman-6118ab494884eea62c388fa1536349f05cdac9d3.tar.gz
podman-6118ab494884eea62c388fa1536349f05cdac9d3.tar.bz2
podman-6118ab494884eea62c388fa1536349f05cdac9d3.zip
generate systemd: `--replace` on named containers/pods
Use `--replace` for named containers and pods. This will clean up previous containers and podsthat may not have been removed after a system crash. Fixes: #5485 Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
Diffstat (limited to 'pkg/systemd/generate/containers.go')
-rw-r--r--pkg/systemd/generate/containers.go38
1 files changed, 28 insertions, 10 deletions
diff --git a/pkg/systemd/generate/containers.go b/pkg/systemd/generate/containers.go
index dced1a3da..4180022cb 100644
--- a/pkg/systemd/generate/containers.go
+++ b/pkg/systemd/generate/containers.go
@@ -207,24 +207,42 @@ func executeContainerTemplate(info *containerInfo, options entities.GenerateSyst
info.CreateCommand = filterPodFlags(info.CreateCommand)
}
- // Enforce detaching
- //
- // since we use systemd `Type=forking` service
- // @see https://www.freedesktop.org/software/systemd/man/systemd.service.html#Type=
- // when we generated systemd service file with the --new param,
- // `ExecStart` will have `/usr/bin/podman run ...`
- // if `info.CreateCommand` has no `-d` or `--detach` param,
- // podman will run the container in default attached mode,
- // as a result, `systemd start` will wait the `podman run` command exit until failed with timeout error.
+ // Presence check for certain flags/options.
hasDetachParam := false
+ hasNameParam := false
+ hasReplaceParam := false
for _, p := range info.CreateCommand[index:] {
- if p == "--detach" || p == "-d" {
+ switch p {
+ case "--detach", "-d":
hasDetachParam = true
+ case "--name":
+ hasNameParam = true
+ case "--replace":
+ hasReplaceParam = true
}
}
+
if !hasDetachParam {
+ // Enforce detaching
+ //
+ // since we use systemd `Type=forking` service @see
+ // https://www.freedesktop.org/software/systemd/man/systemd.service.html#Type=
+ // when we generated systemd service file with the
+ // --new param, `ExecStart` will have `/usr/bin/podman
+ // run ...` if `info.CreateCommand` has no `-d` or
+ // `--detach` param, podman will run the container in
+ // default attached mode, as a result, `systemd start`
+ // will wait the `podman run` command exit until failed
+ // with timeout error.
startCommand = append(startCommand, "-d")
}
+ if hasNameParam && !hasReplaceParam {
+ // Enforce --replace for named containers. This will
+ // make systemd units more robuts as it allows them to
+ // start after system crashes (see
+ // github.com/containers/libpod/issues/5485).
+ startCommand = append(startCommand, "--replace")
+ }
startCommand = append(startCommand, info.CreateCommand[index:]...)
info.ExecStartPre = "/usr/bin/rm -f {{.PIDFile}} {{.ContainerIDFile}}"