daemon_solaris.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. // +build solaris,cgo
  2. package daemon
  3. import (
  4. "fmt"
  5. "net"
  6. "strconv"
  7. "github.com/Sirupsen/logrus"
  8. "github.com/docker/docker/api/types"
  9. containertypes "github.com/docker/docker/api/types/container"
  10. "github.com/docker/docker/container"
  11. "github.com/docker/docker/image"
  12. "github.com/docker/docker/layer"
  13. "github.com/docker/docker/pkg/idtools"
  14. "github.com/docker/docker/pkg/parsers/kernel"
  15. "github.com/docker/docker/pkg/sysinfo"
  16. refstore "github.com/docker/docker/reference"
  17. "github.com/docker/libnetwork"
  18. nwconfig "github.com/docker/libnetwork/config"
  19. "github.com/docker/libnetwork/drivers/solaris/bridge"
  20. "github.com/docker/libnetwork/netlabel"
  21. "github.com/docker/libnetwork/netutils"
  22. lntypes "github.com/docker/libnetwork/types"
  23. "github.com/opencontainers/runc/libcontainer/label"
  24. "github.com/opencontainers/runtime-spec/specs-go"
  25. "github.com/pkg/errors"
  26. )
  27. //#include <zone.h>
  28. import "C"
  29. const (
  30. defaultVirtualSwitch = "Virtual Switch"
  31. platformSupported = true
  32. solarisMinCPUShares = 1
  33. solarisMaxCPUShares = 65535
  34. )
  35. func getMemoryResources(config containertypes.Resources) specs.CappedMemory {
  36. memory := specs.CappedMemory{}
  37. if config.Memory > 0 {
  38. memory.Physical = strconv.FormatInt(config.Memory, 10)
  39. }
  40. if config.MemorySwap != 0 {
  41. memory.Swap = strconv.FormatInt(config.MemorySwap, 10)
  42. }
  43. return memory
  44. }
  45. func getCPUResources(config containertypes.Resources) specs.CappedCPU {
  46. cpu := specs.CappedCPU{}
  47. if config.CpusetCpus != "" {
  48. cpu.Ncpus = config.CpusetCpus
  49. }
  50. return cpu
  51. }
  52. func (daemon *Daemon) cleanupMountsByID(id string) error {
  53. return nil
  54. }
  55. func parseSecurityOpt(container *container.Container, config *containertypes.HostConfig) error {
  56. //Since config.SecurityOpt is specifically defined as a "List of string values to
  57. //customize labels for MLs systems, such as SELinux"
  58. //until we figure out how to map to Trusted Extensions
  59. //this is being disabled for now on Solaris
  60. var (
  61. labelOpts []string
  62. err error
  63. )
  64. if len(config.SecurityOpt) > 0 {
  65. return errors.New("Security options are not supported on Solaris")
  66. }
  67. container.ProcessLabel, container.MountLabel, err = label.InitLabels(labelOpts)
  68. return err
  69. }
  70. func setupRemappedRoot(config *Config) ([]idtools.IDMap, []idtools.IDMap, error) {
  71. return nil, nil, nil
  72. }
  73. func setupDaemonRoot(config *Config, rootDir string, rootUID, rootGID int) error {
  74. return nil
  75. }
  76. func (daemon *Daemon) getLayerInit() func(string) error {
  77. return nil
  78. }
  79. func checkKernel() error {
  80. // solaris can rely upon checkSystem() below, we don't skew kernel versions
  81. return nil
  82. }
  83. func (daemon *Daemon) getCgroupDriver() string {
  84. return ""
  85. }
  86. func (daemon *Daemon) adaptContainerSettings(hostConfig *containertypes.HostConfig, adjustCPUShares bool) error {
  87. if hostConfig.CPUShares < 0 {
  88. logrus.Warnf("Changing requested CPUShares of %d to minimum allowed of %d", hostConfig.CPUShares, solarisMinCPUShares)
  89. hostConfig.CPUShares = solarisMinCPUShares
  90. } else if hostConfig.CPUShares > solarisMaxCPUShares {
  91. logrus.Warnf("Changing requested CPUShares of %d to maximum allowed of %d", hostConfig.CPUShares, solarisMaxCPUShares)
  92. hostConfig.CPUShares = solarisMaxCPUShares
  93. }
  94. if hostConfig.Memory > 0 && hostConfig.MemorySwap == 0 {
  95. // By default, MemorySwap is set to twice the size of Memory.
  96. hostConfig.MemorySwap = hostConfig.Memory * 2
  97. }
  98. if hostConfig.ShmSize != 0 {
  99. hostConfig.ShmSize = container.DefaultSHMSize
  100. }
  101. if hostConfig.OomKillDisable == nil {
  102. defaultOomKillDisable := false
  103. hostConfig.OomKillDisable = &defaultOomKillDisable
  104. }
  105. return nil
  106. }
  107. // UsingSystemd returns true if cli option includes native.cgroupdriver=systemd
  108. func UsingSystemd(config *Config) bool {
  109. return false
  110. }
  111. // verifyPlatformContainerSettings performs platform-specific validation of the
  112. // hostconfig and config structures.
  113. func verifyPlatformContainerSettings(daemon *Daemon, hostConfig *containertypes.HostConfig, config *containertypes.Config, update bool) ([]string, error) {
  114. warnings := []string{}
  115. sysInfo := sysinfo.New(true)
  116. // NOTE: We do not enforce a minimum value for swap limits for zones on Solaris and
  117. // therefore we will not do that for Docker container either.
  118. if hostConfig.Memory > 0 && !sysInfo.MemoryLimit {
  119. warnings = append(warnings, "Your kernel does not support memory limit capabilities. Limitation discarded.")
  120. logrus.Warnf("Your kernel does not support memory limit capabilities. Limitation discarded.")
  121. hostConfig.Memory = 0
  122. hostConfig.MemorySwap = -1
  123. }
  124. if hostConfig.Memory > 0 && hostConfig.MemorySwap != -1 && !sysInfo.SwapLimit {
  125. warnings = append(warnings, "Your kernel does not support swap limit capabilities, memory limited without swap.")
  126. logrus.Warnf("Your kernel does not support swap limit capabilities, memory limited without swap.")
  127. hostConfig.MemorySwap = -1
  128. }
  129. if hostConfig.Memory > 0 && hostConfig.MemorySwap > 0 && hostConfig.MemorySwap < hostConfig.Memory {
  130. return warnings, fmt.Errorf("Minimum memoryswap limit should be larger than memory limit, see usage.")
  131. }
  132. // Solaris NOTE: We allow and encourage setting the swap without setting the memory limit.
  133. if hostConfig.MemorySwappiness != nil && *hostConfig.MemorySwappiness != -1 && !sysInfo.MemorySwappiness {
  134. warnings = append(warnings, "Your kernel does not support memory swappiness capabilities, memory swappiness discarded.")
  135. logrus.Warnf("Your kernel does not support memory swappiness capabilities, memory swappiness discarded.")
  136. hostConfig.MemorySwappiness = nil
  137. }
  138. if hostConfig.MemoryReservation > 0 && !sysInfo.MemoryReservation {
  139. warnings = append(warnings, "Your kernel does not support memory soft limit capabilities. Limitation discarded.")
  140. logrus.Warnf("Your kernel does not support memory soft limit capabilities. Limitation discarded.")
  141. hostConfig.MemoryReservation = 0
  142. }
  143. if hostConfig.Memory > 0 && hostConfig.MemoryReservation > 0 && hostConfig.Memory < hostConfig.MemoryReservation {
  144. return warnings, fmt.Errorf("Minimum memory limit should be larger than memory reservation limit, see usage.")
  145. }
  146. if hostConfig.KernelMemory > 0 && !sysInfo.KernelMemory {
  147. warnings = append(warnings, "Your kernel does not support kernel memory limit capabilities. Limitation discarded.")
  148. logrus.Warnf("Your kernel does not support kernel memory limit capabilities. Limitation discarded.")
  149. hostConfig.KernelMemory = 0
  150. }
  151. if hostConfig.CPUShares != 0 && !sysInfo.CPUShares {
  152. warnings = append(warnings, "Your kernel does not support CPU shares. Shares discarded.")
  153. logrus.Warnf("Your kernel does not support CPU shares. Shares discarded.")
  154. hostConfig.CPUShares = 0
  155. }
  156. if hostConfig.CPUShares < 0 {
  157. warnings = append(warnings, "Invalid CPUShares value. Must be positive. Discarding.")
  158. logrus.Warnf("Invalid CPUShares value. Must be positive. Discarding.")
  159. hostConfig.CPUQuota = 0
  160. }
  161. if hostConfig.CPUShares > 0 && !sysinfo.IsCPUSharesAvailable() {
  162. warnings = append(warnings, "Global zone default scheduling class not FSS. Discarding shares.")
  163. logrus.Warnf("Global zone default scheduling class not FSS. Discarding shares.")
  164. hostConfig.CPUShares = 0
  165. }
  166. // Solaris NOTE: Linux does not do negative checking for CPUShares and Quota here. But it makes sense to.
  167. if hostConfig.CPUPeriod > 0 && !sysInfo.CPUCfsPeriod {
  168. warnings = append(warnings, "Your kernel does not support CPU cfs period. Period discarded.")
  169. logrus.Warnf("Your kernel does not support CPU cfs period. Period discarded.")
  170. if hostConfig.CPUQuota > 0 {
  171. warnings = append(warnings, "Quota will be applied on default period, not period specified.")
  172. logrus.Warnf("Quota will be applied on default period, not period specified.")
  173. }
  174. hostConfig.CPUPeriod = 0
  175. }
  176. if hostConfig.CPUQuota != 0 && !sysInfo.CPUCfsQuota {
  177. warnings = append(warnings, "Your kernel does not support CPU cfs quota. Quota discarded.")
  178. logrus.Warnf("Your kernel does not support CPU cfs quota. Quota discarded.")
  179. hostConfig.CPUQuota = 0
  180. }
  181. if hostConfig.CPUQuota < 0 {
  182. warnings = append(warnings, "Invalid CPUQuota value. Must be positive. Discarding.")
  183. logrus.Warnf("Invalid CPUQuota value. Must be positive. Discarding.")
  184. hostConfig.CPUQuota = 0
  185. }
  186. if (hostConfig.CpusetCpus != "" || hostConfig.CpusetMems != "") && !sysInfo.Cpuset {
  187. warnings = append(warnings, "Your kernel does not support cpuset. Cpuset discarded.")
  188. logrus.Warnf("Your kernel does not support cpuset. Cpuset discarded.")
  189. hostConfig.CpusetCpus = ""
  190. hostConfig.CpusetMems = ""
  191. }
  192. cpusAvailable, err := sysInfo.IsCpusetCpusAvailable(hostConfig.CpusetCpus)
  193. if err != nil {
  194. return warnings, fmt.Errorf("Invalid value %s for cpuset cpus.", hostConfig.CpusetCpus)
  195. }
  196. if !cpusAvailable {
  197. return warnings, fmt.Errorf("Requested CPUs are not available - requested %s, available: %s.", hostConfig.CpusetCpus, sysInfo.Cpus)
  198. }
  199. memsAvailable, err := sysInfo.IsCpusetMemsAvailable(hostConfig.CpusetMems)
  200. if err != nil {
  201. return warnings, fmt.Errorf("Invalid value %s for cpuset mems.", hostConfig.CpusetMems)
  202. }
  203. if !memsAvailable {
  204. return warnings, fmt.Errorf("Requested memory nodes are not available - requested %s, available: %s.", hostConfig.CpusetMems, sysInfo.Mems)
  205. }
  206. if hostConfig.BlkioWeight > 0 && !sysInfo.BlkioWeight {
  207. warnings = append(warnings, "Your kernel does not support Block I/O weight. Weight discarded.")
  208. logrus.Warnf("Your kernel does not support Block I/O weight. Weight discarded.")
  209. hostConfig.BlkioWeight = 0
  210. }
  211. if hostConfig.OomKillDisable != nil && !sysInfo.OomKillDisable {
  212. *hostConfig.OomKillDisable = false
  213. // Don't warn; this is the default setting but only applicable to Linux
  214. }
  215. if sysInfo.IPv4ForwardingDisabled {
  216. warnings = append(warnings, "IPv4 forwarding is disabled. Networking will not work.")
  217. logrus.Warnf("IPv4 forwarding is disabled. Networking will not work")
  218. }
  219. // Solaris NOTE: We do not allow setting Linux specific options, so check and warn for all of them.
  220. if hostConfig.CapAdd != nil || hostConfig.CapDrop != nil {
  221. warnings = append(warnings, "Adding or dropping kernel capabilities unsupported on Solaris.Discarding capabilities lists.")
  222. logrus.Warnf("Adding or dropping kernel capabilities unsupported on Solaris.Discarding capabilities lists.")
  223. hostConfig.CapAdd = nil
  224. hostConfig.CapDrop = nil
  225. }
  226. if hostConfig.GroupAdd != nil {
  227. warnings = append(warnings, "Additional groups unsupported on Solaris.Discarding groups lists.")
  228. logrus.Warnf("Additional groups unsupported on Solaris.Discarding groups lists.")
  229. hostConfig.GroupAdd = nil
  230. }
  231. if hostConfig.IpcMode != "" {
  232. warnings = append(warnings, "IPC namespace assignment unsupported on Solaris.Discarding IPC setting.")
  233. logrus.Warnf("IPC namespace assignment unsupported on Solaris.Discarding IPC setting.")
  234. hostConfig.IpcMode = ""
  235. }
  236. if hostConfig.PidMode != "" {
  237. warnings = append(warnings, "PID namespace setting unsupported on Solaris. Running container in host PID namespace.")
  238. logrus.Warnf("PID namespace setting unsupported on Solaris. Running container in host PID namespace.")
  239. hostConfig.PidMode = ""
  240. }
  241. if hostConfig.Privileged {
  242. warnings = append(warnings, "Privileged mode unsupported on Solaris. Discarding privileged mode setting.")
  243. logrus.Warnf("Privileged mode unsupported on Solaris. Discarding privileged mode setting.")
  244. hostConfig.Privileged = false
  245. }
  246. if hostConfig.UTSMode != "" {
  247. warnings = append(warnings, "UTS namespace assignment unsupported on Solaris.Discarding UTS setting.")
  248. logrus.Warnf("UTS namespace assignment unsupported on Solaris.Discarding UTS setting.")
  249. hostConfig.UTSMode = ""
  250. }
  251. if hostConfig.CgroupParent != "" {
  252. warnings = append(warnings, "Specifying Cgroup parent unsupported on Solaris. Discarding cgroup parent setting.")
  253. logrus.Warnf("Specifying Cgroup parent unsupported on Solaris. Discarding cgroup parent setting.")
  254. hostConfig.CgroupParent = ""
  255. }
  256. if hostConfig.Ulimits != nil {
  257. warnings = append(warnings, "Specifying ulimits unsupported on Solaris. Discarding ulimits setting.")
  258. logrus.Warnf("Specifying ulimits unsupported on Solaris. Discarding ulimits setting.")
  259. hostConfig.Ulimits = nil
  260. }
  261. return warnings, nil
  262. }
  263. // platformReload updates configuration with platform specific options
  264. func (daemon *Daemon) platformReload(config *Config) map[string]string {
  265. return map[string]string{}
  266. }
  267. // verifyDaemonSettings performs validation of daemon config struct
  268. func verifyDaemonSettings(config *Config) error {
  269. if config.DefaultRuntime == "" {
  270. config.DefaultRuntime = stockRuntimeName
  271. }
  272. if config.Runtimes == nil {
  273. config.Runtimes = make(map[string]types.Runtime)
  274. }
  275. stockRuntimeOpts := []string{}
  276. config.Runtimes[stockRuntimeName] = types.Runtime{Path: DefaultRuntimeBinary, Args: stockRuntimeOpts}
  277. // checkSystem validates platform-specific requirements
  278. return nil
  279. }
  280. func checkSystem() error {
  281. // check OS version for compatibility, ensure running in global zone
  282. var err error
  283. var id C.zoneid_t
  284. if id, err = C.getzoneid(); err != nil {
  285. return fmt.Errorf("Exiting. Error getting zone id: %+v", err)
  286. }
  287. if int(id) != 0 {
  288. return fmt.Errorf("Exiting because the Docker daemon is not running in the global zone")
  289. }
  290. v, err := kernel.GetKernelVersion()
  291. if kernel.CompareKernelVersion(*v, kernel.VersionInfo{Kernel: 5, Major: 12, Minor: 0}) < 0 {
  292. return fmt.Errorf("Your Solaris kernel version: %s doesn't support Docker. Please upgrade to 5.12.0", v.String())
  293. }
  294. return err
  295. }
  296. // configureMaxThreads sets the Go runtime max threads threshold
  297. // which is 90% of the kernel setting from /proc/sys/kernel/threads-max
  298. func configureMaxThreads(config *Config) error {
  299. return nil
  300. }
  301. // configureKernelSecuritySupport configures and validate security support for the kernel
  302. func configureKernelSecuritySupport(config *Config, driverName string) error {
  303. return nil
  304. }
  305. func (daemon *Daemon) initNetworkController(config *Config, activeSandboxes map[string]interface{}) (libnetwork.NetworkController, error) {
  306. netOptions, err := daemon.networkOptions(config, daemon.PluginStore, activeSandboxes)
  307. if err != nil {
  308. return nil, err
  309. }
  310. controller, err := libnetwork.New(netOptions...)
  311. if err != nil {
  312. return nil, fmt.Errorf("error obtaining controller instance: %v", err)
  313. }
  314. // Initialize default network on "null"
  315. if _, err := controller.NewNetwork("null", "none", "", libnetwork.NetworkOptionPersist(false)); err != nil {
  316. return nil, fmt.Errorf("Error creating default 'null' network: %v", err)
  317. }
  318. if !config.DisableBridge {
  319. // Initialize default driver "bridge"
  320. if err := initBridgeDriver(controller, config); err != nil {
  321. return nil, err
  322. }
  323. }
  324. return controller, nil
  325. }
  326. func initBridgeDriver(controller libnetwork.NetworkController, config *Config) error {
  327. if n, err := controller.NetworkByName("bridge"); err == nil {
  328. if err = n.Delete(); err != nil {
  329. return fmt.Errorf("could not delete the default bridge network: %v", err)
  330. }
  331. }
  332. bridgeName := bridge.DefaultBridgeName
  333. if config.bridgeConfig.Iface != "" {
  334. bridgeName = config.bridgeConfig.Iface
  335. }
  336. netOption := map[string]string{
  337. bridge.BridgeName: bridgeName,
  338. bridge.DefaultBridge: strconv.FormatBool(true),
  339. netlabel.DriverMTU: strconv.Itoa(config.Mtu),
  340. bridge.EnableICC: strconv.FormatBool(config.bridgeConfig.InterContainerCommunication),
  341. }
  342. // --ip processing
  343. if config.bridgeConfig.DefaultIP != nil {
  344. netOption[bridge.DefaultBindingIP] = config.bridgeConfig.DefaultIP.String()
  345. }
  346. var ipamV4Conf *libnetwork.IpamConf
  347. ipamV4Conf = &libnetwork.IpamConf{AuxAddresses: make(map[string]string)}
  348. nwList, _, err := netutils.ElectInterfaceAddresses(bridgeName)
  349. if err != nil {
  350. return errors.Wrap(err, "list bridge addresses failed")
  351. }
  352. nw := nwList[0]
  353. if len(nwList) > 1 && config.bridgeConfig.FixedCIDR != "" {
  354. _, fCIDR, err := net.ParseCIDR(config.bridgeConfig.FixedCIDR)
  355. if err != nil {
  356. return errors.Wrap(err, "parse CIDR failed")
  357. }
  358. // Iterate through in case there are multiple addresses for the bridge
  359. for _, entry := range nwList {
  360. if fCIDR.Contains(entry.IP) {
  361. nw = entry
  362. break
  363. }
  364. }
  365. }
  366. ipamV4Conf.PreferredPool = lntypes.GetIPNetCanonical(nw).String()
  367. hip, _ := lntypes.GetHostPartIP(nw.IP, nw.Mask)
  368. if hip.IsGlobalUnicast() {
  369. ipamV4Conf.Gateway = nw.IP.String()
  370. }
  371. if config.bridgeConfig.IP != "" {
  372. ipamV4Conf.PreferredPool = config.bridgeConfig.IP
  373. ip, _, err := net.ParseCIDR(config.bridgeConfig.IP)
  374. if err != nil {
  375. return err
  376. }
  377. ipamV4Conf.Gateway = ip.String()
  378. } else if bridgeName == bridge.DefaultBridgeName && ipamV4Conf.PreferredPool != "" {
  379. logrus.Infof("Default bridge (%s) is assigned with an IP address %s. Daemon option --bip can be used to set a preferred IP address", bridgeName, ipamV4Conf.PreferredPool)
  380. }
  381. if config.bridgeConfig.FixedCIDR != "" {
  382. _, fCIDR, err := net.ParseCIDR(config.bridgeConfig.FixedCIDR)
  383. if err != nil {
  384. return err
  385. }
  386. ipamV4Conf.SubPool = fCIDR.String()
  387. }
  388. if config.bridgeConfig.DefaultGatewayIPv4 != nil {
  389. ipamV4Conf.AuxAddresses["DefaultGatewayIPv4"] = config.bridgeConfig.DefaultGatewayIPv4.String()
  390. }
  391. v4Conf := []*libnetwork.IpamConf{ipamV4Conf}
  392. v6Conf := []*libnetwork.IpamConf{}
  393. // Initialize default network on "bridge" with the same name
  394. _, err = controller.NewNetwork("bridge", "bridge", "",
  395. libnetwork.NetworkOptionDriverOpts(netOption),
  396. libnetwork.NetworkOptionIpam("default", "", v4Conf, v6Conf, nil),
  397. libnetwork.NetworkOptionDeferIPv6Alloc(false))
  398. if err != nil {
  399. return fmt.Errorf("Error creating default 'bridge' network: %v", err)
  400. }
  401. return nil
  402. }
  403. // registerLinks sets up links between containers and writes the
  404. // configuration out for persistence.
  405. func (daemon *Daemon) registerLinks(container *container.Container, hostConfig *containertypes.HostConfig) error {
  406. return nil
  407. }
  408. func (daemon *Daemon) cleanupMounts() error {
  409. return nil
  410. }
  411. // conditionalMountOnStart is a platform specific helper function during the
  412. // container start to call mount.
  413. func (daemon *Daemon) conditionalMountOnStart(container *container.Container) error {
  414. return daemon.Mount(container)
  415. }
  416. // conditionalUnmountOnCleanup is a platform specific helper function called
  417. // during the cleanup of a container to unmount.
  418. func (daemon *Daemon) conditionalUnmountOnCleanup(container *container.Container) error {
  419. return daemon.Unmount(container)
  420. }
  421. func restoreCustomImage(is image.Store, ls layer.Store, rs refstore.Store) error {
  422. // Solaris has no custom images to register
  423. return nil
  424. }
  425. func driverOptions(config *Config) []nwconfig.Option {
  426. return []nwconfig.Option{}
  427. }
  428. func (daemon *Daemon) stats(c *container.Container) (*types.StatsJSON, error) {
  429. return nil, nil
  430. }
  431. // setDefaultIsolation determine the default isolation mode for the
  432. // daemon to run in. This is only applicable on Windows
  433. func (daemon *Daemon) setDefaultIsolation() error {
  434. return nil
  435. }
  436. func rootFSToAPIType(rootfs *image.RootFS) types.RootFS {
  437. return types.RootFS{}
  438. }
  439. func setupDaemonProcess(config *Config) error {
  440. return nil
  441. }
  442. func (daemon *Daemon) setupSeccompProfile() error {
  443. return nil
  444. }