38 Commits

Author SHA1 Message Date
221cfc6872 container/pod id file: truncate instead of throwing an error
Truncate the container and pod ID files instead of throwing an error.
The main motivation is to prevent redundant work when starting systemd
units.  Throwing an error when the file already exists is not preventing
races or file corruptions, so let's leave that to the user which in
almost all cases are generated (and tested) systemd units.

Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
2022-10-25 09:37:35 +02:00
fb3d55006f Improve generate systemd format
Fixes: https://github.com/containers/podman/issues/14897

Followup to #13814

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2022-09-21 05:10:55 -04:00
884fd4575c Revert "generate systemd: drop ExecStop"
This reverts commit c20abf12c714f359c7bbb291c444530f70cb1185. In the
absence of `ExecStop` step, systemd will send the stop/kill signals to
the main PID while I asummed that systemd would jump directly to an
ExecStopPost step instead.

Hence revert the commit to let Podman take care of stopping rather than
systemd.

Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
2022-09-15 17:56:45 +02:00
c20abf12c7 generate systemd: drop ExecStop
Drop the ExecStop step to simplify the generated units a bit.

The extra ExecStopPost step was added by commit e5c343294424. If the
main PID (i.e., conmon) is killed, systemd will not execute ExecStop
(since the main PID is already down) but only execute the *Post steps.
Credits to the late Ulrich Obergfell for tracking this issue down; he is
missed.

The ExecStop step can safely be dropped since the Post step will take of
stopping (and removing) in any case.

Context: #15686
Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
2022-09-15 13:15:35 +02:00
dd53ee5998 fix ci: update systemd generate unit test
Two PRs have been merged causing a failure in one unit test.
Fix the unit test to turn CI green again.

Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
2022-09-14 14:13:33 +02:00
ae20f19351 Merge pull request #15777 from vrothberg/fix-14546
generate systemd: fix pod dependencies
2022-09-14 13:08:04 +02:00
0aedddd3b3 [systemd] Ensure that podCreateArgs appear last in ExecStartPre=
When creating a new pod without the `--name` flag, e.g.:
`podman pod create foobar`
it will get the name `foobar` implicitly and this will be recorded as the in the
`podCreateArgs`. Unfortunately, the implicit name only works if it appears as
the **last** argument of the startup command.
With 6e2e3a78ed1d05ee5f23f65b814e8135021961dd we started appending the pod
security policy to the startCommand, resulting in the following `ExecStartPre=`
line:
```
/usr/bin/podman pod create --infra-conmon-pidfile %t/pod-foobar.pid --pod-id-file %t/pod-foobar.pod-id foobar --exit-policy=stop
```
This fails to launch, as the `pod create` command expects only a single
non-flag parameter, but it assumes that `exit-policy=stop` is a second and
terminates immediately instead.

This fixes https://github.com/containers/podman/issues/15592

Signed-off-by: Dan Čermák <dcermak@suse.com>
2022-09-14 09:37:02 +02:00
fe04c86a24 generate systemd: fix pod dependencies
Change the dependencies from a pod unit to its associated container
units from `Requires` to `Wants` to prevent the entire pod from
transitioning to a failed state.  Restart policies for individual
containers can be configured separately.

Also make sure that the pod's RunRoot is always set.

Fixes: #14546
Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
2022-09-13 17:25:48 +02:00
6e2e3a78ed generate systemd: pods: set exit policy
Unless specified in the create command of the pod, enforce the exit
policy to "stop".  With "stop", a pod is stopped when the last container
exits and does not continue running.  This behavior integrates much
better into systemd which is now able to tell whether the service
running as pod is actually running/active or not.

Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
2022-05-03 15:28:50 +02:00
714e5a13d9 Separator is no longer prepended when prefix is empty on podman generate systemd
When podman generate systemd is invoked, it previously did not check if
container-prefix or pod-prefix are empty. When these are empty, the file name
starts with the separator, which is hyphen by default. This results in files
like '-containername.service'.

The code now checks if these prefixes are empty. If they are, the filename no
longer adds a separator. Instead, it uses name or ID of the container or pod.

Closes #13272

Signed-off-by: Nirmal Patel <npate012@gmail.com>
2022-03-16 20:26:59 -04:00
b9a2d8698a Handlers for generate systemd with custom dependencies
This commit includes:
* Handlers for generate systemd unit
  with manually defined dependencies such as:
  Wants=, After= and Requires=

* The new unit and e2e tests for checking generated systemd units
  for container and pod with custom dependencies

* Documented descriptions for custom dependencies options

Signed-off-by: Eugene (Evgenii) Shubin <esendjer@gmail.com>
2022-01-19 21:46:16 +05:00
bd09b7aa79 bump go module to version 4
Automated for .go files via gomove [1]:
`gomove github.com/containers/podman/v3 github.com/containers/podman/v4`

Remaining files via vgrep [2]:
`vgrep github.com/containers/podman/v3`

[1] https://github.com/KSubedi/gomove
[2] https://github.com/vrothberg/vgrep

Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
2022-01-18 12:47:07 +01:00
c9ad1da51c Add restart-sec option to systemd generate
Signed-off-by: Ondra Machacek <omachace@redhat.com>
2021-12-03 12:11:28 +01:00
9a10e2124b systemd: replace multi-user with default.target
Replace `multi-user.target` with `default.target` across the code base.
It seems like the multi-user one is not available for (rootless) users
on F35 anymore is causing issues in all kinds of ways, for instance,
enabling the podman.service or generated systemd units.

Fixes: #12438
Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
2021-11-30 14:37:25 +01:00
d1573b95e3 generate systemd: handle --restart
Handle custom restart policies of containers when generating the unit
files; those should be set on the unit level and removed from ExecStart
flags.

Fixes: #11438
Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
2021-09-07 13:58:11 +02:00
74ab2aaf9f Revert "generate systemd: custom stop signal"
This reverts commit 70801b3d714b067d64744697433c5841926dad4d.

It turns out that letting systemd handle stopping the container is not
working as I thought it will.  Conmon is receiving the stop/kill signals
and may exit non-zero, which in turn lets the systemd service transition
into the `failed` state.

We need to get back to letting Podman stop the containers and do a
partial revert of commit 9ac5267 which removed using --cidfile.

Happening in a following commit.

Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
2021-08-24 13:33:06 +02:00
70801b3d71 generate systemd: custom stop signal
Commit 9ac5267598c3 changed the type of the generated systemd units from
forking to notify.  Parts of these changes was also removing the need to
pass any information via the file system (e.g., PIDFILE, container ID).
That in turn implies that systemd takes care of stopping the container.

By default, systemd first sends a SIGTERM and after a certain timeout,
it'll send a SIGKILL.  That's pretty much what Podman is doing, unless
the container was created with a custom stop signal which is the case
when the --stop-signal flag was used or systemd is mounted.

Account for that by using systemd's KillSignal option which allows for
changing SIGTERM to another signal.  Also make sure that we're using the
correct timeout for units generated with --new.

Fixes: #11304
Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
2021-08-24 10:50:16 +02:00
6f1c7a0b6b systemd: require network*-online*.target
Require the network to be online in all (generated) systemd units to
make sure that containers and Podman run only after the network has been
fully configured.

Fixes: #10655
Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
2021-07-16 14:58:08 +02:00
5307218419 generate systemd: make mounts portable
Commit 748826fc88fc fixed a bug where slow mounting of the runroot was
causing issues when the units are started at boot.  The fix was to add
the container's runroot to the required mounts; the graph root has been
added as well.

Hard-coding the run- and graphroot to the required mounts, however,
breaks the portability of units generated with --now.  Those units are
intended to be running on any machine as, theoreticaly, any user.

Make the mounts portable by using the `%t` macro for the run root.
Since the graphroot's location varies across root and ordinary users,
drop it from the list of required mounts.  The graphroot was not causing
issues.

Fixes: #10493
Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
2021-05-31 11:31:27 +02:00
aabafc5b1b podman generate systemd --new do not duplicate params
podman generate systemd --new inserts extra idfile arguments. The
generated unit can break when the user did provide their own idfile
arguments as they overwrite the arguments added by generate systemd.
This also happens when a user tries to generate the systemd unit on
a container already create with a --new unit. This should now
create a identical unit. The solution is to remove all user provided
idfile arguments.

This commit also ensures that we do not remove arguments that are part
off the containers entrypoint.

Fixes #9776

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
2021-03-28 20:01:17 +02:00
748826fc88 Add RequiresMountsFor= to systemd generate
It is rare but possible that storage locations for the graphroot and the
runroot are not mounted at boot time, and therefore might race when
doing container operations.  An example we've seen in the wild is that a
slow tmpfs mount for the runroot would suddenly mount over /run, causing
the container to lose all currently-running data, requiring a system
refresh to get it back.

This patch adds RequiresMountsFor= to the systemd.unit header to ensure
the paths for both the graphroot and runroot are mounted prior to
starting any generated unit files.

Signed-off-by: Robb Manes <rmanes@redhat.com>
2021-03-26 08:53:26 -04:00
a6e7d19c46 Merge pull request #9445 from jmguzik/no-header-info-for-systemd-generation
No header info for systemd generation
2021-02-22 13:44:43 -05:00
d2f3098c6f --no-header flag implementation for generate systemd
Signed-off-by: Jakub Guzik <jakubmguzik@gmail.com>
2021-02-22 14:48:33 +01:00
5dded6fae7 bump go module to v3
We missed bumping the go module, so let's do it now :)

