Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support to build in Darwin #71

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Check warning on line 16 in ns/joiner_linux.go

View check run for this annotation

Codecov / codecov/patch

ns/joiner_linux.go#L15-L16

Added lines #L15 - L16 were not covered by tests
}

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)

Check warning on line 22 in ns/joiner_linux.go

View check run for this annotation

Codecov / codecov/patch

ns/joiner_linux.go#L22

Added line #L22 was not covered by tests
}
}

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

Check warning on line 27 in ns/joiner_linux.go

View check run for this annotation

Codecov / codecov/patch

ns/joiner_linux.go#L27

Added line #L27 was not covered by tests
}

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
}
}