utils.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. package docker
  2. /*
  3. #include <sys/ioctl.h>
  4. #include <linux/fs.h>
  5. #include <errno.h>
  6. // See linux.git/fs/btrfs/ioctl.h
  7. #define BTRFS_IOCTL_MAGIC 0x94
  8. #define BTRFS_IOC_CLONE _IOW(BTRFS_IOCTL_MAGIC, 9, int)
  9. int
  10. btrfs_reflink(int fd_out, int fd_in)
  11. {
  12. int res;
  13. res = ioctl(fd_out, BTRFS_IOC_CLONE, fd_in);
  14. if (res < 0)
  15. return errno;
  16. return 0;
  17. }
  18. */
  19. import "C"
  20. import (
  21. "fmt"
  22. "github.com/dotcloud/docker/archive"
  23. "github.com/dotcloud/docker/namesgenerator"
  24. "github.com/dotcloud/docker/utils"
  25. "io"
  26. "io/ioutil"
  27. "os"
  28. "strconv"
  29. "strings"
  30. "syscall"
  31. )
  32. type Change struct {
  33. archive.Change
  34. }
  35. // Compare two Config struct. Do not compare the "Image" nor "Hostname" fields
  36. // If OpenStdin is set, then it differs
  37. func CompareConfig(a, b *Config) bool {
  38. if a == nil || b == nil ||
  39. a.OpenStdin || b.OpenStdin {
  40. return false
  41. }
  42. if a.AttachStdout != b.AttachStdout ||
  43. a.AttachStderr != b.AttachStderr ||
  44. a.User != b.User ||
  45. a.Memory != b.Memory ||
  46. a.MemorySwap != b.MemorySwap ||
  47. a.CpuShares != b.CpuShares ||
  48. a.OpenStdin != b.OpenStdin ||
  49. a.Tty != b.Tty ||
  50. a.VolumesFrom != b.VolumesFrom {
  51. return false
  52. }
  53. if len(a.Cmd) != len(b.Cmd) ||
  54. len(a.Dns) != len(b.Dns) ||
  55. len(a.Env) != len(b.Env) ||
  56. len(a.PortSpecs) != len(b.PortSpecs) ||
  57. len(a.ExposedPorts) != len(b.ExposedPorts) ||
  58. len(a.Entrypoint) != len(b.Entrypoint) ||
  59. len(a.Volumes) != len(b.Volumes) {
  60. return false
  61. }
  62. for i := 0; i < len(a.Cmd); i++ {
  63. if a.Cmd[i] != b.Cmd[i] {
  64. return false
  65. }
  66. }
  67. for i := 0; i < len(a.Dns); i++ {
  68. if a.Dns[i] != b.Dns[i] {
  69. return false
  70. }
  71. }
  72. for i := 0; i < len(a.Env); i++ {
  73. if a.Env[i] != b.Env[i] {
  74. return false
  75. }
  76. }
  77. for i := 0; i < len(a.PortSpecs); i++ {
  78. if a.PortSpecs[i] != b.PortSpecs[i] {
  79. return false
  80. }
  81. }
  82. for k := range a.ExposedPorts {
  83. if _, exists := b.ExposedPorts[k]; !exists {
  84. return false
  85. }
  86. }
  87. for i := 0; i < len(a.Entrypoint); i++ {
  88. if a.Entrypoint[i] != b.Entrypoint[i] {
  89. return false
  90. }
  91. }
  92. for key := range a.Volumes {
  93. if _, exists := b.Volumes[key]; !exists {
  94. return false
  95. }
  96. }
  97. return true
  98. }
  99. func MergeConfig(userConf, imageConf *Config) error {
  100. if userConf.User == "" {
  101. userConf.User = imageConf.User
  102. }
  103. if userConf.Memory == 0 {
  104. userConf.Memory = imageConf.Memory
  105. }
  106. if userConf.MemorySwap == 0 {
  107. userConf.MemorySwap = imageConf.MemorySwap
  108. }
  109. if userConf.CpuShares == 0 {
  110. userConf.CpuShares = imageConf.CpuShares
  111. }
  112. if userConf.ExposedPorts == nil || len(userConf.ExposedPorts) == 0 {
  113. userConf.ExposedPorts = imageConf.ExposedPorts
  114. } else if imageConf.ExposedPorts != nil {
  115. if userConf.ExposedPorts == nil {
  116. userConf.ExposedPorts = make(map[Port]struct{})
  117. }
  118. for port := range imageConf.ExposedPorts {
  119. if _, exists := userConf.ExposedPorts[port]; !exists {
  120. userConf.ExposedPorts[port] = struct{}{}
  121. }
  122. }
  123. }
  124. if userConf.PortSpecs != nil && len(userConf.PortSpecs) > 0 {
  125. if userConf.ExposedPorts == nil {
  126. userConf.ExposedPorts = make(map[Port]struct{})
  127. }
  128. ports, _, err := parsePortSpecs(userConf.PortSpecs)
  129. if err != nil {
  130. return err
  131. }
  132. for port := range ports {
  133. if _, exists := userConf.ExposedPorts[port]; !exists {
  134. userConf.ExposedPorts[port] = struct{}{}
  135. }
  136. }
  137. userConf.PortSpecs = nil
  138. }
  139. if imageConf.PortSpecs != nil && len(imageConf.PortSpecs) > 0 {
  140. utils.Debugf("Migrating image port specs to containter: %s", strings.Join(imageConf.PortSpecs, ", "))
  141. if userConf.ExposedPorts == nil {
  142. userConf.ExposedPorts = make(map[Port]struct{})
  143. }
  144. ports, _, err := parsePortSpecs(imageConf.PortSpecs)
  145. if err != nil {
  146. return err
  147. }
  148. for port := range ports {
  149. if _, exists := userConf.ExposedPorts[port]; !exists {
  150. userConf.ExposedPorts[port] = struct{}{}
  151. }
  152. }
  153. }
  154. if !userConf.Tty {
  155. userConf.Tty = imageConf.Tty
  156. }
  157. if !userConf.OpenStdin {
  158. userConf.OpenStdin = imageConf.OpenStdin
  159. }
  160. if !userConf.StdinOnce {
  161. userConf.StdinOnce = imageConf.StdinOnce
  162. }
  163. if userConf.Env == nil || len(userConf.Env) == 0 {
  164. userConf.Env = imageConf.Env
  165. } else {
  166. for _, imageEnv := range imageConf.Env {
  167. found := false
  168. imageEnvKey := strings.Split(imageEnv, "=")[0]
  169. for _, userEnv := range userConf.Env {
  170. userEnvKey := strings.Split(userEnv, "=")[0]
  171. if imageEnvKey == userEnvKey {
  172. found = true
  173. }
  174. }
  175. if !found {
  176. userConf.Env = append(userConf.Env, imageEnv)
  177. }
  178. }
  179. }
  180. if userConf.Cmd == nil || len(userConf.Cmd) == 0 {
  181. userConf.Cmd = imageConf.Cmd
  182. }
  183. if userConf.Dns == nil || len(userConf.Dns) == 0 {
  184. userConf.Dns = imageConf.Dns
  185. } else {
  186. //duplicates aren't an issue here
  187. userConf.Dns = append(userConf.Dns, imageConf.Dns...)
  188. }
  189. if userConf.Entrypoint == nil || len(userConf.Entrypoint) == 0 {
  190. userConf.Entrypoint = imageConf.Entrypoint
  191. }
  192. if userConf.WorkingDir == "" {
  193. userConf.WorkingDir = imageConf.WorkingDir
  194. }
  195. if userConf.VolumesFrom == "" {
  196. userConf.VolumesFrom = imageConf.VolumesFrom
  197. }
  198. if userConf.Volumes == nil || len(userConf.Volumes) == 0 {
  199. userConf.Volumes = imageConf.Volumes
  200. } else {
  201. for k, v := range imageConf.Volumes {
  202. userConf.Volumes[k] = v
  203. }
  204. }
  205. return nil
  206. }
  207. func parseLxcConfOpts(opts utils.ListOpts) ([]KeyValuePair, error) {
  208. out := make([]KeyValuePair, len(opts))
  209. for i, o := range opts {
  210. k, v, err := parseLxcOpt(o)
  211. if err != nil {
  212. return nil, err
  213. }
  214. out[i] = KeyValuePair{Key: k, Value: v}
  215. }
  216. return out, nil
  217. }
  218. func parseLxcOpt(opt string) (string, string, error) {
  219. parts := strings.SplitN(opt, "=", 2)
  220. if len(parts) != 2 {
  221. return "", "", fmt.Errorf("Unable to parse lxc conf option: %s", opt)
  222. }
  223. return strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]), nil
  224. }
  225. // We will receive port specs in the format of ip:public:private/proto and these need to be
  226. // parsed in the internal types
  227. func parsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding, error) {
  228. exposedPorts := make(map[Port]struct{}, len(ports))
  229. bindings := make(map[Port][]PortBinding)
  230. for _, rawPort := range ports {
  231. proto := "tcp"
  232. if i := strings.LastIndex(rawPort, "/"); i != -1 {
  233. proto = rawPort[i+1:]
  234. rawPort = rawPort[:i]
  235. }
  236. if !strings.Contains(rawPort, ":") {
  237. rawPort = fmt.Sprintf("::%s", rawPort)
  238. } else if len(strings.Split(rawPort, ":")) == 2 {
  239. rawPort = fmt.Sprintf(":%s", rawPort)
  240. }
  241. parts, err := utils.PartParser("ip:hostPort:containerPort", rawPort)
  242. if err != nil {
  243. return nil, nil, err
  244. }
  245. containerPort := parts["containerPort"]
  246. rawIp := parts["ip"]
  247. hostPort := parts["hostPort"]
  248. if containerPort == "" {
  249. return nil, nil, fmt.Errorf("No port specified: %s<empty>", rawPort)
  250. }
  251. if _, err := strconv.ParseUint(containerPort, 10, 16); err != nil {
  252. return nil, nil, fmt.Errorf("Invalid containerPort: %s", containerPort)
  253. }
  254. if _, err := strconv.ParseUint(hostPort, 10, 16); hostPort != "" && err != nil {
  255. return nil, nil, fmt.Errorf("Invalid hostPort: %s", hostPort)
  256. }
  257. port := NewPort(proto, containerPort)
  258. if _, exists := exposedPorts[port]; !exists {
  259. exposedPorts[port] = struct{}{}
  260. }
  261. binding := PortBinding{
  262. HostIp: rawIp,
  263. HostPort: hostPort,
  264. }
  265. bslice, exists := bindings[port]
  266. if !exists {
  267. bslice = []PortBinding{}
  268. }
  269. bindings[port] = append(bslice, binding)
  270. }
  271. return exposedPorts, bindings, nil
  272. }
  273. // Splits a port in the format of port/proto
  274. func splitProtoPort(rawPort string) (string, string) {
  275. parts := strings.Split(rawPort, "/")
  276. l := len(parts)
  277. if l == 0 {
  278. return "", ""
  279. }
  280. if l == 1 {
  281. return "tcp", rawPort
  282. }
  283. return parts[0], parts[1]
  284. }
  285. func parsePort(rawPort string) (int, error) {
  286. port, err := strconv.ParseUint(rawPort, 10, 16)
  287. if err != nil {
  288. return 0, err
  289. }
  290. return int(port), nil
  291. }
  292. func migratePortMappings(config *Config, hostConfig *HostConfig) error {
  293. if config.PortSpecs != nil {
  294. ports, bindings, err := parsePortSpecs(config.PortSpecs)
  295. if err != nil {
  296. return err
  297. }
  298. config.PortSpecs = nil
  299. if len(bindings) > 0 {
  300. if hostConfig == nil {
  301. hostConfig = &HostConfig{}
  302. }
  303. hostConfig.PortBindings = bindings
  304. }
  305. if config.ExposedPorts == nil {
  306. config.ExposedPorts = make(map[Port]struct{}, len(ports))
  307. }
  308. for k, v := range ports {
  309. config.ExposedPorts[k] = v
  310. }
  311. }
  312. return nil
  313. }
  314. func BtrfsReflink(fd_out, fd_in uintptr) error {
  315. res := C.btrfs_reflink(C.int(fd_out), C.int(fd_in))
  316. if res != 0 {
  317. return syscall.Errno(res)
  318. }
  319. return nil
  320. }
  321. // Links come in the format of
  322. // name:alias
  323. func parseLink(rawLink string) (map[string]string, error) {
  324. return utils.PartParser("name:alias", rawLink)
  325. }
  326. func RootIsShared() bool {
  327. if data, err := ioutil.ReadFile("/proc/self/mountinfo"); err == nil {
  328. for _, line := range strings.Split(string(data), "\n") {
  329. cols := strings.Split(line, " ")
  330. if len(cols) >= 6 && cols[4] == "/" {
  331. return strings.HasPrefix(cols[6], "shared")
  332. }
  333. }
  334. }
  335. // No idea, probably safe to assume so
  336. return true
  337. }
  338. type checker struct {
  339. runtime *Runtime
  340. }
  341. func (c *checker) Exists(name string) bool {
  342. return c.runtime.containerGraph.Exists("/" + name)
  343. }
  344. // Generate a random and unique name
  345. func generateRandomName(runtime *Runtime) (string, error) {
  346. return namesgenerator.GenerateRandomName(&checker{runtime})
  347. }
  348. func CopyFile(dstFile, srcFile *os.File) error {
  349. err := BtrfsReflink(dstFile.Fd(), srcFile.Fd())
  350. if err == nil {
  351. return nil
  352. }
  353. // Fall back to normal copy
  354. _, err = io.Copy(dstFile, srcFile)
  355. return err
  356. }