Mount the filesystems we need so this can run as PID 1

This commit is contained in:
flamingcow
2026-07-26 10:54:19 -07:00
parent 5e2b36bdaa
commit 832a2b42d6
5 changed files with 101 additions and 16 deletions
+1
View File
@@ -17,6 +17,7 @@ const (
kdText = 0x00 kdText = 0x00
kdGraphics = 0x01 kdGraphics = 0x01
consolePath = "/dev/tty0" consolePath = "/dev/tty0"
fbPath = "/dev/fb0"
) )
// The variable screen info is a flat run of 40 u32s, so it is read as an array // The variable screen info is a flat run of 40 u32s, so it is read as an array
+71
View File
@@ -0,0 +1,71 @@
package main
import (
"fmt"
"golang.org/x/sys/unix"
)
// Running as PID 1 there is nothing underneath us: sysfs and devtmpfs are what
// every check, counter and device node below is reached through, and no one
// else is going to mount them. Written as checks like everything else, so this
// is a silent no-op under a running system.
type mountSpec struct {
dir string
fstype string
magic int64
flags uintptr
}
var wantMounts = []mountSpec{
{"/proc", "proc", unix.PROC_SUPER_MAGIC, unix.MS_NOSUID | unix.MS_NODEV | unix.MS_NOEXEC},
{"/sys", "sysfs", unix.SYSFS_MAGIC, unix.MS_NOSUID | unix.MS_NODEV | unix.MS_NOEXEC},
// devtmpfs reports itself as tmpfs, so an existing tmpfs on /dev is taken
// as good enough; the kernel populates the nodes either way.
{"/dev", "devtmpfs", unix.TMPFS_MAGIC, unix.MS_NOSUID},
}
func checkMount(m mountSpec) checkResult {
res := checkResult{item: "mount " + m.dir}
var st unix.Statfs_t
err := unix.Statfs(m.dir, &st)
if err == unix.ENOENT {
if err := unix.Mkdir(m.dir, 0o755); err != nil {
res.err = fmt.Errorf("creating %s: %w", m.dir, err)
res.fatal = true
return res
}
err = unix.Statfs(m.dir, &st)
}
if err != nil {
res.err = err
res.fatal = true
return res
}
if int64(st.Type) == m.magic {
res.state = m.fstype + " already mounted"
return res
}
if err := unix.Mount(m.fstype, m.dir, m.fstype, m.flags, ""); err != nil {
res.err = err
res.fatal = true
return res
}
res.fixed = true
res.state = "mounted " + m.fstype
return res
}
// Each mount is what the next check stands on, so the first failure ends it.
func mountFilesystems() []checkResult {
var out []checkResult
for _, m := range wantMounts {
res := checkMount(m)
out = append(out, res)
if res.fatal {
return out
}
}
return out
}
+10 -15
View File
@@ -637,10 +637,11 @@ func main() {
) )
flag.Parse() flag.Parse()
// Nothing here is recoverable by the time it reaches this point, and as PID 1
// a plain exit would panic the kernel anyway with less to show for it.
if err := run(*aName, *bName, *sizesArg, if err := run(*aName, *bName, *sizesArg,
*streams, *batch, *nsPerM); err != nil { *streams, *batch, *nsPerM); err != nil {
fmt.Fprintln(os.Stderr, "error:", err) panic(err)
os.Exit(1)
} }
} }
@@ -665,6 +666,11 @@ func run(aName, bName, sizesArg string,
if err != nil { if err != nil {
return err return err
} }
if err := reportChecks("MOUNTS", mountFilesystems()); err != nil {
return err
}
a, err := lookupEndpoint(aName) a, err := lookupEndpoint(aName)
if err != nil { if err != nil {
return err return err
@@ -692,19 +698,8 @@ func run(aName, bName, sizesArg string,
ethertypes[i] = uint16(etherBase + i) ethertypes[i] = uint16(etherBase + i)
} }
var fatal []string if err := reportChecks("HOST SETTINGS", configureSystem(ifnames, ethertypes)); err != nil {
var tuneRows [][]string return err
for _, r := range configureSystem(ifnames, ethertypes) {
tuneRows = append(tuneRows, []string{r.item, r.status(), r.detail()})
if r.fatal {
fatal = append(fatal, r.item)
}
}
fmt.Println(renderBox("HOST SETTINGS",
[]string{"CHECK", "STATUS", "DETAIL"},
[]bool{false, false, false}, tuneRows))
if len(fatal) > 0 {
return fmt.Errorf("cannot test with %s in this state", strings.Join(fatal, ", "))
} }
cfg := config{ cfg := config{
+18
View File
@@ -282,6 +282,24 @@ func (r checkResult) detail() string {
return r.state return r.state
} }
func reportChecks(title string, results []checkResult) error {
var rows [][]string
var fatal []string
for _, r := range results {
rows = append(rows, []string{r.item, r.status(), r.detail()})
if r.fatal {
fatal = append(fatal, r.item)
}
}
fmt.Println(renderBox(title,
[]string{"CHECK", "STATUS", "DETAIL"},
[]bool{false, false, false}, rows))
if len(fatal) > 0 {
return fmt.Errorf("cannot test with %s in this state", strings.Join(fatal, ", "))
}
return nil
}
func ethtoolCall(fd int, ifname string, data unsafe.Pointer) error { func ethtoolCall(fd int, ifname string, data unsafe.Pointer) error {
var ifr dataIfreq var ifr dataIfreq
if len(ifname) >= unix.IFNAMSIZ { if len(ifname) >= unix.IFNAMSIZ {
+1 -1
View File
@@ -58,7 +58,7 @@ type display struct {
} }
func newDisplay() (*display, error) { func newDisplay() (*display, error) {
fb, err := openFramebuffer("/dev/fb0") fb, err := openFramebuffer(fbPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }