github.com/kobeld/docker@v1.12.0-rc1/daemon/daemon.go (about)

     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  
     8  import (
     9  	"encoding/json"
    10  	"fmt"
    11  	"io"
    12  	"io/ioutil"
    13  	"net"
    14  	"os"
    15  	"path"
    16  	"path/filepath"
    17  	"runtime"
    18  	"strings"
    19  	"sync"
    20  	"syscall"
    21  	"time"
    22  
    23  	"github.com/Sirupsen/logrus"
    24  	containerd "github.com/docker/containerd/api/grpc/types"
    25  	"github.com/docker/docker/api"
    26  	"github.com/docker/docker/container"
    27  	"github.com/docker/docker/daemon/events"
    28  	"github.com/docker/docker/daemon/exec"
    29  	"github.com/docker/engine-api/types"
    30  	containertypes "github.com/docker/engine-api/types/container"
    31  	"github.com/docker/libnetwork/cluster"
    32  	// register graph drivers
    33  	_ "github.com/docker/docker/daemon/graphdriver/register"
    34  	dmetadata "github.com/docker/docker/distribution/metadata"
    35  	"github.com/docker/docker/distribution/xfer"
    36  	"github.com/docker/docker/image"
    37  	"github.com/docker/docker/layer"
    38  	"github.com/docker/docker/libcontainerd"
    39  	"github.com/docker/docker/migrate/v1"
    40  	"github.com/docker/docker/pkg/fileutils"
    41  	"github.com/docker/docker/pkg/graphdb"
    42  	"github.com/docker/docker/pkg/idtools"
    43  	"github.com/docker/docker/pkg/progress"
    44  	"github.com/docker/docker/pkg/registrar"
    45  	"github.com/docker/docker/pkg/signal"
    46  	"github.com/docker/docker/pkg/streamformatter"
    47  	"github.com/docker/docker/pkg/sysinfo"
    48  	"github.com/docker/docker/pkg/system"
    49  	"github.com/docker/docker/pkg/truncindex"
    50  	"github.com/docker/docker/reference"
    51  	"github.com/docker/docker/registry"
    52  	"github.com/docker/docker/runconfig"
    53  	"github.com/docker/docker/utils"
    54  	volumedrivers "github.com/docker/docker/volume/drivers"
    55  	"github.com/docker/docker/volume/local"
    56  	"github.com/docker/docker/volume/store"
    57  	"github.com/docker/libnetwork"
    58  	nwconfig "github.com/docker/libnetwork/config"
    59  	"github.com/docker/libtrust"
    60  )
    61  
    62  var (
    63  	// DefaultRuntimeBinary is the default runtime to be used by
    64  	// containerd if none is specified
    65  	DefaultRuntimeBinary = "docker-runc"
    66  
    67  	errSystemNotSupported = fmt.Errorf("The Docker daemon is not supported on this platform.")
    68  )
    69  
    70  // Daemon holds information about the Docker daemon.
    71  type Daemon struct {
    72  	ID                        string
    73  	repository                string
    74  	containers                container.Store
    75  	execCommands              *exec.Store
    76  	referenceStore            reference.Store
    77  	downloadManager           *xfer.LayerDownloadManager
    78  	uploadManager             *xfer.LayerUploadManager
    79  	distributionMetadataStore dmetadata.Store
    80  	trustKey                  libtrust.PrivateKey
    81  	idIndex                   *truncindex.TruncIndex
    82  	configStore               *Config
    83  	statsCollector            *statsCollector
    84  	defaultLogConfig          containertypes.LogConfig
    85  	RegistryService           registry.Service
    86  	EventsService             *events.Events
    87  	netController             libnetwork.NetworkController
    88  	volumes                   *store.VolumeStore
    89  	discoveryWatcher          discoveryReloader
    90  	root                      string
    91  	seccompEnabled            bool
    92  	shutdown                  bool
    93  	uidMaps                   []idtools.IDMap
    94  	gidMaps                   []idtools.IDMap
    95  	layerStore                layer.Store
    96  	imageStore                image.Store
    97  	nameIndex                 *registrar.Registrar
    98  	linkIndex                 *linkIndex
    99  	containerd                libcontainerd.Client
   100  	containerdRemote          libcontainerd.Remote
   101  	defaultIsolation          containertypes.Isolation // Default isolation mode on Windows
   102  	clusterProvider           cluster.Provider
   103  }
   104  
   105  func (daemon *Daemon) restore() error {
   106  	var (
   107  		debug         = utils.IsDebugEnabled()
   108  		currentDriver = daemon.GraphDriverName()
   109  		containers    = make(map[string]*container.Container)
   110  	)
   111  
   112  	if !debug {
   113  		logrus.Info("Loading containers: start.")
   114  	}
   115  	dir, err := ioutil.ReadDir(daemon.repository)
   116  	if err != nil {
   117  		return err
   118  	}
   119  
   120  	for _, v := range dir {
   121  		id := v.Name()
   122  		container, err := daemon.load(id)
   123  		if !debug && logrus.GetLevel() == logrus.InfoLevel {
   124  			fmt.Print(".")
   125  		}
   126  		if err != nil {
   127  			logrus.Errorf("Failed to load container %v: %v", id, err)
   128  			continue
   129  		}
   130  
   131  		// Ignore the container if it does not support the current driver being used by the graph
   132  		if (container.Driver == "" && currentDriver == "aufs") || container.Driver == currentDriver {
   133  			rwlayer, err := daemon.layerStore.GetRWLayer(container.ID)
   134  			if err != nil {
   135  				logrus.Errorf("Failed to load container mount %v: %v", id, err)
   136  				continue
   137  			}
   138  			container.RWLayer = rwlayer
   139  			logrus.Debugf("Loaded container %v", container.ID)
   140  
   141  			containers[container.ID] = container
   142  		} else {
   143  			logrus.Debugf("Cannot load container %s because it was created with another graph driver.", container.ID)
   144  		}
   145  	}
   146  
   147  	var migrateLegacyLinks bool
   148  	restartContainers := make(map[*container.Container]chan struct{})
   149  	activeSandboxes := make(map[string]interface{})
   150  	for _, c := range containers {
   151  		if err := daemon.registerName(c); err != nil {
   152  			logrus.Errorf("Failed to register container %s: %s", c.ID, err)
   153  			continue
   154  		}
   155  		if err := daemon.Register(c); err != nil {
   156  			logrus.Errorf("Failed to register container %s: %s", c.ID, err)
   157  			continue
   158  		}
   159  
   160  		// The LogConfig.Type is empty if the container was created before docker 1.12 with default log driver.
   161  		// We should rewrite it to use the daemon defaults.
   162  		// Fixes https://github.com/docker/docker/issues/22536
   163  		if c.HostConfig.LogConfig.Type == "" {
   164  			if err := daemon.mergeAndVerifyLogConfig(&c.HostConfig.LogConfig); err != nil {
   165  				logrus.Errorf("Failed to verify log config for container %s: %q", c.ID, err)
   166  				continue
   167  			}
   168  		}
   169  	}
   170  	var wg sync.WaitGroup
   171  	var mapLock sync.Mutex
   172  	for _, c := range containers {
   173  		wg.Add(1)
   174  		go func(c *container.Container) {
   175  			defer wg.Done()
   176  			rm := c.RestartManager(false)
   177  			if c.IsRunning() || c.IsPaused() {
   178  				if err := daemon.containerd.Restore(c.ID, libcontainerd.WithRestartManager(rm)); err != nil {
   179  					logrus.Errorf("Failed to restore with containerd: %q", err)
   180  					return
   181  				}
   182  				if !c.HostConfig.NetworkMode.IsContainer() {
   183  					options, err := daemon.buildSandboxOptions(c)
   184  					if err != nil {
   185  						logrus.Warnf("Failed build sandbox option to restore container %s: %v", c.ID, err)
   186  					}
   187  					mapLock.Lock()
   188  					activeSandboxes[c.NetworkSettings.SandboxID] = options
   189  					mapLock.Unlock()
   190  				}
   191  
   192  			}
   193  			// fixme: only if not running
   194  			// get list of containers we need to restart
   195  			if daemon.configStore.AutoRestart && !c.IsRunning() && !c.IsPaused() && c.ShouldRestart() {
   196  				mapLock.Lock()
   197  				restartContainers[c] = make(chan struct{})
   198  				mapLock.Unlock()
   199  			}
   200  
   201  			if c.RemovalInProgress {
   202  				// We probably crashed in the middle of a removal, reset
   203  				// the flag.
   204  				//
   205  				// We DO NOT remove the container here as we do not
   206  				// know if the user had requested for either the
   207  				// associated volumes, network links or both to also
   208  				// be removed. So we put the container in the "dead"
   209  				// state and leave further processing up to them.
   210  				logrus.Debugf("Resetting RemovalInProgress flag from %v", c.ID)
   211  				c.ResetRemovalInProgress()
   212  				c.SetDead()
   213  				c.ToDisk()
   214  			}
   215  
   216  			// if c.hostConfig.Links is nil (not just empty), then it is using the old sqlite links and needs to be migrated
   217  			if c.HostConfig != nil && c.HostConfig.Links == nil {
   218  				migrateLegacyLinks = true
   219  			}
   220  		}(c)
   221  	}
   222  	wg.Wait()
   223  	daemon.netController, err = daemon.initNetworkController(daemon.configStore, activeSandboxes)
   224  	if err != nil {
   225  		return fmt.Errorf("Error initializing network controller: %v", err)
   226  	}
   227  
   228  	// migrate any legacy links from sqlite
   229  	linkdbFile := filepath.Join(daemon.root, "linkgraph.db")
   230  	var legacyLinkDB *graphdb.Database
   231  	if migrateLegacyLinks {
   232  		legacyLinkDB, err = graphdb.NewSqliteConn(linkdbFile)
   233  		if err != nil {
   234  			return fmt.Errorf("error connecting to legacy link graph DB %s, container links may be lost: %v", linkdbFile, err)
   235  		}
   236  		defer legacyLinkDB.Close()
   237  	}
   238  
   239  	// Now that all the containers are registered, register the links
   240  	for _, c := range containers {
   241  		if migrateLegacyLinks {
   242  			if err := daemon.migrateLegacySqliteLinks(legacyLinkDB, c); err != nil {
   243  				return err
   244  			}
   245  		}
   246  		if err := daemon.registerLinks(c, c.HostConfig); err != nil {
   247  			logrus.Errorf("failed to register link for container %s: %v", c.ID, err)
   248  		}
   249  	}
   250  
   251  	group := sync.WaitGroup{}
   252  	for c, notifier := range restartContainers {
   253  		group.Add(1)
   254  
   255  		go func(c *container.Container, chNotify chan struct{}) {
   256  			defer group.Done()
   257  
   258  			logrus.Debugf("Starting container %s", c.ID)
   259  
   260  			// ignore errors here as this is a best effort to wait for children to be
   261  			//   running before we try to start the container
   262  			children := daemon.children(c)
   263  			timeout := time.After(5 * time.Second)
   264  			for _, child := range children {
   265  				if notifier, exists := restartContainers[child]; exists {
   266  					select {
   267  					case <-notifier:
   268  					case <-timeout:
   269  					}
   270  				}
   271  			}
   272  
   273  			// Make sure networks are available before starting
   274  			daemon.waitForNetworks(c)
   275  			if err := daemon.containerStart(c); err != nil {
   276  				logrus.Errorf("Failed to start container %s: %s", c.ID, err)
   277  			}
   278  			close(chNotify)
   279  		}(c, notifier)
   280  
   281  	}
   282  	group.Wait()
   283  
   284  	// any containers that were started above would already have had this done,
   285  	// however we need to now prepare the mountpoints for the rest of the containers as well.
   286  	// This shouldn't cause any issue running on the containers that already had this run.
   287  	// This must be run after any containers with a restart policy so that containerized plugins
   288  	// can have a chance to be running before we try to initialize them.
   289  	for _, c := range containers {
   290  		// if the container has restart policy, do not
   291  		// prepare the mountpoints since it has been done on restarting.
   292  		// This is to speed up the daemon start when a restart container
   293  		// has a volume and the volume dirver is not available.
   294  		if _, ok := restartContainers[c]; ok {
   295  			continue
   296  		}
   297  		group.Add(1)
   298  		go func(c *container.Container) {
   299  			defer group.Done()
   300  			if err := daemon.prepareMountPoints(c); err != nil {
   301  				logrus.Error(err)
   302  			}
   303  		}(c)
   304  	}
   305  
   306  	group.Wait()
   307  
   308  	if !debug {
   309  		if logrus.GetLevel() == logrus.InfoLevel {
   310  			fmt.Println()
   311  		}
   312  		logrus.Info("Loading containers: done.")
   313  	}
   314  
   315  	return nil
   316  }
   317  
   318  // waitForNetworks is used during daemon initialization when starting up containers
   319  // It ensures that all of a container's networks are available before the daemon tries to start the container.
   320  // In practice it just makes sure the discovery service is available for containers which use a network that require discovery.
   321  func (daemon *Daemon) waitForNetworks(c *container.Container) {
   322  	if daemon.discoveryWatcher == nil {
   323  		return
   324  	}
   325  	// Make sure if the container has a network that requires discovery that the discovery service is available before starting
   326  	for netName := range c.NetworkSettings.Networks {
   327  		// If we get `ErrNoSuchNetwork` here, it can assumed that it is due to discovery not being ready
   328  		// Most likely this is because the K/V store used for discovery is in a container and needs to be started
   329  		if _, err := daemon.netController.NetworkByName(netName); err != nil {
   330  			if _, ok := err.(libnetwork.ErrNoSuchNetwork); !ok {
   331  				continue
   332  			}
   333  			// use a longish timeout here due to some slowdowns in libnetwork if the k/v store is on anything other than --net=host
   334  			// FIXME: why is this slow???
   335  			logrus.Debugf("Container %s waiting for network to be ready", c.Name)
   336  			select {
   337  			case <-daemon.discoveryWatcher.ReadyCh():
   338  			case <-time.After(60 * time.Second):
   339  			}
   340  			return
   341  		}
   342  	}
   343  }
   344  
   345  func (daemon *Daemon) children(c *container.Container) map[string]*container.Container {
   346  	return daemon.linkIndex.children(c)
   347  }
   348  
   349  // parents returns the names of the parent containers of the container
   350  // with the given name.
   351  func (daemon *Daemon) parents(c *container.Container) map[string]*container.Container {
   352  	return daemon.linkIndex.parents(c)
   353  }
   354  
   355  func (daemon *Daemon) registerLink(parent, child *container.Container, alias string) error {
   356  	fullName := path.Join(parent.Name, alias)
   357  	if err := daemon.nameIndex.Reserve(fullName, child.ID); err != nil {
   358  		if err == registrar.ErrNameReserved {
   359  			logrus.Warnf("error registering link for %s, to %s, as alias %s, ignoring: %v", parent.ID, child.ID, alias, err)
   360  			return nil
   361  		}
   362  		return err
   363  	}
   364  	daemon.linkIndex.link(parent, child, fullName)
   365  	return nil
   366  }
   367  
   368  // SetClusterProvider sets a component for quering the current cluster state.
   369  func (daemon *Daemon) SetClusterProvider(clusterProvider cluster.Provider) {
   370  	daemon.clusterProvider = clusterProvider
   371  	daemon.netController.SetClusterProvider(clusterProvider)
   372  }
   373  
   374  // IsSwarmCompatible verifies if the current daemon
   375  // configuration is compatible with the swarm mode
   376  func (daemon *Daemon) IsSwarmCompatible() error {
   377  	if daemon.configStore == nil {
   378  		return nil
   379  	}
   380  	return daemon.configStore.isSwarmCompatible()
   381  }
   382  
   383  // NewDaemon sets up everything for the daemon to be able to service
   384  // requests from the webserver.
   385  func NewDaemon(config *Config, registryService registry.Service, containerdRemote libcontainerd.Remote) (daemon *Daemon, err error) {
   386  	setDefaultMtu(config)
   387  
   388  	// Ensure we have compatible and valid configuration options
   389  	if err := verifyDaemonSettings(config); err != nil {
   390  		return nil, err
   391  	}
   392  
   393  	// Do we have a disabled network?
   394  	config.DisableBridge = isBridgeNetworkDisabled(config)
   395  
   396  	// Verify the platform is supported as a daemon
   397  	if !platformSupported {
   398  		return nil, errSystemNotSupported
   399  	}
   400  
   401  	// Validate platform-specific requirements
   402  	if err := checkSystem(); err != nil {
   403  		return nil, err
   404  	}
   405  
   406  	// set up SIGUSR1 handler on Unix-like systems, or a Win32 global event
   407  	// on Windows to dump Go routine stacks
   408  	setupDumpStackTrap()
   409  
   410  	uidMaps, gidMaps, err := setupRemappedRoot(config)
   411  	if err != nil {
   412  		return nil, err
   413  	}
   414  	rootUID, rootGID, err := idtools.GetRootUIDGID(uidMaps, gidMaps)
   415  	if err != nil {
   416  		return nil, err
   417  	}
   418  
   419  	// get the canonical path to the Docker root directory
   420  	var realRoot string
   421  	if _, err := os.Stat(config.Root); err != nil && os.IsNotExist(err) {
   422  		realRoot = config.Root
   423  	} else {
   424  		realRoot, err = fileutils.ReadSymlinkedDirectory(config.Root)
   425  		if err != nil {
   426  			return nil, fmt.Errorf("Unable to get the full path to root (%s): %s", config.Root, err)
   427  		}
   428  	}
   429  
   430  	if err = setupDaemonRoot(config, realRoot, rootUID, rootGID); err != nil {
   431  		return nil, err
   432  	}
   433  
   434  	// set up the tmpDir to use a canonical path
   435  	tmp, err := tempDir(config.Root, rootUID, rootGID)
   436  	if err != nil {
   437  		return nil, fmt.Errorf("Unable to get the TempDir under %s: %s", config.Root, err)
   438  	}
   439  	realTmp, err := fileutils.ReadSymlinkedDirectory(tmp)
   440  	if err != nil {
   441  		return nil, fmt.Errorf("Unable to get the full path to the TempDir (%s): %s", tmp, err)
   442  	}
   443  	os.Setenv("TMPDIR", realTmp)
   444  
   445  	d := &Daemon{configStore: config}
   446  	// Ensure the daemon is properly shutdown if there is a failure during
   447  	// initialization
   448  	defer func() {
   449  		if err != nil {
   450  			if err := d.Shutdown(); err != nil {
   451  				logrus.Error(err)
   452  			}
   453  		}
   454  	}()
   455  
   456  	// Set the default isolation mode (only applicable on Windows)
   457  	if err := d.setDefaultIsolation(); err != nil {
   458  		return nil, fmt.Errorf("error setting default isolation mode: %v", err)
   459  	}
   460  
   461  	logrus.Debugf("Using default logging driver %s", config.LogConfig.Type)
   462  
   463  	if err := configureMaxThreads(config); err != nil {
   464  		logrus.Warnf("Failed to configure golang's threads limit: %v", err)
   465  	}
   466  
   467  	installDefaultAppArmorProfile()
   468  	daemonRepo := filepath.Join(config.Root, "containers")
   469  	if err := idtools.MkdirAllAs(daemonRepo, 0700, rootUID, rootGID); err != nil && !os.IsExist(err) {
   470  		return nil, err
   471  	}
   472  
   473  	driverName := os.Getenv("DOCKER_DRIVER")
   474  	if driverName == "" {
   475  		driverName = config.GraphDriver
   476  	}
   477  	d.layerStore, err = layer.NewStoreFromOptions(layer.StoreOptions{
   478  		StorePath:                 config.Root,
   479  		MetadataStorePathTemplate: filepath.Join(config.Root, "image", "%s", "layerdb"),
   480  		GraphDriver:               driverName,
   481  		GraphDriverOptions:        config.GraphOptions,
   482  		UIDMaps:                   uidMaps,
   483  		GIDMaps:                   gidMaps,
   484  	})
   485  	if err != nil {
   486  		return nil, err
   487  	}
   488  
   489  	graphDriver := d.layerStore.DriverName()
   490  	imageRoot := filepath.Join(config.Root, "image", graphDriver)
   491  
   492  	// Configure and validate the kernels security support
   493  	if err := configureKernelSecuritySupport(config, graphDriver); err != nil {
   494  		return nil, err
   495  	}
   496  
   497  	logrus.Debugf("Max Concurrent Downloads: %d", *config.MaxConcurrentDownloads)
   498  	d.downloadManager = xfer.NewLayerDownloadManager(d.layerStore, *config.MaxConcurrentDownloads)
   499  	logrus.Debugf("Max Concurrent Uploads: %d", *config.MaxConcurrentUploads)
   500  	d.uploadManager = xfer.NewLayerUploadManager(*config.MaxConcurrentUploads)
   501  
   502  	ifs, err := image.NewFSStoreBackend(filepath.Join(imageRoot, "imagedb"))
   503  	if err != nil {
   504  		return nil, err
   505  	}
   506  
   507  	d.imageStore, err = image.NewImageStore(ifs, d.layerStore)
   508  	if err != nil {
   509  		return nil, err
   510  	}
   511  
   512  	// Configure the volumes driver
   513  	volStore, err := d.configureVolumes(rootUID, rootGID)
   514  	if err != nil {
   515  		return nil, err
   516  	}
   517  
   518  	trustKey, err := api.LoadOrCreateTrustKey(config.TrustKeyPath)
   519  	if err != nil {
   520  		return nil, err
   521  	}
   522  
   523  	trustDir := filepath.Join(config.Root, "trust")
   524  
   525  	if err := system.MkdirAll(trustDir, 0700); err != nil {
   526  		return nil, err
   527  	}
   528  
   529  	distributionMetadataStore, err := dmetadata.NewFSMetadataStore(filepath.Join(imageRoot, "distribution"))
   530  	if err != nil {
   531  		return nil, err
   532  	}
   533  
   534  	eventsService := events.New()
   535  
   536  	referenceStore, err := reference.NewReferenceStore(filepath.Join(imageRoot, "repositories.json"))
   537  	if err != nil {
   538  		return nil, fmt.Errorf("Couldn't create Tag store repositories: %s", err)
   539  	}
   540  
   541  	if err := restoreCustomImage(d.imageStore, d.layerStore, referenceStore); err != nil {
   542  		return nil, fmt.Errorf("Couldn't restore custom images: %s", err)
   543  	}
   544  
   545  	migrationStart := time.Now()
   546  	if err := v1.Migrate(config.Root, graphDriver, d.layerStore, d.imageStore, referenceStore, distributionMetadataStore); err != nil {
   547  		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)
   548  	}
   549  	logrus.Infof("Graph migration to content-addressability took %.2f seconds", time.Since(migrationStart).Seconds())
   550  
   551  	// Discovery is only enabled when the daemon is launched with an address to advertise.  When
   552  	// initialized, the daemon is registered and we can store the discovery backend as its read-only
   553  	if err := d.initDiscovery(config); err != nil {
   554  		return nil, err
   555  	}
   556  
   557  	sysInfo := sysinfo.New(false)
   558  	// Check if Devices cgroup is mounted, it is hard requirement for container security,
   559  	// on Linux.
   560  	if runtime.GOOS == "linux" && !sysInfo.CgroupDevicesEnabled {
   561  		return nil, fmt.Errorf("Devices cgroup isn't mounted")
   562  	}
   563  
   564  	d.ID = trustKey.PublicKey().KeyID()
   565  	d.repository = daemonRepo
   566  	d.containers = container.NewMemoryStore()
   567  	d.execCommands = exec.NewStore()
   568  	d.referenceStore = referenceStore
   569  	d.distributionMetadataStore = distributionMetadataStore
   570  	d.trustKey = trustKey
   571  	d.idIndex = truncindex.NewTruncIndex([]string{})
   572  	d.statsCollector = d.newStatsCollector(1 * time.Second)
   573  	d.defaultLogConfig = containertypes.LogConfig{
   574  		Type:   config.LogConfig.Type,
   575  		Config: config.LogConfig.Config,
   576  	}
   577  	d.RegistryService = registryService
   578  	d.EventsService = eventsService
   579  	d.volumes = volStore
   580  	d.root = config.Root
   581  	d.uidMaps = uidMaps
   582  	d.gidMaps = gidMaps
   583  	d.seccompEnabled = sysInfo.Seccomp
   584  
   585  	d.nameIndex = registrar.NewRegistrar()
   586  	d.linkIndex = newLinkIndex()
   587  	d.containerdRemote = containerdRemote
   588  
   589  	go d.execCommandGC()
   590  
   591  	d.containerd, err = containerdRemote.Client(d)
   592  	if err != nil {
   593  		return nil, err
   594  	}
   595  
   596  	if err := d.restore(); err != nil {
   597  		return nil, err
   598  	}
   599  
   600  	return d, nil
   601  }
   602  
   603  func (daemon *Daemon) shutdownContainer(c *container.Container) error {
   604  	// TODO(windows): Handle docker restart with paused containers
   605  	if c.IsPaused() {
   606  		// To terminate a process in freezer cgroup, we should send
   607  		// SIGTERM to this process then unfreeze it, and the process will
   608  		// force to terminate immediately.
   609  		logrus.Debugf("Found container %s is paused, sending SIGTERM before unpause it", c.ID)
   610  		sig, ok := signal.SignalMap["TERM"]
   611  		if !ok {
   612  			return fmt.Errorf("System doesn not support SIGTERM")
   613  		}
   614  		if err := daemon.kill(c, int(sig)); err != nil {
   615  			return fmt.Errorf("sending SIGTERM to container %s with error: %v", c.ID, err)
   616  		}
   617  		if err := daemon.containerUnpause(c); err != nil {
   618  			return fmt.Errorf("Failed to unpause container %s with error: %v", c.ID, err)
   619  		}
   620  		if _, err := c.WaitStop(10 * time.Second); err != nil {
   621  			logrus.Debugf("container %s failed to exit in 10 second of SIGTERM, sending SIGKILL to force", c.ID)
   622  			sig, ok := signal.SignalMap["KILL"]
   623  			if !ok {
   624  				return fmt.Errorf("System does not support SIGKILL")
   625  			}
   626  			if err := daemon.kill(c, int(sig)); err != nil {
   627  				logrus.Errorf("Failed to SIGKILL container %s", c.ID)
   628  			}
   629  			c.WaitStop(-1 * time.Second)
   630  			return err
   631  		}
   632  	}
   633  	// If container failed to exit in 10 seconds of SIGTERM, then using the force
   634  	if err := daemon.containerStop(c, 10); err != nil {
   635  		return fmt.Errorf("Stop container %s with error: %v", c.ID, err)
   636  	}
   637  
   638  	c.WaitStop(-1 * time.Second)
   639  	return nil
   640  }
   641  
   642  // Shutdown stops the daemon.
   643  func (daemon *Daemon) Shutdown() error {
   644  	daemon.shutdown = true
   645  	// Keep mounts and networking running on daemon shutdown if
   646  	// we are to keep containers running and restore them.
   647  	if daemon.configStore.LiveRestore {
   648  		return nil
   649  	}
   650  	if daemon.containers != nil {
   651  		logrus.Debug("starting clean shutdown of all containers...")
   652  		daemon.containers.ApplyAll(func(c *container.Container) {
   653  			if !c.IsRunning() {
   654  				return
   655  			}
   656  			logrus.Debugf("stopping %s", c.ID)
   657  			if err := daemon.shutdownContainer(c); err != nil {
   658  				logrus.Errorf("Stop container error: %v", err)
   659  				return
   660  			}
   661  			if mountid, err := daemon.layerStore.GetMountID(c.ID); err == nil {
   662  				daemon.cleanupMountsByID(mountid)
   663  			}
   664  			logrus.Debugf("container stopped %s", c.ID)
   665  		})
   666  	}
   667  
   668  	// trigger libnetwork Stop only if it's initialized
   669  	if daemon.netController != nil {
   670  		daemon.netController.Stop()
   671  	}
   672  
   673  	if daemon.layerStore != nil {
   674  		if err := daemon.layerStore.Cleanup(); err != nil {
   675  			logrus.Errorf("Error during layer Store.Cleanup(): %v", err)
   676  		}
   677  	}
   678  
   679  	if err := daemon.cleanupMounts(); err != nil {
   680  		return err
   681  	}
   682  
   683  	return nil
   684  }
   685  
   686  // Mount sets container.BaseFS
   687  // (is it not set coming in? why is it unset?)
   688  func (daemon *Daemon) Mount(container *container.Container) error {
   689  	dir, err := container.RWLayer.Mount(container.GetMountLabel())
   690  	if err != nil {
   691  		return err
   692  	}
   693  	logrus.Debugf("container mounted via layerStore: %v", dir)
   694  
   695  	if container.BaseFS != dir {
   696  		// The mount path reported by the graph driver should always be trusted on Windows, since the
   697  		// volume path for a given mounted layer may change over time.  This should only be an error
   698  		// on non-Windows operating systems.
   699  		if container.BaseFS != "" && runtime.GOOS != "windows" {
   700  			daemon.Unmount(container)
   701  			return fmt.Errorf("Error: driver %s is returning inconsistent paths for container %s ('%s' then '%s')",
   702  				daemon.GraphDriverName(), container.ID, container.BaseFS, dir)
   703  		}
   704  	}
   705  	container.BaseFS = dir // TODO: combine these fields
   706  	return nil
   707  }
   708  
   709  // Unmount unsets the container base filesystem
   710  func (daemon *Daemon) Unmount(container *container.Container) error {
   711  	if err := container.RWLayer.Unmount(); err != nil {
   712  		logrus.Errorf("Error unmounting container %s: %s", container.ID, err)
   713  		return err
   714  	}
   715  	return nil
   716  }
   717  
   718  func writeDistributionProgress(cancelFunc func(), outStream io.Writer, progressChan <-chan progress.Progress) {
   719  	progressOutput := streamformatter.NewJSONStreamFormatter().NewProgressOutput(outStream, false)
   720  	operationCancelled := false
   721  
   722  	for prog := range progressChan {
   723  		if err := progressOutput.WriteProgress(prog); err != nil && !operationCancelled {
   724  			// don't log broken pipe errors as this is the normal case when a client aborts
   725  			if isBrokenPipe(err) {
   726  				logrus.Info("Pull session cancelled")
   727  			} else {
   728  				logrus.Errorf("error writing progress to client: %v", err)
   729  			}
   730  			cancelFunc()
   731  			operationCancelled = true
   732  			// Don't return, because we need to continue draining
   733  			// progressChan until it's closed to avoid a deadlock.
   734  		}
   735  	}
   736  }
   737  
   738  func isBrokenPipe(e error) bool {
   739  	if netErr, ok := e.(*net.OpError); ok {
   740  		e = netErr.Err
   741  		if sysErr, ok := netErr.Err.(*os.SyscallError); ok {
   742  			e = sysErr.Err
   743  		}
   744  	}
   745  	return e == syscall.EPIPE
   746  }
   747  
   748  // GraphDriverName returns the name of the graph driver used by the layer.Store
   749  func (daemon *Daemon) GraphDriverName() string {
   750  	return daemon.layerStore.DriverName()
   751  }
   752  
   753  // GetUIDGIDMaps returns the current daemon's user namespace settings
   754  // for the full uid and gid maps which will be applied to containers
   755  // started in this instance.
   756  func (daemon *Daemon) GetUIDGIDMaps() ([]idtools.IDMap, []idtools.IDMap) {
   757  	return daemon.uidMaps, daemon.gidMaps
   758  }
   759  
   760  // GetRemappedUIDGID returns the current daemon's uid and gid values
   761  // if user namespaces are in use for this daemon instance.  If not
   762  // this function will return "real" root values of 0, 0.
   763  func (daemon *Daemon) GetRemappedUIDGID() (int, int) {
   764  	uid, gid, _ := idtools.GetRootUIDGID(daemon.uidMaps, daemon.gidMaps)
   765  	return uid, gid
   766  }
   767  
   768  // tempDir returns the default directory to use for temporary files.
   769  func tempDir(rootDir string, rootUID, rootGID int) (string, error) {
   770  	var tmpDir string
   771  	if tmpDir = os.Getenv("DOCKER_TMPDIR"); tmpDir == "" {
   772  		tmpDir = filepath.Join(rootDir, "tmp")
   773  	}
   774  	return tmpDir, idtools.MkdirAllAs(tmpDir, 0700, rootUID, rootGID)
   775  }
   776  
   777  func (daemon *Daemon) setupInitLayer(initPath string) error {
   778  	rootUID, rootGID := daemon.GetRemappedUIDGID()
   779  	return setupInitLayer(initPath, rootUID, rootGID)
   780  }
   781  
   782  func setDefaultMtu(config *Config) {
   783  	// do nothing if the config does not have the default 0 value.
   784  	if config.Mtu != 0 {
   785  		return
   786  	}
   787  	config.Mtu = defaultNetworkMtu
   788  }
   789  
   790  func (daemon *Daemon) configureVolumes(rootUID, rootGID int) (*store.VolumeStore, error) {
   791  	volumesDriver, err := local.New(daemon.configStore.Root, rootUID, rootGID)
   792  	if err != nil {
   793  		return nil, err
   794  	}
   795  
   796  	if !volumedrivers.Register(volumesDriver, volumesDriver.Name()) {
   797  		return nil, fmt.Errorf("local volume driver could not be registered")
   798  	}
   799  	return store.New(daemon.configStore.Root)
   800  }
   801  
   802  // IsShuttingDown tells whether the daemon is shutting down or not
   803  func (daemon *Daemon) IsShuttingDown() bool {
   804  	return daemon.shutdown
   805  }
   806  
   807  // initDiscovery initializes the discovery watcher for this daemon.
   808  func (daemon *Daemon) initDiscovery(config *Config) error {
   809  	advertise, err := parseClusterAdvertiseSettings(config.ClusterStore, config.ClusterAdvertise)
   810  	if err != nil {
   811  		if err == errDiscoveryDisabled {
   812  			return nil
   813  		}
   814  		return err
   815  	}
   816  
   817  	config.ClusterAdvertise = advertise
   818  	discoveryWatcher, err := initDiscovery(config.ClusterStore, config.ClusterAdvertise, config.ClusterOpts)
   819  	if err != nil {
   820  		return fmt.Errorf("discovery initialization failed (%v)", err)
   821  	}
   822  
   823  	daemon.discoveryWatcher = discoveryWatcher
   824  	return nil
   825  }
   826  
   827  // Reload reads configuration changes and modifies the
   828  // daemon according to those changes.
   829  // These are the settings that Reload changes:
   830  // - Daemon labels.
   831  // - Daemon debug log level.
   832  // - Daemon max concurrent downloads
   833  // - Daemon max concurrent uploads
   834  // - Cluster discovery (reconfigure and restart).
   835  // - Daemon live restore
   836  func (daemon *Daemon) Reload(config *Config) error {
   837  	var err error
   838  	// used to hold reloaded changes
   839  	attributes := map[string]string{}
   840  
   841  	// We need defer here to ensure the lock is released as
   842  	// daemon.SystemInfo() will try to get it too
   843  	defer func() {
   844  		if err == nil {
   845  			daemon.LogDaemonEventWithAttributes("reload", attributes)
   846  		}
   847  	}()
   848  
   849  	daemon.configStore.reloadLock.Lock()
   850  	defer daemon.configStore.reloadLock.Unlock()
   851  
   852  	daemon.platformReload(config, &attributes)
   853  
   854  	if err = daemon.reloadClusterDiscovery(config); err != nil {
   855  		return err
   856  	}
   857  
   858  	if config.IsValueSet("labels") {
   859  		daemon.configStore.Labels = config.Labels
   860  	}
   861  	if config.IsValueSet("debug") {
   862  		daemon.configStore.Debug = config.Debug
   863  	}
   864  	if config.IsValueSet("live-restore") {
   865  		daemon.configStore.LiveRestore = config.LiveRestore
   866  		if err := daemon.containerdRemote.UpdateOptions(libcontainerd.WithLiveRestore(config.LiveRestore)); err != nil {
   867  			return err
   868  		}
   869  
   870  	}
   871  
   872  	// If no value is set for max-concurrent-downloads we assume it is the default value
   873  	// We always "reset" as the cost is lightweight and easy to maintain.
   874  	if config.IsValueSet("max-concurrent-downloads") && config.MaxConcurrentDownloads != nil {
   875  		*daemon.configStore.MaxConcurrentDownloads = *config.MaxConcurrentDownloads
   876  	} else {
   877  		maxConcurrentDownloads := defaultMaxConcurrentDownloads
   878  		daemon.configStore.MaxConcurrentDownloads = &maxConcurrentDownloads
   879  	}
   880  	logrus.Debugf("Reset Max Concurrent Downloads: %d", *daemon.configStore.MaxConcurrentDownloads)
   881  	if daemon.downloadManager != nil {
   882  		daemon.downloadManager.SetConcurrency(*daemon.configStore.MaxConcurrentDownloads)
   883  	}
   884  
   885  	// If no value is set for max-concurrent-upload we assume it is the default value
   886  	// We always "reset" as the cost is lightweight and easy to maintain.
   887  	if config.IsValueSet("max-concurrent-uploads") && config.MaxConcurrentUploads != nil {
   888  		*daemon.configStore.MaxConcurrentUploads = *config.MaxConcurrentUploads
   889  	} else {
   890  		maxConcurrentUploads := defaultMaxConcurrentUploads
   891  		daemon.configStore.MaxConcurrentUploads = &maxConcurrentUploads
   892  	}
   893  	logrus.Debugf("Reset Max Concurrent Uploads: %d", *daemon.configStore.MaxConcurrentUploads)
   894  	if daemon.uploadManager != nil {
   895  		daemon.uploadManager.SetConcurrency(*daemon.configStore.MaxConcurrentUploads)
   896  	}
   897  
   898  	// We emit daemon reload event here with updatable configurations
   899  	attributes["debug"] = fmt.Sprintf("%t", daemon.configStore.Debug)
   900  	attributes["cluster-store"] = daemon.configStore.ClusterStore
   901  	if daemon.configStore.ClusterOpts != nil {
   902  		opts, _ := json.Marshal(daemon.configStore.ClusterOpts)
   903  		attributes["cluster-store-opts"] = string(opts)
   904  	} else {
   905  		attributes["cluster-store-opts"] = "{}"
   906  	}
   907  	attributes["cluster-advertise"] = daemon.configStore.ClusterAdvertise
   908  	if daemon.configStore.Labels != nil {
   909  		labels, _ := json.Marshal(daemon.configStore.Labels)
   910  		attributes["labels"] = string(labels)
   911  	} else {
   912  		attributes["labels"] = "[]"
   913  	}
   914  	attributes["max-concurrent-downloads"] = fmt.Sprintf("%d", *daemon.configStore.MaxConcurrentDownloads)
   915  	attributes["max-concurrent-uploads"] = fmt.Sprintf("%d", *daemon.configStore.MaxConcurrentUploads)
   916  
   917  	return nil
   918  }
   919  
   920  func (daemon *Daemon) reloadClusterDiscovery(config *Config) error {
   921  	var err error
   922  	newAdvertise := daemon.configStore.ClusterAdvertise
   923  	newClusterStore := daemon.configStore.ClusterStore
   924  	if config.IsValueSet("cluster-advertise") {
   925  		if config.IsValueSet("cluster-store") {
   926  			newClusterStore = config.ClusterStore
   927  		}
   928  		newAdvertise, err = parseClusterAdvertiseSettings(newClusterStore, config.ClusterAdvertise)
   929  		if err != nil && err != errDiscoveryDisabled {
   930  			return err
   931  		}
   932  	}
   933  
   934  	if daemon.clusterProvider != nil {
   935  		if err := config.isSwarmCompatible(); err != nil {
   936  			return err
   937  		}
   938  	}
   939  
   940  	// check discovery modifications
   941  	if !modifiedDiscoverySettings(daemon.configStore, newAdvertise, newClusterStore, config.ClusterOpts) {
   942  		return nil
   943  	}
   944  
   945  	// enable discovery for the first time if it was not previously enabled
   946  	if daemon.discoveryWatcher == nil {
   947  		discoveryWatcher, err := initDiscovery(newClusterStore, newAdvertise, config.ClusterOpts)
   948  		if err != nil {
   949  			return fmt.Errorf("discovery initialization failed (%v)", err)
   950  		}
   951  		daemon.discoveryWatcher = discoveryWatcher
   952  	} else {
   953  		if err == errDiscoveryDisabled {
   954  			// disable discovery if it was previously enabled and it's disabled now
   955  			daemon.discoveryWatcher.Stop()
   956  		} else {
   957  			// reload discovery
   958  			if err = daemon.discoveryWatcher.Reload(config.ClusterStore, newAdvertise, config.ClusterOpts); err != nil {
   959  				return err
   960  			}
   961  		}
   962  	}
   963  
   964  	daemon.configStore.ClusterStore = newClusterStore
   965  	daemon.configStore.ClusterOpts = config.ClusterOpts
   966  	daemon.configStore.ClusterAdvertise = newAdvertise
   967  
   968  	if daemon.netController == nil {
   969  		return nil
   970  	}
   971  	netOptions, err := daemon.networkOptions(daemon.configStore, nil)
   972  	if err != nil {
   973  		logrus.Warnf("Failed to reload configuration with network controller: %v", err)
   974  		return nil
   975  	}
   976  	err = daemon.netController.ReloadConfiguration(netOptions...)
   977  	if err != nil {
   978  		logrus.Warnf("Failed to reload configuration with network controller: %v", err)
   979  	}
   980  
   981  	return nil
   982  }
   983  
   984  func isBridgeNetworkDisabled(config *Config) bool {
   985  	return config.bridgeConfig.Iface == disableNetworkBridge
   986  }
   987  
   988  func (daemon *Daemon) networkOptions(dconfig *Config, activeSandboxes map[string]interface{}) ([]nwconfig.Option, error) {
   989  	options := []nwconfig.Option{}
   990  	if dconfig == nil {
   991  		return options, nil
   992  	}
   993  
   994  	options = append(options, nwconfig.OptionDataDir(dconfig.Root))
   995  
   996  	dd := runconfig.DefaultDaemonNetworkMode()
   997  	dn := runconfig.DefaultDaemonNetworkMode().NetworkName()
   998  	options = append(options, nwconfig.OptionDefaultDriver(string(dd)))
   999  	options = append(options, nwconfig.OptionDefaultNetwork(dn))
  1000  
  1001  	if strings.TrimSpace(dconfig.ClusterStore) != "" {
  1002  		kv := strings.Split(dconfig.ClusterStore, "://")
  1003  		if len(kv) != 2 {
  1004  			return nil, fmt.Errorf("kv store daemon config must be of the form KV-PROVIDER://KV-URL")
  1005  		}
  1006  		options = append(options, nwconfig.OptionKVProvider(kv[0]))
  1007  		options = append(options, nwconfig.OptionKVProviderURL(kv[1]))
  1008  	}
  1009  	if len(dconfig.ClusterOpts) > 0 {
  1010  		options = append(options, nwconfig.OptionKVOpts(dconfig.ClusterOpts))
  1011  	}
  1012  
  1013  	if daemon.discoveryWatcher != nil {
  1014  		options = append(options, nwconfig.OptionDiscoveryWatcher(daemon.discoveryWatcher))
  1015  	}
  1016  
  1017  	if dconfig.ClusterAdvertise != "" {
  1018  		options = append(options, nwconfig.OptionDiscoveryAddress(dconfig.ClusterAdvertise))
  1019  	}
  1020  
  1021  	options = append(options, nwconfig.OptionLabels(dconfig.Labels))
  1022  	options = append(options, driverOptions(dconfig)...)
  1023  
  1024  	if daemon.configStore != nil && daemon.configStore.LiveRestore && len(activeSandboxes) != 0 {
  1025  		options = append(options, nwconfig.OptionActiveSandboxes(activeSandboxes))
  1026  	}
  1027  
  1028  	return options, nil
  1029  }
  1030  
  1031  func copyBlkioEntry(entries []*containerd.BlkioStatsEntry) []types.BlkioStatEntry {
  1032  	out := make([]types.BlkioStatEntry, len(entries))
  1033  	for i, re := range entries {
  1034  		out[i] = types.BlkioStatEntry{
  1035  			Major: re.Major,
  1036  			Minor: re.Minor,
  1037  			Op:    re.Op,
  1038  			Value: re.Value,
  1039  		}
  1040  	}
  1041  	return out
  1042  }