daemon_linux_test.go 11 KB

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