daemon_linux_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // +build linux
  2. package daemon // import "github.com/docker/docker/daemon"
  3. import (
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "testing"
  9. containertypes "github.com/docker/docker/api/types/container"
  10. "github.com/docker/docker/container"
  11. "github.com/docker/docker/daemon/config"
  12. "github.com/docker/docker/oci"
  13. "github.com/docker/docker/pkg/idtools"
  14. "github.com/docker/docker/pkg/mount"
  15. "github.com/gotestyourself/gotestyourself/assert"
  16. is "github.com/gotestyourself/gotestyourself/assert/cmp"
  17. )
  18. const mountsFixture = `142 78 0:38 / / rw,relatime - aufs none rw,si=573b861da0b3a05b,dio
  19. 143 142 0:60 / /proc rw,nosuid,nodev,noexec,relatime - proc proc rw
  20. 144 142 0:67 / /dev rw,nosuid - tmpfs tmpfs rw,mode=755
  21. 145 144 0:78 / /dev/pts rw,nosuid,noexec,relatime - devpts devpts rw,gid=5,mode=620,ptmxmode=666
  22. 146 144 0:49 / /dev/mqueue rw,nosuid,nodev,noexec,relatime - mqueue mqueue rw
  23. 147 142 0:84 / /sys rw,nosuid,nodev,noexec,relatime - sysfs sysfs rw
  24. 148 147 0:86 / /sys/fs/cgroup rw,nosuid,nodev,noexec,relatime - tmpfs tmpfs rw,mode=755
  25. 149 148 0:22 /docker/5425782a95e643181d8a485a2bab3c0bb21f51d7dfc03511f0e6fbf3f3aa356a /sys/fs/cgroup/cpuset rw,nosuid,nodev,noexec,relatime - cgroup cgroup rw,cpuset
  26. 150 148 0:25 /docker/5425782a95e643181d8a485a2bab3c0bb21f51d7dfc03511f0e6fbf3f3aa356a /sys/fs/cgroup/cpu rw,nosuid,nodev,noexec,relatime - cgroup cgroup rw,cpu
  27. 151 148 0:27 /docker/5425782a95e643181d8a485a2bab3c0bb21f51d7dfc03511f0e6fbf3f3aa356a /sys/fs/cgroup/cpuacct rw,nosuid,nodev,noexec,relatime - cgroup cgroup rw,cpuacct
  28. 152 148 0:28 /docker/5425782a95e643181d8a485a2bab3c0bb21f51d7dfc03511f0e6fbf3f3aa356a /sys/fs/cgroup/memory rw,nosuid,nodev,noexec,relatime - cgroup cgroup rw,memory
  29. 153 148 0:29 /docker/5425782a95e643181d8a485a2bab3c0bb21f51d7dfc03511f0e6fbf3f3aa356a /sys/fs/cgroup/devices rw,nosuid,nodev,noexec,relatime - cgroup cgroup rw,devices
  30. 154 148 0:30 /docker/5425782a95e643181d8a485a2bab3c0bb21f51d7dfc03511f0e6fbf3f3aa356a /sys/fs/cgroup/freezer rw,nosuid,nodev,noexec,relatime - cgroup cgroup rw,freezer
  31. 155 148 0:31 /docker/5425782a95e643181d8a485a2bab3c0bb21f51d7dfc03511f0e6fbf3f3aa356a /sys/fs/cgroup/blkio rw,nosuid,nodev,noexec,relatime - cgroup cgroup rw,blkio
  32. 156 148 0:32 /docker/5425782a95e643181d8a485a2bab3c0bb21f51d7dfc03511f0e6fbf3f3aa356a /sys/fs/cgroup/perf_event rw,nosuid,nodev,noexec,relatime - cgroup cgroup rw,perf_event
  33. 157 148 0:33 /docker/5425782a95e643181d8a485a2bab3c0bb21f51d7dfc03511f0e6fbf3f3aa356a /sys/fs/cgroup/hugetlb rw,nosuid,nodev,noexec,relatime - cgroup cgroup rw,hugetlb
  34. 158 148 0:35 /docker/5425782a95e643181d8a485a2bab3c0bb21f51d7dfc03511f0e6fbf3f3aa356a /sys/fs/cgroup/systemd rw,nosuid,nodev,noexec,relatime - cgroup systemd rw,name=systemd
  35. 159 142 8:4 /home/mlaventure/gopath /home/mlaventure/gopath rw,relatime - ext4 /dev/disk/by-uuid/d99e196c-1fc4-4b4f-bab9-9962b2b34e99 rw,errors=remount-ro,data=ordered
  36. 160 142 8:4 /var/lib/docker/volumes/9a428b651ee4c538130143cad8d87f603a4bf31b928afe7ff3ecd65480692b35/_data /var/lib/docker rw,relatime - ext4 /dev/disk/by-uuid/d99e196c-1fc4-4b4f-bab9-9962b2b34e99 rw,errors=remount-ro,data=ordered
  37. 164 142 8:4 /home/mlaventure/gopath/src/github.com/docker/docker /go/src/github.com/docker/docker rw,relatime - ext4 /dev/disk/by-uuid/d99e196c-1fc4-4b4f-bab9-9962b2b34e99 rw,errors=remount-ro,data=ordered
  38. 165 142 8:4 /var/lib/docker/containers/5425782a95e643181d8a485a2bab3c0bb21f51d7dfc03511f0e6fbf3f3aa356a/resolv.conf /etc/resolv.conf rw,relatime - ext4 /dev/disk/by-uuid/d99e196c-1fc4-4b4f-bab9-9962b2b34e99 rw,errors=remount-ro,data=ordered
  39. 166 142 8:4 /var/lib/docker/containers/5425782a95e643181d8a485a2bab3c0bb21f51d7dfc03511f0e6fbf3f3aa356a/hostname /etc/hostname rw,relatime - ext4 /dev/disk/by-uuid/d99e196c-1fc4-4b4f-bab9-9962b2b34e99 rw,errors=remount-ro,data=ordered
  40. 167 142 8:4 /var/lib/docker/containers/5425782a95e643181d8a485a2bab3c0bb21f51d7dfc03511f0e6fbf3f3aa356a/hosts /etc/hosts rw,relatime - ext4 /dev/disk/by-uuid/d99e196c-1fc4-4b4f-bab9-9962b2b34e99 rw,errors=remount-ro,data=ordered
  41. 168 144 0:39 / /dev/shm rw,nosuid,nodev,noexec,relatime - tmpfs shm rw,size=65536k
  42. 169 144 0:12 /14 /dev/console rw,nosuid,noexec,relatime - devpts devpts rw,gid=5,mode=620,ptmxmode=000
  43. 83 147 0:10 / /sys/kernel/security rw,relatime - securityfs none rw
  44. 89 142 0:87 / /tmp rw,relatime - tmpfs none rw
  45. 97 142 0:60 / /run/docker/netns/default rw,nosuid,nodev,noexec,relatime - proc proc rw
  46. 100 160 8:4 /var/lib/docker/volumes/9a428b651ee4c538130143cad8d87f603a4bf31b928afe7ff3ecd65480692b35/_data/aufs /var/lib/docker/aufs rw,relatime - ext4 /dev/disk/by-uuid/d99e196c-1fc4-4b4f-bab9-9962b2b34e99 rw,errors=remount-ro,data=ordered
  47. 115 100 0:102 / /var/lib/docker/aufs/mnt/0ecda1c63e5b58b3d89ff380bf646c95cc980252cf0b52466d43619aec7c8432 rw,relatime - aufs none rw,si=573b861dbc01905b,dio
  48. 116 160 0:107 / /var/lib/docker/containers/d045dc441d2e2e1d5b3e328d47e5943811a40819fb47497c5f5a5df2d6d13c37/shm rw,nosuid,nodev,noexec,relatime - tmpfs shm rw,size=65536k
  49. 118 142 0:102 / /run/docker/libcontainerd/d045dc441d2e2e1d5b3e328d47e5943811a40819fb47497c5f5a5df2d6d13c37/rootfs rw,relatime - aufs none rw,si=573b861dbc01905b,dio
  50. 242 142 0:60 / /run/docker/netns/c3664df2a0f7 rw,nosuid,nodev,noexec,relatime - proc proc rw
  51. 120 100 0:122 / /var/lib/docker/aufs/mnt/03ca4b49e71f1e49a41108829f4d5c70ac95934526e2af8984a1f65f1de0715d rw,relatime - aufs none rw,si=573b861eb147805b,dio
  52. 171 142 0:122 / /run/docker/libcontainerd/e406ff6f3e18516d50e03dbca4de54767a69a403a6f7ec1edc2762812824521e/rootfs rw,relatime - aufs none rw,si=573b861eb147805b,dio
  53. 310 142 0:60 / /run/docker/netns/71a18572176b rw,nosuid,nodev,noexec,relatime - proc proc rw
  54. `
  55. func TestCleanupMounts(t *testing.T) {
  56. d := &Daemon{
  57. root: "/var/lib/docker/",
  58. }
  59. expected := "/var/lib/docker/containers/d045dc441d2e2e1d5b3e328d47e5943811a40819fb47497c5f5a5df2d6d13c37/shm"
  60. var unmounted int
  61. unmount := func(target string) error {
  62. if target == expected {
  63. unmounted++
  64. }
  65. return nil
  66. }
  67. d.cleanupMountsFromReaderByID(strings.NewReader(mountsFixture), "", unmount)
  68. if unmounted != 1 {
  69. t.Fatal("Expected to unmount the shm (and the shm only)")
  70. }
  71. }
  72. func TestCleanupMountsByID(t *testing.T) {
  73. d := &Daemon{
  74. root: "/var/lib/docker/",
  75. }
  76. expected := "/var/lib/docker/aufs/mnt/03ca4b49e71f1e49a41108829f4d5c70ac95934526e2af8984a1f65f1de0715d"
  77. var unmounted int
  78. unmount := func(target string) error {
  79. if target == expected {
  80. unmounted++
  81. }
  82. return nil
  83. }
  84. d.cleanupMountsFromReaderByID(strings.NewReader(mountsFixture), "03ca4b49e71f1e49a41108829f4d5c70ac95934526e2af8984a1f65f1de0715d", unmount)
  85. if unmounted != 1 {
  86. t.Fatal("Expected to unmount the auf root (and that only)")
  87. }
  88. }
  89. func TestNotCleanupMounts(t *testing.T) {
  90. d := &Daemon{
  91. repository: "",
  92. }
  93. var unmounted bool
  94. unmount := func(target string) error {
  95. unmounted = true
  96. return nil
  97. }
  98. mountInfo := `234 232 0:59 / /dev/shm rw,nosuid,nodev,noexec,relatime - tmpfs shm rw,size=65536k`
  99. d.cleanupMountsFromReaderByID(strings.NewReader(mountInfo), "", unmount)
  100. if unmounted {
  101. t.Fatal("Expected not to clean up /dev/shm")
  102. }
  103. }
  104. // TestTmpfsDevShmSizeOverride checks that user-specified /dev/tmpfs mount
  105. // size is not overriden by the default shmsize (that should only be used
  106. // for default /dev/shm (as in "shareable" and "private" ipc modes).
  107. // https://github.com/moby/moby/issues/35271
  108. func TestTmpfsDevShmSizeOverride(t *testing.T) {
  109. size := "777m"
  110. mnt := "/dev/shm"
  111. d := Daemon{
  112. idMappings: &idtools.IDMappings{},
  113. }
  114. c := &container.Container{
  115. HostConfig: &containertypes.HostConfig{
  116. ShmSize: 48 * 1024, // size we should NOT end up with
  117. },
  118. }
  119. ms := []container.Mount{
  120. {
  121. Source: "tmpfs",
  122. Destination: mnt,
  123. Data: "size=" + size,
  124. },
  125. }
  126. // convert ms to spec
  127. spec := oci.DefaultSpec()
  128. err := setMounts(&d, &spec, c, ms)
  129. assert.Check(t, err)
  130. // Check the resulting spec for the correct size
  131. found := false
  132. for _, m := range spec.Mounts {
  133. if m.Destination == mnt {
  134. for _, o := range m.Options {
  135. if !strings.HasPrefix(o, "size=") {
  136. continue
  137. }
  138. t.Logf("%+v\n", m.Options)
  139. assert.Check(t, is.Equal("size="+size, o))
  140. found = true
  141. }
  142. }
  143. }
  144. if !found {
  145. t.Fatal("/dev/shm not found in spec, or size option missing")
  146. }
  147. }
  148. func TestValidateContainerIsolationLinux(t *testing.T) {
  149. d := Daemon{}
  150. _, err := d.verifyContainerSettings("linux", &containertypes.HostConfig{Isolation: containertypes.IsolationHyperV}, nil, false)
  151. assert.Check(t, is.Error(err, "invalid isolation 'hyperv' on linux"))
  152. }
  153. func TestShouldUnmountRoot(t *testing.T) {
  154. for _, test := range []struct {
  155. desc string
  156. root string
  157. info *mount.Info
  158. expect bool
  159. }{
  160. {
  161. desc: "root is at /",
  162. root: "/docker",
  163. info: &mount.Info{Root: "/docker", Mountpoint: "/docker"},
  164. expect: true,
  165. },
  166. {
  167. desc: "not a mountpoint",
  168. root: "/docker",
  169. info: nil,
  170. expect: false,
  171. },
  172. {
  173. desc: "root is at in a submount from `/`",
  174. root: "/foo/docker",
  175. info: &mount.Info{Root: "/docker", Mountpoint: "/foo/docker"},
  176. expect: true,
  177. },
  178. {
  179. desc: "root is mounted in from a parent mount namespace same root dir", // dind is an example of this
  180. root: "/docker",
  181. info: &mount.Info{Root: "/docker/volumes/1234657/_data", Mountpoint: "/docker"},
  182. expect: false,
  183. },
  184. {
  185. desc: "root is mounted in from a parent mount namespace different root dir",
  186. root: "/foo/bar",
  187. info: &mount.Info{Root: "/docker/volumes/1234657/_data", Mountpoint: "/foo/bar"},
  188. expect: false,
  189. },
  190. } {
  191. t.Run(test.desc, func(t *testing.T) {
  192. for _, options := range []struct {
  193. desc string
  194. Optional string
  195. expect bool
  196. }{
  197. {desc: "shared", Optional: "shared:", expect: true},
  198. {desc: "slave", Optional: "slave:", expect: false},
  199. {desc: "private", Optional: "private:", expect: false},
  200. } {
  201. t.Run(options.desc, func(t *testing.T) {
  202. expect := options.expect
  203. if expect {
  204. expect = test.expect
  205. }
  206. if test.info != nil {
  207. test.info.Optional = options.Optional
  208. }
  209. assert.Check(t, is.Equal(expect, shouldUnmountRoot(test.root, test.info)))
  210. })
  211. }
  212. })
  213. }
  214. }
  215. func checkMounted(t *testing.T, p string, expect bool) {
  216. t.Helper()
  217. mounted, err := mount.Mounted(p)
  218. assert.Check(t, err)
  219. assert.Check(t, mounted == expect, "expected %v, actual %v", expect, mounted)
  220. }
  221. func TestRootMountCleanup(t *testing.T) {
  222. t.Parallel()
  223. testRoot, err := ioutil.TempDir("", t.Name())
  224. assert.Assert(t, err)
  225. defer os.RemoveAll(testRoot)
  226. cfg := &config.Config{}
  227. err = mount.MakePrivate(testRoot)
  228. assert.Assert(t, err)
  229. defer mount.Unmount(testRoot)
  230. cfg.ExecRoot = filepath.Join(testRoot, "exec")
  231. cfg.Root = filepath.Join(testRoot, "daemon")
  232. err = os.Mkdir(cfg.ExecRoot, 0755)
  233. assert.Assert(t, err)
  234. err = os.Mkdir(cfg.Root, 0755)
  235. assert.Assert(t, err)
  236. d := &Daemon{configStore: cfg, root: cfg.Root}
  237. unmountFile := getUnmountOnShutdownPath(cfg)
  238. t.Run("regular dir no mountpoint", func(t *testing.T) {
  239. err = setupDaemonRootPropagation(cfg)
  240. assert.Assert(t, err)
  241. _, err = os.Stat(unmountFile)
  242. assert.Assert(t, err)
  243. checkMounted(t, cfg.Root, true)
  244. assert.Assert(t, d.cleanupMounts())
  245. checkMounted(t, cfg.Root, false)
  246. _, err = os.Stat(unmountFile)
  247. assert.Assert(t, os.IsNotExist(err))
  248. })
  249. t.Run("root is a private mountpoint", func(t *testing.T) {
  250. err = mount.MakePrivate(cfg.Root)
  251. assert.Assert(t, err)
  252. defer mount.Unmount(cfg.Root)
  253. err = setupDaemonRootPropagation(cfg)
  254. assert.Assert(t, err)
  255. assert.Check(t, ensureShared(cfg.Root))
  256. _, err = os.Stat(unmountFile)
  257. assert.Assert(t, os.IsNotExist(err))
  258. assert.Assert(t, d.cleanupMounts())
  259. checkMounted(t, cfg.Root, true)
  260. })
  261. // mount is pre-configured with a shared mount
  262. t.Run("root is a shared mountpoint", func(t *testing.T) {
  263. err = mount.MakeShared(cfg.Root)
  264. assert.Assert(t, err)
  265. defer mount.Unmount(cfg.Root)
  266. err = setupDaemonRootPropagation(cfg)
  267. assert.Assert(t, err)
  268. if _, err := os.Stat(unmountFile); err == nil {
  269. t.Fatal("unmount file should not exist")
  270. }
  271. assert.Assert(t, d.cleanupMounts())
  272. checkMounted(t, cfg.Root, true)
  273. assert.Assert(t, mount.Unmount(cfg.Root))
  274. })
  275. // does not need mount but unmount file exists from previous run
  276. t.Run("old mount file is cleaned up on setup if not needed", func(t *testing.T) {
  277. err = mount.MakeShared(testRoot)
  278. assert.Assert(t, err)
  279. defer mount.MakePrivate(testRoot)
  280. err = ioutil.WriteFile(unmountFile, nil, 0644)
  281. assert.Assert(t, err)
  282. err = setupDaemonRootPropagation(cfg)
  283. assert.Assert(t, err)
  284. _, err = os.Stat(unmountFile)
  285. assert.Check(t, os.IsNotExist(err), err)
  286. checkMounted(t, cfg.Root, false)
  287. assert.Assert(t, d.cleanupMounts())
  288. })
  289. }