daemon_linux_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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/daemon/config"
  11. "github.com/docker/docker/pkg/mount"
  12. "gotest.tools/v3/assert"
  13. is "gotest.tools/v3/assert/cmp"
  14. )
  15. const mountsFixture = `142 78 0:38 / / rw,relatime - aufs none rw,si=573b861da0b3a05b,dio
  16. 143 142 0:60 / /proc rw,nosuid,nodev,noexec,relatime - proc proc rw
  17. 144 142 0:67 / /dev rw,nosuid - tmpfs tmpfs rw,mode=755
  18. 145 144 0:78 / /dev/pts rw,nosuid,noexec,relatime - devpts devpts rw,gid=5,mode=620,ptmxmode=666
  19. 146 144 0:49 / /dev/mqueue rw,nosuid,nodev,noexec,relatime - mqueue mqueue rw
  20. 147 142 0:84 / /sys rw,nosuid,nodev,noexec,relatime - sysfs sysfs rw
  21. 148 147 0:86 / /sys/fs/cgroup rw,nosuid,nodev,noexec,relatime - tmpfs tmpfs rw,mode=755
  22. 149 148 0:22 /docker/5425782a95e643181d8a485a2bab3c0bb21f51d7dfc03511f0e6fbf3f3aa356a /sys/fs/cgroup/cpuset rw,nosuid,nodev,noexec,relatime - cgroup cgroup rw,cpuset
  23. 150 148 0:25 /docker/5425782a95e643181d8a485a2bab3c0bb21f51d7dfc03511f0e6fbf3f3aa356a /sys/fs/cgroup/cpu rw,nosuid,nodev,noexec,relatime - cgroup cgroup rw,cpu
  24. 151 148 0:27 /docker/5425782a95e643181d8a485a2bab3c0bb21f51d7dfc03511f0e6fbf3f3aa356a /sys/fs/cgroup/cpuacct rw,nosuid,nodev,noexec,relatime - cgroup cgroup rw,cpuacct
  25. 152 148 0:28 /docker/5425782a95e643181d8a485a2bab3c0bb21f51d7dfc03511f0e6fbf3f3aa356a /sys/fs/cgroup/memory rw,nosuid,nodev,noexec,relatime - cgroup cgroup rw,memory
  26. 153 148 0:29 /docker/5425782a95e643181d8a485a2bab3c0bb21f51d7dfc03511f0e6fbf3f3aa356a /sys/fs/cgroup/devices rw,nosuid,nodev,noexec,relatime - cgroup cgroup rw,devices
  27. 154 148 0:30 /docker/5425782a95e643181d8a485a2bab3c0bb21f51d7dfc03511f0e6fbf3f3aa356a /sys/fs/cgroup/freezer rw,nosuid,nodev,noexec,relatime - cgroup cgroup rw,freezer
  28. 155 148 0:31 /docker/5425782a95e643181d8a485a2bab3c0bb21f51d7dfc03511f0e6fbf3f3aa356a /sys/fs/cgroup/blkio rw,nosuid,nodev,noexec,relatime - cgroup cgroup rw,blkio
  29. 156 148 0:32 /docker/5425782a95e643181d8a485a2bab3c0bb21f51d7dfc03511f0e6fbf3f3aa356a /sys/fs/cgroup/perf_event rw,nosuid,nodev,noexec,relatime - cgroup cgroup rw,perf_event
  30. 157 148 0:33 /docker/5425782a95e643181d8a485a2bab3c0bb21f51d7dfc03511f0e6fbf3f3aa356a /sys/fs/cgroup/hugetlb rw,nosuid,nodev,noexec,relatime - cgroup cgroup rw,hugetlb
  31. 158 148 0:35 /docker/5425782a95e643181d8a485a2bab3c0bb21f51d7dfc03511f0e6fbf3f3aa356a /sys/fs/cgroup/systemd rw,nosuid,nodev,noexec,relatime - cgroup systemd rw,name=systemd
  32. 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
  33. 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
  34. 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
  35. 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
  36. 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
  37. 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
  38. 168 144 0:39 / /dev/shm rw,nosuid,nodev,noexec,relatime - tmpfs shm rw,size=65536k
  39. 169 144 0:12 /14 /dev/console rw,nosuid,noexec,relatime - devpts devpts rw,gid=5,mode=620,ptmxmode=000
  40. 83 147 0:10 / /sys/kernel/security rw,relatime - securityfs none rw
  41. 89 142 0:87 / /tmp rw,relatime - tmpfs none rw
  42. 97 142 0:60 / /run/docker/netns/default rw,nosuid,nodev,noexec,relatime - proc proc rw
  43. 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
  44. 115 100 0:102 / /var/lib/docker/aufs/mnt/0ecda1c63e5b58b3d89ff380bf646c95cc980252cf0b52466d43619aec7c8432 rw,relatime - aufs none rw,si=573b861dbc01905b,dio
  45. 116 160 0:107 / /var/lib/docker/containers/d045dc441d2e2e1d5b3e328d47e5943811a40819fb47497c5f5a5df2d6d13c37/shm rw,nosuid,nodev,noexec,relatime - tmpfs shm rw,size=65536k
  46. 118 142 0:102 / /run/docker/libcontainerd/d045dc441d2e2e1d5b3e328d47e5943811a40819fb47497c5f5a5df2d6d13c37/rootfs rw,relatime - aufs none rw,si=573b861dbc01905b,dio
  47. 242 142 0:60 / /run/docker/netns/c3664df2a0f7 rw,nosuid,nodev,noexec,relatime - proc proc rw
  48. 120 100 0:122 / /var/lib/docker/aufs/mnt/03ca4b49e71f1e49a41108829f4d5c70ac95934526e2af8984a1f65f1de0715d rw,relatime - aufs none rw,si=573b861eb147805b,dio
  49. 171 142 0:122 / /run/docker/libcontainerd/e406ff6f3e18516d50e03dbca4de54767a69a403a6f7ec1edc2762812824521e/rootfs rw,relatime - aufs none rw,si=573b861eb147805b,dio
  50. 310 142 0:60 / /run/docker/netns/71a18572176b rw,nosuid,nodev,noexec,relatime - proc proc rw
  51. `
  52. func TestCleanupMounts(t *testing.T) {
  53. d := &Daemon{
  54. root: "/var/lib/docker/",
  55. }
  56. expected := "/var/lib/docker/containers/d045dc441d2e2e1d5b3e328d47e5943811a40819fb47497c5f5a5df2d6d13c37/shm"
  57. var unmounted int
  58. unmount := func(target string) error {
  59. if target == expected {
  60. unmounted++
  61. }
  62. return nil
  63. }
  64. d.cleanupMountsFromReaderByID(strings.NewReader(mountsFixture), "", unmount)
  65. if unmounted != 1 {
  66. t.Fatal("Expected to unmount the shm (and the shm only)")
  67. }
  68. }
  69. func TestCleanupMountsByID(t *testing.T) {
  70. d := &Daemon{
  71. root: "/var/lib/docker/",
  72. }
  73. expected := "/var/lib/docker/aufs/mnt/03ca4b49e71f1e49a41108829f4d5c70ac95934526e2af8984a1f65f1de0715d"
  74. var unmounted int
  75. unmount := func(target string) error {
  76. if target == expected {
  77. unmounted++
  78. }
  79. return nil
  80. }
  81. d.cleanupMountsFromReaderByID(strings.NewReader(mountsFixture), "03ca4b49e71f1e49a41108829f4d5c70ac95934526e2af8984a1f65f1de0715d", unmount)
  82. if unmounted != 1 {
  83. t.Fatal("Expected to unmount the auf root (and that only)")
  84. }
  85. }
  86. func TestNotCleanupMounts(t *testing.T) {
  87. d := &Daemon{
  88. repository: "",
  89. }
  90. var unmounted bool
  91. unmount := func(target string) error {
  92. unmounted = true
  93. return nil
  94. }
  95. mountInfo := `234 232 0:59 / /dev/shm rw,nosuid,nodev,noexec,relatime - tmpfs shm rw,size=65536k`
  96. d.cleanupMountsFromReaderByID(strings.NewReader(mountInfo), "", unmount)
  97. if unmounted {
  98. t.Fatal("Expected not to clean up /dev/shm")
  99. }
  100. }
  101. func TestValidateContainerIsolationLinux(t *testing.T) {
  102. d := Daemon{}
  103. _, err := d.verifyContainerSettings("linux", &containertypes.HostConfig{Isolation: containertypes.IsolationHyperV}, nil, false)
  104. assert.Check(t, is.Error(err, "invalid isolation 'hyperv' on linux"))
  105. }
  106. func TestShouldUnmountRoot(t *testing.T) {
  107. for _, test := range []struct {
  108. desc string
  109. root string
  110. info *mount.Info
  111. expect bool
  112. }{
  113. {
  114. desc: "root is at /",
  115. root: "/docker",
  116. info: &mount.Info{Root: "/docker", Mountpoint: "/docker"},
  117. expect: true,
  118. },
  119. {
  120. desc: "root is at in a submount from `/`",
  121. root: "/foo/docker",
  122. info: &mount.Info{Root: "/docker", Mountpoint: "/foo/docker"},
  123. expect: true,
  124. },
  125. {
  126. desc: "root is mounted in from a parent mount namespace same root dir", // dind is an example of this
  127. root: "/docker",
  128. info: &mount.Info{Root: "/docker/volumes/1234657/_data", Mountpoint: "/docker"},
  129. expect: false,
  130. },
  131. } {
  132. t.Run(test.desc, func(t *testing.T) {
  133. for _, options := range []struct {
  134. desc string
  135. Optional string
  136. expect bool
  137. }{
  138. {desc: "shared", Optional: "shared:", expect: true},
  139. {desc: "slave", Optional: "slave:", expect: false},
  140. {desc: "private", Optional: "private:", expect: false},
  141. } {
  142. t.Run(options.desc, func(t *testing.T) {
  143. expect := options.expect
  144. if expect {
  145. expect = test.expect
  146. }
  147. if test.info != nil {
  148. test.info.Optional = options.Optional
  149. }
  150. assert.Check(t, is.Equal(expect, shouldUnmountRoot(test.root, test.info)))
  151. })
  152. }
  153. })
  154. }
  155. }
  156. func checkMounted(t *testing.T, p string, expect bool) {
  157. t.Helper()
  158. mounted, err := mount.Mounted(p)
  159. assert.Check(t, err)
  160. assert.Check(t, mounted == expect, "expected %v, actual %v", expect, mounted)
  161. }
  162. func TestRootMountCleanup(t *testing.T) {
  163. if os.Getuid() != 0 {
  164. t.Skip("root required")
  165. }
  166. t.Parallel()
  167. testRoot, err := ioutil.TempDir("", t.Name())
  168. assert.NilError(t, err)
  169. defer os.RemoveAll(testRoot)
  170. cfg := &config.Config{}
  171. err = mount.MakePrivate(testRoot)
  172. assert.NilError(t, err)
  173. defer mount.Unmount(testRoot)
  174. cfg.ExecRoot = filepath.Join(testRoot, "exec")
  175. cfg.Root = filepath.Join(testRoot, "daemon")
  176. err = os.Mkdir(cfg.ExecRoot, 0755)
  177. assert.NilError(t, err)
  178. err = os.Mkdir(cfg.Root, 0755)
  179. assert.NilError(t, err)
  180. d := &Daemon{configStore: cfg, root: cfg.Root}
  181. unmountFile := getUnmountOnShutdownPath(cfg)
  182. t.Run("regular dir no mountpoint", func(t *testing.T) {
  183. err = setupDaemonRootPropagation(cfg)
  184. assert.NilError(t, err)
  185. _, err = os.Stat(unmountFile)
  186. assert.NilError(t, err)
  187. checkMounted(t, cfg.Root, true)
  188. assert.Assert(t, d.cleanupMounts())
  189. checkMounted(t, cfg.Root, false)
  190. _, err = os.Stat(unmountFile)
  191. assert.Assert(t, os.IsNotExist(err))
  192. })
  193. t.Run("root is a private mountpoint", func(t *testing.T) {
  194. err = mount.MakePrivate(cfg.Root)
  195. assert.NilError(t, err)
  196. defer mount.Unmount(cfg.Root)
  197. err = setupDaemonRootPropagation(cfg)
  198. assert.NilError(t, err)
  199. assert.Check(t, ensureShared(cfg.Root))
  200. _, err = os.Stat(unmountFile)
  201. assert.Assert(t, os.IsNotExist(err))
  202. assert.Assert(t, d.cleanupMounts())
  203. checkMounted(t, cfg.Root, true)
  204. })
  205. // mount is pre-configured with a shared mount
  206. t.Run("root is a shared mountpoint", func(t *testing.T) {
  207. err = mount.MakeShared(cfg.Root)
  208. assert.NilError(t, err)
  209. defer mount.Unmount(cfg.Root)
  210. err = setupDaemonRootPropagation(cfg)
  211. assert.NilError(t, err)
  212. if _, err := os.Stat(unmountFile); err == nil {
  213. t.Fatal("unmount file should not exist")
  214. }
  215. assert.Assert(t, d.cleanupMounts())
  216. checkMounted(t, cfg.Root, true)
  217. assert.Assert(t, mount.Unmount(cfg.Root))
  218. })
  219. // does not need mount but unmount file exists from previous run
  220. t.Run("old mount file is cleaned up on setup if not needed", func(t *testing.T) {
  221. err = mount.MakeShared(testRoot)
  222. assert.NilError(t, err)
  223. defer mount.MakePrivate(testRoot)
  224. err = ioutil.WriteFile(unmountFile, nil, 0644)
  225. assert.NilError(t, err)
  226. err = setupDaemonRootPropagation(cfg)
  227. assert.NilError(t, err)
  228. _, err = os.Stat(unmountFile)
  229. assert.Check(t, os.IsNotExist(err), err)
  230. checkMounted(t, cfg.Root, false)
  231. assert.Assert(t, d.cleanupMounts())
  232. })
  233. }