protobuf file for block devices

This commit is contained in:
Jeff Carr 2025-08-17 17:02:29 -05:00
parent bd5b16d875
commit 25c604aeee
9 changed files with 101 additions and 21 deletions

1
.gitignore vendored
View File

@ -3,4 +3,5 @@ go.mod
go.sum
fixup
*.so
*pb.go
/files/*

View File

@ -2,7 +2,7 @@ VERSION = $(shell git describe --tags)
GUIVERSION = $(shell git describe --tags)
BUILDTIME = $(shell date +%Y.%m.%d)
all: verbose
all: block.pb.go verbose
# -fixup --gui-check-plugin ../../../toolkits/gocui/gocui.so
fixup --gui gocui --gui-verbose --gui-file ../../toolkits/gocui/gocui.so
@ -32,9 +32,17 @@ goimports:
clean:
rm -f go.*
rm -f *.pb.go
go-mod-clean purge
gpl:
wit-test --witcom
check-git-clean:
@git diff-index --quiet HEAD -- || (echo "Git repository is dirty, please commit your changes first"; exit 1)
block.pb.go: block.proto
autogenpb --proto block.proto
commit:
FORGE_URL="https://forge.grid.wit.com/" forge commit --all

20
block.proto Normal file
View File

@ -0,0 +1,20 @@
syntax = "proto3";
package main;
message Block {
string name = 1; // `autogenpb:sort` `autogenpb:unique`
uint32 major = 2;
uint32 minor = 3;
uint64 size_bytes = 4;
bool removable = 5;
bool read_only = 6;
string type = 7; // e.g., "disk", "part", "rom", "loop"
repeated string mountpoints = 8;
}
message Blocks { // `autogenpb:marshal` `autogenpb:mutex`
string uuid = 1; // `autogenpb:uuid:abe10848-cf2b-4440-b5a8-0aaa2ce50aad`
string version = 2; // `autogenpb:version:v0.0.1`
repeated Block Blocks = 3; // THIS MUST BE Block and then Blocks
}

View File

@ -17,7 +17,11 @@ func doDrives() {
}
for _, p := range parts {
log.Printf("Device: /dev/%s\tSize: %d KiB\n", p.Name, p.Blocks)
log.Printf("RAW Device: /dev/%s\tSize: %d KiB\n", p.Name, p.Blocks)
devname := "/dev/" + p.Name
if blkpb := me.pb.FindByName(devname); blkpb != nil {
blkpb.SizeBytes = uint64(p.Blocks) * 1024
}
}
blockDir := "/sys/block"
@ -44,11 +48,12 @@ func doDrives() {
used := false
for _, part := range partitions {
partName := filepath.Base(part)
partDev := "/dev/" + partName
if partName == dev {
log.Printf(" SKIP MAIN Partition: %s\n", partDev)
continue // skip the main device
}
hasPartition = true
partDev := "/dev/" + partName
isMounted := mounts[partDev]
log.Printf(" Partition: %s [%v]\n", partDev, isMounted)
if isMounted {
@ -61,7 +66,7 @@ func doDrives() {
}
if !used {
log.Println(" Drive appears to be unused.")
log.Println(" * Drive appears to be unused.")
}
}
return nil

View File

@ -6,6 +6,7 @@ package main
// An app to submit patches for the 30 GO GUI repos
import (
"fmt"
"os"
"strings"
"time"
@ -13,16 +14,45 @@ import (
"go.wit.com/gui"
"go.wit.com/lib/gadgets"
"go.wit.com/lib/gui/shell"
"go.wit.com/lib/protobuf/virtpb"
"go.wit.com/log"
)
func debug() {
for {
time.Sleep(3 * time.Second)
log.Info("TODO: use this?")
time.Sleep(10 * time.Second)
log.Info("TODO: use this debug loop for something?")
}
}
// adds a new drive to the dropdown menu and protobuf
func addDrive(devname string, displayDesc string) *Block {
if blkpb := me.pb.FindByName(devname); blkpb != nil {
return blkpb
}
s := log.Sprintf("%-12s %-8s", devname, displayDesc)
log.Info(s)
me.dd.AddText(s)
blkpb := me.pb.InsertByName(devname)
return blkpb
}
func switchDrive(blk *Block) {
me.currentDev = blk
me.parted.SetText("Partition " + blk.Name)
log.Info("check if", me.currentDev.Name, "is in use")
result, err := shell.RunVerbose([]string{"parted", me.currentDev.Name, "print"})
log.Info("result =", result.Stdout, "err =", err)
out := strings.Join(result.Stdout, "\n")
if err != nil {
out += fmt.Sprintf("err = %v", err)
}
me.driveInfoBox.SetText(out)
}
func doGui() {
me.myGui = gui.New()
me.myGui.InitEmbed(resources)
@ -51,19 +81,21 @@ func drawWindow(win *gadgets.GenericWindow) {
if len(fields) < 1 {
return
}
me.currentDev = fields[0]
me.parted.SetText("Partition " + me.currentDev)
if blkpb := me.pb.FindByName(fields[0]); blkpb != nil {
switchDrive(blkpb)
} else {
log.Info("couldn't find in protobuf:", fields)
}
}
grid.NextRow()
// a button to format or blank a drive
me.parted = grid.NewButton("select drive", func() {
if me.currentDev == "" {
if me.currentDev == nil {
log.Info("You must select a drive first")
return
}
log.Info("check if", me.currentDev, "is in use")
shell.RunVerbose([]string{"parted", me.currentDev, "print"})
log.Info("TODO: figure out if the drive is in use")
})
grid.NextRow()
@ -72,6 +104,16 @@ func drawWindow(win *gadgets.GenericWindow) {
doDrives()
})
doDrives2()
grid.NewButton("show blkpb", func() {
for blk := range me.pb.IterAll() {
log.Printf("found %-12s %s\n", blk.Name, virtpb.HumanFormatBytes(int64(blk.SizeBytes)))
}
})
grid.NextRow()
grid = win.Middle.RawGrid()
me.driveInfoBox = grid.NewLabel("<drive info>")
doDrives2()
doDrives()
}

View File

@ -43,9 +43,7 @@ func doDrives2() {
for _, info := range devs {
devPath := "/dev/" + info.Name
if info.IsRaw {
s := log.Sprintf("%-12s -> %-8s (raw)", devPath, info.Type)
log.Info(s)
me.dd.AddText(s)
addDrive(devPath, log.Sprintf("-> %-8s (raw)", info.Type))
} else {
if info.Parent != "" {
log.Printf("%-12s -> partition of /dev/%s\n", devPath, info.Parent)

View File

@ -49,6 +49,8 @@ func main() {
os.Exit(0)
}
me.pb = NewBlocks()
/*
if argv.Drives != nil {
doDrives()

View File

@ -12,9 +12,11 @@ var me *autoType
// this app's variables
type autoType struct {
pp *arg.Parser // go-arg preprocessor
myGui *gui.Node // the gui toolkit handle
dd *gui.Node // the drives dropdown list
parted *gui.Node // the current drive to run parted on
currentDev string // the current dev entry to work on
pp *arg.Parser // go-arg preprocessor
myGui *gui.Node // the gui toolkit handle
dd *gui.Node // the drives dropdown list
parted *gui.Node // the current drive to run parted on
currentDev *Block // the current dev entry to work on
pb *Blocks // the block dev protobuf
driveInfoBox *gui.Node // displays the drive info
}

View File

@ -44,7 +44,9 @@ func listenForBlockEvents() {
msg := parseUevent(buf[:n])
if msg["SUBSYSTEM"] == "block" && msg["ACTION"] == "add" {
log.Printf("New block device added: %s\n", msg["DEVNAME"])
me.dd.AddText("/dev/" + msg["DEVNAME"] + " new")
devname := "/dev/" + msg["DEVNAME"]
me.dd.AddText(devname + " new")
me.pb.InsertByName(devname)
}
log.Printf("New syscall.NETLINK_KOBJECT_UEVENT: %v\n", msg)
}