github.com/brahmaroutu/docker@v1.2.1-0.20160809185609-eb28dde01f16/daemon/oci_windows.go (about)

     1  package daemon
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  	"syscall"
     9  
    10  	"github.com/docker/docker/container"
    11  	"github.com/docker/docker/image"
    12  	"github.com/docker/docker/layer"
    13  	"github.com/docker/docker/libcontainerd"
    14  	"github.com/docker/docker/libcontainerd/windowsoci"
    15  	"github.com/docker/docker/oci"
    16  )
    17  
    18  func (daemon *Daemon) createSpec(c *container.Container) (*libcontainerd.Spec, error) {
    19  	s := oci.DefaultSpec()
    20  
    21  	linkedEnv, err := daemon.setupLinkedContainers(c)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  
    26  	// TODO Windows - this can be removed. Not used (UID/GID)
    27  	rootUID, rootGID := daemon.GetRemappedUIDGID()
    28  	if err := c.SetupWorkingDirectory(rootUID, rootGID); err != nil {
    29  		return nil, err
    30  	}
    31  
    32  	img, err := daemon.imageStore.Get(c.ImageID)
    33  	if err != nil {
    34  		return nil, fmt.Errorf("Failed to graph.Get on ImageID %s - %s", c.ImageID, err)
    35  	}
    36  
    37  	s.Platform.OSVersion = img.OSVersion
    38  
    39  	// In base spec
    40  	s.Hostname = c.FullHostname()
    41  
    42  	// In s.Mounts
    43  	mounts, err := daemon.setupMounts(c)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	for _, mount := range mounts {
    48  		s.Mounts = append(s.Mounts, windowsoci.Mount{
    49  			Source:      mount.Source,
    50  			Destination: mount.Destination,
    51  			Readonly:    !mount.Writable,
    52  		})
    53  	}
    54  
    55  	// In s.Process
    56  	s.Process.Args = append([]string{c.Path}, c.Args...)
    57  	if !c.Config.ArgsEscaped {
    58  		s.Process.Args = escapeArgs(s.Process.Args)
    59  	}
    60  	s.Process.Cwd = c.Config.WorkingDir
    61  	if len(s.Process.Cwd) == 0 {
    62  		// We default to C:\ to workaround the oddity of the case that the
    63  		// default directory for cmd running as LocalSystem (or
    64  		// ContainerAdministrator) is c:\windows\system32. Hence docker run
    65  		// <image> cmd will by default end in c:\windows\system32, rather
    66  		// than 'root' (/) on Linux. The oddity is that if you have a dockerfile
    67  		// which has no WORKDIR and has a COPY file ., . will be interpreted
    68  		// as c:\. Hence, setting it to default of c:\ makes for consistency.
    69  		s.Process.Cwd = `C:\`
    70  	}
    71  	s.Process.Env = c.CreateDaemonEnvironment(linkedEnv)
    72  	s.Process.InitialConsoleSize = c.HostConfig.ConsoleSize
    73  	s.Process.Terminal = c.Config.Tty
    74  	s.Process.User.User = c.Config.User
    75  
    76  	// In spec.Root
    77  	s.Root.Path = c.BaseFS
    78  	s.Root.Readonly = c.HostConfig.ReadonlyRootfs
    79  
    80  	// In s.Windows
    81  	s.Windows.FirstStart = !c.HasBeenStartedBefore
    82  
    83  	// s.Windows.LayerFolder.
    84  	m, err := c.RWLayer.Metadata()
    85  	if err != nil {
    86  		return nil, fmt.Errorf("Failed to get layer metadata - %s", err)
    87  	}
    88  	s.Windows.LayerFolder = m["dir"]
    89  
    90  	// s.Windows.LayerPaths
    91  	var layerPaths []string
    92  	if img.RootFS != nil && img.RootFS.Type == image.TypeLayers {
    93  		// Get the layer path for each layer.
    94  		max := len(img.RootFS.DiffIDs)
    95  		for i := 1; i <= max; i++ {
    96  			img.RootFS.DiffIDs = img.RootFS.DiffIDs[:i]
    97  			path, err := layer.GetLayerPath(daemon.layerStore, img.RootFS.ChainID())
    98  			if err != nil {
    99  				return nil, fmt.Errorf("Failed to get layer path from graphdriver %s for ImageID %s - %s", daemon.layerStore, img.RootFS.ChainID(), err)
   100  			}
   101  			// Reverse order, expecting parent most first
   102  			layerPaths = append([]string{path}, layerPaths...)
   103  		}
   104  	}
   105  	s.Windows.LayerPaths = layerPaths
   106  
   107  	// Are we going to run as a Hyper-V container?
   108  	hv := false
   109  	if c.HostConfig.Isolation.IsDefault() {
   110  		// Container is set to use the default, so take the default from the daemon configuration
   111  		hv = daemon.defaultIsolation.IsHyperV()
   112  	} else {
   113  		// Container is requesting an isolation mode. Honour it.
   114  		hv = c.HostConfig.Isolation.IsHyperV()
   115  	}
   116  	if hv {
   117  		hvr := &windowsoci.HvRuntime{}
   118  		if img.RootFS != nil && img.RootFS.Type == image.TypeLayers {
   119  			// For TP5, the utility VM is part of the base layer.
   120  			// TODO-jstarks: Add support for separate utility VM images
   121  			// once it is decided how they can be stored.
   122  			uvmpath := filepath.Join(layerPaths[len(layerPaths)-1], "UtilityVM")
   123  			_, err = os.Stat(uvmpath)
   124  			if err != nil {
   125  				if os.IsNotExist(err) {
   126  					err = errors.New("container image does not contain a utility VM")
   127  				}
   128  				return nil, err
   129  			}
   130  
   131  			hvr.ImagePath = uvmpath
   132  		}
   133  
   134  		s.Windows.HvRuntime = hvr
   135  	}
   136  
   137  	// In s.Windows.Networking
   138  	// Connect all the libnetwork allocated networks to the container
   139  	var epList []string
   140  	if c.NetworkSettings != nil {
   141  		for n := range c.NetworkSettings.Networks {
   142  			sn, err := daemon.FindNetwork(n)
   143  			if err != nil {
   144  				continue
   145  			}
   146  
   147  			ep, err := c.GetEndpointInNetwork(sn)
   148  			if err != nil {
   149  				continue
   150  			}
   151  
   152  			data, err := ep.DriverInfo()
   153  			if err != nil {
   154  				continue
   155  			}
   156  			if data["hnsid"] != nil {
   157  				epList = append(epList, data["hnsid"].(string))
   158  			}
   159  		}
   160  	}
   161  	s.Windows.Networking = &windowsoci.Networking{
   162  		EndpointList: epList,
   163  	}
   164  
   165  	// In s.Windows.Resources
   166  	// @darrenstahlmsft implement these resources
   167  	cpuShares := uint64(c.HostConfig.CPUShares)
   168  	s.Windows.Resources = &windowsoci.Resources{
   169  		CPU: &windowsoci.CPU{
   170  			Percent: &c.HostConfig.CPUPercent,
   171  			Shares:  &cpuShares,
   172  		},
   173  		Memory: &windowsoci.Memory{
   174  			Limit: &c.HostConfig.Memory,
   175  			//TODO Reservation: ...,
   176  		},
   177  		Network: &windowsoci.Network{
   178  		//TODO Bandwidth: ...,
   179  		},
   180  		Storage: &windowsoci.Storage{
   181  			Bps:  &c.HostConfig.IOMaximumBandwidth,
   182  			Iops: &c.HostConfig.IOMaximumIOps,
   183  		},
   184  	}
   185  	return (*libcontainerd.Spec)(&s), nil
   186  }
   187  
   188  func escapeArgs(args []string) []string {
   189  	escapedArgs := make([]string, len(args))
   190  	for i, a := range args {
   191  		escapedArgs[i] = syscall.EscapeArg(a)
   192  	}
   193  	return escapedArgs
   194  }