sandbox: implement autoetc as setup op
All checks were successful
Test / Create distribution (push) Successful in 27s
Test / Sandbox (push) Successful in 1m48s
Test / Fortify (push) Successful in 2m42s
Test / Sandbox (race detector) (push) Successful in 2m51s
Test / Fpkg (push) Successful in 3m37s
Test / Fortify (race detector) (push) Successful in 4m9s
Test / Flake checks (push) Successful in 1m4s

This significantly reduces setup op count and the readdir call now happens in the context of the init process.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
2025-04-10 18:54:25 +09:00
parent 584405f7cc
commit c806f43881
4 changed files with 60 additions and 253 deletions

View File

@@ -440,3 +440,57 @@ func (f *Ops) PlaceP(name string, dataP **[]byte) *Ops {
*f = append(*f, t)
return f
}
func init() { gob.Register(new(AutoEtc)) }
// AutoEtc creates a toplevel symlink mirror of a directory in sysroot 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
}
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/"
if err := os.MkdirAll(target, 0755); err != nil {
return wrapErrSelf(err)
}
if d, err := os.ReadDir(toSysroot(e.HostEtc)); err != nil {
return wrapErrSelf(err)
} else {
for _, ent := range d {
n := ent.Name()
switch n {
case "passwd":
case "group":
case "mtab":
if err = os.Symlink("/proc/mounts", target+n); err != nil {
return wrapErrSelf(err)
}
default:
if err = os.Symlink(path.Join(e.HostEtc, n), target+n); err != nil {
return wrapErrSelf(err)
}
}
}
}
return nil
}
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 }