@@ -37,6 +37,7 @@ import (
3737 "github.com/docker/docker/pkg/discovery"
3838 "github.com/docker/docker/pkg/fileutils"
3939 "github.com/docker/docker/pkg/graphdb"
40+ "github.com/docker/docker/pkg/idtools"
4041 "github.com/docker/docker/pkg/ioutils"
4142 "github.com/docker/docker/pkg/namesgenerator"
4243 "github.com/docker/docker/pkg/nat"
@@ -121,6 +122,8 @@ type Daemon struct {
121122 discoveryWatcher discovery.Watcher
122123 root string
123124 shutdown bool
125+ uidMaps []idtools.IDMap
126+ gidMaps []idtools.IDMap
124127}
125128
126129// Get looks for a container using the provided information, which could be
@@ -632,6 +635,15 @@ func NewDaemon(config *Config, registryService *registry.Service) (daemon *Daemo
632635 // on Windows to dump Go routine stacks
633636 setupDumpStackTrap ()
634637
638+ uidMaps , gidMaps , err := setupRemappedRoot (config )
639+ if err != nil {
640+ return nil , err
641+ }
642+ rootUID , rootGID , err := idtools .GetRootUIDGID (uidMaps , gidMaps )
643+ if err != nil {
644+ return nil , err
645+ }
646+
635647 // get the canonical path to the Docker root directory
636648 var realRoot string
637649 if _ , err := os .Stat (config .Root ); err != nil && os .IsNotExist (err ) {
@@ -642,14 +654,13 @@ func NewDaemon(config *Config, registryService *registry.Service) (daemon *Daemo
642654 return nil , fmt .Errorf ("Unable to get the full path to root (%s): %s" , config .Root , err )
643655 }
644656 }
645- config .Root = realRoot
646- // Create the root directory if it doesn't exists
647- if err := system .MkdirAll (config .Root , 0700 ); err != nil {
657+
658+ if err = setupDaemonRoot (config , realRoot , rootUID , rootGID ); err != nil {
648659 return nil , err
649660 }
650661
651662 // set up the tmpDir to use a canonical path
652- tmp , err := tempDir (config .Root )
663+ tmp , err := tempDir (config .Root , rootUID , rootGID )
653664 if err != nil {
654665 return nil , fmt .Errorf ("Unable to get the TempDir under %s: %s" , config .Root , err )
655666 }
@@ -663,7 +674,7 @@ func NewDaemon(config *Config, registryService *registry.Service) (daemon *Daemo
663674 graphdriver .DefaultDriver = config .GraphDriver
664675
665676 // Load storage driver
666- driver , err := graphdriver .New (config .Root , config .GraphOptions )
677+ driver , err := graphdriver .New (config .Root , config .GraphOptions , uidMaps , gidMaps )
667678 if err != nil {
668679 return nil , fmt .Errorf ("error initializing graphdriver: %v" , err )
669680 }
@@ -696,7 +707,7 @@ func NewDaemon(config *Config, registryService *registry.Service) (daemon *Daemo
696707
697708 daemonRepo := filepath .Join (config .Root , "containers" )
698709
699- if err := system . MkdirAll (daemonRepo , 0700 ); err != nil {
710+ if err := idtools . MkdirAllAs (daemonRepo , 0700 , rootUID , rootGID ); err != nil && ! os . IsExist ( err ) {
700711 return nil , err
701712 }
702713
@@ -706,13 +717,13 @@ func NewDaemon(config *Config, registryService *registry.Service) (daemon *Daemo
706717 }
707718
708719 logrus .Debug ("Creating images graph" )
709- g , err := graph .NewGraph (filepath .Join (config .Root , "graph" ), d .driver )
720+ g , err := graph .NewGraph (filepath .Join (config .Root , "graph" ), d .driver , uidMaps , gidMaps )
710721 if err != nil {
711722 return nil , err
712723 }
713724
714725 // Configure the volumes driver
715- volStore , err := configureVolumes (config )
726+ volStore , err := configureVolumes (config , rootUID , rootGID )
716727 if err != nil {
717728 return nil , err
718729 }
@@ -777,7 +788,7 @@ func NewDaemon(config *Config, registryService *registry.Service) (daemon *Daemo
777788
778789 var sysInitPath string
779790 if config .ExecDriver == "lxc" {
780- initPath , err := configureSysInit (config )
791+ initPath , err := configureSysInit (config , rootUID , rootGID )
781792 if err != nil {
782793 return nil , err
783794 }
@@ -812,6 +823,8 @@ func NewDaemon(config *Config, registryService *registry.Service) (daemon *Daemo
812823 d .EventsService = eventsService
813824 d .volumes = volStore
814825 d .root = config .Root
826+ d .uidMaps = uidMaps
827+ d .gidMaps = gidMaps
815828
816829 if err := d .cleanupMounts (); err != nil {
817830 return nil , err
@@ -974,7 +987,11 @@ func (daemon *Daemon) diff(container *Container) (archive.Archive, error) {
974987func (daemon * Daemon ) createRootfs (container * Container ) error {
975988 // Step 1: create the container directory.
976989 // This doubles as a barrier to avoid race conditions.
977- if err := os .Mkdir (container .root , 0700 ); err != nil {
990+ rootUID , rootGID , err := idtools .GetRootUIDGID (daemon .uidMaps , daemon .gidMaps )
991+ if err != nil {
992+ return err
993+ }
994+ if err := idtools .MkdirAs (container .root , 0700 , rootUID , rootGID ); err != nil {
978995 return err
979996 }
980997 initID := fmt .Sprintf ("%s-init" , container .ID )
@@ -986,7 +1003,7 @@ func (daemon *Daemon) createRootfs(container *Container) error {
9861003 return err
9871004 }
9881005
989- if err := setupInitLayer (initPath ); err != nil {
1006+ if err := setupInitLayer (initPath , rootUID , rootGID ); err != nil {
9901007 daemon .driver .Put (initID )
9911008 return err
9921009 }
@@ -1105,6 +1122,21 @@ func (daemon *Daemon) containerGraph() *graphdb.Database {
11051122 return daemon .containerGraphDB
11061123}
11071124
1125+ // GetUIDGIDMaps returns the current daemon's user namespace settings
1126+ // for the full uid and gid maps which will be applied to containers
1127+ // started in this instance.
1128+ func (daemon * Daemon ) GetUIDGIDMaps () ([]idtools.IDMap , []idtools.IDMap ) {
1129+ return daemon .uidMaps , daemon .gidMaps
1130+ }
1131+
1132+ // GetRemappedUIDGID returns the current daemon's uid and gid values
1133+ // if user namespaces are in use for this daemon instance. If not
1134+ // this function will return "real" root values of 0, 0.
1135+ func (daemon * Daemon ) GetRemappedUIDGID () (int , int ) {
1136+ uid , gid , _ := idtools .GetRootUIDGID (daemon .uidMaps , daemon .gidMaps )
1137+ return uid , gid
1138+ }
1139+
11081140// ImageGetCached returns the earliest created image that is a child
11091141// of the image with imgID, that had the same config when it was
11101142// created. nil is returned if a child cannot be found. An error is
@@ -1139,12 +1171,12 @@ func (daemon *Daemon) ImageGetCached(imgID string, config *runconfig.Config) (*i
11391171}
11401172
11411173// tempDir returns the default directory to use for temporary files.
1142- func tempDir (rootDir string ) (string , error ) {
1174+ func tempDir (rootDir string , rootUID , rootGID int ) (string , error ) {
11431175 var tmpDir string
11441176 if tmpDir = os .Getenv ("DOCKER_TMPDIR" ); tmpDir == "" {
11451177 tmpDir = filepath .Join (rootDir , "tmp" )
11461178 }
1147- return tmpDir , system . MkdirAll (tmpDir , 0700 )
1179+ return tmpDir , idtools . MkdirAllAs (tmpDir , 0700 , rootUID , rootGID )
11481180}
11491181
11501182func (daemon * Daemon ) setHostConfig (container * Container , hostConfig * runconfig.HostConfig ) error {
@@ -1228,8 +1260,8 @@ func (daemon *Daemon) verifyContainerSettings(hostConfig *runconfig.HostConfig,
12281260 return verifyPlatformContainerSettings (daemon , hostConfig , config )
12291261}
12301262
1231- func configureVolumes (config * Config ) (* store.VolumeStore , error ) {
1232- volumesDriver , err := local .New (config .Root )
1263+ func configureVolumes (config * Config , rootUID , rootGID int ) (* store.VolumeStore , error ) {
1264+ volumesDriver , err := local .New (config .Root , rootUID , rootGID )
12331265 if err != nil {
12341266 return nil , err
12351267 }
0 commit comments