Skip to content

Commit

Permalink
feat: support to build in Darwin
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Yu <[email protected]>
  • Loading branch information
Yu-Jack authored and c3y1huang committed Sep 26, 2024
1 parent 797b589 commit 3a320d8
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 46 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_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
}
38 changes: 38 additions & 0 deletions ns/joiner_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ns

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

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

// 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
11 changes: 0 additions & 11 deletions types/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package types

import (
"time"

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

const (
Expand All @@ -28,15 +26,6 @@ const (
NamespaceNet = Namespace("net")
)

func (ns Namespace) Flag() uintptr {
switch ns {
case NamespaceNet:
return unix.CLONE_NEWNET
default:
return 0
}
}

func (ns Namespace) String() string {
return string(ns)
}
5 changes: 5 additions & 0 deletions types/namespace_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package types

func (ns Namespace) Flag() uintptr {
return 0
}
14 changes: 14 additions & 0 deletions types/namespace_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package types

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

func (ns Namespace) Flag() uintptr {
switch ns {
case NamespaceNet:
return unix.CLONE_NEWNET
default:
return 0
}
}

0 comments on commit 3a320d8

Please sign in to comment.