-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from yukiisbored/yuki_is_bored/openbsd_mounter
Create proper mounter for OpenBSD
- Loading branch information
Showing
2 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// +build freebsd,cgo openbsd,cgo | ||
// +build freebsd,cgo | ||
|
||
package mount | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// +build openbsd,cgo | ||
|
||
/* | ||
Due to how OpenBSD mount(2) works, filesystem types need to be | ||
supported explicitly since it uses separate structs to pass | ||
filesystem-specific arguments. | ||
For now only UFS/FFS is supported as it's the default fs | ||
on OpenBSD systems. | ||
See: https://man.openbsd.org/mount.2 | ||
*/ | ||
|
||
package mount | ||
|
||
/* | ||
#include <sys/types.h> | ||
#include <sys/mount.h> | ||
*/ | ||
import "C" | ||
|
||
import ( | ||
"fmt" | ||
"syscall" | ||
"unsafe" | ||
) | ||
|
||
func createExportInfo(readOnly bool) C.struct_export_args { | ||
exportFlags := C.int(0) | ||
if readOnly { | ||
exportFlags = C.MNT_EXRDONLY | ||
} | ||
out := C.struct_export_args{ | ||
ex_root: 0, | ||
ex_flags: exportFlags, | ||
} | ||
return out | ||
} | ||
|
||
func createUfsArgs(device string, readOnly bool) unsafe.Pointer { | ||
out := &C.struct_ufs_args{ | ||
fspec: C.CString(device), | ||
export_info: createExportInfo(readOnly), | ||
} | ||
return unsafe.Pointer(out) | ||
} | ||
|
||
func mount(device, target, mType string, flag uintptr, data string) error { | ||
readOnly := flag&RDONLY != 0 | ||
|
||
var fsArgs unsafe.Pointer | ||
|
||
switch mType { | ||
case "ffs": | ||
fsArgs = createUfsArgs(device, readOnly) | ||
default: | ||
return &mountError{ | ||
op: "mount", | ||
source: device, | ||
target: target, | ||
flags: flag, | ||
err: fmt.Errorf("unsupported file system type: %s", mType), | ||
} | ||
} | ||
|
||
if errno := C.mount(C.CString(mType), C.CString(target), C.int(flag), fsArgs); errno != 0 { | ||
return &mountError{ | ||
op: "mount", | ||
source: device, | ||
target: target, | ||
flags: flag, | ||
err: syscall.Errno(errno), | ||
} | ||
} | ||
|
||
return nil | ||
} |