container: use new netlink implementation
Some checks failed
Test / Create distribution (push) Successful in 1m3s
Test / Hakurei (race detector) (push) Has started running
Test / Flake checks (push) Has been cancelled
Test / Sandbox (push) Has been cancelled
Test / ShareFS (push) Has been cancelled
Test / Sandbox (race detector) (push) Has been cancelled
Test / Hakurei (push) Has been cancelled

This is adapted from the container netlink implementation and is much more reusable.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
2026-03-16 23:32:42 +09:00
parent 72bd3fb05e
commit c22ca89e27
3 changed files with 44 additions and 342 deletions

View File

@@ -3,6 +3,8 @@ package container
import (
"io"
"io/fs"
"log"
"net"
"os"
"os/exec"
"os/signal"
@@ -12,6 +14,7 @@ import (
"hakurei.app/container/seccomp"
"hakurei.app/container/std"
"hakurei.app/internal/netlink"
"hakurei.app/message"
)
@@ -167,7 +170,47 @@ func (k direct) mountTmpfs(fsname, target string, flags uintptr, size int, perm
func (direct) ensureFile(name string, perm, pperm os.FileMode) error {
return ensureFile(name, perm, pperm)
}
func (direct) mustLoopback(msg message.Msg) { mustLoopback(msg) }
func (direct) mustLoopback(msg message.Msg) {
var lo int
if ifi, err := net.InterfaceByName("lo"); err != nil {
log.Fatalln(err)
} else {
lo = ifi.Index
}
c, err := netlink.DialRoute()
if err != nil {
log.Fatalln(err)
}
must := func(err error) {
if err == nil {
return
}
if closeErr := c.Close(); closeErr != nil {
msg.Verbosef("cannot close RTNETLINK: %v", closeErr)
}
switch err.(type) {
case *os.SyscallError:
msg.GetLogger().Fatalf("cannot %v", err)
case syscall.Errno:
msg.GetLogger().Fatalf("RTNETLINK answers: %v", err)
default:
msg.GetLogger().Fatalf("RTNETLINK answers with malformed message")
}
}
must(c.SendNewaddrLo(uint32(lo)))
must(c.SendIfInfomsg(syscall.RTM_NEWLINK, 0, &syscall.IfInfomsg{
Family: syscall.AF_UNSPEC,
Index: int32(lo),
Flags: syscall.IFF_UP,
Change: syscall.IFF_UP,
}))
must(c.Close())
}
func (direct) seccompLoad(rules []std.NativeRule, flags seccomp.ExportFlag) error {
return seccomp.Load(rules, flags)