Advertisement
Guest User

Untitled

a guest
May 27th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. func doMount(source string, target string, fstype string, options []string) (string, error) {
  2. // Build mount command as follows:
  3. // mount [-t $fstype] [-o $options] [--source $source] --target $target
  4. mountArgs := []string{}
  5. if len(fstype) > 0 {
  6. mountArgs = append(mountArgs, "-t", fstype)
  7. }
  8. if options != nil && len(options) > 0 {
  9. mountArgs = append(mountArgs, "-o", strings.Join(options, ","))
  10. }
  11. if len(source) > 0 {
  12. mountArgs = append(mountArgs, "--source", source)
  13. }
  14. mountArgs = append(mountArgs, "--target", target)
  15. command := exec.Command("mount", mountArgs...)
  16. return command.CombinedOutput()
  17. }
  18.  
  19. func (mounter *Mounter) Mount(source string, target string, fstype string, options []string) error {
  20. remountOpts := []string{"remount"}
  21. if options != nil {
  22. for _, option := range options {
  23. switch option {
  24. case "bind":
  25. remountOpts = append(remountOpts, options)
  26. options = "bind"
  27. break
  28. case "remount":
  29. err := fmt.Errorf("invalid mount option 'remount'\n")
  30. glog.Errorf("Mount failed: %v\nMounting arguments: %s %s %s %v\n", err, source, target, fstype)
  31. return err
  32. }
  33. }
  34. }
  35.  
  36. glog.V(5).Infof("Mounting %s %s %s %v", source, target, fstype, options)
  37. output, err := doMount(source, target, fstype, options)
  38. if err != nil {
  39. glog.Errorf("Mount failed: %v\nMounting arguments: %s %s %s %v\nOutput: %s\n",
  40. err, source, target, fstype, options, string(output))
  41. return err
  42. }
  43.  
  44. if len(remountOpts) == 1 {
  45. return nil
  46. }
  47. // Bind mounts do not respect mount options, so we have to do those as a remount
  48. glog.V(5).Infof("Remounting %s as to support VFS args (like ro)", target)
  49. output, err = doMount(source, target, fstype, remountOpts)
  50. if err != nil {
  51. glog.Errorf("Remount failed: %v\nMounting arguments: %s %s %s %v\nOutput: %s\n",
  52. err, source, target, fstype, options, string(output))
  53. return err
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement