github.com/kim0/docker@v0.6.2-0.20161130212042-4addda3f07e7/volume/volume_linux.go (about) 1 // +build linux 2 3 package volume 4 5 import ( 6 "fmt" 7 "strings" 8 9 mounttypes "github.com/docker/docker/api/types/mount" 10 ) 11 12 // ConvertTmpfsOptions converts *mounttypes.TmpfsOptions to the raw option string 13 // for mount(2). 14 // The logic is copy-pasted from daemon/cluster/executer/container.getMountMask. 15 // It will be deduplicated when we migrated the cluster to the new mount scheme. 16 func ConvertTmpfsOptions(opt *mounttypes.TmpfsOptions) (string, error) { 17 if opt == nil { 18 return "", nil 19 } 20 var rawOpts []string 21 if opt.Mode != 0 { 22 rawOpts = append(rawOpts, fmt.Sprintf("mode=%o", opt.Mode)) 23 } 24 25 if opt.SizeBytes != 0 { 26 // calculate suffix here, making this linux specific, but that is 27 // okay, since API is that way anyways. 28 29 // we do this by finding the suffix that divides evenly into the 30 // value, returing the value itself, with no suffix, if it fails. 31 // 32 // For the most part, we don't enforce any semantic to this values. 33 // The operating system will usually align this and enforce minimum 34 // and maximums. 35 var ( 36 size = opt.SizeBytes 37 suffix string 38 ) 39 for _, r := range []struct { 40 suffix string 41 divisor int64 42 }{ 43 {"g", 1 << 30}, 44 {"m", 1 << 20}, 45 {"k", 1 << 10}, 46 } { 47 if size%r.divisor == 0 { 48 size = size / r.divisor 49 suffix = r.suffix 50 break 51 } 52 } 53 54 rawOpts = append(rawOpts, fmt.Sprintf("size=%d%s", size, suffix)) 55 } 56 return strings.Join(rawOpts, ","), nil 57 }