From 8730e2f5e6ecf19a2e8186b681a60f60bf253bac Mon Sep 17 00:00:00 2001 From: "Ronald G. Minnich" Date: Sun, 29 Sep 2024 02:13:15 +0000 Subject: [PATCH 1/3] Get cpu client working on freebsd Signed-off-by: Ronald G. Minnich --- client/cpu9p_unix.go | 8 +++-- client/cpu9p_unix_test.go | 10 ++++--- client/cpu_freebsd.go | 20 +++++++++++++ client/getattr_freebsd.go | 63 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 7 deletions(-) create mode 100644 client/cpu_freebsd.go create mode 100644 client/getattr_freebsd.go diff --git a/client/cpu9p_unix.go b/client/cpu9p_unix.go index f132098a..4525c23d 100644 --- a/client/cpu9p_unix.go +++ b/client/cpu9p_unix.go @@ -9,6 +9,7 @@ package client import ( "errors" + "fmt" "os" "time" @@ -34,7 +35,7 @@ func (l *CPU9P) SetAttr(mask p9.SetAttrMask, attr p9.SetAttr) error { if mask.Size { if e := unix.Truncate(l.path, int64(attr.Size)); e != nil { - err = errors.Join(err, e) + err = errors.Join(err, fmt.Errorf("Truncate:%w", err)) } } if mask.ATime || mask.MTime { @@ -56,8 +57,9 @@ func (l *CPU9P) SetAttr(mask p9.SetAttrMask, attr p9.SetAttr) error { verbose("mask.CTime is set by client; ignoring") } if mask.Permissions { - if e := unix.Chmod(l.path, uint32(attr.Permissions)); e != nil { - err = errors.Join(err, e) + perm := uint32(attr.Permissions) + if e := unix.Chmod(l.path, perm); e != nil { + err = errors.Join(err, fmt.Errorf("%q:%o:%w", l.path, perm, err)) } } diff --git a/client/cpu9p_unix_test.go b/client/cpu9p_unix_test.go index af9bdfa3..a4083e58 100644 --- a/client/cpu9p_unix_test.go +++ b/client/cpu9p_unix_test.go @@ -10,6 +10,7 @@ package client import ( "os" "reflect" + "runtime" "path/filepath" "testing" @@ -87,7 +88,7 @@ func Test9pUnix(t *testing.T) { sam.Permissions = true if err := c.SetAttr(sam, sa); err != nil { - t.Fatalf("Setattr with mode: %v != nil", err) + t.Fatalf("Setattr(%v, %v): %v != nil", sam, sa, err) } _, _, ga2, err = c.GetAttr(m) if err != nil { @@ -178,13 +179,14 @@ func Test9pUnix(t *testing.T) { sam.Permissions = true sa.Permissions = 0 if err := c.SetAttr(sam, sa); err != nil { - t.Fatalf("Setattr with mode: %v != nil", err) + t.Fatalf("Setattr(%v,%v): %v != nil", sam, sa, err) } // The second time, since we blew permissions to // zero, we ought to get an error. - if err := c.SetAttr(sam, sa); err == nil { - t.Fatalf("Setattr with mode: nil != %v", unix.EPERM) + // It seems, on freebsd, there is no error. + if err := c.SetAttr(sam, sa); runtime.GOOS != "freebsd" && err == nil { + t.Fatalf("Setattr(%v,%v): nil != %v", sam, sa, unix.EPERM) } // But getattr should work. diff --git a/client/cpu_freebsd.go b/client/cpu_freebsd.go new file mode 100644 index 00000000..f2ed9d8c --- /dev/null +++ b/client/cpu_freebsd.go @@ -0,0 +1,20 @@ +// Copyright 2018-2019 the u-root Authors. All rights reserved +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package client + +import ( + "os" + "syscall" + + "github.com/hugelgupf/p9/p9" +) + +func osflags(fi os.FileInfo, mode p9.OpenFlags) int { + flags := int(mode) + if fi.IsDir() { + flags |= syscall.O_DIRECTORY + } + return flags +} diff --git a/client/getattr_freebsd.go b/client/getattr_freebsd.go new file mode 100644 index 00000000..795e5a61 --- /dev/null +++ b/client/getattr_freebsd.go @@ -0,0 +1,63 @@ +// Copyright 2018 The gVisor Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package client + +import ( + "syscall" + + "github.com/hugelgupf/p9/p9" +) + +// GetAttr implements p9.File.GetAttr. +// +// Not fully implemented. +func (l *CPU9P) GetAttr(req p9.AttrMask) (p9.QID, p9.AttrMask, p9.Attr, error) { + qid, fi, err := l.info() + if err != nil { + return qid, p9.AttrMask{}, p9.Attr{}, err + } + + stat := fi.Sys().(*syscall.Stat_t) + attr := p9.Attr{ + Mode: p9.FileMode(stat.Mode), + UID: p9.UID(stat.Uid), + GID: p9.GID(stat.Gid), + NLink: p9.NLink(stat.Nlink), + RDev: p9.Dev(stat.Rdev), + Size: uint64(stat.Size), + BlockSize: uint64(stat.Blksize), + Blocks: uint64(stat.Blocks), + ATimeSeconds: uint64(stat.Atimespec.Sec), + ATimeNanoSeconds: uint64(stat.Atimespec.Nsec), + MTimeSeconds: uint64(stat.Mtimespec.Sec), + MTimeNanoSeconds: uint64(stat.Mtimespec.Nsec), + CTimeSeconds: uint64(stat.Ctimespec.Sec), + CTimeNanoSeconds: uint64(stat.Ctimespec.Nsec), + } + valid := p9.AttrMask{ + Mode: true, + UID: true, + GID: true, + NLink: true, + RDev: true, + Size: true, + Blocks: true, + ATime: true, + MTime: true, + CTime: true, + } + + return qid, valid, attr, nil +} From 05895c1d67ad74c89d2204e46a04429974483798 Mon Sep 17 00:00:00 2001 From: "Ronald G. Minnich" Date: Tue, 8 Oct 2024 08:03:29 +0000 Subject: [PATCH 2/3] Tidy up following review comments. Signed-off-by: Ronald G. Minnich --- client/cpu9p_unix.go | 2 +- client/cpu_freebsd.go | 2 +- client/getattr_freebsd.go | 2 -- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/client/cpu9p_unix.go b/client/cpu9p_unix.go index 4525c23d..e0dfe80a 100644 --- a/client/cpu9p_unix.go +++ b/client/cpu9p_unix.go @@ -35,7 +35,7 @@ func (l *CPU9P) SetAttr(mask p9.SetAttrMask, attr p9.SetAttr) error { if mask.Size { if e := unix.Truncate(l.path, int64(attr.Size)); e != nil { - err = errors.Join(err, fmt.Errorf("Truncate:%w", err)) + err = errors.Join(err, fmt.Errorf("truncate:%w", err)) } } if mask.ATime || mask.MTime { diff --git a/client/cpu_freebsd.go b/client/cpu_freebsd.go index f2ed9d8c..a56d89d3 100644 --- a/client/cpu_freebsd.go +++ b/client/cpu_freebsd.go @@ -1,4 +1,4 @@ -// Copyright 2018-2019 the u-root Authors. All rights reserved +// Copyright 2018-2024 the u-root Authors. All rights reserved // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. diff --git a/client/getattr_freebsd.go b/client/getattr_freebsd.go index 795e5a61..03d370fd 100644 --- a/client/getattr_freebsd.go +++ b/client/getattr_freebsd.go @@ -21,8 +21,6 @@ import ( ) // GetAttr implements p9.File.GetAttr. -// -// Not fully implemented. func (l *CPU9P) GetAttr(req p9.AttrMask) (p9.QID, p9.AttrMask, p9.Attr, error) { qid, fi, err := l.info() if err != nil { From 0109c883528b052b097cd26dcb8b22174324439a Mon Sep 17 00:00:00 2001 From: Changyuan Lyu Date: Mon, 7 Oct 2024 20:33:36 -0700 Subject: [PATCH 3/3] cpud: print fstab mount errors In most cases when we get an error "CPUD: your $PWD is not in the remote namespace", it is due to some missing mounts, for example, a mount target dir does not exist on the remote machine. Printing out the mount errors helps debugging. Signed-off-by: Changyuan Lyu --- session/session.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/session/session.go b/session/session.go index 12ecc959..a76512fa 100644 --- a/session/session.go +++ b/session/session.go @@ -169,7 +169,7 @@ func (s *Session) Run() error { if tab, ok := os.LookupEnv(fstab); ok { verbose("Mounting %q", tab) if err := mount.Mount(tab); err != nil { - verbose("fstab mount failure: %v", err) + log.Printf("fstab mount failure: %v", err) // Should we die if the mounts fail? For now, we think not; // the user may be able to debug. Just record that it failed. s.fail = true