utils.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. }
  115. if userConf.PortSpecs != nil && len(userConf.PortSpecs) > 0 {
  116. if userConf.ExposedPorts == nil {
  117. userConf.ExposedPorts = make(map[Port]struct{})
  118. }
  119. ports, _, err := parsePortSpecs(userConf.PortSpecs)
  120. if err != nil {
  121. return err
  122. }
  123. for port := range ports {
  124. if _, exists := userConf.ExposedPorts[port]; !exists {
  125. userConf.ExposedPorts[port] = struct{}{}
  126. }
  127. }
  128. userConf.PortSpecs = nil
  129. }
  130. if imageConf.PortSpecs != nil && len(imageConf.PortSpecs) > 0 {
  131. utils.Debugf("Migrating image port specs to containter: %s", strings.Join(imageConf.PortSpecs, ", "))
  132. if userConf.ExposedPorts == nil {
  133. userConf.ExposedPorts = make(map[Port]struct{})
  134. }
  135. ports, _, err := parsePortSpecs(imageConf.PortSpecs)
  136. if err != nil {
  137. return err
  138. }
  139. for port := range ports {
  140. if _, exists := userConf.ExposedPorts[port]; !exists {
  141. userConf.ExposedPorts[port] = struct{}{}
  142. }
  143. }
  144. }
  145. if !userConf.Tty {
  146. userConf.Tty = imageConf.Tty
  147. }
  148. if !userConf.OpenStdin {
  149. userConf.OpenStdin = imageConf.OpenStdin
  150. }
  151. if !userConf.StdinOnce {
  152. userConf.StdinOnce = imageConf.StdinOnce
  153. }
  154. if userConf.Env == nil || len(userConf.Env) == 0 {
  155. userConf.Env = imageConf.Env
  156. } else {
  157. for _, imageEnv := range imageConf.Env {
  158. found := false
  159. imageEnvKey := strings.Split(imageEnv, "=")[0]
  160. for _, userEnv := range userConf.Env {
  161. userEnvKey := strings.Split(userEnv, "=")[0]
  162. if imageEnvKey == userEnvKey {
  163. found = true
  164. }
  165. }
  166. if !found {
  167. userConf.Env = append(userConf.Env, imageEnv)
  168. }
  169. }
  170. }
  171. if userConf.Cmd == nil || len(userConf.Cmd) == 0 {
  172. userConf.Cmd = imageConf.Cmd
  173. }
  174. if userConf.Dns == nil || len(userConf.Dns) == 0 {
  175. userConf.Dns = imageConf.Dns
  176. } else {
  177. //duplicates aren't an issue here
  178. userConf.Dns = append(userConf.Dns, imageConf.Dns...)
  179. }
  180. if userConf.Entrypoint == nil || len(userConf.Entrypoint) == 0 {
  181. userConf.Entrypoint = imageConf.Entrypoint
  182. }
  183. if userConf.WorkingDir == "" {
  184. userConf.WorkingDir = imageConf.WorkingDir
  185. }
  186. if userConf.VolumesFrom == "" {
  187. userConf.VolumesFrom = imageConf.VolumesFrom
  188. }
  189. if userConf.Volumes == nil || len(userConf.Volumes) == 0 {
  190. userConf.Volumes = imageConf.Volumes
  191. } else {
  192. for k, v := range imageConf.Volumes {
  193. userConf.Volumes[k] = v
  194. }
  195. }
  196. return nil
  197. }
  198. func parseLxcConfOpts(opts utils.ListOpts) ([]KeyValuePair, error) {
  199. out := make([]KeyValuePair, len(opts))
  200. for i, o := range opts {
  201. k, v, err := parseLxcOpt(o)
  202. if err != nil {
  203. return nil, err
  204. }
  205. out[i] = KeyValuePair{Key: k, Value: v}
  206. }
  207. return out, nil
  208. }
  209. func parseLxcOpt(opt string) (string, string, error) {
  210. parts := strings.SplitN(opt, "=", 2)
  211. if len(parts) != 2 {
  212. return "", "", fmt.Errorf("Unable to parse lxc conf option: %s", opt)
  213. }
  214. return strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]), nil
  215. }
  216. // We will receive port specs in the format of ip:public:private/proto and these need to be
  217. // parsed in the internal types
  218. func parsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding, error) {
  219. exposedPorts := make(map[Port]struct{}, len(ports))
  220. bindings := make(map[Port][]PortBinding)
  221. for _, rawPort := range ports {
  222. proto := "tcp"
  223. if i := strings.LastIndex(rawPort, "/"); i != -1 {
  224. proto = rawPort[i+1:]
  225. rawPort = rawPort[:i]
  226. }
  227. if !strings.Contains(rawPort, ":") {
  228. rawPort = fmt.Sprintf("::%s", rawPort)
  229. } else if len(strings.Split(rawPort, ":")) == 2 {
  230. rawPort = fmt.Sprintf(":%s", rawPort)
  231. }
  232. parts, err := utils.PartParser("ip:hostPort:containerPort", rawPort)
  233. if err != nil {
  234. return nil, nil, err
  235. }
  236. containerPort := parts["containerPort"]
  237. rawIp := parts["ip"]
  238. hostPort := parts["hostPort"]
  239. if containerPort == "" {
  240. return nil, nil, fmt.Errorf("No port specified: %s<empty>", rawPort)
  241. }
  242. if _, err := strconv.ParseUint(containerPort, 10, 16); err != nil {
  243. return nil, nil, fmt.Errorf("Invalid containerPort: %s", containerPort)
  244. }
  245. if _, err := strconv.ParseUint(hostPort, 10, 16); hostPort != "" && err != nil {
  246. return nil, nil, fmt.Errorf("Invalid hostPort: %s", hostPort)
  247. }
  248. port := NewPort(proto, containerPort)
  249. if _, exists := exposedPorts[port]; !exists {
  250. exposedPorts[port] = struct{}{}
  251. }
  252. binding := PortBinding{
  253. HostIp: rawIp,
  254. HostPort: hostPort,
  255. }
  256. bslice, exists := bindings[port]
  257. if !exists {
  258. bslice = []PortBinding{}
  259. }
  260. bindings[port] = append(bslice, binding)
  261. }
  262. return exposedPorts, bindings, nil
  263. }
  264. // Splits a port in the format of port/proto
  265. func splitProtoPort(rawPort string) (string, string) {
  266. parts := strings.Split(rawPort, "/")
  267. l := len(parts)
  268. if l == 0 {
  269. return "", ""
  270. }
  271. if l == 1 {
  272. return "tcp", rawPort
  273. }
  274. return parts[0], parts[1]
  275. }
  276. func parsePort(rawPort string) (int, error) {
  277. port, err := strconv.ParseUint(rawPort, 10, 16)
  278. if err != nil {
  279. return 0, err
  280. }
  281. return int(port), nil
  282. }
  283. func migratePortMappings(config *Config, hostConfig *HostConfig) error {
  284. if config.PortSpecs != nil {
  285. ports, bindings, err := parsePortSpecs(config.PortSpecs)
  286. if err != nil {
  287. return err
  288. }
  289. config.PortSpecs = nil
  290. if len(bindings) > 0 {
  291. if hostConfig == nil {
  292. hostConfig = &HostConfig{}
  293. }
  294. hostConfig.PortBindings = bindings
  295. }
  296. if config.ExposedPorts == nil {
  297. config.ExposedPorts = make(map[Port]struct{}, len(ports))
  298. }
  299. for k, v := range ports {
  300. config.ExposedPorts[k] = v
  301. }
  302. }
  303. return nil
  304. }
  305. func RootIsShared() bool {
  306. if data, err := ioutil.ReadFile("/proc/self/mountinfo"); err == nil {
  307. for _, line := range strings.Split(string(data), "\n") {
  308. cols := strings.Split(line, " ")
  309. if len(cols) >= 6 && cols[4] == "/" {
  310. return strings.HasPrefix(cols[6], "shared")
  311. }
  312. }
  313. }
  314. // No idea, probably safe to assume so
  315. return true
  316. }
  317. func BtrfsReflink(fd_out, fd_in uintptr) error {
  318. res := C.btrfs_reflink(C.int(fd_out), C.int(fd_in))
  319. if res != 0 {
  320. return syscall.Errno(res)
  321. }
  322. return nil
  323. }
  324. // Links come in the format of
  325. // name:alias
  326. func parseLink(rawLink string) (map[string]string, error) {
  327. return utils.PartParser("name:alias", rawLink)
  328. }
  329. type checker struct {
  330. runtime *Runtime
  331. }
  332. func (c *checker) Exists(name string) bool {
  333. return c.runtime.containerGraph.Exists("/" + name)
  334. }
  335. // Generate a random and unique name
  336. func generateRandomName(runtime *Runtime) (string, error) {
  337. return namesgenerator.GenerateRandomName(&checker{runtime})
  338. }
  339. func CopyFile(dstFile, srcFile *os.File) error {
  340. err := BtrfsReflink(dstFile.Fd(), srcFile.Fd())
  341. if err == nil {
  342. return nil
  343. }
  344. // Fall back to normal copy
  345. _, err = io.Copy(dstFile, srcFile)
  346. return err
  347. }