daemon.go 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  1. // Package daemon exposes the functions that occur on the host server
  2. // that the Docker daemon is running.
  3. //
  4. // In implementing the various functions of the daemon, there is often
  5. // a method-specific struct for configuring the runtime behavior.
  6. package daemon // import "github.com/docker/docker/daemon"
  7. import (
  8. "context"
  9. "fmt"
  10. "io/ioutil"
  11. "net"
  12. "os"
  13. "path"
  14. "path/filepath"
  15. "runtime"
  16. "strings"
  17. "sync"
  18. "time"
  19. "google.golang.org/grpc"
  20. "github.com/containerd/containerd"
  21. "github.com/containerd/containerd/defaults"
  22. "github.com/containerd/containerd/pkg/dialer"
  23. "github.com/docker/docker/api/types"
  24. containertypes "github.com/docker/docker/api/types/container"
  25. "github.com/docker/docker/api/types/swarm"
  26. "github.com/docker/docker/builder"
  27. "github.com/docker/docker/container"
  28. "github.com/docker/docker/daemon/config"
  29. "github.com/docker/docker/daemon/discovery"
  30. "github.com/docker/docker/daemon/events"
  31. "github.com/docker/docker/daemon/exec"
  32. "github.com/docker/docker/daemon/images"
  33. "github.com/docker/docker/daemon/logger"
  34. "github.com/docker/docker/daemon/network"
  35. "github.com/docker/docker/errdefs"
  36. "github.com/sirupsen/logrus"
  37. // register graph drivers
  38. _ "github.com/docker/docker/daemon/graphdriver/register"
  39. "github.com/docker/docker/daemon/stats"
  40. dmetadata "github.com/docker/docker/distribution/metadata"
  41. "github.com/docker/docker/dockerversion"
  42. "github.com/docker/docker/image"
  43. "github.com/docker/docker/layer"
  44. "github.com/docker/docker/libcontainerd"
  45. "github.com/docker/docker/migrate/v1"
  46. "github.com/docker/docker/pkg/idtools"
  47. "github.com/docker/docker/pkg/locker"
  48. "github.com/docker/docker/pkg/plugingetter"
  49. "github.com/docker/docker/pkg/sysinfo"
  50. "github.com/docker/docker/pkg/system"
  51. "github.com/docker/docker/pkg/truncindex"
  52. "github.com/docker/docker/plugin"
  53. pluginexec "github.com/docker/docker/plugin/executor/containerd"
  54. refstore "github.com/docker/docker/reference"
  55. "github.com/docker/docker/registry"
  56. "github.com/docker/docker/runconfig"
  57. volumesservice "github.com/docker/docker/volume/service"
  58. "github.com/docker/libnetwork"
  59. "github.com/docker/libnetwork/cluster"
  60. nwconfig "github.com/docker/libnetwork/config"
  61. "github.com/pkg/errors"
  62. )
  63. // ContainersNamespace is the name of the namespace used for users containers
  64. const ContainersNamespace = "moby"
  65. var (
  66. errSystemNotSupported = errors.New("the Docker daemon is not supported on this platform")
  67. )
  68. // Daemon holds information about the Docker daemon.
  69. type Daemon struct {
  70. ID string
  71. repository string
  72. containers container.Store
  73. containersReplica container.ViewDB
  74. execCommands *exec.Store
  75. imageService *images.ImageService
  76. idIndex *truncindex.TruncIndex
  77. configStore *config.Config
  78. statsCollector *stats.Collector
  79. defaultLogConfig containertypes.LogConfig
  80. RegistryService registry.Service
  81. EventsService *events.Events
  82. netController libnetwork.NetworkController
  83. volumes *volumesservice.VolumesService
  84. discoveryWatcher discovery.Reloader
  85. root string
  86. seccompEnabled bool
  87. apparmorEnabled bool
  88. shutdown bool
  89. idMapping *idtools.IdentityMapping
  90. // TODO: move graphDrivers field to an InfoService
  91. graphDrivers map[string]string // By operating system
  92. PluginStore *plugin.Store // todo: remove
  93. pluginManager *plugin.Manager
  94. linkIndex *linkIndex
  95. containerdCli *containerd.Client
  96. containerd libcontainerd.Client
  97. defaultIsolation containertypes.Isolation // Default isolation mode on Windows
  98. clusterProvider cluster.Provider
  99. cluster Cluster
  100. genericResources []swarm.GenericResource
  101. metricsPluginListener net.Listener
  102. machineMemory uint64
  103. seccompProfile []byte
  104. seccompProfilePath string
  105. diskUsageRunning int32
  106. pruneRunning int32
  107. hosts map[string]bool // hosts stores the addresses the daemon is listening on
  108. startupDone chan struct{}
  109. attachmentStore network.AttachmentStore
  110. attachableNetworkLock *locker.Locker
  111. }
  112. // StoreHosts stores the addresses the daemon is listening on
  113. func (daemon *Daemon) StoreHosts(hosts []string) {
  114. if daemon.hosts == nil {
  115. daemon.hosts = make(map[string]bool)
  116. }
  117. for _, h := range hosts {
  118. daemon.hosts[h] = true
  119. }
  120. }
  121. // HasExperimental returns whether the experimental features of the daemon are enabled or not
  122. func (daemon *Daemon) HasExperimental() bool {
  123. return daemon.configStore != nil && daemon.configStore.Experimental
  124. }
  125. // Features returns the features map from configStore
  126. func (daemon *Daemon) Features() *map[string]bool {
  127. return &daemon.configStore.Features
  128. }
  129. func (daemon *Daemon) restore() error {
  130. containers := make(map[string]*container.Container)
  131. logrus.Info("Loading containers: start.")
  132. dir, err := ioutil.ReadDir(daemon.repository)
  133. if err != nil {
  134. return err
  135. }
  136. for _, v := range dir {
  137. id := v.Name()
  138. container, err := daemon.load(id)
  139. if err != nil {
  140. logrus.Errorf("Failed to load container %v: %v", id, err)
  141. continue
  142. }
  143. if !system.IsOSSupported(container.OS) {
  144. logrus.Errorf("Failed to load container %v: %s (%q)", id, system.ErrNotSupportedOperatingSystem, container.OS)
  145. continue
  146. }
  147. // Ignore the container if it does not support the current driver being used by the graph
  148. currentDriverForContainerOS := daemon.graphDrivers[container.OS]
  149. if (container.Driver == "" && currentDriverForContainerOS == "aufs") || container.Driver == currentDriverForContainerOS {
  150. rwlayer, err := daemon.imageService.GetLayerByID(container.ID, container.OS)
  151. if err != nil {
  152. logrus.Errorf("Failed to load container mount %v: %v", id, err)
  153. continue
  154. }
  155. container.RWLayer = rwlayer
  156. logrus.Debugf("Loaded container %v, isRunning: %v", container.ID, container.IsRunning())
  157. containers[container.ID] = container
  158. } else {
  159. logrus.Debugf("Cannot load container %s because it was created with another graph driver.", container.ID)
  160. }
  161. }
  162. removeContainers := make(map[string]*container.Container)
  163. restartContainers := make(map[*container.Container]chan struct{})
  164. activeSandboxes := make(map[string]interface{})
  165. for id, c := range containers {
  166. if err := daemon.registerName(c); err != nil {
  167. logrus.Errorf("Failed to register container name %s: %s", c.ID, err)
  168. delete(containers, id)
  169. continue
  170. }
  171. if err := daemon.Register(c); err != nil {
  172. logrus.Errorf("Failed to register container %s: %s", c.ID, err)
  173. delete(containers, id)
  174. continue
  175. }
  176. // The LogConfig.Type is empty if the container was created before docker 1.12 with default log driver.
  177. // We should rewrite it to use the daemon defaults.
  178. // Fixes https://github.com/docker/docker/issues/22536
  179. if c.HostConfig.LogConfig.Type == "" {
  180. if err := daemon.mergeAndVerifyLogConfig(&c.HostConfig.LogConfig); err != nil {
  181. logrus.Errorf("Failed to verify log config for container %s: %q", c.ID, err)
  182. continue
  183. }
  184. }
  185. }
  186. var (
  187. wg sync.WaitGroup
  188. mapLock sync.Mutex
  189. )
  190. for _, c := range containers {
  191. wg.Add(1)
  192. go func(c *container.Container) {
  193. defer wg.Done()
  194. daemon.backportMountSpec(c)
  195. if err := daemon.checkpointAndSave(c); err != nil {
  196. logrus.WithError(err).WithField("container", c.ID).Error("error saving backported mountspec to disk")
  197. }
  198. daemon.setStateCounter(c)
  199. logrus.WithFields(logrus.Fields{
  200. "container": c.ID,
  201. "running": c.IsRunning(),
  202. "paused": c.IsPaused(),
  203. }).Debug("restoring container")
  204. var (
  205. err error
  206. alive bool
  207. ec uint32
  208. exitedAt time.Time
  209. )
  210. alive, _, err = daemon.containerd.Restore(context.Background(), c.ID, c.InitializeStdio)
  211. if err != nil && !errdefs.IsNotFound(err) {
  212. logrus.Errorf("Failed to restore container %s with containerd: %s", c.ID, err)
  213. return
  214. }
  215. if !alive {
  216. ec, exitedAt, err = daemon.containerd.DeleteTask(context.Background(), c.ID)
  217. if err != nil && !errdefs.IsNotFound(err) {
  218. logrus.WithError(err).Errorf("Failed to delete container %s from containerd", c.ID)
  219. return
  220. }
  221. } else if !daemon.configStore.LiveRestoreEnabled {
  222. if err := daemon.kill(c, c.StopSignal()); err != nil && !errdefs.IsNotFound(err) {
  223. logrus.WithError(err).WithField("container", c.ID).Error("error shutting down container")
  224. return
  225. }
  226. }
  227. if c.IsRunning() || c.IsPaused() {
  228. c.RestartManager().Cancel() // manually start containers because some need to wait for swarm networking
  229. if c.IsPaused() && alive {
  230. s, err := daemon.containerd.Status(context.Background(), c.ID)
  231. if err != nil {
  232. logrus.WithError(err).WithField("container", c.ID).
  233. Errorf("Failed to get container status")
  234. } else {
  235. logrus.WithField("container", c.ID).WithField("state", s).
  236. Info("restored container paused")
  237. switch s {
  238. case libcontainerd.StatusPaused, libcontainerd.StatusPausing:
  239. // nothing to do
  240. case libcontainerd.StatusStopped:
  241. alive = false
  242. case libcontainerd.StatusUnknown:
  243. logrus.WithField("container", c.ID).
  244. Error("Unknown status for container during restore")
  245. default:
  246. // running
  247. c.Lock()
  248. c.Paused = false
  249. daemon.setStateCounter(c)
  250. if err := c.CheckpointTo(daemon.containersReplica); err != nil {
  251. logrus.WithError(err).WithField("container", c.ID).
  252. Error("Failed to update stopped container state")
  253. }
  254. c.Unlock()
  255. }
  256. }
  257. }
  258. if !alive {
  259. c.Lock()
  260. c.SetStopped(&container.ExitStatus{ExitCode: int(ec), ExitedAt: exitedAt})
  261. daemon.Cleanup(c)
  262. if err := c.CheckpointTo(daemon.containersReplica); err != nil {
  263. logrus.Errorf("Failed to update stopped container %s state: %v", c.ID, err)
  264. }
  265. c.Unlock()
  266. }
  267. // we call Mount and then Unmount to get BaseFs of the container
  268. if err := daemon.Mount(c); err != nil {
  269. // The mount is unlikely to fail. However, in case mount fails
  270. // the container should be allowed to restore here. Some functionalities
  271. // (like docker exec -u user) might be missing but container is able to be
  272. // stopped/restarted/removed.
  273. // See #29365 for related information.
  274. // The error is only logged here.
  275. logrus.Warnf("Failed to mount container on getting BaseFs path %v: %v", c.ID, err)
  276. } else {
  277. if err := daemon.Unmount(c); err != nil {
  278. logrus.Warnf("Failed to umount container on getting BaseFs path %v: %v", c.ID, err)
  279. }
  280. }
  281. c.ResetRestartManager(false)
  282. if !c.HostConfig.NetworkMode.IsContainer() && c.IsRunning() {
  283. options, err := daemon.buildSandboxOptions(c)
  284. if err != nil {
  285. logrus.Warnf("Failed build sandbox option to restore container %s: %v", c.ID, err)
  286. }
  287. mapLock.Lock()
  288. activeSandboxes[c.NetworkSettings.SandboxID] = options
  289. mapLock.Unlock()
  290. }
  291. }
  292. // get list of containers we need to restart
  293. // Do not autostart containers which
  294. // has endpoints in a swarm scope
  295. // network yet since the cluster is
  296. // not initialized yet. We will start
  297. // it after the cluster is
  298. // initialized.
  299. if daemon.configStore.AutoRestart && c.ShouldRestart() && !c.NetworkSettings.HasSwarmEndpoint && c.HasBeenStartedBefore {
  300. mapLock.Lock()
  301. restartContainers[c] = make(chan struct{})
  302. mapLock.Unlock()
  303. } else if c.HostConfig != nil && c.HostConfig.AutoRemove {
  304. mapLock.Lock()
  305. removeContainers[c.ID] = c
  306. mapLock.Unlock()
  307. }
  308. c.Lock()
  309. if c.RemovalInProgress {
  310. // We probably crashed in the middle of a removal, reset
  311. // the flag.
  312. //
  313. // We DO NOT remove the container here as we do not
  314. // know if the user had requested for either the
  315. // associated volumes, network links or both to also
  316. // be removed. So we put the container in the "dead"
  317. // state and leave further processing up to them.
  318. logrus.Debugf("Resetting RemovalInProgress flag from %v", c.ID)
  319. c.RemovalInProgress = false
  320. c.Dead = true
  321. if err := c.CheckpointTo(daemon.containersReplica); err != nil {
  322. logrus.Errorf("Failed to update RemovalInProgress container %s state: %v", c.ID, err)
  323. }
  324. }
  325. c.Unlock()
  326. }(c)
  327. }
  328. wg.Wait()
  329. daemon.netController, err = daemon.initNetworkController(daemon.configStore, activeSandboxes)
  330. if err != nil {
  331. return fmt.Errorf("Error initializing network controller: %v", err)
  332. }
  333. // Now that all the containers are registered, register the links
  334. for _, c := range containers {
  335. if err := daemon.registerLinks(c, c.HostConfig); err != nil {
  336. logrus.Errorf("failed to register link for container %s: %v", c.ID, err)
  337. }
  338. }
  339. group := sync.WaitGroup{}
  340. for c, notifier := range restartContainers {
  341. group.Add(1)
  342. go func(c *container.Container, chNotify chan struct{}) {
  343. defer group.Done()
  344. logrus.Debugf("Starting container %s", c.ID)
  345. // ignore errors here as this is a best effort to wait for children to be
  346. // running before we try to start the container
  347. children := daemon.children(c)
  348. timeout := time.After(5 * time.Second)
  349. for _, child := range children {
  350. if notifier, exists := restartContainers[child]; exists {
  351. select {
  352. case <-notifier:
  353. case <-timeout:
  354. }
  355. }
  356. }
  357. // Make sure networks are available before starting
  358. daemon.waitForNetworks(c)
  359. if err := daemon.containerStart(c, "", "", true); err != nil {
  360. logrus.Errorf("Failed to start container %s: %s", c.ID, err)
  361. }
  362. close(chNotify)
  363. }(c, notifier)
  364. }
  365. group.Wait()
  366. removeGroup := sync.WaitGroup{}
  367. for id := range removeContainers {
  368. removeGroup.Add(1)
  369. go func(cid string) {
  370. if err := daemon.ContainerRm(cid, &types.ContainerRmConfig{ForceRemove: true, RemoveVolume: true}); err != nil {
  371. logrus.Errorf("Failed to remove container %s: %s", cid, err)
  372. }
  373. removeGroup.Done()
  374. }(id)
  375. }
  376. removeGroup.Wait()
  377. // any containers that were started above would already have had this done,
  378. // however we need to now prepare the mountpoints for the rest of the containers as well.
  379. // This shouldn't cause any issue running on the containers that already had this run.
  380. // This must be run after any containers with a restart policy so that containerized plugins
  381. // can have a chance to be running before we try to initialize them.
  382. for _, c := range containers {
  383. // if the container has restart policy, do not
  384. // prepare the mountpoints since it has been done on restarting.
  385. // This is to speed up the daemon start when a restart container
  386. // has a volume and the volume driver is not available.
  387. if _, ok := restartContainers[c]; ok {
  388. continue
  389. } else if _, ok := removeContainers[c.ID]; ok {
  390. // container is automatically removed, skip it.
  391. continue
  392. }
  393. group.Add(1)
  394. go func(c *container.Container) {
  395. defer group.Done()
  396. if err := daemon.prepareMountPoints(c); err != nil {
  397. logrus.Error(err)
  398. }
  399. }(c)
  400. }
  401. group.Wait()
  402. logrus.Info("Loading containers: done.")
  403. return nil
  404. }
  405. // RestartSwarmContainers restarts any autostart container which has a
  406. // swarm endpoint.
  407. func (daemon *Daemon) RestartSwarmContainers() {
  408. group := sync.WaitGroup{}
  409. for _, c := range daemon.List() {
  410. if !c.IsRunning() && !c.IsPaused() {
  411. // Autostart all the containers which has a
  412. // swarm endpoint now that the cluster is
  413. // initialized.
  414. if daemon.configStore.AutoRestart && c.ShouldRestart() && c.NetworkSettings.HasSwarmEndpoint && c.HasBeenStartedBefore {
  415. group.Add(1)
  416. go func(c *container.Container) {
  417. defer group.Done()
  418. if err := daemon.containerStart(c, "", "", true); err != nil {
  419. logrus.Error(err)
  420. }
  421. }(c)
  422. }
  423. }
  424. }
  425. group.Wait()
  426. }
  427. // waitForNetworks is used during daemon initialization when starting up containers
  428. // It ensures that all of a container's networks are available before the daemon tries to start the container.
  429. // In practice it just makes sure the discovery service is available for containers which use a network that require discovery.
  430. func (daemon *Daemon) waitForNetworks(c *container.Container) {
  431. if daemon.discoveryWatcher == nil {
  432. return
  433. }
  434. // Make sure if the container has a network that requires discovery that the discovery service is available before starting
  435. for netName := range c.NetworkSettings.Networks {
  436. // If we get `ErrNoSuchNetwork` here, we can assume that it is due to discovery not being ready
  437. // Most likely this is because the K/V store used for discovery is in a container and needs to be started
  438. if _, err := daemon.netController.NetworkByName(netName); err != nil {
  439. if _, ok := err.(libnetwork.ErrNoSuchNetwork); !ok {
  440. continue
  441. }
  442. // use a longish timeout here due to some slowdowns in libnetwork if the k/v store is on anything other than --net=host
  443. // FIXME: why is this slow???
  444. logrus.Debugf("Container %s waiting for network to be ready", c.Name)
  445. select {
  446. case <-daemon.discoveryWatcher.ReadyCh():
  447. case <-time.After(60 * time.Second):
  448. }
  449. return
  450. }
  451. }
  452. }
  453. func (daemon *Daemon) children(c *container.Container) map[string]*container.Container {
  454. return daemon.linkIndex.children(c)
  455. }
  456. // parents returns the names of the parent containers of the container
  457. // with the given name.
  458. func (daemon *Daemon) parents(c *container.Container) map[string]*container.Container {
  459. return daemon.linkIndex.parents(c)
  460. }
  461. func (daemon *Daemon) registerLink(parent, child *container.Container, alias string) error {
  462. fullName := path.Join(parent.Name, alias)
  463. if err := daemon.containersReplica.ReserveName(fullName, child.ID); err != nil {
  464. if err == container.ErrNameReserved {
  465. logrus.Warnf("error registering link for %s, to %s, as alias %s, ignoring: %v", parent.ID, child.ID, alias, err)
  466. return nil
  467. }
  468. return err
  469. }
  470. daemon.linkIndex.link(parent, child, fullName)
  471. return nil
  472. }
  473. // DaemonJoinsCluster informs the daemon has joined the cluster and provides
  474. // the handler to query the cluster component
  475. func (daemon *Daemon) DaemonJoinsCluster(clusterProvider cluster.Provider) {
  476. daemon.setClusterProvider(clusterProvider)
  477. }
  478. // DaemonLeavesCluster informs the daemon has left the cluster
  479. func (daemon *Daemon) DaemonLeavesCluster() {
  480. // Daemon is in charge of removing the attachable networks with
  481. // connected containers when the node leaves the swarm
  482. daemon.clearAttachableNetworks()
  483. // We no longer need the cluster provider, stop it now so that
  484. // the network agent will stop listening to cluster events.
  485. daemon.setClusterProvider(nil)
  486. // Wait for the networking cluster agent to stop
  487. daemon.netController.AgentStopWait()
  488. // Daemon is in charge of removing the ingress network when the
  489. // node leaves the swarm. Wait for job to be done or timeout.
  490. // This is called also on graceful daemon shutdown. We need to
  491. // wait, because the ingress release has to happen before the
  492. // network controller is stopped.
  493. if done, err := daemon.ReleaseIngress(); err == nil {
  494. select {
  495. case <-done:
  496. case <-time.After(5 * time.Second):
  497. logrus.Warn("timeout while waiting for ingress network removal")
  498. }
  499. } else {
  500. logrus.Warnf("failed to initiate ingress network removal: %v", err)
  501. }
  502. daemon.attachmentStore.ClearAttachments()
  503. }
  504. // setClusterProvider sets a component for querying the current cluster state.
  505. func (daemon *Daemon) setClusterProvider(clusterProvider cluster.Provider) {
  506. daemon.clusterProvider = clusterProvider
  507. daemon.netController.SetClusterProvider(clusterProvider)
  508. daemon.attachableNetworkLock = locker.New()
  509. }
  510. // IsSwarmCompatible verifies if the current daemon
  511. // configuration is compatible with the swarm mode
  512. func (daemon *Daemon) IsSwarmCompatible() error {
  513. if daemon.configStore == nil {
  514. return nil
  515. }
  516. return daemon.configStore.IsSwarmCompatible()
  517. }
  518. // NewDaemon sets up everything for the daemon to be able to service
  519. // requests from the webserver.
  520. func NewDaemon(ctx context.Context, config *config.Config, pluginStore *plugin.Store) (daemon *Daemon, err error) {
  521. setDefaultMtu(config)
  522. registryService, err := registry.NewService(config.ServiceOptions)
  523. if err != nil {
  524. return nil, err
  525. }
  526. // Ensure that we have a correct root key limit for launching containers.
  527. if err := ModifyRootKeyLimit(); err != nil {
  528. logrus.Warnf("unable to modify root key limit, number of containers could be limited by this quota: %v", err)
  529. }
  530. // Ensure we have compatible and valid configuration options
  531. if err := verifyDaemonSettings(config); err != nil {
  532. return nil, err
  533. }
  534. // Do we have a disabled network?
  535. config.DisableBridge = isBridgeNetworkDisabled(config)
  536. // Setup the resolv.conf
  537. setupResolvConf(config)
  538. // Verify the platform is supported as a daemon
  539. if !platformSupported {
  540. return nil, errSystemNotSupported
  541. }
  542. // Validate platform-specific requirements
  543. if err := checkSystem(); err != nil {
  544. return nil, err
  545. }
  546. idMapping, err := setupRemappedRoot(config)
  547. if err != nil {
  548. return nil, err
  549. }
  550. rootIDs := idMapping.RootPair()
  551. if err := setupDaemonProcess(config); err != nil {
  552. return nil, err
  553. }
  554. // set up the tmpDir to use a canonical path
  555. tmp, err := prepareTempDir(config.Root, rootIDs)
  556. if err != nil {
  557. return nil, fmt.Errorf("Unable to get the TempDir under %s: %s", config.Root, err)
  558. }
  559. realTmp, err := getRealPath(tmp)
  560. if err != nil {
  561. return nil, fmt.Errorf("Unable to get the full path to the TempDir (%s): %s", tmp, err)
  562. }
  563. if runtime.GOOS == "windows" {
  564. if _, err := os.Stat(realTmp); err != nil && os.IsNotExist(err) {
  565. if err := system.MkdirAll(realTmp, 0700, ""); err != nil {
  566. return nil, fmt.Errorf("Unable to create the TempDir (%s): %s", realTmp, err)
  567. }
  568. }
  569. os.Setenv("TEMP", realTmp)
  570. os.Setenv("TMP", realTmp)
  571. } else {
  572. os.Setenv("TMPDIR", realTmp)
  573. }
  574. d := &Daemon{
  575. configStore: config,
  576. PluginStore: pluginStore,
  577. startupDone: make(chan struct{}),
  578. }
  579. // Ensure the daemon is properly shutdown if there is a failure during
  580. // initialization
  581. defer func() {
  582. if err != nil {
  583. if err := d.Shutdown(); err != nil {
  584. logrus.Error(err)
  585. }
  586. }
  587. }()
  588. if err := d.setGenericResources(config); err != nil {
  589. return nil, err
  590. }
  591. // set up SIGUSR1 handler on Unix-like systems, or a Win32 global event
  592. // on Windows to dump Go routine stacks
  593. stackDumpDir := config.Root
  594. if execRoot := config.GetExecRoot(); execRoot != "" {
  595. stackDumpDir = execRoot
  596. }
  597. d.setupDumpStackTrap(stackDumpDir)
  598. if err := d.setupSeccompProfile(); err != nil {
  599. return nil, err
  600. }
  601. // Set the default isolation mode (only applicable on Windows)
  602. if err := d.setDefaultIsolation(); err != nil {
  603. return nil, fmt.Errorf("error setting default isolation mode: %v", err)
  604. }
  605. if err := configureMaxThreads(config); err != nil {
  606. logrus.Warnf("Failed to configure golang's threads limit: %v", err)
  607. }
  608. if err := ensureDefaultAppArmorProfile(); err != nil {
  609. logrus.Errorf(err.Error())
  610. }
  611. daemonRepo := filepath.Join(config.Root, "containers")
  612. if err := idtools.MkdirAllAndChown(daemonRepo, 0700, rootIDs); err != nil {
  613. return nil, err
  614. }
  615. // Create the directory where we'll store the runtime scripts (i.e. in
  616. // order to support runtimeArgs)
  617. daemonRuntimes := filepath.Join(config.Root, "runtimes")
  618. if err := system.MkdirAll(daemonRuntimes, 0700, ""); err != nil {
  619. return nil, err
  620. }
  621. if err := d.loadRuntimes(); err != nil {
  622. return nil, err
  623. }
  624. if runtime.GOOS == "windows" {
  625. if err := system.MkdirAll(filepath.Join(config.Root, "credentialspecs"), 0, ""); err != nil {
  626. return nil, err
  627. }
  628. }
  629. // On Windows we don't support the environment variable, or a user supplied graphdriver
  630. // as Windows has no choice in terms of which graphdrivers to use. It's a case of
  631. // running Windows containers on Windows - windowsfilter, running Linux containers on Windows,
  632. // lcow. Unix platforms however run a single graphdriver for all containers, and it can
  633. // be set through an environment variable, a daemon start parameter, or chosen through
  634. // initialization of the layerstore through driver priority order for example.
  635. d.graphDrivers = make(map[string]string)
  636. layerStores := make(map[string]layer.Store)
  637. if runtime.GOOS == "windows" {
  638. d.graphDrivers[runtime.GOOS] = "windowsfilter"
  639. if system.LCOWSupported() {
  640. d.graphDrivers["linux"] = "lcow"
  641. }
  642. } else {
  643. driverName := os.Getenv("DOCKER_DRIVER")
  644. if driverName == "" {
  645. driverName = config.GraphDriver
  646. } else {
  647. logrus.Infof("Setting the storage driver from the $DOCKER_DRIVER environment variable (%s)", driverName)
  648. }
  649. d.graphDrivers[runtime.GOOS] = driverName // May still be empty. Layerstore init determines instead.
  650. }
  651. d.RegistryService = registryService
  652. logger.RegisterPluginGetter(d.PluginStore)
  653. metricsSockPath, err := d.listenMetricsSock()
  654. if err != nil {
  655. return nil, err
  656. }
  657. registerMetricsPluginCallback(d.PluginStore, metricsSockPath)
  658. gopts := []grpc.DialOption{
  659. grpc.WithInsecure(),
  660. grpc.WithBackoffMaxDelay(3 * time.Second),
  661. grpc.WithDialer(dialer.Dialer),
  662. // TODO(stevvooe): We may need to allow configuration of this on the client.
  663. grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(defaults.DefaultMaxRecvMsgSize)),
  664. grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(defaults.DefaultMaxSendMsgSize)),
  665. }
  666. if config.ContainerdAddr != "" {
  667. d.containerdCli, err = containerd.New(config.ContainerdAddr, containerd.WithDefaultNamespace(ContainersNamespace), containerd.WithDialOpts(gopts), containerd.WithTimeout(60*time.Second))
  668. if err != nil {
  669. return nil, errors.Wrapf(err, "failed to dial %q", config.ContainerdAddr)
  670. }
  671. }
  672. createPluginExec := func(m *plugin.Manager) (plugin.Executor, error) {
  673. var pluginCli *containerd.Client
  674. // Windows is not currently using containerd, keep the
  675. // client as nil
  676. if config.ContainerdAddr != "" {
  677. pluginCli, err = containerd.New(config.ContainerdAddr, containerd.WithDefaultNamespace(pluginexec.PluginNamespace), containerd.WithDialOpts(gopts), containerd.WithTimeout(60*time.Second))
  678. if err != nil {
  679. return nil, errors.Wrapf(err, "failed to dial %q", config.ContainerdAddr)
  680. }
  681. }
  682. return pluginexec.New(ctx, getPluginExecRoot(config.Root), pluginCli, m)
  683. }
  684. // Plugin system initialization should happen before restore. Do not change order.
  685. d.pluginManager, err = plugin.NewManager(plugin.ManagerConfig{
  686. Root: filepath.Join(config.Root, "plugins"),
  687. ExecRoot: getPluginExecRoot(config.Root),
  688. Store: d.PluginStore,
  689. CreateExecutor: createPluginExec,
  690. RegistryService: registryService,
  691. LiveRestoreEnabled: config.LiveRestoreEnabled,
  692. LogPluginEvent: d.LogPluginEvent, // todo: make private
  693. AuthzMiddleware: config.AuthzMiddleware,
  694. })
  695. if err != nil {
  696. return nil, errors.Wrap(err, "couldn't create plugin manager")
  697. }
  698. if err := d.setupDefaultLogConfig(); err != nil {
  699. return nil, err
  700. }
  701. for operatingSystem, gd := range d.graphDrivers {
  702. layerStores[operatingSystem], err = layer.NewStoreFromOptions(layer.StoreOptions{
  703. Root: config.Root,
  704. MetadataStorePathTemplate: filepath.Join(config.Root, "image", "%s", "layerdb"),
  705. GraphDriver: gd,
  706. GraphDriverOptions: config.GraphOptions,
  707. IDMapping: idMapping,
  708. PluginGetter: d.PluginStore,
  709. ExperimentalEnabled: config.Experimental,
  710. OS: operatingSystem,
  711. })
  712. if err != nil {
  713. return nil, err
  714. }
  715. }
  716. // As layerstore initialization may set the driver
  717. for os := range d.graphDrivers {
  718. d.graphDrivers[os] = layerStores[os].DriverName()
  719. }
  720. // Configure and validate the kernels security support. Note this is a Linux/FreeBSD
  721. // operation only, so it is safe to pass *just* the runtime OS graphdriver.
  722. if err := configureKernelSecuritySupport(config, d.graphDrivers[runtime.GOOS]); err != nil {
  723. return nil, err
  724. }
  725. imageRoot := filepath.Join(config.Root, "image", d.graphDrivers[runtime.GOOS])
  726. ifs, err := image.NewFSStoreBackend(filepath.Join(imageRoot, "imagedb"))
  727. if err != nil {
  728. return nil, err
  729. }
  730. lgrMap := make(map[string]image.LayerGetReleaser)
  731. for os, ls := range layerStores {
  732. lgrMap[os] = ls
  733. }
  734. imageStore, err := image.NewImageStore(ifs, lgrMap)
  735. if err != nil {
  736. return nil, err
  737. }
  738. d.volumes, err = volumesservice.NewVolumeService(config.Root, d.PluginStore, rootIDs, d)
  739. if err != nil {
  740. return nil, err
  741. }
  742. trustKey, err := loadOrCreateTrustKey(config.TrustKeyPath)
  743. if err != nil {
  744. return nil, err
  745. }
  746. trustDir := filepath.Join(config.Root, "trust")
  747. if err := system.MkdirAll(trustDir, 0700, ""); err != nil {
  748. return nil, err
  749. }
  750. // We have a single tag/reference store for the daemon globally. However, it's
  751. // stored under the graphdriver. On host platforms which only support a single
  752. // container OS, but multiple selectable graphdrivers, this means depending on which
  753. // graphdriver is chosen, the global reference store is under there. For
  754. // platforms which support multiple container operating systems, this is slightly
  755. // more problematic as where does the global ref store get located? Fortunately,
  756. // for Windows, which is currently the only daemon supporting multiple container
  757. // operating systems, the list of graphdrivers available isn't user configurable.
  758. // For backwards compatibility, we just put it under the windowsfilter
  759. // directory regardless.
  760. refStoreLocation := filepath.Join(imageRoot, `repositories.json`)
  761. rs, err := refstore.NewReferenceStore(refStoreLocation)
  762. if err != nil {
  763. return nil, fmt.Errorf("Couldn't create reference store repository: %s", err)
  764. }
  765. distributionMetadataStore, err := dmetadata.NewFSMetadataStore(filepath.Join(imageRoot, "distribution"))
  766. if err != nil {
  767. return nil, err
  768. }
  769. // No content-addressability migration on Windows as it never supported pre-CA
  770. if runtime.GOOS != "windows" {
  771. migrationStart := time.Now()
  772. if err := v1.Migrate(config.Root, d.graphDrivers[runtime.GOOS], layerStores[runtime.GOOS], imageStore, rs, distributionMetadataStore); err != nil {
  773. logrus.Errorf("Graph migration failed: %q. Your old graph data was found to be too inconsistent for upgrading to content-addressable storage. Some of the old data was probably not upgraded. We recommend starting over with a clean storage directory if possible.", err)
  774. }
  775. logrus.Infof("Graph migration to content-addressability took %.2f seconds", time.Since(migrationStart).Seconds())
  776. }
  777. // Discovery is only enabled when the daemon is launched with an address to advertise. When
  778. // initialized, the daemon is registered and we can store the discovery backend as it's read-only
  779. if err := d.initDiscovery(config); err != nil {
  780. return nil, err
  781. }
  782. sysInfo := sysinfo.New(false)
  783. // Check if Devices cgroup is mounted, it is hard requirement for container security,
  784. // on Linux.
  785. if runtime.GOOS == "linux" && !sysInfo.CgroupDevicesEnabled {
  786. return nil, errors.New("Devices cgroup isn't mounted")
  787. }
  788. d.ID = trustKey.PublicKey().KeyID()
  789. d.repository = daemonRepo
  790. d.containers = container.NewMemoryStore()
  791. if d.containersReplica, err = container.NewViewDB(); err != nil {
  792. return nil, err
  793. }
  794. d.execCommands = exec.NewStore()
  795. d.idIndex = truncindex.NewTruncIndex([]string{})
  796. d.statsCollector = d.newStatsCollector(1 * time.Second)
  797. d.EventsService = events.New()
  798. d.root = config.Root
  799. d.idMapping = idMapping
  800. d.seccompEnabled = sysInfo.Seccomp
  801. d.apparmorEnabled = sysInfo.AppArmor
  802. d.linkIndex = newLinkIndex()
  803. // TODO: imageStore, distributionMetadataStore, and ReferenceStore are only
  804. // used above to run migration. They could be initialized in ImageService
  805. // if migration is called from daemon/images. layerStore might move as well.
  806. d.imageService = images.NewImageService(images.ImageServiceConfig{
  807. ContainerStore: d.containers,
  808. DistributionMetadataStore: distributionMetadataStore,
  809. EventsService: d.EventsService,
  810. ImageStore: imageStore,
  811. LayerStores: layerStores,
  812. MaxConcurrentDownloads: *config.MaxConcurrentDownloads,
  813. MaxConcurrentUploads: *config.MaxConcurrentUploads,
  814. ReferenceStore: rs,
  815. RegistryService: registryService,
  816. TrustKey: trustKey,
  817. })
  818. go d.execCommandGC()
  819. d.containerd, err = libcontainerd.NewClient(ctx, d.containerdCli, filepath.Join(config.ExecRoot, "containerd"), ContainersNamespace, d)
  820. if err != nil {
  821. return nil, err
  822. }
  823. if err := d.restore(); err != nil {
  824. return nil, err
  825. }
  826. close(d.startupDone)
  827. // FIXME: this method never returns an error
  828. info, _ := d.SystemInfo()
  829. engineInfo.WithValues(
  830. dockerversion.Version,
  831. dockerversion.GitCommit,
  832. info.Architecture,
  833. info.Driver,
  834. info.KernelVersion,
  835. info.OperatingSystem,
  836. info.OSType,
  837. info.ID,
  838. ).Set(1)
  839. engineCpus.Set(float64(info.NCPU))
  840. engineMemory.Set(float64(info.MemTotal))
  841. gd := ""
  842. for os, driver := range d.graphDrivers {
  843. if len(gd) > 0 {
  844. gd += ", "
  845. }
  846. gd += driver
  847. if len(d.graphDrivers) > 1 {
  848. gd = fmt.Sprintf("%s (%s)", gd, os)
  849. }
  850. }
  851. logrus.WithFields(logrus.Fields{
  852. "version": dockerversion.Version,
  853. "commit": dockerversion.GitCommit,
  854. "graphdriver(s)": gd,
  855. }).Info("Docker daemon")
  856. return d, nil
  857. }
  858. // DistributionServices returns services controlling daemon storage
  859. func (daemon *Daemon) DistributionServices() images.DistributionServices {
  860. return daemon.imageService.DistributionServices()
  861. }
  862. func (daemon *Daemon) waitForStartupDone() {
  863. <-daemon.startupDone
  864. }
  865. func (daemon *Daemon) shutdownContainer(c *container.Container) error {
  866. stopTimeout := c.StopTimeout()
  867. // If container failed to exit in stopTimeout seconds of SIGTERM, then using the force
  868. if err := daemon.containerStop(c, stopTimeout); err != nil {
  869. return fmt.Errorf("Failed to stop container %s with error: %v", c.ID, err)
  870. }
  871. // Wait without timeout for the container to exit.
  872. // Ignore the result.
  873. <-c.Wait(context.Background(), container.WaitConditionNotRunning)
  874. return nil
  875. }
  876. // ShutdownTimeout returns the timeout (in seconds) before containers are forcibly
  877. // killed during shutdown. The default timeout can be configured both on the daemon
  878. // and per container, and the longest timeout will be used. A grace-period of
  879. // 5 seconds is added to the configured timeout.
  880. //
  881. // A negative (-1) timeout means "indefinitely", which means that containers
  882. // are not forcibly killed, and the daemon shuts down after all containers exit.
  883. func (daemon *Daemon) ShutdownTimeout() int {
  884. shutdownTimeout := daemon.configStore.ShutdownTimeout
  885. if shutdownTimeout < 0 {
  886. return -1
  887. }
  888. if daemon.containers == nil {
  889. return shutdownTimeout
  890. }
  891. graceTimeout := 5
  892. for _, c := range daemon.containers.List() {
  893. stopTimeout := c.StopTimeout()
  894. if stopTimeout < 0 {
  895. return -1
  896. }
  897. if stopTimeout+graceTimeout > shutdownTimeout {
  898. shutdownTimeout = stopTimeout + graceTimeout
  899. }
  900. }
  901. return shutdownTimeout
  902. }
  903. // Shutdown stops the daemon.
  904. func (daemon *Daemon) Shutdown() error {
  905. daemon.shutdown = true
  906. // Keep mounts and networking running on daemon shutdown if
  907. // we are to keep containers running and restore them.
  908. if daemon.configStore.LiveRestoreEnabled && daemon.containers != nil {
  909. // check if there are any running containers, if none we should do some cleanup
  910. if ls, err := daemon.Containers(&types.ContainerListOptions{}); len(ls) != 0 || err != nil {
  911. // metrics plugins still need some cleanup
  912. daemon.cleanupMetricsPlugins()
  913. return nil
  914. }
  915. }
  916. if daemon.containers != nil {
  917. logrus.Debugf("daemon configured with a %d seconds minimum shutdown timeout", daemon.configStore.ShutdownTimeout)
  918. logrus.Debugf("start clean shutdown of all containers with a %d seconds timeout...", daemon.ShutdownTimeout())
  919. daemon.containers.ApplyAll(func(c *container.Container) {
  920. if !c.IsRunning() {
  921. return
  922. }
  923. logrus.Debugf("stopping %s", c.ID)
  924. if err := daemon.shutdownContainer(c); err != nil {
  925. logrus.Errorf("Stop container error: %v", err)
  926. return
  927. }
  928. if mountid, err := daemon.imageService.GetLayerMountID(c.ID, c.OS); err == nil {
  929. daemon.cleanupMountsByID(mountid)
  930. }
  931. logrus.Debugf("container stopped %s", c.ID)
  932. })
  933. }
  934. if daemon.volumes != nil {
  935. if err := daemon.volumes.Shutdown(); err != nil {
  936. logrus.Errorf("Error shutting down volume store: %v", err)
  937. }
  938. }
  939. if daemon.imageService != nil {
  940. daemon.imageService.Cleanup()
  941. }
  942. // If we are part of a cluster, clean up cluster's stuff
  943. if daemon.clusterProvider != nil {
  944. logrus.Debugf("start clean shutdown of cluster resources...")
  945. daemon.DaemonLeavesCluster()
  946. }
  947. daemon.cleanupMetricsPlugins()
  948. // Shutdown plugins after containers and layerstore. Don't change the order.
  949. daemon.pluginShutdown()
  950. // trigger libnetwork Stop only if it's initialized
  951. if daemon.netController != nil {
  952. daemon.netController.Stop()
  953. }
  954. if daemon.containerdCli != nil {
  955. daemon.containerdCli.Close()
  956. }
  957. return daemon.cleanupMounts()
  958. }
  959. // Mount sets container.BaseFS
  960. // (is it not set coming in? why is it unset?)
  961. func (daemon *Daemon) Mount(container *container.Container) error {
  962. if container.RWLayer == nil {
  963. return errors.New("RWLayer of container " + container.ID + " is unexpectedly nil")
  964. }
  965. dir, err := container.RWLayer.Mount(container.GetMountLabel())
  966. if err != nil {
  967. return err
  968. }
  969. logrus.Debugf("container mounted via layerStore: %v", dir)
  970. if container.BaseFS != nil && container.BaseFS.Path() != dir.Path() {
  971. // The mount path reported by the graph driver should always be trusted on Windows, since the
  972. // volume path for a given mounted layer may change over time. This should only be an error
  973. // on non-Windows operating systems.
  974. if runtime.GOOS != "windows" {
  975. daemon.Unmount(container)
  976. return fmt.Errorf("Error: driver %s is returning inconsistent paths for container %s ('%s' then '%s')",
  977. daemon.imageService.GraphDriverForOS(container.OS), container.ID, container.BaseFS, dir)
  978. }
  979. }
  980. container.BaseFS = dir // TODO: combine these fields
  981. return nil
  982. }
  983. // Unmount unsets the container base filesystem
  984. func (daemon *Daemon) Unmount(container *container.Container) error {
  985. if container.RWLayer == nil {
  986. return errors.New("RWLayer of container " + container.ID + " is unexpectedly nil")
  987. }
  988. if err := container.RWLayer.Unmount(); err != nil {
  989. logrus.Errorf("Error unmounting container %s: %s", container.ID, err)
  990. return err
  991. }
  992. return nil
  993. }
  994. // Subnets return the IPv4 and IPv6 subnets of networks that are manager by Docker.
  995. func (daemon *Daemon) Subnets() ([]net.IPNet, []net.IPNet) {
  996. var v4Subnets []net.IPNet
  997. var v6Subnets []net.IPNet
  998. managedNetworks := daemon.netController.Networks()
  999. for _, managedNetwork := range managedNetworks {
  1000. v4infos, v6infos := managedNetwork.Info().IpamInfo()
  1001. for _, info := range v4infos {
  1002. if info.IPAMData.Pool != nil {
  1003. v4Subnets = append(v4Subnets, *info.IPAMData.Pool)
  1004. }
  1005. }
  1006. for _, info := range v6infos {
  1007. if info.IPAMData.Pool != nil {
  1008. v6Subnets = append(v6Subnets, *info.IPAMData.Pool)
  1009. }
  1010. }
  1011. }
  1012. return v4Subnets, v6Subnets
  1013. }
  1014. // prepareTempDir prepares and returns the default directory to use
  1015. // for temporary files.
  1016. // If it doesn't exist, it is created. If it exists, its content is removed.
  1017. func prepareTempDir(rootDir string, rootIdentity idtools.Identity) (string, error) {
  1018. var tmpDir string
  1019. if tmpDir = os.Getenv("DOCKER_TMPDIR"); tmpDir == "" {
  1020. tmpDir = filepath.Join(rootDir, "tmp")
  1021. newName := tmpDir + "-old"
  1022. if err := os.Rename(tmpDir, newName); err == nil {
  1023. go func() {
  1024. if err := os.RemoveAll(newName); err != nil {
  1025. logrus.Warnf("failed to delete old tmp directory: %s", newName)
  1026. }
  1027. }()
  1028. } else if !os.IsNotExist(err) {
  1029. logrus.Warnf("failed to rename %s for background deletion: %s. Deleting synchronously", tmpDir, err)
  1030. if err := os.RemoveAll(tmpDir); err != nil {
  1031. logrus.Warnf("failed to delete old tmp directory: %s", tmpDir)
  1032. }
  1033. }
  1034. }
  1035. // We don't remove the content of tmpdir if it's not the default,
  1036. // it may hold things that do not belong to us.
  1037. return tmpDir, idtools.MkdirAllAndChown(tmpDir, 0700, rootIdentity)
  1038. }
  1039. func (daemon *Daemon) setGenericResources(conf *config.Config) error {
  1040. genericResources, err := config.ParseGenericResources(conf.NodeGenericResources)
  1041. if err != nil {
  1042. return err
  1043. }
  1044. daemon.genericResources = genericResources
  1045. return nil
  1046. }
  1047. func setDefaultMtu(conf *config.Config) {
  1048. // do nothing if the config does not have the default 0 value.
  1049. if conf.Mtu != 0 {
  1050. return
  1051. }
  1052. conf.Mtu = config.DefaultNetworkMtu
  1053. }
  1054. // IsShuttingDown tells whether the daemon is shutting down or not
  1055. func (daemon *Daemon) IsShuttingDown() bool {
  1056. return daemon.shutdown
  1057. }
  1058. // initDiscovery initializes the discovery watcher for this daemon.
  1059. func (daemon *Daemon) initDiscovery(conf *config.Config) error {
  1060. advertise, err := config.ParseClusterAdvertiseSettings(conf.ClusterStore, conf.ClusterAdvertise)
  1061. if err != nil {
  1062. if err == discovery.ErrDiscoveryDisabled {
  1063. return nil
  1064. }
  1065. return err
  1066. }
  1067. conf.ClusterAdvertise = advertise
  1068. discoveryWatcher, err := discovery.Init(conf.ClusterStore, conf.ClusterAdvertise, conf.ClusterOpts)
  1069. if err != nil {
  1070. return fmt.Errorf("discovery initialization failed (%v)", err)
  1071. }
  1072. daemon.discoveryWatcher = discoveryWatcher
  1073. return nil
  1074. }
  1075. func isBridgeNetworkDisabled(conf *config.Config) bool {
  1076. return conf.BridgeConfig.Iface == config.DisableNetworkBridge
  1077. }
  1078. func (daemon *Daemon) networkOptions(dconfig *config.Config, pg plugingetter.PluginGetter, activeSandboxes map[string]interface{}) ([]nwconfig.Option, error) {
  1079. options := []nwconfig.Option{}
  1080. if dconfig == nil {
  1081. return options, nil
  1082. }
  1083. options = append(options, nwconfig.OptionExperimental(dconfig.Experimental))
  1084. options = append(options, nwconfig.OptionDataDir(dconfig.Root))
  1085. options = append(options, nwconfig.OptionExecRoot(dconfig.GetExecRoot()))
  1086. dd := runconfig.DefaultDaemonNetworkMode()
  1087. dn := runconfig.DefaultDaemonNetworkMode().NetworkName()
  1088. options = append(options, nwconfig.OptionDefaultDriver(string(dd)))
  1089. options = append(options, nwconfig.OptionDefaultNetwork(dn))
  1090. if strings.TrimSpace(dconfig.ClusterStore) != "" {
  1091. kv := strings.Split(dconfig.ClusterStore, "://")
  1092. if len(kv) != 2 {
  1093. return nil, errors.New("kv store daemon config must be of the form KV-PROVIDER://KV-URL")
  1094. }
  1095. options = append(options, nwconfig.OptionKVProvider(kv[0]))
  1096. options = append(options, nwconfig.OptionKVProviderURL(kv[1]))
  1097. }
  1098. if len(dconfig.ClusterOpts) > 0 {
  1099. options = append(options, nwconfig.OptionKVOpts(dconfig.ClusterOpts))
  1100. }
  1101. if daemon.discoveryWatcher != nil {
  1102. options = append(options, nwconfig.OptionDiscoveryWatcher(daemon.discoveryWatcher))
  1103. }
  1104. if dconfig.ClusterAdvertise != "" {
  1105. options = append(options, nwconfig.OptionDiscoveryAddress(dconfig.ClusterAdvertise))
  1106. }
  1107. options = append(options, nwconfig.OptionLabels(dconfig.Labels))
  1108. options = append(options, driverOptions(dconfig)...)
  1109. if len(dconfig.NetworkConfig.DefaultAddressPools.Value()) > 0 {
  1110. options = append(options, nwconfig.OptionDefaultAddressPoolConfig(dconfig.NetworkConfig.DefaultAddressPools.Value()))
  1111. }
  1112. if daemon.configStore != nil && daemon.configStore.LiveRestoreEnabled && len(activeSandboxes) != 0 {
  1113. options = append(options, nwconfig.OptionActiveSandboxes(activeSandboxes))
  1114. }
  1115. if pg != nil {
  1116. options = append(options, nwconfig.OptionPluginGetter(pg))
  1117. }
  1118. options = append(options, nwconfig.OptionNetworkControlPlaneMTU(dconfig.NetworkControlPlaneMTU))
  1119. return options, nil
  1120. }
  1121. // GetCluster returns the cluster
  1122. func (daemon *Daemon) GetCluster() Cluster {
  1123. return daemon.cluster
  1124. }
  1125. // SetCluster sets the cluster
  1126. func (daemon *Daemon) SetCluster(cluster Cluster) {
  1127. daemon.cluster = cluster
  1128. }
  1129. func (daemon *Daemon) pluginShutdown() {
  1130. manager := daemon.pluginManager
  1131. // Check for a valid manager object. In error conditions, daemon init can fail
  1132. // and shutdown called, before plugin manager is initialized.
  1133. if manager != nil {
  1134. manager.Shutdown()
  1135. }
  1136. }
  1137. // PluginManager returns current pluginManager associated with the daemon
  1138. func (daemon *Daemon) PluginManager() *plugin.Manager { // set up before daemon to avoid this method
  1139. return daemon.pluginManager
  1140. }
  1141. // PluginGetter returns current pluginStore associated with the daemon
  1142. func (daemon *Daemon) PluginGetter() *plugin.Store {
  1143. return daemon.PluginStore
  1144. }
  1145. // CreateDaemonRoot creates the root for the daemon
  1146. func CreateDaemonRoot(config *config.Config) error {
  1147. // get the canonical path to the Docker root directory
  1148. var realRoot string
  1149. if _, err := os.Stat(config.Root); err != nil && os.IsNotExist(err) {
  1150. realRoot = config.Root
  1151. } else {
  1152. realRoot, err = getRealPath(config.Root)
  1153. if err != nil {
  1154. return fmt.Errorf("Unable to get the full path to root (%s): %s", config.Root, err)
  1155. }
  1156. }
  1157. idMapping, err := setupRemappedRoot(config)
  1158. if err != nil {
  1159. return err
  1160. }
  1161. return setupDaemonRoot(config, realRoot, idMapping.RootPair())
  1162. }
  1163. // checkpointAndSave grabs a container lock to safely call container.CheckpointTo
  1164. func (daemon *Daemon) checkpointAndSave(container *container.Container) error {
  1165. container.Lock()
  1166. defer container.Unlock()
  1167. if err := container.CheckpointTo(daemon.containersReplica); err != nil {
  1168. return fmt.Errorf("Error saving container state: %v", err)
  1169. }
  1170. return nil
  1171. }
  1172. // because the CLI sends a -1 when it wants to unset the swappiness value
  1173. // we need to clear it on the server side
  1174. func fixMemorySwappiness(resources *containertypes.Resources) {
  1175. if resources.MemorySwappiness != nil && *resources.MemorySwappiness == -1 {
  1176. resources.MemorySwappiness = nil
  1177. }
  1178. }
  1179. // GetAttachmentStore returns current attachment store associated with the daemon
  1180. func (daemon *Daemon) GetAttachmentStore() *network.AttachmentStore {
  1181. return &daemon.attachmentStore
  1182. }
  1183. // IdentityMapping returns uid/gid mapping or a SID (in the case of Windows) for the builder
  1184. func (daemon *Daemon) IdentityMapping() *idtools.IdentityMapping {
  1185. return daemon.idMapping
  1186. }
  1187. // ImageService returns the Daemon's ImageService
  1188. func (daemon *Daemon) ImageService() *images.ImageService {
  1189. return daemon.imageService
  1190. }
  1191. // BuilderBackend returns the backend used by builder
  1192. func (daemon *Daemon) BuilderBackend() builder.Backend {
  1193. return struct {
  1194. *Daemon
  1195. *images.ImageService
  1196. }{daemon, daemon.imageService}
  1197. }