* Automated go code with github.com/sirkon/go-imports-rename
* Manually via `vgrep podman/v2` the rest

Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
2021-02-22 09:03:51 +01:00
c3cbaa355c Make generate systemd --new robust against double curly braces
If the container create command contains an argument with double
curly braces the golang template parsing can fail since it tries
to interpret the value as variable. To fix this change the default
delimiter for the internal template to `{{{{`.

Fixes #9034

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
2021-01-20 15:07:37 +01:00
68ca9066d0 More /var/run -> /run
PR #8851 broke CI: it included "/var/run" strings that,
per #8771, should have been just "/run".

Signed-off-by: Ed Santiago <santiago@redhat.com>
2021-01-12 12:38:11 -07:00
0ccc88813e Merge pull request #8851 from Luap99/fix-generate-systemd-flag-parsing
Make podman generate systemd --new flag parsing more robust
2021-01-12 11:47:14 -05:00
ef82be4e00 Make podman generate systemd --new flag parsing more robust
First, use the pflag library to parse the flags. With this we can
handle all corner cases such as -td or --detach=false.

Second, preserve the root args with --new. They are used for all podman
commands in the unit file. (e.g. podman --root /tmp run alpine)

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
2021-01-07 11:50:28 +01:00
d9ebbbfe5b Switch references of /var/run -> /run
Systemd is now complaining or mentioning /var/run as a legacy directory.
It has been many years where /var/run is a symlink to /run on all
most distributions, make the change to the default.

Partial fix for https://github.com/containers/podman/issues/8369

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2021-01-07 05:37:24 -05:00
219c69ef03 generate systemd: do not set KillMode
`KillMode=none` has been deprecated in systemd and is now throwing big
warnings when being used.  Users have reported the issues upstream
(see #8615) and on the mailing list.

This deprecation was mainly motivated by an abusive use of third-party
vendors causing all kinds of undesired side-effects.  For instance, busy
mounts that delay reboot.

After talking to the systemd team, we came up with the following plan:

 **Short term**: we can use TimeoutStopSec and remove KillMode=none which
 will default to cgroup.

 **Long term**: we want to change the type to sdnotify. The plumbing for
 Podman is done but we need it for conmon. Once sdnotify is working, we
 can get rid of the pidfile handling etc. and let Podman handle it.
 Michal Seklatar came up with a nice idea that Podman increase the time
 out on demand. That's a much cleaner way than hard-coding the time out
 in the unit as suggest in the short-term solution.

This change is executing the short-term plan and sets a minimum timeout
of 60 seconds.  User-specified timeouts are added to that.

Fixes: #8615
Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
2021-01-05 16:37:24 +01:00
ebfea2f4f8 APIv2 add generate systemd endpoint
Add support for generating systemd units
via the api and podman-remote.

Change the GenerateSystemdReport type to return the
units as map[string]string with the unit name as key.

Add `--format` flag to `podman generate systemd`
to allow the output to be formatted as json.

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
2020-09-02 22:06:19 +02:00
b1ffa2324e generate systemd: quote arguments with whitespace
Make sure that arguments with whitespace are properly quoted so they are
interpreted as one (and not multiple ones) by systemd.

Now `-e tz="america/new york"` will be generated as `-e "tz=america/new york"`.
The quotes are moving but the argument is still correct.

Fixes: #7285
Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
2020-08-19 08:32:51 +02:00
a5e37ad280 Switch all references to github.com/containers/libpod -> podman
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2020-07-28 08:23:45 -04:00
8489dc4345 move go module to v2
With the advent of Podman 2.0.0 we crossed the magical barrier of go
modules.  While we were able to continue importing all packages inside
of the project, the project could not be vendored anymore from the
outside.

Move the go module to new major version and change all imports to
`github.com/containers/libpod/v2`.  The renaming of the imports
was done via `gomove` [1].

[1] https://github.com/KSubedi/gomove

Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
2020-07-06 15:50:12 +02:00
f18d2751f6 correct the absolute path of rm executable
Signed-off-by: Yuan-Hao Chen <yhchen0906@gmail.com>
2020-06-19 22:23:35 +08:00
e5c3432944 generate systemd: ExecStopPost for all units
Add an `ExecStopPost` run even for units generated without `--new`.
Although it may seem redundant to run `container/pod stop` twice at
first glance, we really need the post run.  If the main PID (i.e.,
conmon) is killed, systemd will not execute `ExecStop` but only the
post one.  We made this obeservation in a customer issue and could
reproduce the behavior consistently.  Hence, the post run is needed
to properly clean up when conmon is killed and it's pretty much a
NOP in all other cases.

Credits to Ulrich Obergfell for throrough and detailed analyses,
which ultimately lead to this fix.

Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
2020-06-17 18:52:44 +02:00
6118ab4948 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>
2020-06-15 15:56:02 +02:00
8d8746adee generate systemd: create pod template
Create a new template for generating a pod unit file. Eventually, this
allows for treating and extending pod and container generation
seprately.

The `--new` flag now also works on pods.

Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
2020-06-11 11:01:13 +02:00