|
| 1 | +package network |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "syscall" |
| 6 | + |
| 7 | + "github.com/ObolNetwork/obol-stack/internal/ui" |
| 8 | +) |
| 9 | + |
| 10 | +// diskSpaceRequirementGB returns the recommended free-disk minimum for |
| 11 | +// (network, mode) in gigabytes. Numbers include ~30% headroom for chain |
| 12 | +// growth between releases. Sizes are reth-anchored; other clients are in |
| 13 | +// the same ballpark. |
| 14 | +func diskSpaceRequirementGB(network, mode string) uint64 { |
| 15 | + archive := mode == "archive" |
| 16 | + switch network { |
| 17 | + case "mainnet": |
| 18 | + if archive { |
| 19 | + return 5000 |
| 20 | + } |
| 21 | + return 700 |
| 22 | + default: |
| 23 | + // sepolia, hoodi, and other testnets |
| 24 | + if archive { |
| 25 | + return 400 |
| 26 | + } |
| 27 | + return 150 |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// freeDiskBytes returns the free disk bytes available at path. Used to |
| 32 | +// check whether a network install has room to grow before we let helmfile |
| 33 | +// schedule a 4TB PVC that will silently fill the host overnight. |
| 34 | +func freeDiskBytes(path string) (uint64, error) { |
| 35 | + var stat syscall.Statfs_t |
| 36 | + if err := syscall.Statfs(path, &stat); err != nil { |
| 37 | + return 0, fmt.Errorf("statfs %s: %w", path, err) |
| 38 | + } |
| 39 | + // Bavail is reserved-block-aware (vs Bfree); use it for "what a regular |
| 40 | + // process can actually allocate". |
| 41 | + return stat.Bavail * uint64(stat.Bsize), nil |
| 42 | +} |
| 43 | + |
| 44 | +// CheckNetworkDiskSpace warns when the data directory has less free disk |
| 45 | +// than the install is expected to need. The default answer is to continue: |
| 46 | +// in non-interactive contexts (no TTY, JSON mode) the prompt auto-accepts |
| 47 | +// so scripted installs don't deadlock. The user only blocks the install by |
| 48 | +// explicitly declining at an interactive prompt. |
| 49 | +func CheckNetworkDiskSpace(u *ui.UI, dataDir, network, mode string) error { |
| 50 | + requiredGB := diskSpaceRequirementGB(network, mode) |
| 51 | + |
| 52 | + freeBytes, err := freeDiskBytes(dataDir) |
| 53 | + if err != nil { |
| 54 | + // Best-effort: a statfs failure shouldn't block install. |
| 55 | + u.Warnf("Could not check free disk space at %s: %v", dataDir, err) |
| 56 | + return nil |
| 57 | + } |
| 58 | + |
| 59 | + freeGB := freeBytes / (1024 * 1024 * 1024) |
| 60 | + |
| 61 | + u.Detail("Disk space", fmt.Sprintf("%d GB free at %s (this network needs ~%d GB)", freeGB, dataDir, requiredGB)) |
| 62 | + |
| 63 | + if freeGB >= requiredGB { |
| 64 | + return nil |
| 65 | + } |
| 66 | + |
| 67 | + u.Warnf("Low disk space: %d GB free, ~%d GB recommended for %s/%s", |
| 68 | + freeGB, requiredGB, network, mode) |
| 69 | + if mode != "archive" { |
| 70 | + u.Dim(" (full mode is the lighter option; archive mode would need ~4-5 TB on mainnet)") |
| 71 | + } |
| 72 | + |
| 73 | + if !u.Confirm("Continue with install anyway?", true) { |
| 74 | + return fmt.Errorf("install cancelled: insufficient disk space (%d GB free, ~%d GB recommended)", freeGB, requiredGB) |
| 75 | + } |
| 76 | + |
| 77 | + return nil |
| 78 | +} |
0 commit comments