client_windows.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. package libcontainerd
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "path/filepath"
  7. "strings"
  8. "syscall"
  9. "github.com/Microsoft/hcsshim"
  10. "github.com/Sirupsen/logrus"
  11. )
  12. type client struct {
  13. clientCommon
  14. // Platform specific properties below here (none presently on Windows)
  15. }
  16. // Win32 error codes that are used for various workarounds
  17. // These really should be ALL_CAPS to match golangs syscall library and standard
  18. // Win32 error conventions, but golint insists on CamelCase.
  19. const (
  20. CoEClassstring = syscall.Errno(0x800401F3) // Invalid class string
  21. ErrorNoNetwork = syscall.Errno(1222) // The network is not present or not started
  22. ErrorBadPathname = syscall.Errno(161) // The specified path is invalid
  23. ErrorInvalidObject = syscall.Errno(0x800710D8) // The object identifier does not represent a valid object
  24. )
  25. // defaultOwner is a tag passed to HCS to allow it to differentiate between
  26. // container creator management stacks. We hard code "docker" in the case
  27. // of docker.
  28. const defaultOwner = "docker"
  29. // Create is the entrypoint to create a container from a spec, and if successfully
  30. // created, start it too.
  31. func (clnt *client) Create(containerID string, spec Spec, options ...CreateOption) error {
  32. logrus.Debugln("LCD client.Create() with spec", spec)
  33. configuration := &hcsshim.ContainerConfig{
  34. SystemType: "Container",
  35. Name: containerID,
  36. Owner: defaultOwner,
  37. VolumePath: spec.Root.Path,
  38. IgnoreFlushesDuringBoot: spec.Windows.FirstStart,
  39. LayerFolderPath: spec.Windows.LayerFolder,
  40. HostName: spec.Hostname,
  41. }
  42. if spec.Windows.Networking != nil {
  43. configuration.EndpointList = spec.Windows.Networking.EndpointList
  44. }
  45. if spec.Windows.Resources != nil {
  46. if spec.Windows.Resources.CPU != nil {
  47. if spec.Windows.Resources.CPU.Shares != nil {
  48. configuration.ProcessorWeight = *spec.Windows.Resources.CPU.Shares
  49. }
  50. if spec.Windows.Resources.CPU.Percent != nil {
  51. configuration.ProcessorMaximum = *spec.Windows.Resources.CPU.Percent * 100 // ProcessorMaximum is a value between 1 and 10000
  52. }
  53. }
  54. if spec.Windows.Resources.Memory != nil {
  55. if spec.Windows.Resources.Memory.Limit != nil {
  56. configuration.MemoryMaximumInMB = *spec.Windows.Resources.Memory.Limit / 1024 / 1024
  57. }
  58. }
  59. if spec.Windows.Resources.Storage != nil {
  60. if spec.Windows.Resources.Storage.Bps != nil {
  61. configuration.StorageBandwidthMaximum = *spec.Windows.Resources.Storage.Bps
  62. }
  63. if spec.Windows.Resources.Storage.Iops != nil {
  64. configuration.StorageIOPSMaximum = *spec.Windows.Resources.Storage.Iops
  65. }
  66. if spec.Windows.Resources.Storage.SandboxSize != nil {
  67. configuration.StorageSandboxSize = *spec.Windows.Resources.Storage.SandboxSize
  68. }
  69. }
  70. }
  71. if spec.Windows.HvRuntime != nil {
  72. configuration.HvPartition = true
  73. configuration.HvRuntime = &hcsshim.HvRuntime{
  74. ImagePath: spec.Windows.HvRuntime.ImagePath,
  75. }
  76. }
  77. if configuration.HvPartition {
  78. configuration.SandboxPath = filepath.Dir(spec.Windows.LayerFolder)
  79. } else {
  80. configuration.VolumePath = spec.Root.Path
  81. configuration.LayerFolderPath = spec.Windows.LayerFolder
  82. }
  83. for _, option := range options {
  84. if s, ok := option.(*ServicingOption); ok {
  85. configuration.Servicing = s.IsServicing
  86. break
  87. }
  88. }
  89. for _, layerPath := range spec.Windows.LayerPaths {
  90. _, filename := filepath.Split(layerPath)
  91. g, err := hcsshim.NameToGuid(filename)
  92. if err != nil {
  93. return err
  94. }
  95. configuration.Layers = append(configuration.Layers, hcsshim.Layer{
  96. ID: g.ToString(),
  97. Path: layerPath,
  98. })
  99. }
  100. // Add the mounts (volumes, bind mounts etc) to the structure
  101. mds := make([]hcsshim.MappedDir, len(spec.Mounts))
  102. for i, mount := range spec.Mounts {
  103. mds[i] = hcsshim.MappedDir{
  104. HostPath: mount.Source,
  105. ContainerPath: mount.Destination,
  106. ReadOnly: mount.Readonly}
  107. }
  108. configuration.MappedDirectories = mds
  109. hcsContainer, err := hcsshim.CreateContainer(containerID, configuration)
  110. if err != nil {
  111. return err
  112. }
  113. // Construct a container object for calling start on it.
  114. container := &container{
  115. containerCommon: containerCommon{
  116. process: process{
  117. processCommon: processCommon{
  118. containerID: containerID,
  119. client: clnt,
  120. friendlyName: InitFriendlyName,
  121. },
  122. commandLine: strings.Join(spec.Process.Args, " "),
  123. },
  124. processes: make(map[string]*process),
  125. },
  126. ociSpec: spec,
  127. hcsContainer: hcsContainer,
  128. }
  129. container.options = options
  130. for _, option := range options {
  131. if err := option.Apply(container); err != nil {
  132. logrus.Error(err)
  133. }
  134. }
  135. // Call start, and if it fails, delete the container from our
  136. // internal structure, start will keep HCS in sync by deleting the
  137. // container there.
  138. logrus.Debugf("Create() id=%s, Calling start()", containerID)
  139. if err := container.start(); err != nil {
  140. clnt.deleteContainer(containerID)
  141. return err
  142. }
  143. logrus.Debugf("Create() id=%s completed successfully", containerID)
  144. return nil
  145. }
  146. // AddProcess is the handler for adding a process to an already running
  147. // container. It's called through docker exec.
  148. func (clnt *client) AddProcess(containerID, processFriendlyName string, procToAdd Process) error {
  149. clnt.lock(containerID)
  150. defer clnt.unlock(containerID)
  151. container, err := clnt.getContainer(containerID)
  152. if err != nil {
  153. return err
  154. }
  155. // Note we always tell HCS to
  156. // create stdout as it's required regardless of '-i' or '-t' options, so that
  157. // docker can always grab the output through logs. We also tell HCS to always
  158. // create stdin, even if it's not used - it will be closed shortly. Stderr
  159. // is only created if it we're not -t.
  160. createProcessParms := hcsshim.ProcessConfig{
  161. EmulateConsole: procToAdd.Terminal,
  162. ConsoleSize: procToAdd.InitialConsoleSize,
  163. CreateStdInPipe: true,
  164. CreateStdOutPipe: true,
  165. CreateStdErrPipe: !procToAdd.Terminal,
  166. }
  167. // Take working directory from the process to add if it is defined,
  168. // otherwise take from the first process.
  169. if procToAdd.Cwd != "" {
  170. createProcessParms.WorkingDirectory = procToAdd.Cwd
  171. } else {
  172. createProcessParms.WorkingDirectory = container.ociSpec.Process.Cwd
  173. }
  174. // Configure the environment for the process
  175. createProcessParms.Environment = setupEnvironmentVariables(procToAdd.Env)
  176. createProcessParms.CommandLine = strings.Join(procToAdd.Args, " ")
  177. logrus.Debugf("commandLine: %s", createProcessParms.CommandLine)
  178. // Start the command running in the container.
  179. var stdout, stderr io.ReadCloser
  180. var stdin io.WriteCloser
  181. newProcess, err := container.hcsContainer.CreateProcess(&createProcessParms)
  182. if err != nil {
  183. logrus.Errorf("AddProcess %s CreateProcess() failed %s", containerID, err)
  184. return err
  185. }
  186. stdin, stdout, stderr, err = newProcess.Stdio()
  187. if err != nil {
  188. logrus.Errorf("%s getting std pipes failed %s", containerID, err)
  189. return err
  190. }
  191. iopipe := &IOPipe{Terminal: procToAdd.Terminal}
  192. iopipe.Stdin = createStdInCloser(stdin, newProcess)
  193. // TEMP: Work around Windows BS/DEL behavior.
  194. iopipe.Stdin = fixStdinBackspaceBehavior(iopipe.Stdin, container.ociSpec.Platform.OSVersion, procToAdd.Terminal)
  195. // Convert io.ReadClosers to io.Readers
  196. if stdout != nil {
  197. iopipe.Stdout = openReaderFromPipe(stdout)
  198. }
  199. if stderr != nil {
  200. iopipe.Stderr = openReaderFromPipe(stderr)
  201. }
  202. pid := newProcess.Pid()
  203. proc := &process{
  204. processCommon: processCommon{
  205. containerID: containerID,
  206. friendlyName: processFriendlyName,
  207. client: clnt,
  208. systemPid: uint32(pid),
  209. },
  210. commandLine: createProcessParms.CommandLine,
  211. hcsProcess: newProcess,
  212. }
  213. // Add the process to the container's list of processes
  214. container.processes[processFriendlyName] = proc
  215. // Make sure the lock is not held while calling back into the daemon
  216. clnt.unlock(containerID)
  217. // Tell the engine to attach streams back to the client
  218. if err := clnt.backend.AttachStreams(processFriendlyName, *iopipe); err != nil {
  219. return err
  220. }
  221. // Lock again so that the defer unlock doesn't fail. (I really don't like this code)
  222. clnt.lock(containerID)
  223. // Spin up a go routine waiting for exit to handle cleanup
  224. go container.waitExit(proc, false)
  225. return nil
  226. }
  227. // Signal handles `docker stop` on Windows. While Linux has support for
  228. // the full range of signals, signals aren't really implemented on Windows.
  229. // We fake supporting regular stop and -9 to force kill.
  230. func (clnt *client) Signal(containerID string, sig int) error {
  231. var (
  232. cont *container
  233. err error
  234. )
  235. // Get the container as we need it to find the pid of the process.
  236. clnt.lock(containerID)
  237. defer clnt.unlock(containerID)
  238. if cont, err = clnt.getContainer(containerID); err != nil {
  239. return err
  240. }
  241. cont.manualStopRequested = true
  242. logrus.Debugf("lcd: Signal() containerID=%s sig=%d pid=%d", containerID, sig, cont.systemPid)
  243. if syscall.Signal(sig) == syscall.SIGKILL {
  244. // Terminate the compute system
  245. if err := cont.hcsContainer.Terminate(); err != nil {
  246. if err != hcsshim.ErrVmcomputeOperationPending {
  247. logrus.Errorf("Failed to terminate %s - %q", containerID, err)
  248. }
  249. }
  250. } else {
  251. // Terminate Process
  252. if err := cont.hcsProcess.Kill(); err != nil {
  253. logrus.Warnf("Failed to terminate pid %d in %s: %q", cont.systemPid, containerID, err)
  254. // Ignore errors
  255. err = nil
  256. }
  257. }
  258. return nil
  259. }
  260. // Resize handles a CLI event to resize an interactive docker run or docker exec
  261. // window.
  262. func (clnt *client) Resize(containerID, processFriendlyName string, width, height int) error {
  263. // Get the libcontainerd container object
  264. clnt.lock(containerID)
  265. defer clnt.unlock(containerID)
  266. cont, err := clnt.getContainer(containerID)
  267. if err != nil {
  268. return err
  269. }
  270. h, w := uint16(height), uint16(width)
  271. if processFriendlyName == InitFriendlyName {
  272. logrus.Debugln("Resizing systemPID in", containerID, cont.process.systemPid)
  273. return cont.process.hcsProcess.ResizeConsole(w, h)
  274. }
  275. for _, p := range cont.processes {
  276. if p.friendlyName == processFriendlyName {
  277. logrus.Debugln("Resizing exec'd process", containerID, p.systemPid)
  278. return p.hcsProcess.ResizeConsole(w, h)
  279. }
  280. }
  281. return fmt.Errorf("Resize could not find containerID %s to resize", containerID)
  282. }
  283. // Pause handles pause requests for containers
  284. func (clnt *client) Pause(containerID string) error {
  285. return errors.New("Windows: Containers cannot be paused")
  286. }
  287. // Resume handles resume requests for containers
  288. func (clnt *client) Resume(containerID string) error {
  289. return errors.New("Windows: Containers cannot be paused")
  290. }
  291. // Stats handles stats requests for containers
  292. func (clnt *client) Stats(containerID string) (*Stats, error) {
  293. return nil, errors.New("Windows: Stats not implemented")
  294. }
  295. // Restore is the handler for restoring a container
  296. func (clnt *client) Restore(containerID string, unusedOnWindows ...CreateOption) error {
  297. // TODO Windows: Implement this. For now, just tell the backend the container exited.
  298. logrus.Debugf("lcd Restore %s", containerID)
  299. return clnt.backend.StateChanged(containerID, StateInfo{
  300. CommonStateInfo: CommonStateInfo{
  301. State: StateExit,
  302. ExitCode: 1 << 31,
  303. }})
  304. }
  305. // GetPidsForContainer returns a list of process IDs running in a container.
  306. // Although implemented, this is not used in Windows.
  307. func (clnt *client) GetPidsForContainer(containerID string) ([]int, error) {
  308. var pids []int
  309. clnt.lock(containerID)
  310. defer clnt.unlock(containerID)
  311. cont, err := clnt.getContainer(containerID)
  312. if err != nil {
  313. return nil, err
  314. }
  315. // Add the first process
  316. pids = append(pids, int(cont.containerCommon.systemPid))
  317. // And add all the exec'd processes
  318. for _, p := range cont.processes {
  319. pids = append(pids, int(p.processCommon.systemPid))
  320. }
  321. return pids, nil
  322. }
  323. // Summary returns a summary of the processes running in a container.
  324. // This is present in Windows to support docker top. In linux, the
  325. // engine shells out to ps to get process information. On Windows, as
  326. // the containers could be Hyper-V containers, they would not be
  327. // visible on the container host. However, libcontainerd does have
  328. // that information.
  329. func (clnt *client) Summary(containerID string) ([]Summary, error) {
  330. var s []Summary
  331. clnt.lock(containerID)
  332. defer clnt.unlock(containerID)
  333. cont, err := clnt.getContainer(containerID)
  334. if err != nil {
  335. return nil, err
  336. }
  337. // Add the first process
  338. s = append(s, Summary{
  339. Pid: cont.containerCommon.systemPid,
  340. Command: cont.ociSpec.Process.Args[0]})
  341. // And add all the exec'd processes
  342. for _, p := range cont.processes {
  343. s = append(s, Summary{
  344. Pid: p.processCommon.systemPid,
  345. Command: p.commandLine})
  346. }
  347. return s, nil
  348. }
  349. // UpdateResources updates resources for a running container.
  350. func (clnt *client) UpdateResources(containerID string, resources Resources) error {
  351. // Updating resource isn't supported on Windows
  352. // but we should return nil for enabling updating container
  353. return nil
  354. }