daemon.go 44 KB

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