Space: the unit is a disk on its own or a ZFS pool as a whole, picked from a dropdown
A pool's member disks are listed under it, not offered apart; the picker shows the layout, members, size and scan state. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5.1
parent
4280b2226a
commit
e9fa21768e
+75
-41
@@ -1,14 +1,15 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
// The Space tab: what is using a disk. A disk is picked, not a partition: the scan covers
|
// The Space tab: what is using a disk or a pool. The unit is a whole volume, never a
|
||||||
// every filesystem the disk backs — its partitions' mountpoints, and for a disk in a ZFS
|
// partition: a disk on its own is scanned through every filesystem it backs, and a ZFS pool
|
||||||
// pool, every mounted dataset of that pool (so any of the pool's disks shows the pool).
|
// through every mounted dataset, as one thing — its member disks are not scanned apart.
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -18,51 +19,84 @@ import (
|
|||||||
"hamlab/internal/storage"
|
"hamlab/internal/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
type spaceDisk struct {
|
// spaceTarget is one thing a scan can measure: a disk on its own, or a ZFS pool as a whole.
|
||||||
|
// A disk that is a member of a pool is not a target of its own — its share of the pool means
|
||||||
|
// nothing by itself — it is listed under the pool. (A disk with a pool partition *and* a
|
||||||
|
// filesystem of its own, root on one partition and a pool on another, is both.)
|
||||||
|
type spaceTarget struct {
|
||||||
|
ID string `json:"id"` // "nvme0" or "pool:tank"
|
||||||
|
Kind string `json:"kind"` // disk | pool
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Kind string `json:"kind"`
|
Model string `json:"model,omitempty"` // disk: its model
|
||||||
Model string `json:"model,omitempty"`
|
Layout string `json:"layout,omitempty"` // pool: raidz2, mirror, …
|
||||||
|
Health string `json:"health,omitempty"` // pool: ONLINE, DEGRADED, …
|
||||||
Size uint64 `json:"size"`
|
Size uint64 `json:"size"`
|
||||||
Pool string `json:"pool,omitempty"` // the ZFS pool this disk is part of
|
Used uint64 `json:"used,omitempty"` // pool: allocated
|
||||||
|
Disks []string `json:"disks,omitempty"` // pool: its member disks
|
||||||
Roots []string `json:"roots"` // the mountpoints a scan walks
|
Roots []string `json:"roots"` // the mountpoints a scan walks
|
||||||
Scan *space.Scan `json:"scan,omitempty"` // the last scan, if any
|
Scan *space.Scan `json:"scan,omitempty"` // the last scan, if any
|
||||||
}
|
}
|
||||||
|
|
||||||
// spaceDisks maps every disk to the mountpoints it backs.
|
var layoutRe = regexp.MustCompile(`\b(draid\d*|raidz[123]?|mirror)\b`)
|
||||||
func (s *Server) spaceDisks(ctx context.Context) []spaceDisk {
|
|
||||||
|
// spaceTargets lists the disks and pools a scan can measure.
|
||||||
|
func (s *Server) spaceTargets(ctx context.Context) []spaceTarget {
|
||||||
devs, _ := storage.Devices(ctx)
|
devs, _ := storage.Devices(ctx)
|
||||||
var out []spaceDisk
|
var out []spaceTarget
|
||||||
poolMounts := map[string][]string{}
|
pools := map[string]*spaceTarget{}
|
||||||
|
var poolOrder []string
|
||||||
for _, d := range devs {
|
for _, d := range devs {
|
||||||
sd := spaceDisk{Name: d.Name, Kind: d.Kind, Model: d.Model, Size: d.Size, Roots: []string{}}
|
disk := spaceTarget{ID: d.Name, Kind: "disk", Name: d.Name, Model: d.Model, Size: d.Size, Roots: []string{}}
|
||||||
seen := map[string]bool{}
|
seen := map[string]bool{}
|
||||||
|
inPool := false
|
||||||
for _, p := range d.Parts {
|
for _, p := range d.Parts {
|
||||||
|
if p.FSType == "zfs_member" && p.Label != "" {
|
||||||
|
inPool = true
|
||||||
|
pt := pools[p.Label]
|
||||||
|
if pt == nil {
|
||||||
|
pt = &spaceTarget{ID: "pool:" + p.Label, Kind: "pool", Name: p.Label, Roots: zfsMounts(ctx, p.Label)}
|
||||||
|
if pt.Roots == nil {
|
||||||
|
pt.Roots = []string{}
|
||||||
|
}
|
||||||
|
pools[p.Label] = pt
|
||||||
|
poolOrder = append(poolOrder, p.Label)
|
||||||
|
}
|
||||||
|
pt.Disks = append(pt.Disks, d.Name)
|
||||||
|
continue
|
||||||
|
}
|
||||||
if p.Mount != "" && p.Mount != "[SWAP]" && !seen[p.Mount] {
|
if p.Mount != "" && p.Mount != "[SWAP]" && !seen[p.Mount] {
|
||||||
seen[p.Mount] = true
|
seen[p.Mount] = true
|
||||||
sd.Roots = append(sd.Roots, p.Mount)
|
disk.Roots = append(disk.Roots, p.Mount)
|
||||||
}
|
|
||||||
if p.FSType == "zfs_member" && p.Label != "" && sd.Pool == "" {
|
|
||||||
sd.Pool = p.Label
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if sd.Pool != "" {
|
if !inPool || len(disk.Roots) > 0 {
|
||||||
mounts, ok := poolMounts[sd.Pool]
|
sort.Strings(disk.Roots)
|
||||||
if !ok {
|
out = append(out, disk)
|
||||||
mounts = zfsMounts(ctx, sd.Pool)
|
|
||||||
poolMounts[sd.Pool] = mounts
|
|
||||||
}
|
}
|
||||||
for _, m := range mounts {
|
}
|
||||||
if !seen[m] {
|
if len(pools) > 0 {
|
||||||
seen[m] = true
|
// size, health and layout from zpool; the vdev tree names the layout
|
||||||
sd.Roots = append(sd.Roots, m)
|
for _, p := range s.storage.ZFS(ctx).Pools {
|
||||||
|
if pt := pools[p.Name]; pt != nil {
|
||||||
|
pt.Size, pt.Used, pt.Health = p.Size, p.Alloc, p.Health
|
||||||
|
if m := layoutRe.FindString(p.Config); m != "" {
|
||||||
|
pt.Layout = m
|
||||||
|
} else if len(pt.Disks) == 1 {
|
||||||
|
pt.Layout = "single disk"
|
||||||
|
} else if len(pt.Disks) > 1 {
|
||||||
|
pt.Layout = "stripe"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sort.Strings(sd.Roots)
|
var ps []spaceTarget
|
||||||
out = append(out, sd)
|
for _, name := range poolOrder {
|
||||||
|
sort.Strings(pools[name].Roots)
|
||||||
|
ps = append(ps, *pools[name])
|
||||||
|
}
|
||||||
|
out = append(ps, out...) // pools first: they are what a NAS is
|
||||||
}
|
}
|
||||||
if out == nil {
|
if out == nil {
|
||||||
out = []spaceDisk{}
|
out = []spaceTarget{}
|
||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
@@ -86,22 +120,22 @@ func zfsMounts(ctx context.Context, pool string) []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) spaceOverview(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) spaceOverview(w http.ResponseWriter, r *http.Request) {
|
||||||
disks := s.spaceDisks(r.Context())
|
targets := s.spaceTargets(r.Context())
|
||||||
scans := s.space.All()
|
scans := s.space.All()
|
||||||
for i := range disks {
|
for i := range targets {
|
||||||
disks[i].Scan = scans[disks[i].Name]
|
targets[i].Scan = scans[targets[i].ID]
|
||||||
}
|
}
|
||||||
jsonOK(w, 200, map[string]any{"disks": disks})
|
jsonOK(w, 200, map[string]any{"targets": targets})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) spaceDisk(w http.ResponseWriter, r *http.Request, name string) (spaceDisk, bool) {
|
func (s *Server) spaceTarget(w http.ResponseWriter, r *http.Request, id string) (spaceTarget, bool) {
|
||||||
for _, d := range s.spaceDisks(r.Context()) {
|
for _, t := range s.spaceTargets(r.Context()) {
|
||||||
if d.Name == name {
|
if t.ID == id {
|
||||||
return d, true
|
return t, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
jsonErr(w, 404, "no such disk")
|
jsonErr(w, 404, "no such disk or pool")
|
||||||
return spaceDisk{}, false
|
return spaceTarget{}, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) spaceStatus(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) spaceStatus(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -114,16 +148,16 @@ func (s *Server) spaceStatus(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) spaceScan(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) spaceScan(w http.ResponseWriter, r *http.Request) {
|
||||||
d, ok := s.spaceDisk(w, r, r.PathValue("disk"))
|
t, ok := s.spaceTarget(w, r, r.PathValue("disk"))
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
st, err := s.space.Start(d.Name, d.Roots)
|
st, err := s.space.Start(t.ID, t.Roots)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jsonErr(w, 409, err.Error())
|
jsonErr(w, 409, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Printf("space: scan of %s (%s) started by %s", d.Name, strings.Join(d.Roots, " "), clientIP(r))
|
log.Printf("space: scan of %s (%s) started by %s", t.ID, strings.Join(t.Roots, " "), clientIP(r))
|
||||||
jsonOK(w, 202, st)
|
jsonOK(w, 202, st)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user