| 1 |
|
| 2 |
|
| 3 |
package mount |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
import "C" |
| 14 |
|
| 15 |
import ( |
| 16 |
"strings" |
| 17 |
"syscall" |
| 18 |
"unsafe" |
| 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 |
return &mountError{ |
| 53 |
op: "mount", |
| 54 |
source: device, |
| 55 |
target: target, |
| 56 |
flags: flag, |
| 57 |
err: syscall.Errno(errno), |
| 58 |
} |
| 59 |
} |
| 60 |
return nil |
| 61 |
} |