github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/container/container_windows.go (about) 1 package container // import "github.com/Prakhar-Agarwal-byte/moby/container" 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 8 "github.com/Prakhar-Agarwal-byte/moby/api/types" 9 containertypes "github.com/Prakhar-Agarwal-byte/moby/api/types/container" 10 "github.com/Prakhar-Agarwal-byte/moby/api/types/events" 11 swarmtypes "github.com/Prakhar-Agarwal-byte/moby/api/types/swarm" 12 "github.com/Prakhar-Agarwal-byte/moby/pkg/system" 13 ) 14 15 const ( 16 containerConfigMountPath = `C:\` 17 containerSecretMountPath = `C:\ProgramData\Docker\secrets` 18 containerInternalSecretMountPath = `C:\ProgramData\Docker\internal\secrets` 19 containerInternalConfigsDirPath = `C:\ProgramData\Docker\internal\configs` 20 21 // defaultStopSignal is the default syscall signal used to stop a container. 22 defaultStopSignal = "SIGTERM" 23 24 // defaultStopTimeout is the timeout (in seconds) for the shutdown call on a container 25 defaultStopTimeout = 30 26 ) 27 28 // UnmountIpcMount unmounts Ipc related mounts. 29 // This is a NOOP on windows. 30 func (container *Container) UnmountIpcMount() error { 31 return nil 32 } 33 34 // IpcMounts returns the list of Ipc related mounts. 35 func (container *Container) IpcMounts() []Mount { 36 return nil 37 } 38 39 // CreateSecretSymlinks creates symlinks to files in the secret mount. 40 func (container *Container) CreateSecretSymlinks() error { 41 for _, r := range container.SecretReferences { 42 if r.File == nil { 43 continue 44 } 45 resolvedPath, _, err := container.ResolvePath(getSecretTargetPath(r)) 46 if err != nil { 47 return err 48 } 49 if err := system.MkdirAll(filepath.Dir(resolvedPath), 0); err != nil { 50 return err 51 } 52 if err := os.Symlink(filepath.Join(containerInternalSecretMountPath, r.SecretID), resolvedPath); err != nil { 53 return err 54 } 55 } 56 57 return nil 58 } 59 60 // SecretMounts returns the mount for the secret path. 61 // All secrets are stored in a single mount on Windows. Target symlinks are 62 // created for each secret, pointing to the files in this mount. 63 func (container *Container) SecretMounts() ([]Mount, error) { 64 var mounts []Mount 65 if len(container.SecretReferences) > 0 { 66 src, err := container.SecretMountPath() 67 if err != nil { 68 return nil, err 69 } 70 mounts = append(mounts, Mount{ 71 Source: src, 72 Destination: containerInternalSecretMountPath, 73 Writable: false, 74 }) 75 } 76 77 return mounts, nil 78 } 79 80 // UnmountSecrets unmounts the fs for secrets 81 func (container *Container) UnmountSecrets() error { 82 p, err := container.SecretMountPath() 83 if err != nil { 84 return err 85 } 86 return os.RemoveAll(p) 87 } 88 89 // CreateConfigSymlinks creates symlinks to files in the config mount. 90 func (container *Container) CreateConfigSymlinks() error { 91 for _, configRef := range container.ConfigReferences { 92 if configRef.File == nil { 93 continue 94 } 95 resolvedPath, _, err := container.ResolvePath(getConfigTargetPath(configRef)) 96 if err != nil { 97 return err 98 } 99 if err := system.MkdirAll(filepath.Dir(resolvedPath), 0); err != nil { 100 return err 101 } 102 if err := os.Symlink(filepath.Join(containerInternalConfigsDirPath, configRef.ConfigID), resolvedPath); err != nil { 103 return err 104 } 105 } 106 107 return nil 108 } 109 110 // ConfigMounts returns the mount for configs. 111 // TODO: Right now Windows doesn't really have a "secure" storage for secrets, 112 // however some configs may contain secrets. Once secure storage is worked out, 113 // configs and secret handling should be merged. 114 func (container *Container) ConfigMounts() []Mount { 115 var mounts []Mount 116 if len(container.ConfigReferences) > 0 { 117 mounts = append(mounts, Mount{ 118 Source: container.ConfigsDirPath(), 119 Destination: containerInternalConfigsDirPath, 120 Writable: false, 121 }) 122 } 123 124 return mounts 125 } 126 127 // DetachAndUnmount unmounts all volumes. 128 // On Windows it only delegates to `UnmountVolumes` since there is nothing to 129 // force unmount. 130 func (container *Container) DetachAndUnmount(volumeEventLog func(name string, action events.Action, attributes map[string]string)) error { 131 return container.UnmountVolumes(volumeEventLog) 132 } 133 134 // TmpfsMounts returns the list of tmpfs mounts 135 func (container *Container) TmpfsMounts() ([]Mount, error) { 136 var mounts []Mount 137 return mounts, nil 138 } 139 140 // UpdateContainer updates configuration of a container. Callers must hold a Lock on the Container. 141 func (container *Container) UpdateContainer(hostConfig *containertypes.HostConfig) error { 142 resources := hostConfig.Resources 143 if resources.CPUShares != 0 || 144 resources.Memory != 0 || 145 resources.NanoCPUs != 0 || 146 resources.CgroupParent != "" || 147 resources.BlkioWeight != 0 || 148 len(resources.BlkioWeightDevice) != 0 || 149 len(resources.BlkioDeviceReadBps) != 0 || 150 len(resources.BlkioDeviceWriteBps) != 0 || 151 len(resources.BlkioDeviceReadIOps) != 0 || 152 len(resources.BlkioDeviceWriteIOps) != 0 || 153 resources.CPUPeriod != 0 || 154 resources.CPUQuota != 0 || 155 resources.CPURealtimePeriod != 0 || 156 resources.CPURealtimeRuntime != 0 || 157 resources.CpusetCpus != "" || 158 resources.CpusetMems != "" || 159 len(resources.Devices) != 0 || 160 len(resources.DeviceCgroupRules) != 0 || 161 resources.KernelMemory != 0 || 162 resources.MemoryReservation != 0 || 163 resources.MemorySwap != 0 || 164 resources.MemorySwappiness != nil || 165 resources.OomKillDisable != nil || 166 (resources.PidsLimit != nil && *resources.PidsLimit != 0) || 167 len(resources.Ulimits) != 0 || 168 resources.CPUCount != 0 || 169 resources.CPUPercent != 0 || 170 resources.IOMaximumIOps != 0 || 171 resources.IOMaximumBandwidth != 0 { 172 return fmt.Errorf("resource updating isn't supported on Windows") 173 } 174 // update HostConfig of container 175 if hostConfig.RestartPolicy.Name != "" { 176 if container.HostConfig.AutoRemove && !hostConfig.RestartPolicy.IsNone() { 177 return fmt.Errorf("Restart policy cannot be updated because AutoRemove is enabled for the container") 178 } 179 container.HostConfig.RestartPolicy = hostConfig.RestartPolicy 180 } 181 return nil 182 } 183 184 // BuildHostnameFile writes the container's hostname file. 185 func (container *Container) BuildHostnameFile() error { 186 return nil 187 } 188 189 // GetMountPoints gives a platform specific transformation to types.MountPoint. Callers must hold a Container lock. 190 func (container *Container) GetMountPoints() []types.MountPoint { 191 mountPoints := make([]types.MountPoint, 0, len(container.MountPoints)) 192 for _, m := range container.MountPoints { 193 mountPoints = append(mountPoints, types.MountPoint{ 194 Type: m.Type, 195 Name: m.Name, 196 Source: m.Path(), 197 Destination: m.Destination, 198 Driver: m.Driver, 199 RW: m.RW, 200 }) 201 } 202 return mountPoints 203 } 204 205 func (container *Container) ConfigsDirPath() string { 206 return filepath.Join(container.Root, "configs") 207 } 208 209 // ConfigFilePath returns the path to the on-disk location of a config. 210 func (container *Container) ConfigFilePath(configRef swarmtypes.ConfigReference) (string, error) { 211 return filepath.Join(container.ConfigsDirPath(), configRef.ConfigID), nil 212 }