gitee.com/bomy/docker.git@v1.13.1/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  func ConvertTmpfsOptions(opt *mounttypes.TmpfsOptions, readOnly bool) (string, error) {
    15  	var rawOpts []string
    16  	if readOnly {
    17  		rawOpts = append(rawOpts, "ro")
    18  	}
    19  
    20  	if opt != nil && opt.Mode != 0 {
    21  		rawOpts = append(rawOpts, fmt.Sprintf("mode=%o", opt.Mode))
    22  	}
    23  
    24  	if opt != nil && opt.SizeBytes != 0 {
    25  		// calculate suffix here, making this linux specific, but that is
    26  		// okay, since API is that way anyways.
    27  
    28  		// we do this by finding the suffix that divides evenly into the
    29  		// value, returing the value itself, with no suffix, if it fails.
    30  		//
    31  		// For the most part, we don't enforce any semantic to this values.
    32  		// The operating system will usually align this and enforce minimum
    33  		// and maximums.
    34  		var (
    35  			size   = opt.SizeBytes
    36  			suffix string
    37  		)
    38  		for _, r := range []struct {
    39  			suffix  string
    40  			divisor int64
    41  		}{
    42  			{"g", 1 << 30},
    43  			{"m", 1 << 20},
    44  			{"k", 1 << 10},
    45  		} {
    46  			if size%r.divisor == 0 {
    47  				size = size / r.divisor
    48  				suffix = r.suffix
    49  				break
    50  			}
    51  		}
    52  
    53  		rawOpts = append(rawOpts, fmt.Sprintf("size=%d%s", size, suffix))
    54  	}
    55  	return strings.Join(rawOpts, ","), nil
    56  }