Skip to content

Commit

Permalink
feat: support to build in Darwin
Browse files Browse the repository at this point in the history
  • Loading branch information
Yu-Jack committed Sep 25, 2024
1 parent 797b589 commit d9ab722
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 35 deletions.
13 changes: 7 additions & 6 deletions io/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/pkg/errors"
"github.com/shirou/gopsutil/v3/disk"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"

"github.com/longhorn/go-common-libs/types"
)
Expand Down Expand Up @@ -227,8 +228,8 @@ func GetDiskStat(path string) (diskStat types.DiskStat, err error) {
err = errors.Wrapf(err, "failed to get fs stat for %v", path)
}()

var statfs syscall.Statfs_t
if err := syscall.Statfs(path, &statfs); err != nil {
var statfs unix.Statfs_t
if err := unix.Statfs(path, &statfs); err != nil {
return diskStat, err
}

Expand All @@ -239,7 +240,7 @@ func GetDiskStat(path string) (diskStat types.DiskStat, err error) {

// Convert the FSID components to a single uint64 FSID value
var fsidValue uint64
for _, component := range statfs.Fsid.X__val {
for _, component := range statfs.Fsid.Val {
// Combine components using bit manipulation
fsidValue = (fsidValue << 32) | uint64(uint32(component))
}
Expand All @@ -254,9 +255,9 @@ func GetDiskStat(path string) (diskStat types.DiskStat, err error) {
Driver: types.DiskDriverNone,
FreeBlocks: int64(statfs.Bfree),
TotalBlocks: int64(statfs.Blocks),
BlockSize: statfs.Bsize,
StorageMaximum: int64(statfs.Blocks) * statfs.Bsize,
StorageAvailable: int64(statfs.Bfree) * statfs.Bsize,
BlockSize: int64(statfs.Bsize),
StorageMaximum: int64(statfs.Blocks) * int64(statfs.Bsize),
StorageAvailable: int64(statfs.Bfree) * int64(statfs.Bsize),
}, nil
}

Expand Down
27 changes: 1 addition & 26 deletions ns/joiner.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,6 @@ func (joiners *Joiners) JoinReverse() (err error) {
return joiners.Join()
}

// Join joins all the namespaces in the Joiners.
func (joiners *Joiners) Join() (err error) {
for _, joiner := range *joiners {
if joiner.isJoined {
logrus.Tracef("Already joined namespace: %s", joiner.namespace)
continue
}

if joiner.namespace == types.NamespaceMnt {
err := unix.Unshare(unix.CLONE_NEWNS)
if err != nil {
return errors.Wrapf(err, "failed to unshare namespace: %+s", joiner.namespace)
}
}

if err := unix.Setns(joiner.fd, 0); err != nil {
return errors.Wrapf(err, "failed to set namespace: %+s", joiner.namespace)
}

joiner.isJoined = true
logrus.Tracef("Joined namespace: %v", joiner.namespace)
}
return nil
}

// Reset resets all the Joiners.
func (joiners *Joiners) Reset() (err error) {
for _, joiner := range *joiners {
Expand Down Expand Up @@ -214,7 +189,7 @@ func (jd *JoinerDescriptor) openAndRecordNamespaceFiles(namespace types.Namespac
// The original namespace file is the namespace file of the process thread that is
// executing the joiner (e.g. /proc/1/task/2/ns/mnt)
func (jd *JoinerDescriptor) openAndRecordOriginalNamespaceFile(ns string, namespace types.Namespace) error {
pthreadFile := filepath.Join("/proc", fmt.Sprint(os.Getpid()), "task", fmt.Sprint(unix.Gettid()), "ns", ns)
pthreadFile := filepath.Join("/proc", fmt.Sprint(os.Getpid()), "task", fmt.Sprint(Gettid()), "ns", ns)
originFd, err := jd.origin.OpenFile(pthreadFile)
if err != nil {
return errors.Wrapf(err, "failed to open process thread file %v", pthreadFile)
Expand Down
10 changes: 10 additions & 0 deletions ns/joiner_helper_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ns

// Join joins all the namespaces in the Joiners.
func (joiners *Joiners) Join() (err error) {
return nil
}

func Gettid() int {
return 0
}
32 changes: 32 additions & 0 deletions ns/joiner_helper_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ns

import "golang.org/x/sys/unix"

// Join joins all the namespaces in the Joiners.
func (joiners *Joiners) Join() (err error) {
for _, joiner := range *joiners {
if joiner.isJoined {
logrus.Tracef("Already joined namespace: %s", joiner.namespace)
continue
}

if joiner.namespace == types.NamespaceMnt {
err := unix.Unshare(unix.CLONE_NEWNS)
if err != nil {
return errors.Wrapf(err, "failed to unshare namespace: %+s", joiner.namespace)
}
}

if err := unix.Setns(joiner.fd, 0); err != nil {
return errors.Wrapf(err, "failed to set namespace: %+s", joiner.namespace)
}

joiner.isJoined = true
logrus.Tracef("Joined namespace: %v", joiner.namespace)
}
return nil
}

func Gettid() int {
return unix.Gettid()
}
6 changes: 3 additions & 3 deletions sys/sys.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import (
"path/filepath"
"strconv"
"strings"
"syscall"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"

"github.com/longhorn/go-common-libs/types"
)

// GetKernelRelease returns the kernel release string.
func GetKernelRelease() (string, error) {
utsname := &syscall.Utsname{}
if err := syscall.Uname(utsname); err != nil {
utsname := &unix.Utsname{}
if err := unix.Uname(utsname); err != nil {
logrus.WithError(err).Warn("Failed to get kernel release")
return "", err
}
Expand Down
35 changes: 35 additions & 0 deletions types/namespace_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package types

import (
"time"
)

const (
HostProcDirectory = "/host/proc"
ProcDirectory = "/proc"
)

const NsBinary = "nsenter"

const (
ErrNamespaceCastResultFmt = "failed casting result to %T: %v"
ErrNamespaceFuncFmt = "failed function: %v"
)

var NsJoinerDefaultTimeout = 24 * time.Hour

type Namespace string

const (
NamespaceIpc = Namespace("ipc")
NamespaceMnt = Namespace("mnt")
NamespaceNet = Namespace("net")
)

func (ns Namespace) Flag() uintptr {
return 0
}

func (ns Namespace) String() string {
return string(ns)
}
File renamed without changes.

0 comments on commit d9ab722

Please sign in to comment.