Skip to content

Instantly share code, notes, and snippets.

@eparis
Created April 16, 2015 21:29
Show Gist options
  • Save eparis/ddbf2b41ad14f5e0d993 to your computer and use it in GitHub Desktop.
Save eparis/ddbf2b41ad14f5e0d993 to your computer and use it in GitHub Desktop.
My thoughts on the kubernetes Mount() function
func doMount(source string, target string, fstype string, options []string) (string, error) {
// Build mount command as follows:
// mount [-t $fstype] [-o $options] [--source $source] --target $target
mountArgs := []string{}
if len(fstype) > 0 {
mountArgs = append(mountArgs, "-t", fstype)
}
if options != nil && len(options) > 0 {
mountArgs = append(mountArgs, "-o", strings.Join(options, ","))
}
if len(source) > 0 {
mountArgs = append(mountArgs, "--source", source)
}
mountArgs = append(mountArgs, "--target", target)
command := exec.Command("mount", mountArgs...)
return command.CombinedOutput()
}
func (mounter *Mounter) Mount(source string, target string, fstype string, options []string) error {
remountOpts := []string{"remount"}
if options != nil {
for _, option := range options {
switch option {
case "bind":
remountOpts = append(remountOpts, options)
options = "bind"
break
case "remount":
err := fmt.Errorf("invalid mount option 'remount'\n")
glog.Errorf("Mount failed: %v\nMounting arguments: %s %s %s %v\n", err, source, target, fstype)
return err
}
}
}
glog.V(5).Infof("Mounting %s %s %s %v", source, target, fstype, options)
output, err := doMount(source, target, fstype, options)
if err != nil {
glog.Errorf("Mount failed: %v\nMounting arguments: %s %s %s %v\nOutput: %s\n",
err, source, target, fstype, options, string(output))
return err
}
if len(remountOpts) == 1 {
return nil
}
// Bind mounts do not respect mount options, so we have to do those as a remount
glog.V(5).Infof("Remounting %s as to support VFS args (like ro)", target)
output, err = doMount(source, target, fstype, remountOpts)
if err != nil {
glog.Errorf("Remount failed: %v\nMounting arguments: %s %s %s %v\nOutput: %s\n",
err, source, target, fstype, options, string(output))
return err
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment