github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/pkg/mount/mounter_freebsd.go (about)

     1  package mount // import "github.com/docker/docker/pkg/mount"
     2  
     3  /*
     4  #include <errno.h>
     5  #include <stdlib.h>
     6  #include <string.h>
     7  #include <sys/_iovec.h>
     8  #include <sys/mount.h>
     9  #include <sys/param.h>
    10  */
    11  import "C"
    12  
    13  import (
    14  	"strings"
    15  	"syscall"
    16  	"unsafe"
    17  )
    18  
    19  func allocateIOVecs(options []string) []C.struct_iovec {
    20  	out := make([]C.struct_iovec, len(options))
    21  	for i, option := range options {
    22  		out[i].iov_base = unsafe.Pointer(C.CString(option))
    23  		out[i].iov_len = C.size_t(len(option) + 1)
    24  	}
    25  	return out
    26  }
    27  
    28  func mount(device, target, mType string, flag uintptr, data string) error {
    29  	isNullFS := false
    30  
    31  	xs := strings.Split(data, ",")
    32  	for _, x := range xs {
    33  		if x == "bind" {
    34  			isNullFS = true
    35  		}
    36  	}
    37  
    38  	options := []string{"fspath", target}
    39  	if isNullFS {
    40  		options = append(options, "fstype", "nullfs", "target", device)
    41  	} else {
    42  		options = append(options, "fstype", mType, "from", device)
    43  	}
    44  	rawOptions := allocateIOVecs(options)
    45  	for _, rawOption := range rawOptions {
    46  		defer C.free(rawOption.iov_base)
    47  	}
    48  
    49  	if errno := C.nmount(&rawOptions[0], C.uint(len(options)), C.int(flag)); errno != 0 {
    50  		return &mountError{
    51  			op:     "mount",
    52  			source: device,
    53  			target: target,
    54  			flags:  flag,
    55  			err:    syscall.Errno(errno),
    56  		}
    57  	}
    58  	return nil
    59  }