mounter.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "fmt"
  16. "os"
  17. "os/exec"
  18. "path/filepath"
  19. "strings"
  20. )
  21. const (
  22. // Location of the mount file to use
  23. chrootCmd = "chroot"
  24. mountCmd = "mount"
  25. rootfs = "rootfs"
  26. nfsRPCBindErrMsg = "mount.nfs: rpc.statd is not running but is required for remote locking.\nmount.nfs: Either use '-o nolock' to keep locks local, or start statd.\nmount.nfs: an incorrect mount option was specified\n"
  27. rpcBindCmd = "/sbin/rpcbind"
  28. defaultRootfs = "/home/kubernetes/containerized_mounter/rootfs"
  29. )
  30. func main() {
  31. if len(os.Args) < 2 {
  32. fmt.Fprintf(os.Stderr, "Command failed: must provide a command to run.\n")
  33. return
  34. }
  35. path, _ := filepath.Split(os.Args[0])
  36. rootfsPath := filepath.Join(path, rootfs)
  37. if _, err := os.Stat(rootfsPath); os.IsNotExist(err) {
  38. rootfsPath = defaultRootfs
  39. }
  40. command := os.Args[1]
  41. switch command {
  42. case mountCmd:
  43. mountErr := mountInChroot(rootfsPath, os.Args[2:])
  44. if mountErr != nil {
  45. fmt.Fprintf(os.Stderr, "Mount failed: %v", mountErr)
  46. os.Exit(1)
  47. }
  48. default:
  49. fmt.Fprintf(os.Stderr, "Unknown command, must be %s", mountCmd)
  50. os.Exit(1)
  51. }
  52. }
  53. // MountInChroot is to run mount within chroot with the passing root directory
  54. func mountInChroot(rootfsPath string, args []string) error {
  55. if _, err := os.Stat(rootfsPath); os.IsNotExist(err) {
  56. return fmt.Errorf("path <%s> does not exist", rootfsPath)
  57. }
  58. args = append([]string{rootfsPath, mountCmd}, args...)
  59. output, err := exec.Command(chrootCmd, args...).CombinedOutput()
  60. if err == nil {
  61. return nil
  62. }
  63. if !strings.EqualFold(string(output), nfsRPCBindErrMsg) {
  64. // Mount failed but not because of RPC bind error
  65. return fmt.Errorf("mount failed: %v\nMounting command: %s\nMounting arguments: %v\nOutput: %s", err, chrootCmd, args, string(output))
  66. }
  67. // Mount failed because it is NFS V3 and we need to run rpcBind
  68. output, err = exec.Command(chrootCmd, rootfsPath, rpcBindCmd, "-w").CombinedOutput()
  69. if err != nil {
  70. return fmt.Errorf("mount issued for NFS V3 but unable to run rpcbind:\n Output: %s\n Error: %v", string(output), err)
  71. }
  72. // Rpcbind is running, try mounting again
  73. output, err = exec.Command(chrootCmd, args...).CombinedOutput()
  74. if err != nil {
  75. return fmt.Errorf("mount failed for NFS V3 even after running rpcBind %s, %v", string(output), err)
  76. }
  77. return nil
  78. }