add flag "--pidfile" for podman create/run

Signed-off-by: chenkang <kongchen28@gmail.com>
This commit is contained in:
wuhua.ck
2021-04-15 22:36:50 +08:00
committed by chenkang
parent 373f15f617
commit 8fbe06b8cb
12 changed files with 53 additions and 1 deletions

View File

@ -817,6 +817,12 @@ func DefineCreateFlags(cmd *cobra.Command, cf *ContainerCLIOpts) {
) )
_ = cmd.RegisterFlagCompletionFunc(cgroupConfFlagName, completion.AutocompleteNone) _ = cmd.RegisterFlagCompletionFunc(cgroupConfFlagName, completion.AutocompleteNone)
pidFileFlagName := "pidfile"
createFlags.StringVar(
&cf.PidFile,
pidFileFlagName, "",
"Write the container process ID to the file")
_ = createFlags.MarkHidden("signature-policy") _ = createFlags.MarkHidden("signature-policy")
if registry.IsRemote() { if registry.IsRemote() {
_ = createFlags.MarkHidden("env-host") _ = createFlags.MarkHidden("env-host")

View File

@ -122,6 +122,7 @@ type ContainerCLIOpts struct {
VolumesFrom []string VolumesFrom []string
Workdir string Workdir string
SeccompPolicy string SeccompPolicy string
PidFile string
Net *entities.NetOptions Net *entities.NetOptions

View File

@ -644,6 +644,7 @@ func FillOutSpecGen(s *specgen.SpecGenerator, c *ContainerCLIOpts, args []string
s.Timezone = c.Timezone s.Timezone = c.Timezone
s.Umask = c.Umask s.Umask = c.Umask
s.Secrets = c.Secrets s.Secrets = c.Secrets
s.PidFile = c.PidFile
return nil return nil
} }

View File

@ -63,6 +63,10 @@ func createFlags(cmd *cobra.Command) {
common.DefineNetFlags(cmd) common.DefineNetFlags(cmd)
flags.SetNormalizeFunc(utils.AliasFlags) flags.SetNormalizeFunc(utils.AliasFlags)
if registry.IsRemote() {
_ = flags.MarkHidden("pidfile")
}
} }
func init() { func init() {

View File

@ -76,8 +76,10 @@ func runFlags(cmd *cobra.Command) {
detachKeysFlagName := "detach-keys" detachKeysFlagName := "detach-keys"
flags.StringVar(&runOpts.DetachKeys, detachKeysFlagName, containerConfig.DetachKeys(), "Override the key sequence for detaching a container. Format is a single character `[a-Z]` or a comma separated sequence of `ctrl-<value>`, where `<value>` is one of: `a-cf`, `@`, `^`, `[`, `\\`, `]`, `^` or `_`") flags.StringVar(&runOpts.DetachKeys, detachKeysFlagName, containerConfig.DetachKeys(), "Override the key sequence for detaching a container. Format is a single character `[a-Z]` or a comma separated sequence of `ctrl-<value>`, where `<value>` is one of: `a-cf`, `@`, `^`, `[`, `\\`, `]`, `^` or `_`")
_ = cmd.RegisterFlagCompletionFunc(detachKeysFlagName, common.AutocompleteDetachKeys) _ = cmd.RegisterFlagCompletionFunc(detachKeysFlagName, common.AutocompleteDetachKeys)
if registry.IsRemote() { if registry.IsRemote() {
_ = flags.MarkHidden("preserve-fds") _ = flags.MarkHidden("preserve-fds")
_ = flags.MarkHidden("pidfile")
} }
} }

View File

@ -1224,6 +1224,13 @@ The default working directory for running binaries within a container is the roo
The image developer can set a different default with the WORKDIR instruction. The operator The image developer can set a different default with the WORKDIR instruction. The operator
can override the working directory by using the **-w** option. can override the working directory by using the **-w** option.
#### **\-\-pidfile**=*path*
Write the pid of the container process to a file.
The default pidfile is RunDir/pidfile.
## EXAMPLES ## EXAMPLES
### Create a container using a local image ### Create a container using a local image

View File

@ -1305,6 +1305,12 @@ The default working directory for running binaries within a container is the roo
The image developer can set a different default with the WORKDIR instruction. The operator The image developer can set a different default with the WORKDIR instruction. The operator
can override the working directory by using the **-w** option. can override the working directory by using the **-w** option.
#### **\-\-pidfile**=*path*
Write the pid of the container process to a file.
The default pidfile is RunDir/pidfile.
## Exit Status ## Exit Status
The exit code from **podman run** gives information about why the container The exit code from **podman run** gives information about why the container

View File

@ -364,4 +364,6 @@ type ContainerMiscConfig struct {
Timezone string `json:"timezone,omitempty"` Timezone string `json:"timezone,omitempty"`
// Umask is the umask inside the container. // Umask is the umask inside the container.
Umask string `json:"umask,omitempty"` Umask string `json:"umask,omitempty"`
// PidFile is the file that saves the pid of the container process
PidFile string `json:"pid_file,omitempty"`
} }

View File

@ -1025,7 +1025,12 @@ func (r *ConmonOCIRuntime) createOCIContainer(ctr *Container, restoreOptions *Co
} }
} }
args := r.sharedConmonArgs(ctr, ctr.ID(), ctr.bundlePath(), filepath.Join(ctr.state.RunDir, "pidfile"), ctr.LogPath(), r.exitsDir, ociLog, ctr.LogDriver(), logTag) pidfile := ctr.config.PidFile
if pidfile == "" {
pidfile = filepath.Join(ctr.state.RunDir, "pidfile")
}
args := r.sharedConmonArgs(ctr, ctr.ID(), ctr.bundlePath(), pidfile, ctr.LogPath(), r.exitsDir, ociLog, ctr.LogDriver(), logTag)
if ctr.config.Spec.Process.Terminal { if ctr.config.Spec.Process.Terminal {
args = append(args, "-t") args = append(args, "-t")

View File

@ -1692,6 +1692,17 @@ func WithSecrets(secretNames []string) CtrCreateOption {
} }
} }
// WithPidFile adds pidFile to the container
func WithPidFile(pidFile string) CtrCreateOption {
return func(ctr *Container) error {
if ctr.valid {
return define.ErrCtrFinalized
}
ctr.config.PidFile = pidFile
return nil
}
}
// Pod Creation Options // Pod Creation Options
// WithInfraImage sets the infra image for libpod. // WithInfraImage sets the infra image for libpod.

View File

@ -375,6 +375,9 @@ func createContainerOptions(ctx context.Context, rt *libpod.Runtime, s *specgen.
} }
options = append(options, libpod.WithDependencyCtrs(deps)) options = append(options, libpod.WithDependencyCtrs(deps))
} }
if s.PidFile != "" {
options = append(options, libpod.WithPidFile(s.PidFile))
}
return options, nil return options, nil
} }

View File

@ -171,6 +171,10 @@ type ContainerBasicConfig struct {
// container. Dependencies can be specified by name or full/partial ID. // container. Dependencies can be specified by name or full/partial ID.
// Optional. // Optional.
DependencyContainers []string `json:"dependencyContainers,omitempty"` DependencyContainers []string `json:"dependencyContainers,omitempty"`
// PidFile is the file that saves container process id.
// set tags as `json:"-"` for not supported remote
// Optional.
PidFile string `json:"-"`
} }
// ContainerStorageConfig contains information on the storage configuration of a // ContainerStorageConfig contains information on the storage configuration of a