Repository navigation
orchestrator: add inspect-nbd CLI tool and DevicePool.Status() #3547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AdaAibaby
wants to merge
4
commits into
e2b-dev:main
Choose a base branch
from
AdaAibaby:feat/inspect-nbd-tool
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8534344
fix(api): add structured logs for sandbox delete 500/404 paths
adababys 5ec3453
orchestrator: add inspect-nbd CLI tool and DevicePool.Status()
adababys e82252d
cmd/inspect-nbd: document operator usage model in package comment
adababys a2102c9
Merge branch 'main' into feat/inspect-nbd-tool
AdaAibaby File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| //go:build linux | ||
|
|
||
| // inspect-nbd lists the NBD devices that are currently connected on this host. | ||
| // | ||
| // Background: the nbd kernel module is loaded with nbds_max=4096, creating | ||
| // 4096 /dev/nbdX entries in the OS even when only a handful are in use. | ||
| // `lsblk | grep nbd` produces 4096 lines of noise. This tool reads the kernel's | ||
| // /sys/block/nbdX/pid sentinel — which only exists for connected devices — and | ||
| // prints a concise table of active slots. | ||
| // | ||
| // This is an operator debugging tool, not a service. It is not part of the | ||
| // orchestrator Nomad job and is not uploaded to GCS. To use it on a sandbox | ||
| // node, compile it locally and copy the binary: | ||
| // | ||
| // GOOS=linux GOARCH=amd64 go build -o /tmp/inspect-nbd ./cmd/inspect-nbd | ||
| // scp /tmp/inspect-nbd <node>:/tmp/inspect-nbd | ||
| // | ||
| // Usage: | ||
| // | ||
| // inspect-nbd [-json] | ||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "flag" | ||
| "fmt" | ||
| "os" | ||
| "strconv" | ||
| "strings" | ||
| "text/tabwriter" | ||
|
|
||
| "github.com/e2b-dev/infra/packages/orchestrator/pkg/sandbox/nbd" | ||
| ) | ||
|
|
||
| func main() { | ||
| jsonOut := flag.Bool("json", false, "output as JSON") | ||
| flag.Parse() | ||
|
|
||
| maxData, err := os.ReadFile("/sys/module/nbd/parameters/nbds_max") | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "error: nbd module not loaded or /sys unavailable: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| nbdsMax, _ := strconv.ParseUint(strings.TrimSpace(string(maxData)), 10, 64) | ||
|
|
||
| devices, err := nbd.ConnectedDevices() | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "error: failed to list connected devices: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| type deviceInfo struct { | ||
| Slot uint32 `json:"slot"` | ||
| Path string `json:"path"` | ||
| SizeMB uint64 `json:"size_mb"` | ||
| PID string `json:"pid"` | ||
| } | ||
|
|
||
| infos := make([]deviceInfo, 0, len(devices)) | ||
| for _, slot := range devices { | ||
| sizeRaw, _ := os.ReadFile(fmt.Sprintf("/sys/block/nbd%d/size", slot)) | ||
| sectors, _ := strconv.ParseUint(strings.TrimSpace(string(sizeRaw)), 10, 64) | ||
|
|
||
| pidRaw, _ := os.ReadFile(fmt.Sprintf("/sys/block/nbd%d/pid", slot)) | ||
|
|
||
| infos = append(infos, deviceInfo{ | ||
| Slot: slot, | ||
| Path: nbd.GetDevicePath(slot), | ||
| SizeMB: (sectors * 512) / (1024 * 1024), | ||
| PID: strings.TrimSpace(string(pidRaw)), | ||
| }) | ||
| } | ||
|
|
||
| if *jsonOut { | ||
| type output struct { | ||
| NBDsMax uint64 `json:"nbds_max"` | ||
| Connected int `json:"connected"` | ||
| Devices []deviceInfo `json:"devices"` | ||
| } | ||
| enc := json.NewEncoder(os.Stdout) | ||
| enc.SetIndent("", " ") | ||
| _ = enc.Encode(output{NBDsMax: nbdsMax, Connected: len(infos), Devices: infos}) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| fmt.Printf("NBD devices: %d connected / %d configured\n", len(infos), nbdsMax) | ||
|
|
||
| if len(infos) == 0 { | ||
| fmt.Println("No connected NBD devices.") | ||
|
|
||
| return | ||
| } | ||
|
|
||
| fmt.Println() | ||
| w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) | ||
| fmt.Fprintln(w, "SLOT\tDEVICE\tSIZE (MB)\tPID") | ||
| for _, info := range infos { | ||
| fmt.Fprintf(w, "%d\t%s\t%d\t%s\n", info.Slot, info.Path, info.SizeMB, info.PID) | ||
| } | ||
| _ = w.Flush() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.