16
"github.com/containers/buildah"
17
"github.com/containers/common/libnetwork/types"
18
"github.com/containers/common/pkg/cgroups"
19
"github.com/containers/common/pkg/config"
20
"github.com/containers/podman/v5/libpod/define"
21
"github.com/containers/podman/v5/libpod/events"
22
"github.com/containers/podman/v5/libpod/shutdown"
23
"github.com/containers/podman/v5/pkg/domain/entities/reports"
24
"github.com/containers/podman/v5/pkg/rootless"
25
"github.com/containers/podman/v5/pkg/specgen"
26
"github.com/containers/podman/v5/pkg/util"
27
"github.com/containers/storage"
28
"github.com/containers/storage/pkg/stringid"
29
"github.com/docker/go-units"
30
spec "github.com/opencontainers/runtime-spec/specs-go"
31
"github.com/opencontainers/runtime-tools/generate"
32
"github.com/sirupsen/logrus"
33
"golang.org/x/exp/slices"
40
type CtrCreateOption func(*Container) error
45
type ContainerFilter func(*Container) bool
48
func (r *Runtime) NewContainer(ctx context.Context, rSpec *spec.Spec, spec *specgen.SpecGenerator, infra bool, options ...CtrCreateOption) (*Container, error) {
50
return nil, define.ErrRuntimeStopped
53
options = append(options, withIsInfra())
55
return r.newContainer(ctx, rSpec, options...)
58
func (r *Runtime) PrepareVolumeOnCreateContainer(ctx context.Context, ctr *Container) error {
61
if !r.config.Containers.PrepareVolumeOnCreate {
66
if err := ctr.cleanupStorage(); err != nil {
67
logrus.Errorf("Cleaning up container storage %s: %v", ctr.ID(), err)
71
mountPoint, err := ctr.mountStorage()
74
ctr.state.Mounted = true
75
ctr.state.Mountpoint = mountPoint
76
if err = ctr.save(); err != nil {
77
logrus.Errorf("Saving container %s state: %v", ctr.ID(), err)
85
func (r *Runtime) RestoreContainer(ctx context.Context, rSpec *spec.Spec, config *ContainerConfig) (*Container, error) {
87
return nil, define.ErrRuntimeStopped
90
ctr, err := r.initContainerVariables(rSpec, config)
92
return nil, fmt.Errorf("initializing container variables: %w", err)
95
ctr.state.StartedTime = time.Now()
100
if strings.HasPrefix(ctr.config.ConmonPidFile, r.storageConfig.RunRoot) {
101
ctr.config.ConmonPidFile = ""
107
if strings.HasPrefix(ctr.config.PidFile, r.storageConfig.RunRoot) {
108
ctr.config.PidFile = ""
111
return r.setupContainer(ctx, ctr)
116
func (r *Runtime) RenameContainer(ctx context.Context, ctr *Container, newName string) (*Container, error) {
118
defer ctr.lock.Unlock()
120
if err := ctr.syncContainer(); err != nil {
124
newName = strings.TrimPrefix(newName, "/")
125
if newName == "" || !define.NameRegex.MatchString(newName) {
126
return nil, define.RegexError
131
newConf, err := r.state.GetContainerConfig(ctr.ID())
133
return nil, fmt.Errorf("retrieving container %s configuration from DB to remove: %w", ctr.ID(), err)
137
logrus.Infof("Going to rename container %s from %q to %q", ctr.ID(), ctr.Name(), newName)
141
oldName := ctr.config.Name
142
ctr.config.Name = newName
145
if err := r.state.SafeRewriteContainerConfig(ctr, oldName, ctr.config.Name, ctr.config); err != nil {
149
ctr.config.Name = oldName
150
return nil, fmt.Errorf("renaming container %s: %w", ctr.ID(), err)
159
if err := r.store.SetNames(ctr.ID(), []string{ctr.Name()}); err != nil {
163
ctr.newContainerEvent(events.Rename)
167
func (r *Runtime) initContainerVariables(rSpec *spec.Spec, config *ContainerConfig) (*Container, error) {
169
return nil, fmt.Errorf("must provide a valid runtime spec to create container: %w", define.ErrInvalidArg)
171
ctr := new(Container)
172
ctr.config = new(ContainerConfig)
173
ctr.state = new(ContainerState)
176
ctr.config.ID = stringid.GenerateRandomID()
177
size, err := units.FromHumanSize(r.config.Containers.ShmSize)
180
return nil, fmt.Errorf("converting containers.conf ShmSize %s to an int: %w", r.config.Containers.ShmSize, err)
182
ctr.config.ShmSize = size
183
ctr.config.NoShm = false
184
ctr.config.NoShmShare = false
186
ctr.config.NoShm = true
187
ctr.config.NoShmShare = true
189
ctr.config.StopSignal = 15
191
ctr.config.StopTimeout = r.config.Engine.StopTimeout
194
ctr.restoreFromCheckpoint = true
195
if err := JSONDeepCopy(config, ctr.config); err != nil {
196
return nil, fmt.Errorf("copying container config for restore: %w", err)
199
if ctr.config.ID == "" {
200
ctr.config.ID = stringid.GenerateRandomID()
203
ctr.config.LogPath = ""
206
if config.StaticMAC == nil {
207
ctr.config.StaticMAC = nil
211
ctr.config.Spec = rSpec
212
ctr.config.CreatedTime = time.Now()
214
ctr.state.BindMounts = make(map[string]string)
216
ctr.config.OCIRuntime = r.defaultOCIRuntime.Name()
220
if r.config.Engine.Namespace != "" {
221
ctr.config.Namespace = r.config.Engine.Namespace
229
func (r *Runtime) newContainer(ctx context.Context, rSpec *spec.Spec, options ...CtrCreateOption) (*Container, error) {
233
ctr, err = r.initContainerVariables(rSpec, nil)
236
return nil, fmt.Errorf("initializing container variables: %w", err)
239
for _, option := range options {
240
if err := option(ctr); err != nil {
241
return nil, fmt.Errorf("running container create option: %w", err)
245
return r.setupContainer(ctx, ctr)
248
func (r *Runtime) setupContainer(ctx context.Context, ctr *Container) (_ *Container, retErr error) {
252
if len(ctr.config.Networks) > 0 {
253
normalizeNetworks := make(map[string]types.PerNetworkOptions, len(ctr.config.Networks))
255
usedIfNames := make([]string, 0, len(ctr.config.Networks))
256
for _, opts := range ctr.config.Networks {
257
if opts.InterfaceName != "" {
259
if slices.Contains(usedIfNames, opts.InterfaceName) {
260
return nil, fmt.Errorf("network interface name %q is already assigned to another network", opts.InterfaceName)
262
usedIfNames = append(usedIfNames, opts.InterfaceName)
266
for nameOrID, opts := range ctr.config.Networks {
267
netName, nicName, err := r.normalizeNetworkName(nameOrID)
274
if opts.InterfaceName == "" {
275
opts.InterfaceName = nicName
279
if opts.InterfaceName == "" {
281
ifName := fmt.Sprintf("eth%d", i)
282
if !slices.Contains(usedIfNames, ifName) {
283
opts.InterfaceName = ifName
284
usedIfNames = append(usedIfNames, ifName)
290
if opts.InterfaceName == "" {
291
return nil, errors.New("failed to find free network interface name")
294
opts.Aliases = append(opts.Aliases, getExtraNetworkAliases(ctr)...)
296
normalizeNetworks[netName] = opts
298
ctr.config.Networks = normalizeNetworks
302
if err := ctr.validate(); err != nil {
305
if ctr.config.IsInfra {
306
ctr.config.StopTimeout = 10
311
defer shutdown.Uninhibit()
314
lock, err := r.lockManager.AllocateLock()
316
return nil, fmt.Errorf("allocating lock for new container: %w", err)
319
ctr.config.LockID = ctr.lock.ID()
320
logrus.Debugf("Allocated lock %d for container %s", ctr.lock.ID(), ctr.ID())
324
if err := ctr.lock.Free(); err != nil {
325
logrus.Errorf("Freeing lock for container after creation failed: %v", err)
331
ctr.state.State = define.ContainerStateConfigured
334
if ctr.config.OCIRuntime == "" {
335
ctr.ociRuntime = r.defaultOCIRuntime
337
ociRuntime, ok := r.ociRuntimes[ctr.config.OCIRuntime]
339
return nil, fmt.Errorf("requested OCI runtime %s is not available: %w", ctr.config.OCIRuntime, define.ErrInvalidArg)
341
ctr.ociRuntime = ociRuntime
345
if ctr.config.NoCgroups {
346
if !ctr.ociRuntime.SupportsNoCgroups() {
347
return nil, fmt.Errorf("requested OCI runtime %s is not compatible with NoCgroups: %w", ctr.ociRuntime.Name(), define.ErrInvalidArg)
352
if ctr.config.Pod != "" {
354
pod, err = r.state.Pod(ctr.config.Pod)
356
return nil, fmt.Errorf("cannot add container %s to pod %s: %w", ctr.ID(), ctr.config.Pod, err)
362
if !ctr.config.NoCgroups {
363
ctr.config.CgroupManager = r.config.Engine.CgroupManager
364
switch r.config.Engine.CgroupManager {
365
case config.CgroupfsCgroupsManager:
366
if ctr.config.CgroupParent == "" {
367
if pod != nil && pod.config.UsePodCgroup && !ctr.IsInfra() {
368
podCgroup, err := pod.CgroupPath()
370
return nil, fmt.Errorf("retrieving pod %s cgroup: %w", pod.ID(), err)
372
expectPodCgroup, err := ctr.expectPodCgroup()
376
if expectPodCgroup && podCgroup == "" {
377
return nil, fmt.Errorf("pod %s cgroup is not set: %w", pod.ID(), define.ErrInternal)
379
canUseCgroup := !rootless.IsRootless() || isRootlessCgroupSet(podCgroup)
381
ctr.config.CgroupParent = podCgroup
383
} else if !rootless.IsRootless() {
384
ctr.config.CgroupParent = CgroupfsDefaultCgroupParent
386
} else if strings.HasSuffix(path.Base(ctr.config.CgroupParent), ".slice") {
387
return nil, fmt.Errorf("systemd slice received as cgroup parent when using cgroupfs: %w", define.ErrInvalidArg)
389
case config.SystemdCgroupsManager:
390
if ctr.config.CgroupParent == "" {
392
case pod != nil && pod.config.UsePodCgroup && !ctr.IsInfra():
393
podCgroup, err := pod.CgroupPath()
395
return nil, fmt.Errorf("retrieving pod %s cgroup: %w", pod.ID(), err)
397
expectPodCgroup, err := ctr.expectPodCgroup()
401
if expectPodCgroup && podCgroup == "" {
402
return nil, fmt.Errorf("pod %s cgroup is not set: %w", pod.ID(), define.ErrInternal)
404
ctr.config.CgroupParent = podCgroup
405
case rootless.IsRootless() && ctr.config.CgroupsMode != cgroupSplit:
406
ctr.config.CgroupParent = SystemdDefaultRootlessCgroupParent
407
case ctr.config.CgroupsMode != cgroupSplit:
408
ctr.config.CgroupParent = SystemdDefaultCgroupParent
410
} else if len(ctr.config.CgroupParent) < 6 || !strings.HasSuffix(path.Base(ctr.config.CgroupParent), ".slice") {
411
return nil, fmt.Errorf("did not receive systemd slice as cgroup parent when using systemd to manage cgroups: %w", define.ErrInvalidArg)
414
return nil, fmt.Errorf("unsupported Cgroup manager: %s - cannot validate cgroup parent: %w", r.config.Engine.CgroupManager, define.ErrInvalidArg)
418
if ctr.config.Timezone == "" {
419
ctr.config.Timezone = r.config.Containers.TZ
422
if ctr.restoreFromCheckpoint {
428
g := generate.NewFromSpec(ctr.config.Spec)
429
g.RemoveMount("/dev/shm")
430
ctr.config.ShmDir = ""
431
g.RemoveMount("/etc/resolv.conf")
432
g.RemoveMount("/etc/hostname")
433
g.RemoveMount("/etc/hosts")
434
g.RemoveMount("/run/.containerenv")
435
g.RemoveMount("/run/secrets")
436
g.RemoveMount("/var/run/.containerenv")
437
g.RemoveMount("/var/run/secrets")
441
cgroupPath, err := ctr.getOCICgroupPath()
445
g.SetLinuxCgroupsPath(cgroupPath)
449
if err := ctr.setupStorage(ctx); err != nil {
454
if err := ctr.teardownStorage(); err != nil {
455
logrus.Errorf("Removing partially-created container root filesystem: %v", err)
460
ctr.config.SecretsPath = filepath.Join(ctr.config.StaticDir, "secrets")
461
err = os.MkdirAll(ctr.config.SecretsPath, 0755)
465
for _, secr := range ctr.config.Secrets {
466
err = ctr.extractSecretToCtrStorage(secr)
472
if ctr.config.ConmonPidFile == "" {
473
ctr.config.ConmonPidFile = filepath.Join(ctr.state.RunDir, "conmon.pid")
476
if ctr.config.PidFile == "" {
477
ctr.config.PidFile = filepath.Join(ctr.state.RunDir, "pidfile")
482
for _, vol := range ctr.config.NamedVolumes {
487
vol.Name = stringid.GenerateRandomID()
491
_, err := r.state.Volume(vol.Name)
495
} else if !errors.Is(err, define.ErrNoSuchVolume) {
496
return nil, fmt.Errorf("retrieving named volume %s for new container: %w", vol.Name, err)
505
logrus.Debugf("Creating new volume %s for container", vol.Name)
508
volOptions := []VolumeCreateOption{
509
WithVolumeName(vol.Name),
510
WithVolumeMountLabel(ctr.MountLabel()),
513
volOptions = append(volOptions, withSetAnon())
519
if len(vol.Options) > 0 {
520
isDriverOpts := false
521
driverOpts := make(map[string]string)
522
for _, opts := range vol.Options {
526
if strings.HasPrefix(opts, "volume-opt") {
528
driverOptKey, driverOptValue, err := util.ParseDriverOpts(opts)
532
driverOpts[driverOptKey] = driverOptValue
536
parsedOptions := []VolumeCreateOption{WithVolumeOptions(driverOpts)}
537
volOptions = append(volOptions, parsedOptions...)
542
volOptions = append(volOptions, WithVolumeUID(ctr.RootUID()), WithVolumeGID(ctr.RootGID()))
544
volOptions = append(volOptions, WithVolumeNoChown())
547
_, err = r.newVolume(ctx, false, volOptions...)
549
return nil, fmt.Errorf("creating named volume %q: %w", vol.Name, err)
553
switch ctr.config.LogDriver {
554
case define.NoLogging, define.PassthroughLogging, define.JournaldLogging:
557
if ctr.config.LogPath == "" {
558
ctr.config.LogPath = filepath.Join(ctr.config.StaticDir, "ctr.log")
562
if useDevShm && !MountExists(ctr.config.Spec.Mounts, "/dev/shm") && ctr.config.ShmDir == "" && !ctr.config.NoShm {
563
ctr.config.ShmDir = filepath.Join(ctr.bundlePath(), "shm")
564
if err := os.MkdirAll(ctr.config.ShmDir, 0700); err != nil {
565
if !os.IsExist(err) {
566
return nil, fmt.Errorf("unable to create shm dir: %w", err)
569
ctr.config.Mounts = append(ctr.config.Mounts, ctr.config.ShmDir)
574
if ctr.config.Pod != "" {
578
defer pod.lock.Unlock()
580
if err := r.state.AddContainerToPod(pod, ctr); err != nil {
583
} else if err := r.state.AddContainer(ctr); err != nil {
587
if ctr.runtime.config.Engine.EventsContainerCreateInspectData {
588
if err := ctr.newContainerEventWithInspectData(events.Create, "", true); err != nil {
592
ctr.newContainerEvent(events.Create)
602
func (r *Runtime) RemoveContainer(ctx context.Context, c *Container, force bool, removeVolume bool, timeout *uint) error {
605
RemoveVolume: removeVolume,
611
_, _, err := r.removeContainer(ctx, c, opts)
622
func (r *Runtime) RemoveContainerAndDependencies(ctx context.Context, c *Container, force bool, removeVolume bool, timeout *uint) (map[string]error, map[string]error, error) {
625
RemoveVolume: removeVolume,
632
return r.removeContainer(ctx, c, opts)
636
type ctrRmOpts struct {
672
func (r *Runtime) removeContainer(ctx context.Context, c *Container, opts ctrRmOpts) (removedCtrs map[string]error, removedPods map[string]error, retErr error) {
673
removedCtrs = make(map[string]error)
674
removedPods = make(map[string]error)
677
if ok, _ := r.state.HasContainer(c.ID()); !ok {
680
removedCtrs[c.ID()] = nil
685
if opts.RemovePod && opts.RemoveDeps {
686
retErr = fmt.Errorf("cannot remove dependencies while also removing a pod: %w", define.ErrInvalidArg)
696
newConf, err := r.state.GetContainerConfig(c.ID())
698
retErr = fmt.Errorf("retrieving container %s configuration from DB to remove: %w", c.ID(), err)
703
logrus.Debugf("Removing container %s", c.ID())
711
if c.config.Pod != "" {
712
pod, err = r.state.Pod(c.config.Pod)
719
if errors.Is(err, define.ErrNoSuchPod) {
725
removedCtrs[c.ID()] = nil
735
if pod.config.LockID == c.config.LockID {
736
retErr = fmt.Errorf("container %s and pod %s share lock ID %d: %w", c.ID(), pod.ID(), c.config.LockID, define.ErrWillDeadlock)
741
defer pod.lock.Unlock()
743
if err := pod.updatePod(); err != nil {
746
if errors.Is(err, define.ErrNoSuchPod) {
747
removedCtrs[c.ID()] = nil
755
infraID := pod.state.InfraContainerID
756
if c.ID() == infraID && !opts.RemoveDeps {
757
retErr = fmt.Errorf("container %s is the infra container of pod %s and cannot be removed without removing the pod", c.ID(), pod.ID())
776
retErr = define.ErrRuntimeStopped
781
if err := c.syncContainer(); err != nil {
786
serviceForPod := false
788
for _, id := range c.state.Service.Pods {
789
depPod, err := c.runtime.LookupPod(id)
791
if errors.Is(err, define.ErrNoSuchPod) {
797
if !opts.RemoveDeps {
798
retErr = fmt.Errorf("container %s is the service container of pod(s) %s and cannot be removed without removing the pod(s)", c.ID(), strings.Join(c.state.Service.Pods, ","))
804
if pod != nil && pod.ID() == depPod.ID() {
808
logrus.Infof("Removing pod %s as container %s is its service container", depPod.ID(), c.ID())
809
podRemovedCtrs, err := r.RemovePod(ctx, depPod, true, opts.Force, opts.Timeout)
810
for ctr, err := range podRemovedCtrs {
811
removedCtrs[ctr] = err
813
if err != nil && !errors.Is(err, define.ErrNoSuchPod) && !errors.Is(err, define.ErrPodRemoved) {
814
removedPods[depPod.ID()] = err
815
retErr = fmt.Errorf("error removing container %s dependency pods: %w", c.ID(), err)
818
removedPods[depPod.ID()] = nil
821
if (serviceForPod || c.config.IsInfra) && !opts.RemovePod {
830
logrus.Infof("Removing pod %s (dependency of container %s)", pod.ID(), c.ID())
831
podRemovedCtrs, err := r.removePod(ctx, pod, true, opts.Force, opts.Timeout)
832
for ctr, err := range podRemovedCtrs {
833
removedCtrs[ctr] = err
835
if err != nil && !errors.Is(err, define.ErrNoSuchPod) && !errors.Is(err, define.ErrPodRemoved) {
836
removedPods[pod.ID()] = err
837
retErr = fmt.Errorf("error removing container %s pod: %w", c.ID(), err)
840
removedPods[pod.ID()] = nil
847
if err := c.checkReadyForRemoval(); err != nil {
853
if c.state.State == define.ContainerStatePaused {
854
isV2, err := cgroups.IsCgroup2UnifiedMode()
861
if err := c.unpause(); err != nil {
866
if err := c.ociRuntime.KillContainer(c, 9, false); err != nil {
871
if err := c.waitForExitFileAndSync(); err != nil {
880
if !opts.IgnoreDeps {
881
deps, err := r.state.ContainerInUse(c)
886
if !opts.RemoveDeps {
888
depsStr := strings.Join(deps, ", ")
889
retErr = fmt.Errorf("container %s has dependent containers which must be removed before it: %s: %w", c.ID(), depsStr, define.ErrCtrExists)
893
for _, depCtr := range deps {
894
dep, err := r.GetContainer(depCtr)
899
logrus.Infof("Removing container %s (dependency of container %s)", dep.ID(), c.ID())
900
recursiveOpts := ctrRmOpts{
902
RemoveVolume: opts.RemoveVolume,
905
Timeout: opts.Timeout,
907
ctrs, pods, err := r.removeContainer(ctx, dep, recursiveOpts)
908
for rmCtr, err := range ctrs {
909
if errors.Is(err, define.ErrNoSuchCtr) || errors.Is(err, define.ErrCtrRemoved) {
910
removedCtrs[rmCtr] = nil
912
removedCtrs[rmCtr] = err
915
for rmPod, err := range pods {
916
removedPods[rmPod] = err
918
if err != nil && !errors.Is(err, define.ErrNoSuchCtr) && !errors.Is(err, define.ErrCtrRemoved) {
926
if c.ensureState(define.ContainerStateRunning, define.ContainerStateStopping) {
927
time := c.StopTimeout()
928
if opts.Timeout != nil {
933
if err := c.stop(time); err != nil && !errors.Is(err, define.ErrConmonDead) {
934
retErr = fmt.Errorf("cannot remove container %s as it could not be stopped: %w", c.ID(), err)
943
if ok, _ := r.state.HasContainer(c.ID()); !ok {
945
if err := c.cleanupRuntime(ctx); err != nil {
946
retErr = fmt.Errorf("cleaning up container %s from OCI runtime: %w", c.ID(), err)
955
reportErrorf := func(msg string, args ...any) {
956
err := fmt.Errorf(msg, args...)
960
logrus.Errorf("%s", err.Error())
967
if err := c.cleanup(ctx); err != nil {
968
reportErrorf("cleaning up container %s: %w", c.ID(), err)
975
if err := c.removeAllExecSessions(); err != nil {
976
reportErrorf("removing exec sessions: %w", err)
981
c.state.State = define.ContainerStateRemoving
983
if err := c.save(); err != nil {
984
if !errors.Is(err, define.ErrCtrRemoved) {
985
reportErrorf("saving container: %w", err)
990
if err := c.teardownStorage(); err != nil {
991
reportErrorf("cleaning up storage: %w", err)
995
if cidFile, ok := c.config.Spec.Annotations[define.InspectAnnotationCIDFile]; ok {
996
if err := os.Remove(cidFile); err != nil && !errors.Is(err, os.ErrNotExist) {
997
reportErrorf("cleaning up CID file: %w", err)
1001
if c.config.Pod != "" {
1004
if err := r.state.RemoveContainerFromPod(pod, c); err != nil {
1005
reportErrorf("removing container %s from database: %w", c.ID(), err)
1008
if err := r.state.RemoveContainer(c); err != nil {
1009
reportErrorf("removing container %s from database: %w", c.ID(), err)
1012
removedCtrs[c.ID()] = nil
1015
if err := c.lock.Free(); err != nil && !errors.Is(err, fs.ErrNotExist) {
1016
reportErrorf("freeing lock for container %s: %w", c.ID(), err)
1022
c.newContainerEvent(events.Remove)
1024
if !opts.RemoveVolume {
1028
for _, v := range c.config.NamedVolumes {
1029
if volume, err := runtime.state.Volume(v.Name); err == nil {
1030
if !volume.Anonymous() {
1033
if err := runtime.removeVolume(ctx, volume, false, opts.Timeout, false); err != nil && !errors.Is(err, define.ErrNoSuchVolume) {
1034
if errors.Is(err, define.ErrVolumeBeingUsed) {
1036
volumesFrom, _ := c.volumesFrom()
1037
if len(volumesFrom) > 0 {
1038
logrus.Debugf("Cleaning up volume not possible since volume is in use (%s)", v.Name)
1042
logrus.Errorf("Cleaning up volume (%s): %v", v.Name, err)
1058
func (r *Runtime) EvictContainer(ctx context.Context, idOrName string, removeVolume bool) (string, error) {
1059
return r.evictContainer(ctx, idOrName, removeVolume)
1069
func (r *Runtime) evictContainer(ctx context.Context, idOrName string, removeVolume bool) (string, error) {
1074
return "", define.ErrRuntimeStopped
1077
id, err := r.state.LookupContainerID(idOrName)
1083
tmpCtr, err := r.state.Container(id)
1085
logrus.Infof("Container %s successfully retrieved from state, attempting normal removal", id)
1089
RemoveVolume: removeVolume,
1092
_, _, err = r.removeContainer(ctx, tmpCtr, opts)
1102
logrus.Errorf("Container %s not removed with no error", id)
1104
logrus.Warnf("Failed to removal container %s normally, proceeding with evict: %v", id, err)
1109
exists, err := r.state.HasContainer(id)
1119
c.config, err = r.state.GetContainerConfig(id)
1121
return id, fmt.Errorf("failed to retrieve config for ctr ID %q: %w", id, err)
1123
c.state = new(ContainerState)
1130
if c.config.Pod != "" {
1131
pod, err = r.state.Pod(c.config.Pod)
1133
return id, fmt.Errorf("container %s is in pod %s, but pod cannot be retrieved: %w", c.ID(), pod.ID(), err)
1138
defer pod.lock.Unlock()
1139
if err := pod.updatePod(); err != nil {
1143
infraID, err := pod.infraContainerID()
1147
if c.ID() == infraID {
1148
return id, fmt.Errorf("container %s is the infra container of pod %s and cannot be removed without removing the pod", c.ID(), pod.ID())
1153
report, err := c.canStopServiceContainer()
1157
if !report.canBeStopped {
1158
return id, fmt.Errorf("container %s is the service container of pod(s) %s and cannot be removed without removing the pod(s)", c.ID(), strings.Join(c.state.Service.Pods, ","))
1162
var cleanupErr error
1164
if c.config.Pod != "" {
1167
if err := r.state.RemoveContainerFromPod(pod, c); err != nil {
1171
if err := r.state.RemoveContainer(c); err != nil {
1177
for _, mount := range c.config.Mounts {
1182
if err := r.RemoveStorageContainer(id, true); err != nil {
1183
if cleanupErr == nil {
1189
return id, cleanupErr
1192
for _, v := range c.config.NamedVolumes {
1193
if volume, err := r.state.Volume(v.Name); err == nil {
1194
if !volume.Anonymous() {
1197
if err := r.removeVolume(ctx, volume, false, timeout, false); err != nil && err != define.ErrNoSuchVolume && err != define.ErrVolumeBeingUsed {
1198
logrus.Errorf("Cleaning up volume (%s): %v", v.Name, err)
1203
return id, cleanupErr
1207
func (r *Runtime) GetContainer(id string) (*Container, error) {
1209
return nil, define.ErrRuntimeStopped
1212
return r.state.Container(id)
1216
func (r *Runtime) HasContainer(id string) (bool, error) {
1218
return false, define.ErrRuntimeStopped
1221
return r.state.HasContainer(id)
1226
func (r *Runtime) LookupContainer(idOrName string) (*Container, error) {
1228
return nil, define.ErrRuntimeStopped
1230
return r.state.LookupContainer(idOrName)
1235
func (r *Runtime) LookupContainerID(idOrName string) (string, error) {
1237
return "", define.ErrRuntimeStopped
1239
return r.state.LookupContainerID(idOrName)
1247
func (r *Runtime) GetContainers(loadState bool, filters ...ContainerFilter) ([]*Container, error) {
1249
return nil, define.ErrRuntimeStopped
1252
ctrs, err := r.state.AllContainers(loadState)
1257
ctrsFiltered := make([]*Container, 0, len(ctrs))
1259
for _, ctr := range ctrs {
1261
for _, filter := range filters {
1262
include = include && filter(ctr)
1266
ctrsFiltered = append(ctrsFiltered, ctr)
1270
return ctrsFiltered, nil
1274
func (r *Runtime) GetAllContainers() ([]*Container, error) {
1275
return r.state.AllContainers(false)
1279
func (r *Runtime) GetRunningContainers() ([]*Container, error) {
1280
running := func(c *Container) bool {
1281
state, _ := c.State()
1282
return state == define.ContainerStateRunning
1284
return r.GetContainers(false, running)
1289
func (r *Runtime) GetContainersByList(containers []string) ([]*Container, error) {
1290
ctrs := make([]*Container, 0, len(containers))
1291
for _, inputContainer := range containers {
1292
ctr, err := r.LookupContainer(inputContainer)
1294
return ctrs, fmt.Errorf("unable to look up container %s: %w", inputContainer, err)
1296
ctrs = append(ctrs, ctr)
1302
func (r *Runtime) GetLatestContainer() (*Container, error) {
1303
lastCreatedIndex := -1
1304
var lastCreatedTime time.Time
1305
ctrs, err := r.GetAllContainers()
1307
return nil, fmt.Errorf("unable to find latest container: %w", err)
1310
return nil, define.ErrNoSuchCtr
1312
for containerIndex, ctr := range ctrs {
1313
createdTime := ctr.config.CreatedTime
1314
if createdTime.After(lastCreatedTime) {
1315
lastCreatedTime = createdTime
1316
lastCreatedIndex = containerIndex
1319
return ctrs[lastCreatedIndex], nil
1324
func (r *Runtime) GetExecSessionContainer(id string) (*Container, error) {
1326
return nil, define.ErrRuntimeStopped
1329
ctrID, err := r.state.GetExecSession(id)
1334
return r.state.Container(ctrID)
1339
func (r *Runtime) PruneContainers(filterFuncs []ContainerFilter) ([]*reports.PruneReport, error) {
1340
preports := make([]*reports.PruneReport, 0)
1342
containerStateFilter := func(c *Container) bool {
1343
if c.PodID() != "" {
1346
state, err := c.State()
1351
if state == define.ContainerStateStopped || state == define.ContainerStateExited ||
1352
state == define.ContainerStateCreated || state == define.ContainerStateConfigured {
1357
filterFuncs = append(filterFuncs, containerStateFilter)
1358
delContainers, err := r.GetContainers(false, filterFuncs...)
1362
for _, c := range delContainers {
1363
report := new(reports.PruneReport)
1367
size, err := c.RWSize()
1370
preports = append(preports, report)
1374
err = r.RemoveContainer(context.Background(), c, false, false, time)
1378
report.Size = (uint64)(size)
1380
preports = append(preports, report)
1382
return preports, nil
1386
func (r *Runtime) MountStorageContainer(id string) (string, error) {
1387
if _, err := r.GetContainer(id); err == nil {
1388
return "", fmt.Errorf("ctr %s is a libpod container: %w", id, define.ErrCtrExists)
1390
container, err := r.store.Container(id)
1394
mountPoint, err := r.store.Mount(container.ID, "")
1396
return "", fmt.Errorf("mounting storage for container %s: %w", id, err)
1398
return mountPoint, nil
1402
func (r *Runtime) UnmountStorageContainer(id string, force bool) (bool, error) {
1403
if _, err := r.GetContainer(id); err == nil {
1404
return false, fmt.Errorf("ctr %s is a libpod container: %w", id, define.ErrCtrExists)
1406
container, err := r.store.Container(id)
1410
return r.store.Unmount(container.ID, force)
1415
func (r *Runtime) IsStorageContainerMounted(id string) (bool, string, error) {
1417
if _, err := r.GetContainer(id); err == nil {
1418
return false, "", fmt.Errorf("ctr %s is a libpod container: %w", id, define.ErrCtrExists)
1421
mountCnt, err := r.storageService.MountedContainerImage(id)
1423
return false, "", err
1425
mounted := mountCnt > 0
1427
path, err = r.storageService.GetMountpoint(id)
1429
return false, "", err
1432
return mounted, path, nil
1437
func (r *Runtime) StorageContainers() ([]storage.Container, error) {
1439
return nil, define.ErrStoreNotInitialized
1442
storeContainers, err := r.store.Containers()
1444
return nil, fmt.Errorf("reading list of all storage containers: %w", err)
1446
retCtrs := []storage.Container{}
1447
for _, container := range storeContainers {
1448
exists, err := r.state.HasContainer(container.ID)
1449
if err != nil && err != define.ErrNoSuchCtr {
1450
return nil, fmt.Errorf("failed to check if %s container exists in database: %w", container.ID, err)
1455
retCtrs = append(retCtrs, container)
1461
func (r *Runtime) IsBuildahContainer(id string) (bool, error) {
1462
return buildah.IsContainer(id, r.store)