mt.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // +build linux
  2. /*
  3. Copyright 2015 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package main
  15. import (
  16. "flag"
  17. "fmt"
  18. "io/ioutil"
  19. "os"
  20. "syscall"
  21. "time"
  22. )
  23. var (
  24. fsTypePath = ""
  25. fileModePath = ""
  26. filePermPath = ""
  27. fileOwnerPath = ""
  28. newFilePath0644 = ""
  29. newFilePath0666 = ""
  30. newFilePath0660 = ""
  31. newFilePath0777 = ""
  32. readFileContentPath = ""
  33. readFileContentInLoopPath = ""
  34. retryDuration = 180
  35. breakOnExpectedContent = true
  36. )
  37. func init() {
  38. flag.StringVar(&fsTypePath, "fs_type", "", "Path to print the fs type for")
  39. flag.StringVar(&fileModePath, "file_mode", "", "Path to print the mode bits of")
  40. flag.StringVar(&filePermPath, "file_perm", "", "Path to print the perms of")
  41. flag.StringVar(&fileOwnerPath, "file_owner", "", "Path to print the owning UID and GID of")
  42. flag.StringVar(&newFilePath0644, "new_file_0644", "", "Path to write to and read from with perm 0644")
  43. flag.StringVar(&newFilePath0666, "new_file_0666", "", "Path to write to and read from with perm 0666")
  44. flag.StringVar(&newFilePath0660, "new_file_0660", "", "Path to write to and read from with perm 0660")
  45. flag.StringVar(&newFilePath0777, "new_file_0777", "", "Path to write to and read from with perm 0777")
  46. flag.StringVar(&readFileContentPath, "file_content", "", "Path to read the file content from")
  47. flag.StringVar(&readFileContentInLoopPath, "file_content_in_loop", "", "Path to read the file content in loop from")
  48. flag.IntVar(&retryDuration, "retry_time", 180, "Retry time during the loop")
  49. flag.BoolVar(&breakOnExpectedContent, "break_on_expected_content", true, "Break out of loop on expected content, (use with --file_content_in_loop flag only)")
  50. }
  51. // This program performs some tests on the filesystem as dictated by the
  52. // flags passed by the user.
  53. func main() {
  54. flag.Parse()
  55. var (
  56. err error
  57. errs = []error{}
  58. )
  59. // Clear the umask so we can set any mode bits we want.
  60. syscall.Umask(0000)
  61. // NOTE: the ordering of execution of the various command line
  62. // flags is intentional and allows a single command to:
  63. //
  64. // 1. Check the fstype of a path
  65. // 2. Write a new file within that path
  66. // 3. Check that the file's content can be read
  67. //
  68. // Changing the ordering of the following code will break tests.
  69. err = fsType(fsTypePath)
  70. if err != nil {
  71. errs = append(errs, err)
  72. }
  73. err = readWriteNewFile(newFilePath0644, 0644)
  74. if err != nil {
  75. errs = append(errs, err)
  76. }
  77. err = readWriteNewFile(newFilePath0666, 0666)
  78. if err != nil {
  79. errs = append(errs, err)
  80. }
  81. err = readWriteNewFile(newFilePath0660, 0660)
  82. if err != nil {
  83. errs = append(errs, err)
  84. }
  85. err = readWriteNewFile(newFilePath0777, 0777)
  86. if err != nil {
  87. errs = append(errs, err)
  88. }
  89. err = fileMode(fileModePath)
  90. if err != nil {
  91. errs = append(errs, err)
  92. }
  93. err = filePerm(filePermPath)
  94. if err != nil {
  95. errs = append(errs, err)
  96. }
  97. err = fileOwner(fileOwnerPath)
  98. if err != nil {
  99. errs = append(errs, err)
  100. }
  101. err = readFileContent(readFileContentPath)
  102. if err != nil {
  103. errs = append(errs, err)
  104. }
  105. err = readFileContentInLoop(readFileContentInLoopPath, retryDuration, breakOnExpectedContent)
  106. if err != nil {
  107. errs = append(errs, err)
  108. }
  109. if len(errs) != 0 {
  110. os.Exit(1)
  111. }
  112. os.Exit(0)
  113. }
  114. // Defined by Linux (sys/statfs.h) - the type number for tmpfs mounts.
  115. const linuxTmpfsMagic = 0x01021994
  116. func fsType(path string) error {
  117. if path == "" {
  118. return nil
  119. }
  120. buf := syscall.Statfs_t{}
  121. if err := syscall.Statfs(path, &buf); err != nil {
  122. fmt.Printf("error from statfs(%q): %v\n", path, err)
  123. return err
  124. }
  125. if buf.Type == linuxTmpfsMagic {
  126. fmt.Printf("mount type of %q: tmpfs\n", path)
  127. } else {
  128. fmt.Printf("mount type of %q: %v\n", path, buf.Type)
  129. }
  130. return nil
  131. }
  132. func fileMode(path string) error {
  133. if path == "" {
  134. return nil
  135. }
  136. fileinfo, err := os.Stat(path)
  137. if err != nil {
  138. fmt.Printf("error from Stat(%q): %v\n", path, err)
  139. return err
  140. }
  141. fmt.Printf("mode of file %q: %v\n", path, fileinfo.Mode())
  142. return nil
  143. }
  144. func filePerm(path string) error {
  145. if path == "" {
  146. return nil
  147. }
  148. fileinfo, err := os.Stat(path)
  149. if err != nil {
  150. fmt.Printf("error from Stat(%q): %v\n", path, err)
  151. return err
  152. }
  153. fmt.Printf("perms of file %q: %v\n", path, fileinfo.Mode().Perm())
  154. return nil
  155. }
  156. func fileOwner(path string) error {
  157. if path == "" {
  158. return nil
  159. }
  160. buf := syscall.Stat_t{}
  161. if err := syscall.Stat(path, &buf); err != nil {
  162. fmt.Printf("error from stat(%q): %v\n", path, err)
  163. return err
  164. }
  165. fmt.Printf("owner UID of %q: %v\n", path, buf.Uid)
  166. fmt.Printf("owner GID of %q: %v\n", path, buf.Gid)
  167. return nil
  168. }
  169. func readFileContent(path string) error {
  170. if path == "" {
  171. return nil
  172. }
  173. contentBytes, err := ioutil.ReadFile(path)
  174. if err != nil {
  175. fmt.Printf("error reading file content for %q: %v\n", path, err)
  176. return err
  177. }
  178. fmt.Printf("content of file %q: %v\n", path, string(contentBytes))
  179. return nil
  180. }
  181. const initialContent string = "mount-tester new file\n"
  182. func readWriteNewFile(path string, perm os.FileMode) error {
  183. if path == "" {
  184. return nil
  185. }
  186. err := ioutil.WriteFile(path, []byte(initialContent), perm)
  187. if err != nil {
  188. fmt.Printf("error writing new file %q: %v\n", path, err)
  189. return err
  190. }
  191. return readFileContent(path)
  192. }
  193. func readFileContentInLoop(path string, retryDuration int, breakOnExpectedContent bool) error {
  194. if path == "" {
  195. return nil
  196. }
  197. return testFileContent(path, retryDuration, breakOnExpectedContent)
  198. }
  199. func testFileContent(filePath string, retryDuration int, breakOnExpectedContent bool) error {
  200. var (
  201. contentBytes []byte
  202. err error
  203. )
  204. retryTime := time.Second * time.Duration(retryDuration)
  205. for start := time.Now(); time.Since(start) < retryTime; time.Sleep(2 * time.Second) {
  206. contentBytes, err = ioutil.ReadFile(filePath)
  207. if err != nil {
  208. fmt.Printf("Error reading file %s: %v, retrying\n", filePath, err)
  209. continue
  210. }
  211. fmt.Printf("content of file %q: %v\n", filePath, string(contentBytes))
  212. if breakOnExpectedContent {
  213. if string(contentBytes) != initialContent {
  214. fmt.Printf("Unexpected content. Expected: %s. Retrying", initialContent)
  215. continue
  216. }
  217. break
  218. }
  219. }
  220. return err
  221. }