github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/daemon/daemon_windows.go (about) 1 package daemon 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 8 "github.com/Microsoft/hcsshim" 9 "github.com/Sirupsen/logrus" 10 "github.com/docker/docker/api/types" 11 containertypes "github.com/docker/docker/api/types/container" 12 "github.com/docker/docker/container" 13 "github.com/docker/docker/image" 14 "github.com/docker/docker/pkg/idtools" 15 "github.com/docker/docker/pkg/parsers" 16 "github.com/docker/docker/pkg/platform" 17 "github.com/docker/docker/pkg/sysinfo" 18 "github.com/docker/docker/pkg/system" 19 "github.com/docker/docker/runconfig" 20 "github.com/docker/libnetwork" 21 nwconfig "github.com/docker/libnetwork/config" 22 "github.com/docker/libnetwork/datastore" 23 winlibnetwork "github.com/docker/libnetwork/drivers/windows" 24 "github.com/docker/libnetwork/netlabel" 25 "github.com/docker/libnetwork/options" 26 blkiodev "github.com/opencontainers/runc/libcontainer/configs" 27 "golang.org/x/sys/windows" 28 ) 29 30 const ( 31 defaultNetworkSpace = "172.16.0.0/12" 32 platformSupported = true 33 windowsMinCPUShares = 1 34 windowsMaxCPUShares = 10000 35 windowsMinCPUPercent = 1 36 windowsMaxCPUPercent = 100 37 windowsMinCPUCount = 1 38 ) 39 40 func getBlkioWeightDevices(config *containertypes.HostConfig) ([]blkiodev.WeightDevice, error) { 41 return nil, nil 42 } 43 44 func parseSecurityOpt(container *container.Container, config *containertypes.HostConfig) error { 45 return nil 46 } 47 48 func getBlkioReadIOpsDevices(config *containertypes.HostConfig) ([]blkiodev.ThrottleDevice, error) { 49 return nil, nil 50 } 51 52 func getBlkioWriteIOpsDevices(config *containertypes.HostConfig) ([]blkiodev.ThrottleDevice, error) { 53 return nil, nil 54 } 55 56 func getBlkioReadBpsDevices(config *containertypes.HostConfig) ([]blkiodev.ThrottleDevice, error) { 57 return nil, nil 58 } 59 60 func getBlkioWriteBpsDevices(config *containertypes.HostConfig) ([]blkiodev.ThrottleDevice, error) { 61 return nil, nil 62 } 63 64 func (daemon *Daemon) getLayerInit() func(string) error { 65 return nil 66 } 67 68 func checkKernel() error { 69 return nil 70 } 71 72 func (daemon *Daemon) getCgroupDriver() string { 73 return "" 74 } 75 76 // adaptContainerSettings is called during container creation to modify any 77 // settings necessary in the HostConfig structure. 78 func (daemon *Daemon) adaptContainerSettings(hostConfig *containertypes.HostConfig, adjustCPUShares bool) error { 79 if hostConfig == nil { 80 return nil 81 } 82 83 return nil 84 } 85 86 func verifyContainerResources(resources *containertypes.Resources, isHyperv bool) ([]string, error) { 87 warnings := []string{} 88 89 if !isHyperv { 90 // The processor resource controls are mutually exclusive on 91 // Windows Server Containers, the order of precedence is 92 // CPUCount first, then CPUShares, and CPUPercent last. 93 if resources.CPUCount > 0 { 94 if resources.CPUShares > 0 { 95 warnings = append(warnings, "Conflicting options: CPU count takes priority over CPU shares on Windows Server Containers. CPU shares discarded") 96 logrus.Warn("Conflicting options: CPU count takes priority over CPU shares on Windows Server Containers. CPU shares discarded") 97 resources.CPUShares = 0 98 } 99 if resources.CPUPercent > 0 { 100 warnings = append(warnings, "Conflicting options: CPU count takes priority over CPU percent on Windows Server Containers. CPU percent discarded") 101 logrus.Warn("Conflicting options: CPU count takes priority over CPU percent on Windows Server Containers. CPU percent discarded") 102 resources.CPUPercent = 0 103 } 104 } else if resources.CPUShares > 0 { 105 if resources.CPUPercent > 0 { 106 warnings = append(warnings, "Conflicting options: CPU shares takes priority over CPU percent on Windows Server Containers. CPU percent discarded") 107 logrus.Warn("Conflicting options: CPU shares takes priority over CPU percent on Windows Server Containers. CPU percent discarded") 108 resources.CPUPercent = 0 109 } 110 } 111 } 112 113 if resources.CPUShares < 0 || resources.CPUShares > windowsMaxCPUShares { 114 return warnings, fmt.Errorf("range of CPUShares is from %d to %d", windowsMinCPUShares, windowsMaxCPUShares) 115 } 116 if resources.CPUPercent < 0 || resources.CPUPercent > windowsMaxCPUPercent { 117 return warnings, fmt.Errorf("range of CPUPercent is from %d to %d", windowsMinCPUPercent, windowsMaxCPUPercent) 118 } 119 if resources.CPUCount < 0 { 120 return warnings, fmt.Errorf("invalid CPUCount: CPUCount cannot be negative") 121 } 122 123 if resources.NanoCPUs > 0 && resources.CPUPercent > 0 { 124 return warnings, fmt.Errorf("conflicting options: Nano CPUs and CPU Percent cannot both be set") 125 } 126 if resources.NanoCPUs > 0 && resources.CPUShares > 0 { 127 return warnings, fmt.Errorf("conflicting options: Nano CPUs and CPU Shares cannot both be set") 128 } 129 // The precision we could get is 0.01, because on Windows we have to convert to CPUPercent. 130 // We don't set the lower limit here and it is up to the underlying platform (e.g., Windows) to return an error. 131 if resources.NanoCPUs < 0 || resources.NanoCPUs > int64(sysinfo.NumCPU())*1e9 { 132 return warnings, fmt.Errorf("range of CPUs is from 0.01 to %d.00, as there are only %d CPUs available", sysinfo.NumCPU(), sysinfo.NumCPU()) 133 } 134 135 if len(resources.BlkioDeviceReadBps) > 0 { 136 return warnings, fmt.Errorf("invalid option: Windows does not support BlkioDeviceReadBps") 137 } 138 if len(resources.BlkioDeviceReadIOps) > 0 { 139 return warnings, fmt.Errorf("invalid option: Windows does not support BlkioDeviceReadIOps") 140 } 141 if len(resources.BlkioDeviceWriteBps) > 0 { 142 return warnings, fmt.Errorf("invalid option: Windows does not support BlkioDeviceWriteBps") 143 } 144 if len(resources.BlkioDeviceWriteIOps) > 0 { 145 return warnings, fmt.Errorf("invalid option: Windows does not support BlkioDeviceWriteIOps") 146 } 147 if resources.BlkioWeight > 0 { 148 return warnings, fmt.Errorf("invalid option: Windows does not support BlkioWeight") 149 } 150 if len(resources.BlkioWeightDevice) > 0 { 151 return warnings, fmt.Errorf("invalid option: Windows does not support BlkioWeightDevice") 152 } 153 if resources.CgroupParent != "" { 154 return warnings, fmt.Errorf("invalid option: Windows does not support CgroupParent") 155 } 156 if resources.CPUPeriod != 0 { 157 return warnings, fmt.Errorf("invalid option: Windows does not support CPUPeriod") 158 } 159 if resources.CpusetCpus != "" { 160 return warnings, fmt.Errorf("invalid option: Windows does not support CpusetCpus") 161 } 162 if resources.CpusetMems != "" { 163 return warnings, fmt.Errorf("invalid option: Windows does not support CpusetMems") 164 } 165 if resources.KernelMemory != 0 { 166 return warnings, fmt.Errorf("invalid option: Windows does not support KernelMemory") 167 } 168 if resources.MemoryReservation != 0 { 169 return warnings, fmt.Errorf("invalid option: Windows does not support MemoryReservation") 170 } 171 if resources.MemorySwap != 0 { 172 return warnings, fmt.Errorf("invalid option: Windows does not support MemorySwap") 173 } 174 if resources.MemorySwappiness != nil && *resources.MemorySwappiness != -1 { 175 return warnings, fmt.Errorf("invalid option: Windows does not support MemorySwappiness") 176 } 177 if resources.OomKillDisable != nil && *resources.OomKillDisable { 178 return warnings, fmt.Errorf("invalid option: Windows does not support OomKillDisable") 179 } 180 if resources.PidsLimit != 0 { 181 return warnings, fmt.Errorf("invalid option: Windows does not support PidsLimit") 182 } 183 if len(resources.Ulimits) != 0 { 184 return warnings, fmt.Errorf("invalid option: Windows does not support Ulimits") 185 } 186 return warnings, nil 187 } 188 189 // verifyPlatformContainerSettings performs platform-specific validation of the 190 // hostconfig and config structures. 191 func verifyPlatformContainerSettings(daemon *Daemon, hostConfig *containertypes.HostConfig, config *containertypes.Config, update bool) ([]string, error) { 192 warnings := []string{} 193 194 hyperv := daemon.runAsHyperVContainer(hostConfig) 195 if !hyperv && system.IsWindowsClient() { 196 // @engine maintainers. This block should not be removed. It partially enforces licensing 197 // restrictions on Windows. Ping @jhowardmsft if there are concerns or PRs to change this. 198 return warnings, fmt.Errorf("Windows client operating systems only support Hyper-V containers") 199 } 200 201 w, err := verifyContainerResources(&hostConfig.Resources, hyperv) 202 warnings = append(warnings, w...) 203 return warnings, err 204 } 205 206 // platformReload updates configuration with platform specific options 207 func (daemon *Daemon) platformReload(config *Config) map[string]string { 208 return map[string]string{} 209 } 210 211 // verifyDaemonSettings performs validation of daemon config struct 212 func verifyDaemonSettings(config *Config) error { 213 return nil 214 } 215 216 // checkSystem validates platform-specific requirements 217 func checkSystem() error { 218 // Validate the OS version. Note that docker.exe must be manifested for this 219 // call to return the correct version. 220 osv := system.GetOSVersion() 221 if osv.MajorVersion < 10 { 222 return fmt.Errorf("This version of Windows does not support the docker daemon") 223 } 224 if osv.Build < 14393 { 225 return fmt.Errorf("The docker daemon requires build 14393 or later of Windows Server 2016 or Windows 10") 226 } 227 228 vmcompute := windows.NewLazySystemDLL("vmcompute.dll") 229 if vmcompute.Load() != nil { 230 return fmt.Errorf("Failed to load vmcompute.dll. Ensure that the Containers role is installed.") 231 } 232 return nil 233 } 234 235 // configureKernelSecuritySupport configures and validate security support for the kernel 236 func configureKernelSecuritySupport(config *Config, driverName string) error { 237 return nil 238 } 239 240 // configureMaxThreads sets the Go runtime max threads threshold 241 func configureMaxThreads(config *Config) error { 242 return nil 243 } 244 245 func (daemon *Daemon) initNetworkController(config *Config, activeSandboxes map[string]interface{}) (libnetwork.NetworkController, error) { 246 netOptions, err := daemon.networkOptions(config, nil, nil) 247 if err != nil { 248 return nil, err 249 } 250 controller, err := libnetwork.New(netOptions...) 251 if err != nil { 252 return nil, fmt.Errorf("error obtaining controller instance: %v", err) 253 } 254 255 hnsresponse, err := hcsshim.HNSListNetworkRequest("GET", "", "") 256 if err != nil { 257 return nil, err 258 } 259 260 // Remove networks not present in HNS 261 for _, v := range controller.Networks() { 262 options := v.Info().DriverOptions() 263 hnsid := options[winlibnetwork.HNSID] 264 found := false 265 266 for _, v := range hnsresponse { 267 if v.Id == hnsid { 268 found = true 269 break 270 } 271 } 272 273 if !found { 274 // global networks should not be deleted by local HNS 275 if v.Info().Scope() != datastore.GlobalScope { 276 err = v.Delete() 277 if err != nil { 278 logrus.Errorf("Error occurred when removing network %v", err) 279 } 280 } 281 } 282 } 283 284 _, err = controller.NewNetwork("null", "none", "", libnetwork.NetworkOptionPersist(false)) 285 if err != nil { 286 return nil, err 287 } 288 289 defaultNetworkExists := false 290 291 if network, err := controller.NetworkByName(runconfig.DefaultDaemonNetworkMode().NetworkName()); err == nil { 292 options := network.Info().DriverOptions() 293 for _, v := range hnsresponse { 294 if options[winlibnetwork.HNSID] == v.Id { 295 defaultNetworkExists = true 296 break 297 } 298 } 299 } 300 301 // discover and add HNS networks to windows 302 // network that exist are removed and added again 303 for _, v := range hnsresponse { 304 var n libnetwork.Network 305 s := func(current libnetwork.Network) bool { 306 options := current.Info().DriverOptions() 307 if options[winlibnetwork.HNSID] == v.Id { 308 n = current 309 return true 310 } 311 return false 312 } 313 314 controller.WalkNetworks(s) 315 if n != nil { 316 // global networks should not be deleted by local HNS 317 if n.Info().Scope() == datastore.GlobalScope { 318 continue 319 } 320 v.Name = n.Name() 321 // This will not cause network delete from HNS as the network 322 // is not yet populated in the libnetwork windows driver 323 n.Delete() 324 } 325 326 netOption := map[string]string{ 327 winlibnetwork.NetworkName: v.Name, 328 winlibnetwork.HNSID: v.Id, 329 } 330 331 v4Conf := []*libnetwork.IpamConf{} 332 for _, subnet := range v.Subnets { 333 ipamV4Conf := libnetwork.IpamConf{} 334 ipamV4Conf.PreferredPool = subnet.AddressPrefix 335 ipamV4Conf.Gateway = subnet.GatewayAddress 336 v4Conf = append(v4Conf, &ipamV4Conf) 337 } 338 339 name := v.Name 340 341 // If there is no nat network create one from the first NAT network 342 // encountered 343 if !defaultNetworkExists && runconfig.DefaultDaemonNetworkMode() == containertypes.NetworkMode(strings.ToLower(v.Type)) { 344 name = runconfig.DefaultDaemonNetworkMode().NetworkName() 345 defaultNetworkExists = true 346 } 347 348 v6Conf := []*libnetwork.IpamConf{} 349 _, err := controller.NewNetwork(strings.ToLower(v.Type), name, "", 350 libnetwork.NetworkOptionGeneric(options.Generic{ 351 netlabel.GenericData: netOption, 352 }), 353 libnetwork.NetworkOptionIpam("default", "", v4Conf, v6Conf, nil), 354 ) 355 356 if err != nil { 357 logrus.Errorf("Error occurred when creating network %v", err) 358 } 359 } 360 361 if !config.DisableBridge { 362 // Initialize default driver "bridge" 363 if err := initBridgeDriver(controller, config); err != nil { 364 return nil, err 365 } 366 } 367 368 return controller, nil 369 } 370 371 func initBridgeDriver(controller libnetwork.NetworkController, config *Config) error { 372 if _, err := controller.NetworkByName(runconfig.DefaultDaemonNetworkMode().NetworkName()); err == nil { 373 return nil 374 } 375 376 netOption := map[string]string{ 377 winlibnetwork.NetworkName: runconfig.DefaultDaemonNetworkMode().NetworkName(), 378 } 379 380 var ipamOption libnetwork.NetworkOption 381 var subnetPrefix string 382 383 if config.bridgeConfig.FixedCIDR != "" { 384 subnetPrefix = config.bridgeConfig.FixedCIDR 385 } else { 386 // TP5 doesn't support properly detecting subnet 387 osv := system.GetOSVersion() 388 if osv.Build < 14360 { 389 subnetPrefix = defaultNetworkSpace 390 } 391 } 392 393 if subnetPrefix != "" { 394 ipamV4Conf := libnetwork.IpamConf{} 395 ipamV4Conf.PreferredPool = subnetPrefix 396 v4Conf := []*libnetwork.IpamConf{&ipamV4Conf} 397 v6Conf := []*libnetwork.IpamConf{} 398 ipamOption = libnetwork.NetworkOptionIpam("default", "", v4Conf, v6Conf, nil) 399 } 400 401 _, err := controller.NewNetwork(string(runconfig.DefaultDaemonNetworkMode()), runconfig.DefaultDaemonNetworkMode().NetworkName(), "", 402 libnetwork.NetworkOptionGeneric(options.Generic{ 403 netlabel.GenericData: netOption, 404 }), 405 ipamOption, 406 ) 407 408 if err != nil { 409 return fmt.Errorf("Error creating default network: %v", err) 410 } 411 412 return nil 413 } 414 415 // registerLinks sets up links between containers and writes the 416 // configuration out for persistence. As of Windows TP4, links are not supported. 417 func (daemon *Daemon) registerLinks(container *container.Container, hostConfig *containertypes.HostConfig) error { 418 return nil 419 } 420 421 func (daemon *Daemon) cleanupMountsByID(in string) error { 422 return nil 423 } 424 425 func (daemon *Daemon) cleanupMounts() error { 426 return nil 427 } 428 429 func setupRemappedRoot(config *Config) ([]idtools.IDMap, []idtools.IDMap, error) { 430 return nil, nil, nil 431 } 432 433 func setupDaemonRoot(config *Config, rootDir string, rootUID, rootGID int) error { 434 config.Root = rootDir 435 // Create the root directory if it doesn't exists 436 if err := system.MkdirAllWithACL(config.Root, 0); err != nil && !os.IsExist(err) { 437 return err 438 } 439 return nil 440 } 441 442 // runasHyperVContainer returns true if we are going to run as a Hyper-V container 443 func (daemon *Daemon) runAsHyperVContainer(hostConfig *containertypes.HostConfig) bool { 444 if hostConfig.Isolation.IsDefault() { 445 // Container is set to use the default, so take the default from the daemon configuration 446 return daemon.defaultIsolation.IsHyperV() 447 } 448 449 // Container is requesting an isolation mode. Honour it. 450 return hostConfig.Isolation.IsHyperV() 451 452 } 453 454 // conditionalMountOnStart is a platform specific helper function during the 455 // container start to call mount. 456 func (daemon *Daemon) conditionalMountOnStart(container *container.Container) error { 457 // We do not mount if a Hyper-V container 458 if !daemon.runAsHyperVContainer(container.HostConfig) { 459 return daemon.Mount(container) 460 } 461 return nil 462 } 463 464 // conditionalUnmountOnCleanup is a platform specific helper function called 465 // during the cleanup of a container to unmount. 466 func (daemon *Daemon) conditionalUnmountOnCleanup(container *container.Container) error { 467 // We do not unmount if a Hyper-V container 468 if !daemon.runAsHyperVContainer(container.HostConfig) { 469 return daemon.Unmount(container) 470 } 471 return nil 472 } 473 474 func driverOptions(config *Config) []nwconfig.Option { 475 return []nwconfig.Option{} 476 } 477 478 func (daemon *Daemon) stats(c *container.Container) (*types.StatsJSON, error) { 479 if !c.IsRunning() { 480 return nil, errNotRunning{c.ID} 481 } 482 483 // Obtain the stats from HCS via libcontainerd 484 stats, err := daemon.containerd.Stats(c.ID) 485 if err != nil { 486 return nil, err 487 } 488 489 // Start with an empty structure 490 s := &types.StatsJSON{} 491 492 // Populate the CPU/processor statistics 493 s.CPUStats = types.CPUStats{ 494 CPUUsage: types.CPUUsage{ 495 TotalUsage: stats.Processor.TotalRuntime100ns, 496 UsageInKernelmode: stats.Processor.RuntimeKernel100ns, 497 UsageInUsermode: stats.Processor.RuntimeKernel100ns, 498 }, 499 } 500 501 // Populate the memory statistics 502 s.MemoryStats = types.MemoryStats{ 503 Commit: stats.Memory.UsageCommitBytes, 504 CommitPeak: stats.Memory.UsageCommitPeakBytes, 505 PrivateWorkingSet: stats.Memory.UsagePrivateWorkingSetBytes, 506 } 507 508 // Populate the storage statistics 509 s.StorageStats = types.StorageStats{ 510 ReadCountNormalized: stats.Storage.ReadCountNormalized, 511 ReadSizeBytes: stats.Storage.ReadSizeBytes, 512 WriteCountNormalized: stats.Storage.WriteCountNormalized, 513 WriteSizeBytes: stats.Storage.WriteSizeBytes, 514 } 515 516 // Populate the network statistics 517 s.Networks = make(map[string]types.NetworkStats) 518 519 for _, nstats := range stats.Network { 520 s.Networks[nstats.EndpointId] = types.NetworkStats{ 521 RxBytes: nstats.BytesReceived, 522 RxPackets: nstats.PacketsReceived, 523 RxDropped: nstats.DroppedPacketsIncoming, 524 TxBytes: nstats.BytesSent, 525 TxPackets: nstats.PacketsSent, 526 TxDropped: nstats.DroppedPacketsOutgoing, 527 } 528 } 529 530 // Set the timestamp 531 s.Stats.Read = stats.Timestamp 532 s.Stats.NumProcs = platform.NumProcs() 533 534 return s, nil 535 } 536 537 // setDefaultIsolation determine the default isolation mode for the 538 // daemon to run in. This is only applicable on Windows 539 func (daemon *Daemon) setDefaultIsolation() error { 540 daemon.defaultIsolation = containertypes.Isolation("process") 541 // On client SKUs, default to Hyper-V 542 if system.IsWindowsClient() { 543 daemon.defaultIsolation = containertypes.Isolation("hyperv") 544 } 545 for _, option := range daemon.configStore.ExecOptions { 546 key, val, err := parsers.ParseKeyValueOpt(option) 547 if err != nil { 548 return err 549 } 550 key = strings.ToLower(key) 551 switch key { 552 553 case "isolation": 554 if !containertypes.Isolation(val).IsValid() { 555 return fmt.Errorf("Invalid exec-opt value for 'isolation':'%s'", val) 556 } 557 if containertypes.Isolation(val).IsHyperV() { 558 daemon.defaultIsolation = containertypes.Isolation("hyperv") 559 } 560 if containertypes.Isolation(val).IsProcess() { 561 if system.IsWindowsClient() { 562 // @engine maintainers. This block should not be removed. It partially enforces licensing 563 // restrictions on Windows. Ping @jhowardmsft if there are concerns or PRs to change this. 564 return fmt.Errorf("Windows client operating systems only support Hyper-V containers") 565 } 566 daemon.defaultIsolation = containertypes.Isolation("process") 567 } 568 default: 569 return fmt.Errorf("Unrecognised exec-opt '%s'\n", key) 570 } 571 } 572 573 logrus.Infof("Windows default isolation mode: %s", daemon.defaultIsolation) 574 return nil 575 } 576 577 func rootFSToAPIType(rootfs *image.RootFS) types.RootFS { 578 var layers []string 579 for _, l := range rootfs.DiffIDs { 580 layers = append(layers, l.String()) 581 } 582 return types.RootFS{ 583 Type: rootfs.Type, 584 Layers: layers, 585 } 586 } 587 588 func setupDaemonProcess(config *Config) error { 589 return nil 590 } 591 592 // verifyVolumesInfo is a no-op on windows. 593 // This is called during daemon initialization to migrate volumes from pre-1.7. 594 // volumes were not supported on windows pre-1.7 595 func (daemon *Daemon) verifyVolumesInfo(container *container.Container) error { 596 return nil 597 } 598 599 func (daemon *Daemon) setupSeccompProfile() error { 600 return nil 601 }