github.com/cdoern/storage@v1.12.13/pkg/mount/mounter_freebsd.go (about)

     1  package 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  	"fmt"
    15  	"strings"
    16  	"unsafe"
    17  
    18  	"golang.org/x/sys/unix"
    19  )
    20  
    21  func allocateIOVecs(options []string) []C.struct_iovec {
    22  	out := make([]C.struct_iovec, len(options))
    23  	for i, option := range options {
    24  		out[i].iov_base = unsafe.Pointer(C.CString(option))
    25  		out[i].iov_len = C.size_t(len(option) + 1)
    26  	}
    27  	return out
    28  }
    29  
    30  func mount(device, target, mType string, flag uintptr, data string) error {
    31  	isNullFS := false
    32  
    33  	xs := strings.Split(data, ",")
    34  	for _, x := range xs {
    35  		if x == "bind" {
    36  			isNullFS = true
    37  		}
    38  	}
    39  
    40  	options := []string{"fspath", target}
    41  	if isNullFS {
    42  		options = append(options, "fstype", "nullfs", "target", device)
    43  	} else {
    44  		options = append(options, "fstype", mType, "from", device)
    45  	}
    46  	rawOptions := allocateIOVecs(options)
    47  	for _, rawOption := range rawOptions {
    48  		defer C.free(rawOption.iov_base)
    49  	}
    50  
    51  	if errno := C.nmount(&rawOptions[0], C.uint(len(options)), C.int(flag)); errno != 0 {
    52  		reason := C.GoString(C.strerror(*C.__error()))
    53  		return fmt.Errorf("Failed to call nmount: %s", reason)
    54  	}
    55  	return nil
    56  }
    57  
    58  func unmount(target string, flag int) error {
    59  	return unix.Unmount(target, flag)
    60  }