sandbox: relative autoetc links
All checks were successful
Test / Create distribution (push) Successful in 26s
Test / Sandbox (push) Successful in 1m44s
Test / Fortify (push) Successful in 2m41s
Test / Sandbox (race detector) (push) Successful in 2m48s
Test / Fpkg (push) Successful in 3m35s
Test / Fortify (race detector) (push) Successful in 4m13s
Test / Flake checks (push) Successful in 1m3s

This allows nested containers to use autoetc, and increases compatibility with other implementations.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
2025-04-11 18:54:00 +09:00
parent c806f43881
commit 9967909460
8 changed files with 43 additions and 49 deletions

View File

@@ -443,31 +443,26 @@ func (f *Ops) PlaceP(name string, dataP **[]byte) *Ops {
func init() { gob.Register(new(AutoEtc)) }
// AutoEtc creates a toplevel symlink mirror of a directory in sysroot with /etc semantics.
// AutoEtc expands host /etc into a toplevel symlink mirror with /etc semantics.
// This is not a generic setup op. It is implemented here to reduce ipc overhead.
type AutoEtc struct {
// this is an absolute path within sysroot
HostEtc string
}
type AutoEtc struct{ Prefix string }
func (e *AutoEtc) early(*Params) error { return nil }
func (e *AutoEtc) apply(*Params) error {
if !path.IsAbs(e.HostEtc) {
return msg.WrapErr(syscall.EBADE,
fmt.Sprintf("path %q is not absolute", e.HostEtc))
}
const target = sysrootPath + "/etc/"
rel := e.hostRel() + "/"
if err := os.MkdirAll(target, 0755); err != nil {
return wrapErrSelf(err)
}
if d, err := os.ReadDir(toSysroot(e.HostEtc)); err != nil {
if d, err := os.ReadDir(toSysroot(e.hostPath())); err != nil {
return wrapErrSelf(err)
} else {
for _, ent := range d {
n := ent.Name()
switch n {
case ".host":
case "passwd":
case "group":
@@ -477,7 +472,7 @@ func (e *AutoEtc) apply(*Params) error {
}
default:
if err = os.Symlink(path.Join(e.HostEtc, n), target+n); err != nil {
if err = os.Symlink(rel+n, target+n); err != nil {
return wrapErrSelf(err)
}
}
@@ -486,11 +481,19 @@ func (e *AutoEtc) apply(*Params) error {
return nil
}
func (e *AutoEtc) hostPath() string { return "/etc/" + e.hostRel() }
func (e *AutoEtc) hostRel() string { return ".host/" + e.Prefix }
func (e *AutoEtc) Is(op Op) bool {
ve, ok := op.(*AutoEtc)
return ok && ((e == nil && ve == nil) || (e != nil && ve != nil && *e == *ve))
}
func (*AutoEtc) prefix() string { return "setting up" }
func (e *AutoEtc) String() string { return fmt.Sprintf("auto etc via %s", e.HostEtc) }
func (f *Ops) Etc(host string) *Ops { *f = append(*f, &AutoEtc{host}); return f }
func (*AutoEtc) prefix() string { return "setting up" }
func (e *AutoEtc) String() string { return fmt.Sprintf("auto etc %s", e.Prefix) }
func (f *Ops) Etc(host, prefix string) *Ops {
e := &AutoEtc{prefix}
f.Mkdir("/etc", 0755)
f.Bind(host, e.hostPath(), 0)
*f = append(*f, e)
return f
}