github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/daemon/cluster/executor/container/validate.go (about)

     1  package container
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/docker/swarmkit/api"
    10  )
    11  
    12  func validateMounts(mounts []api.Mount) error {
    13  	for _, mount := range mounts {
    14  		// Target must always be absolute
    15  		if !filepath.IsAbs(mount.Target) {
    16  			return fmt.Errorf("invalid mount target, must be an absolute path: %s", mount.Target)
    17  		}
    18  
    19  		switch mount.Type {
    20  		// The checks on abs paths are required due to the container API confusing
    21  		// volume mounts as bind mounts when the source is absolute (and vice-versa)
    22  		// See #25253
    23  		// TODO: This is probably not necessary once #22373 is merged
    24  		case api.MountTypeBind:
    25  			if !filepath.IsAbs(mount.Source) {
    26  				return fmt.Errorf("invalid bind mount source, must be an absolute path: %s", mount.Source)
    27  			}
    28  			if _, err := os.Stat(mount.Source); os.IsNotExist(err) {
    29  				return fmt.Errorf("invalid bind mount source, source path not found: %s", mount.Source)
    30  			}
    31  		case api.MountTypeVolume:
    32  			if filepath.IsAbs(mount.Source) {
    33  				return fmt.Errorf("invalid volume mount source, must not be an absolute path: %s", mount.Source)
    34  			}
    35  		case api.MountTypeTmpfs:
    36  			if mount.Source != "" {
    37  				return errors.New("invalid tmpfs source, source must be empty")
    38  			}
    39  		default:
    40  			return fmt.Errorf("invalid mount type: %s", mount.Type)
    41  		}
    42  	}
    43  	return nil
    44  }