github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/libpod/container_config.go (about) 1 package libpod 2 3 import ( 4 "net" 5 "time" 6 7 "github.com/containers/common/libnetwork/types" 8 "github.com/containers/common/pkg/secrets" 9 "github.com/containers/image/v5/manifest" 10 "github.com/hanks177/podman/v4/pkg/namespaces" 11 "github.com/hanks177/podman/v4/pkg/specgen" 12 "github.com/containers/storage" 13 spec "github.com/opencontainers/runtime-spec/specs-go" 14 ) 15 16 // ContainerConfig contains all information that was used to create the 17 // container. It may not be changed once created. 18 // It is stored, read-only, on disk in Libpod's State. 19 // Any changes will not be written back to the database, and will cause 20 // inconsistencies with other Libpod instances. 21 type ContainerConfig struct { 22 // Spec is OCI runtime spec used to create the container. This is passed 23 // in when the container is created, but it is not the final spec used 24 // to run the container - it will be modified by Libpod to add things we 25 // manage (e.g. bind mounts for /etc/resolv.conf, named volumes, a 26 // network namespace prepared by CNI or slirp4netns) in the 27 // generateSpec() function. 28 Spec *spec.Spec `json:"spec"` 29 30 // ID is a hex-encoded 256-bit pseudorandom integer used as a unique 31 // identifier for the container. IDs are globally unique in Libpod - 32 // once an ID is in use, no other container or pod will be created with 33 // the same one until the holder of the ID has been removed. 34 // ID is generated by Libpod, and cannot be chosen or influenced by the 35 // user (except when restoring a checkpointed container). 36 // ID is guaranteed to be 64 characters long. 37 ID string `json:"id"` 38 39 // Name is a human-readable name for the container. All containers must 40 // have a non-empty name. Name may be provided when the container is 41 // created; if no name is chosen, a name will be auto-generated. 42 Name string `json:"name"` 43 44 // Pod is the full ID of the pod the container belongs to. If the 45 // container does not belong to a pod, this will be empty. 46 // If this is not empty, a pod with this ID is guaranteed to exist in 47 // the state for the duration of this container's existence. 48 Pod string `json:"pod,omitempty"` 49 50 // Namespace is the libpod Namespace the container is in. 51 // Namespaces are used to divide containers in the state. 52 Namespace string `json:"namespace,omitempty"` 53 54 // LockID is the ID of this container's lock. Each container, pod, and 55 // volume is assigned a unique Lock (from one of several backends) by 56 // the libpod Runtime. This lock will belong only to this container for 57 // the duration of the container's lifetime. 58 LockID uint32 `json:"lockID"` 59 60 // CreateCommand is the full command plus arguments that were used to 61 // create the container. It is shown in the output of Inspect, and may 62 // be used to recreate an identical container for automatic updates or 63 // portable systemd unit files. 64 CreateCommand []string `json:"CreateCommand,omitempty"` 65 66 // RawImageName is the raw and unprocessed name of the image when creating 67 // the container (as specified by the user). May or may not be set. One 68 // use case to store this data are auto-updates where we need the _exact_ 69 // name and not some normalized instance of it. 70 RawImageName string `json:"RawImageName,omitempty"` 71 72 // IDMappings are UID/GID mappings used by the container's user 73 // namespace. They are used by the OCI runtime when creating the 74 // container, and by c/storage to ensure that the container's files have 75 // the appropriate owner. 76 IDMappings storage.IDMappingOptions `json:"idMappingsOptions,omitempty"` 77 78 // Dependencies are the IDs of dependency containers. 79 // These containers must be started before this container is started. 80 Dependencies []string 81 82 // rewrite is an internal bool to indicate that the config was modified after 83 // a read from the db, e.g. to migrate config fields after an upgrade. 84 // This field should never be written to the db, the json tag ensures this. 85 rewrite bool `json:"-"` 86 87 // embedded sub-configs 88 ContainerRootFSConfig 89 ContainerSecurityConfig 90 ContainerNameSpaceConfig 91 ContainerNetworkConfig 92 ContainerImageConfig 93 ContainerMiscConfig 94 } 95 96 // ContainerRootFSConfig is an embedded sub-config providing config info 97 // about the container's root fs. 98 type ContainerRootFSConfig struct { 99 // RootfsImageID is the ID of the image used to create the container. 100 // If the container was created from a Rootfs, this will be empty. 101 // If non-empty, Podman will create a root filesystem for the container 102 // based on an image with this ID. 103 // This conflicts with Rootfs. 104 RootfsImageID string `json:"rootfsImageID,omitempty"` 105 // RootfsImageName is the (normalized) name of the image used to create 106 // the container. If the container was created from a Rootfs, this will 107 // be empty. 108 RootfsImageName string `json:"rootfsImageName,omitempty"` 109 // Rootfs is a directory to use as the container's root filesystem. 110 // If RootfsImageID is set, this will be empty. 111 // If this is set, Podman will not create a root filesystem for the 112 // container based on an image, and will instead use the given directory 113 // as the container's root. 114 // Conflicts with RootfsImageID. 115 Rootfs string `json:"rootfs,omitempty"` 116 // RootfsOverlay tells if rootfs has to be mounted as an overlay 117 RootfsOverlay bool `json:"rootfs_overlay,omitempty"` 118 // ShmDir is the path to be mounted on /dev/shm in container. 119 // If not set manually at creation time, Libpod will create a tmpfs 120 // with the size specified in ShmSize and populate this with the path of 121 // said tmpfs. 122 ShmDir string `json:"ShmDir,omitempty"` 123 // NoShmShare indicates whether /dev/shm can be shared with other containers 124 NoShmShare bool `json:"NOShmShare,omitempty"` 125 // NoShm indicates whether a tmpfs should be created and mounted on /dev/shm 126 NoShm bool `json:"NoShm,omitempty"` 127 // ShmSize is the size of the container's SHM. Only used if ShmDir was 128 // not set manually at time of creation. 129 ShmSize int64 `json:"shmSize"` 130 // Static directory for container content that will persist across 131 // reboot. 132 // StaticDir is a persistent directory for Libpod files that will 133 // survive system reboot. It is not part of the container's rootfs and 134 // is not mounted into the container. It will be removed when the 135 // container is removed. 136 // Usually used to store container log files, files that will be bind 137 // mounted into the container (e.g. the resolv.conf we made for the 138 // container), and other per-container content. 139 StaticDir string `json:"staticDir"` 140 // Mounts contains all additional mounts into the container rootfs. 141 // It is presently only used for the container's SHM directory. 142 // These must be unmounted before the container's rootfs is unmounted. 143 Mounts []string `json:"mounts,omitempty"` 144 // NamedVolumes lists the Libpod named volumes to mount into the 145 // container. Each named volume is guaranteed to exist so long as this 146 // container exists. 147 NamedVolumes []*ContainerNamedVolume `json:"namedVolumes,omitempty"` 148 // OverlayVolumes lists the overlay volumes to mount into the container. 149 OverlayVolumes []*ContainerOverlayVolume `json:"overlayVolumes,omitempty"` 150 // ImageVolumes lists the image volumes to mount into the container. 151 // Please note that this is named ctrImageVolumes in JSON to 152 // distinguish between these and the old `imageVolumes` field in Podman 153 // pre-1.8, which was used in very old Podman versions to determine how 154 // image volumes were handled in Libpod (support for these eventually 155 // moved out of Libpod into pkg/specgen). 156 // Please DO NOT re-use the `imageVolumes` name in container JSON again. 157 ImageVolumes []*ContainerImageVolume `json:"ctrImageVolumes,omitempty"` 158 // CreateWorkingDir indicates that Libpod should create the container's 159 // working directory if it does not exist. Some OCI runtimes do this by 160 // default, but others do not. 161 CreateWorkingDir bool `json:"createWorkingDir,omitempty"` 162 // Secrets lists secrets to mount into the container 163 Secrets []*ContainerSecret `json:"secrets,omitempty"` 164 // SecretPath is the secrets location in storage 165 SecretsPath string `json:"secretsPath"` 166 // StorageOpts to be used when creating rootfs 167 StorageOpts map[string]string `json:"storageOpts"` 168 // Volatile specifies whether the container storage can be optimized 169 // at the cost of not syncing all the dirty files in memory. 170 Volatile bool `json:"volatile,omitempty"` 171 // Passwd allows to user to override podman's passwd/group file setup 172 Passwd *bool `json:"passwd,omitempty"` 173 // ChrootDirs is an additional set of directories that need to be 174 // treated as root directories. Standard bind mounts will be mounted 175 // into paths relative to these directories. 176 ChrootDirs []string `json:"chroot_directories,omitempty"` 177 } 178 179 // ContainerSecurityConfig is an embedded sub-config providing security configuration 180 // to the container. 181 type ContainerSecurityConfig struct { 182 // Privileged is whether the container is privileged. Privileged 183 // containers have lessened security and increased access to the system. 184 // Note that this does NOT directly correspond to Podman's --privileged 185 // flag - most of the work of that flag is done in creating the OCI spec 186 // given to Libpod. This only enables a small subset of the overall 187 // operation, mostly around mounting the container image with reduced 188 // security. 189 Privileged bool `json:"privileged"` 190 // ProcessLabel is the SELinux process label for the container. 191 ProcessLabel string `json:"ProcessLabel,omitempty"` 192 // MountLabel is the SELinux mount label for the container's root 193 // filesystem. Only used if the container was created from an image. 194 // If not explicitly set, an unused random MLS label will be assigned by 195 // containers/storage (but only if SELinux is enabled). 196 MountLabel string `json:"MountLabel,omitempty"` 197 // LabelOpts are options passed in by the user to setup SELinux labels. 198 // These are used by the containers/storage library. 199 LabelOpts []string `json:"labelopts,omitempty"` 200 // User and group to use in the container. Can be specified as only user 201 // (in which case we will attempt to look up the user in the container 202 // to determine the appropriate group) or user and group separated by a 203 // colon. 204 // Can be specified by name or UID/GID. 205 // If unset, this will default to UID and GID 0 (root). 206 User string `json:"user,omitempty"` 207 // Groups are additional groups to add the container's user to. These 208 // are resolved within the container using the container's /etc/passwd. 209 Groups []string `json:"groups,omitempty"` 210 // HostUsers are a list of host user accounts to add to /etc/passwd 211 HostUsers []string `json:"HostUsers,omitempty"` 212 // AddCurrentUserPasswdEntry indicates that Libpod should ensure that 213 // the container's /etc/passwd contains an entry for the user running 214 // Libpod - mostly used in rootless containers where the user running 215 // Libpod wants to retain their UID inside the container. 216 AddCurrentUserPasswdEntry bool `json:"addCurrentUserPasswdEntry,omitempty"` 217 } 218 219 // ContainerNameSpaceConfig is an embedded sub-config providing 220 // namespace configuration to the container. 221 type ContainerNameSpaceConfig struct { 222 // IDs of container to share namespaces with 223 // NetNsCtr conflicts with the CreateNetNS bool 224 // These containers are considered dependencies of the given container 225 // They must be started before the given container is started 226 IPCNsCtr string `json:"ipcNsCtr,omitempty"` 227 MountNsCtr string `json:"mountNsCtr,omitempty"` 228 NetNsCtr string `json:"netNsCtr,omitempty"` 229 PIDNsCtr string `json:"pidNsCtr,omitempty"` 230 UserNsCtr string `json:"userNsCtr,omitempty"` 231 UTSNsCtr string `json:"utsNsCtr,omitempty"` 232 CgroupNsCtr string `json:"cgroupNsCtr,omitempty"` 233 } 234 235 // ContainerNetworkConfig is an embedded sub-config providing network configuration 236 // to the container. 237 type ContainerNetworkConfig struct { 238 // CreateNetNS indicates that libpod should create and configure a new 239 // network namespace for the container. 240 // This cannot be set if NetNsCtr is also set. 241 CreateNetNS bool `json:"createNetNS"` 242 // StaticIP is a static IP to request for the container. 243 // This cannot be set unless CreateNetNS is set. 244 // If not set, the container will be dynamically assigned an IP by CNI. 245 // Deprecated: Do no use this anymore, this is only for DB backwards compat. 246 StaticIP net.IP `json:"staticIP,omitempty"` 247 // StaticMAC is a static MAC to request for the container. 248 // This cannot be set unless CreateNetNS is set. 249 // If not set, the container will be dynamically assigned a MAC by CNI. 250 // Deprecated: Do no use this anymore, this is only for DB backwards compat. 251 StaticMAC types.HardwareAddr `json:"staticMAC,omitempty"` 252 // PortMappings are the ports forwarded to the container's network 253 // namespace 254 // These are not used unless CreateNetNS is true 255 PortMappings []types.PortMapping `json:"newPortMappings,omitempty"` 256 // OldPortMappings are the ports forwarded to the container's network 257 // namespace. As of podman 4.0 this field is deprecated, use PortMappings 258 // instead. The db will convert the old ports to the new structure for you. 259 // These are not used unless CreateNetNS is true 260 OldPortMappings []types.OCICNIPortMapping `json:"portMappings,omitempty"` 261 // ExposedPorts are the ports which are exposed but not forwarded 262 // into the container. 263 // The map key is the port and the string slice contains the protocols, 264 // e.g. tcp and udp 265 // These are only set when exposed ports are given but not published. 266 ExposedPorts map[uint16][]string `json:"exposedPorts,omitempty"` 267 // UseImageResolvConf indicates that resolv.conf should not be 268 // bind-mounted inside the container. 269 // Conflicts with DNSServer, DNSSearch, DNSOption. 270 UseImageResolvConf bool 271 // DNS servers to use in container resolv.conf 272 // Will override servers in host resolv if set 273 DNSServer []net.IP `json:"dnsServer,omitempty"` 274 // DNS Search domains to use in container resolv.conf 275 // Will override search domains in host resolv if set 276 DNSSearch []string `json:"dnsSearch,omitempty"` 277 // DNS options to be set in container resolv.conf 278 // With override options in host resolv if set 279 DNSOption []string `json:"dnsOption,omitempty"` 280 // UseImageHosts indicates that /etc/hosts should not be 281 // bind-mounted inside the container. 282 // Conflicts with HostAdd. 283 UseImageHosts bool 284 // Hosts to add in container 285 // Will be appended to host's host file 286 HostAdd []string `json:"hostsAdd,omitempty"` 287 // Network names with the network specific options. 288 // Please note that these can be altered at runtime. The actual list is 289 // stored in the DB and should be retrieved from there via c.networks() 290 // this value is only used for container create. 291 // Added in podman 4.0, previously NetworksDeprecated was used. Make 292 // sure to not change the json tags. 293 Networks map[string]types.PerNetworkOptions `json:"newNetworks,omitempty"` 294 // Network names to add container to. Empty to use default network. 295 // Please note that these can be altered at runtime. The actual list is 296 // stored in the DB and should be retrieved from there; this is only the 297 // set of networks the container was *created* with. 298 // Deprecated: Do no use this anymore, this is only for DB backwards compat. 299 // Also note that we need to keep the old json tag to decode from DB correctly 300 NetworksDeprecated []string `json:"networks,omitempty"` 301 // Network mode specified for the default network. 302 NetMode namespaces.NetworkMode `json:"networkMode,omitempty"` 303 // NetworkOptions are additional options for each network 304 NetworkOptions map[string][]string `json:"network_options,omitempty"` 305 } 306 307 // ContainerImageConfig is an embedded sub-config providing image configuration 308 // to the container. 309 type ContainerImageConfig struct { 310 // UserVolumes contains user-added volume mounts in the container. 311 // These will not be added to the container's spec, as it is assumed 312 // they are already present in the spec given to Libpod. Instead, it is 313 // used when committing containers to generate the VOLUMES field of the 314 // image that is created, and for triggering some OCI hooks which do not 315 // fire unless user-added volume mounts are present. 316 UserVolumes []string `json:"userVolumes,omitempty"` 317 // Entrypoint is the container's entrypoint. 318 // It is not used in spec generation, but will be used when the 319 // container is committed to populate the entrypoint of the new image. 320 Entrypoint []string `json:"entrypoint,omitempty"` 321 // Command is the container's command. 322 // It is not used in spec generation, but will be used when the 323 // container is committed to populate the command of the new image. 324 Command []string `json:"command,omitempty"` 325 } 326 327 // ContainerMiscConfig is an embedded sub-config providing misc configuration 328 // to the container. 329 type ContainerMiscConfig struct { 330 // Whether to keep container STDIN open 331 Stdin bool `json:"stdin,omitempty"` 332 // Labels is a set of key-value pairs providing additional information 333 // about a container 334 Labels map[string]string `json:"labels,omitempty"` 335 // StopSignal is the signal that will be used to stop the container 336 StopSignal uint `json:"stopSignal,omitempty"` 337 // StopTimeout is the signal that will be used to stop the container 338 StopTimeout uint `json:"stopTimeout,omitempty"` 339 // Timeout is maximum time a container will run before getting the kill signal 340 Timeout uint `json:"timeout,omitempty"` 341 // Time container was created 342 CreatedTime time.Time `json:"createdTime"` 343 // CgroupManager is the cgroup manager used to create this container. 344 // If empty, the runtime default will be used. 345 CgroupManager string `json:"cgroupManager,omitempty"` 346 // NoCgroups indicates that the container will not create Cgroups. It is 347 // incompatible with CgroupParent. Deprecated in favor of CgroupsMode. 348 NoCgroups bool `json:"noCgroups,omitempty"` 349 // CgroupsMode indicates how the container will create cgroups 350 // (disabled, no-conmon, enabled). It supersedes NoCgroups. 351 CgroupsMode string `json:"cgroupsMode,omitempty"` 352 // Cgroup parent of the container. 353 CgroupParent string `json:"cgroupParent"` 354 // LogPath log location 355 LogPath string `json:"logPath"` 356 // LogTag is the tag used for logging 357 LogTag string `json:"logTag"` 358 // LogSize is the tag used for logging 359 LogSize int64 `json:"logSize"` 360 // LogDriver driver for logs 361 LogDriver string `json:"logDriver"` 362 // File containing the conmon PID 363 ConmonPidFile string `json:"conmonPidFile,omitempty"` 364 // RestartPolicy indicates what action the container will take upon 365 // exiting naturally. 366 // Allowed options are "no" (take no action), "on-failure" (restart on 367 // non-zero exit code, up an a maximum of RestartRetries times), 368 // and "always" (always restart the container on any exit code). 369 // The empty string is treated as the default ("no") 370 RestartPolicy string `json:"restart_policy,omitempty"` 371 // RestartRetries indicates the number of attempts that will be made to 372 // restart the container. Used only if RestartPolicy is set to 373 // "on-failure". 374 RestartRetries uint `json:"restart_retries,omitempty"` 375 // PostConfigureNetNS needed when a user namespace is created by an OCI runtime 376 // if the network namespace is created before the user namespace it will be 377 // owned by the wrong user namespace. 378 PostConfigureNetNS bool `json:"postConfigureNetNS"` 379 // OCIRuntime used to create the container 380 OCIRuntime string `json:"runtime,omitempty"` 381 // IsInfra is a bool indicating whether this container is an infra container used for 382 // sharing kernel namespaces in a pod 383 IsInfra bool `json:"pause"` 384 // IsService is a bool indicating whether this container is a service container used for 385 // tracking the life cycle of K8s service. 386 IsService bool `json:"isService"` 387 // SdNotifyMode tells libpod what to do with a NOTIFY_SOCKET if passed 388 SdNotifyMode string `json:"sdnotifyMode,omitempty"` 389 // Systemd tells libpod to setup the container in systemd mode, a value of nil denotes false 390 Systemd *bool `json:"systemd,omitempty"` 391 // HealthCheckConfig has the health check command and related timings 392 HealthCheckConfig *manifest.Schema2HealthConfig `json:"healthcheck"` 393 // PreserveFDs is a number of additional file descriptors (in addition 394 // to 0, 1, 2) that will be passed to the executed process. The total FDs 395 // passed will be 3 + PreserveFDs. 396 PreserveFDs uint `json:"preserveFds,omitempty"` 397 // Timezone is the timezone inside the container. 398 // Local means it has the same timezone as the host machine 399 Timezone string `json:"timezone,omitempty"` 400 // Umask is the umask inside the container. 401 Umask string `json:"umask,omitempty"` 402 // PidFile is the file that saves the pid of the container process 403 PidFile string `json:"pid_file,omitempty"` 404 // CDIDevices contains devices that use the CDI 405 CDIDevices []string `json:"cdiDevices,omitempty"` 406 // DeviceHostSrc contains the original source on the host 407 DeviceHostSrc []spec.LinuxDevice `json:"device_host_src,omitempty"` 408 // EnvSecrets are secrets that are set as environment variables 409 EnvSecrets map[string]*secrets.Secret `json:"secret_env,omitempty"` 410 // InitContainerType specifies if the container is an initcontainer 411 // and if so, what type: always or once are possible non-nil entries 412 InitContainerType string `json:"init_container_type,omitempty"` 413 // PasswdEntry specifies arbitrary data to append to a file. 414 PasswdEntry string `json:"passwd_entry,omitempty"` 415 // MountAllDevices is an option to indicate whether a privileged container 416 // will mount all the host's devices 417 MountAllDevices bool `json:"mountAllDevices"` 418 } 419 420 // InfraInherit contains the compatible options inheritable from the infra container 421 type InfraInherit struct { 422 ApparmorProfile string `json:"apparmor_profile,omitempty"` 423 CapAdd []string `json:"cap_add,omitempty"` 424 CapDrop []string `json:"cap_drop,omitempty"` 425 HostDeviceList []spec.LinuxDevice `json:"host_device_list,omitempty"` 426 ImageVolumes []*specgen.ImageVolume `json:"image_volumes,omitempty"` 427 InfraResources *spec.LinuxResources `json:"resource_limits,omitempty"` 428 Mounts []spec.Mount `json:"mounts,omitempty"` 429 NoNewPrivileges bool `json:"no_new_privileges,omitempty"` 430 OverlayVolumes []*specgen.OverlayVolume `json:"overlay_volumes,omitempty"` 431 SeccompPolicy string `json:"seccomp_policy,omitempty"` 432 SeccompProfilePath string `json:"seccomp_profile_path,omitempty"` 433 SelinuxOpts []string `json:"selinux_opts,omitempty"` 434 Volumes []*specgen.NamedVolume `json:"volumes,omitempty"` 435 }