forked from wrenn/wrenn
Shrink minimal rootfs on graceful host agent shutdown
On startup EnsureImageSizes expands the minimal rootfs to the configured disk size. This adds the inverse: ShrinkMinimalImage runs e2fsck + resize2fs -M during graceful shutdown so the image is stored compactly on disk.
This commit is contained in:
@ -1,14 +1,18 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
@ -43,14 +47,20 @@ func main() {
|
|||||||
cleanupLog := logging.Setup(filepath.Join(rootDir, "logs"), "host-agent")
|
cleanupLog := logging.Setup(filepath.Join(rootDir, "logs"), "host-agent")
|
||||||
defer cleanupLog()
|
defer cleanupLog()
|
||||||
|
|
||||||
if os.Geteuid() != 0 {
|
if err := checkPrivileges(); err != nil {
|
||||||
slog.Error("host agent must run as root")
|
slog.Error("insufficient privileges", "error", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enable IP forwarding (required for NAT).
|
// Enable IP forwarding (required for NAT). The write may fail if running
|
||||||
|
// as non-root without DAC_OVERRIDE on this path — that's OK if the systemd
|
||||||
|
// unit's ExecStartPre already set it. We verify the value regardless.
|
||||||
if err := os.WriteFile("/proc/sys/net/ipv4/ip_forward", []byte("1"), 0644); err != nil {
|
if err := os.WriteFile("/proc/sys/net/ipv4/ip_forward", []byte("1"), 0644); err != nil {
|
||||||
slog.Warn("failed to enable ip_forward", "error", err)
|
slog.Warn("failed to enable ip_forward (may have been set by systemd unit)", "error", err)
|
||||||
|
}
|
||||||
|
if b, err := os.ReadFile("/proc/sys/net/ipv4/ip_forward"); err != nil || strings.TrimSpace(string(b)) != "1" {
|
||||||
|
slog.Error("ip_forward is not enabled — sandbox networking will be broken", "error", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up stale resources from a previous crash.
|
// Clean up stale resources from a previous crash.
|
||||||
@ -170,6 +180,7 @@ func main() {
|
|||||||
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 30*time.Second)
|
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
defer shutdownCancel()
|
defer shutdownCancel()
|
||||||
mgr.Shutdown(shutdownCtx)
|
mgr.Shutdown(shutdownCtx)
|
||||||
|
sandbox.ShrinkMinimalImage(rootDir)
|
||||||
if err := httpServer.Shutdown(shutdownCtx); err != nil {
|
if err := httpServer.Shutdown(shutdownCtx); err != nil {
|
||||||
slog.Error("http server shutdown error", "error", err)
|
slog.Error("http server shutdown error", "error", err)
|
||||||
}
|
}
|
||||||
@ -245,3 +256,63 @@ func envOrDefault(key, def string) string {
|
|||||||
}
|
}
|
||||||
return def
|
return def
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// checkPrivileges verifies the process has the required Linux capabilities.
|
||||||
|
// Always reads CapEff — even for root — because a root process inside a
|
||||||
|
// restricted container (e.g. docker --cap-drop=all) may not have all caps.
|
||||||
|
func checkPrivileges() error {
|
||||||
|
capEff, err := readEffectiveCaps()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("read capabilities: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// All capabilities required by the host agent at runtime.
|
||||||
|
required := []struct {
|
||||||
|
bit uint
|
||||||
|
name string
|
||||||
|
}{
|
||||||
|
{1, "CAP_DAC_OVERRIDE"}, // /dev/loop*, /dev/mapper/*, /dev/net/tun
|
||||||
|
{5, "CAP_KILL"}, // SIGTERM/SIGKILL to Firecracker processes
|
||||||
|
{12, "CAP_NET_ADMIN"}, // netlink, iptables, routing, TAP/veth
|
||||||
|
{13, "CAP_NET_RAW"}, // raw sockets (iptables)
|
||||||
|
{19, "CAP_SYS_PTRACE"}, // reading /proc/self/ns/net (netns.Get)
|
||||||
|
{21, "CAP_SYS_ADMIN"}, // netns, mount ns, losetup, dmsetup
|
||||||
|
{27, "CAP_MKNOD"}, // device-mapper node creation
|
||||||
|
}
|
||||||
|
|
||||||
|
var missing []string
|
||||||
|
for _, cap := range required {
|
||||||
|
if capEff&(1<<cap.bit) == 0 {
|
||||||
|
missing = append(missing, cap.name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(missing) > 0 {
|
||||||
|
return fmt.Errorf("missing capabilities: %s — run as root or apply setcap to the binary",
|
||||||
|
strings.Join(missing, ", "))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// readEffectiveCaps parses the CapEff bitmask from /proc/self/status.
|
||||||
|
func readEffectiveCaps() (uint64, error) {
|
||||||
|
f, err := os.Open("/proc/self/status")
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(f)
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
if hexStr, ok := strings.CutPrefix(line, "CapEff:"); ok {
|
||||||
|
return strconv.ParseUint(strings.TrimSpace(hexStr), 16, 64)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return 0, fmt.Errorf("read /proc/self/status: %w", err)
|
||||||
|
}
|
||||||
|
return 0, fmt.Errorf("CapEff not found in /proc/self/status")
|
||||||
|
}
|
||||||
|
|||||||
@ -104,6 +104,37 @@ func ParseSizeToMB(s string) (int, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ShrinkMinimalImage shrinks the built-in minimal rootfs back to its minimum
|
||||||
|
// size using resize2fs -M. This is the inverse of EnsureImageSizes and should
|
||||||
|
// be called during graceful shutdown so the image is stored compactly on disk.
|
||||||
|
func ShrinkMinimalImage(wrennDir string) {
|
||||||
|
minimalRootfs := layout.TemplateRootfs(wrennDir, id.PlatformTeamID, id.MinimalTemplateID)
|
||||||
|
shrinkImage(minimalRootfs)
|
||||||
|
}
|
||||||
|
|
||||||
|
// shrinkImage shrinks a single rootfs image to its minimum size.
|
||||||
|
func shrinkImage(rootfs string) {
|
||||||
|
if _, err := os.Stat(rootfs); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Info("shrinking base image", "path", rootfs)
|
||||||
|
|
||||||
|
if out, err := exec.Command("e2fsck", "-fy", rootfs).CombinedOutput(); err != nil {
|
||||||
|
if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() > 1 {
|
||||||
|
slog.Warn("e2fsck before shrink failed", "path", rootfs, "output", string(out), "error", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if out, err := exec.Command("resize2fs", "-M", rootfs).CombinedOutput(); err != nil {
|
||||||
|
slog.Warn("resize2fs -M failed", "path", rootfs, "output", string(out), "error", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Info("base image shrunk", "path", rootfs)
|
||||||
|
}
|
||||||
|
|
||||||
// expandImage expands a single rootfs image if it is smaller than targetBytes.
|
// expandImage expands a single rootfs image if it is smaller than targetBytes.
|
||||||
func expandImage(rootfs string, targetBytes int64, targetMB int) error {
|
func expandImage(rootfs string, targetBytes int64, targetMB int) error {
|
||||||
info, err := os.Stat(rootfs)
|
info, err := os.Stat(rootfs)
|
||||||
|
|||||||
Reference in New Issue
Block a user