Skip to content

Commit

Permalink
govc: add disk.ls '-a' flag
Browse files Browse the repository at this point in the history
Deleting an FCD file backing can create an orphan FCD. An orphaned ID is returned by the ListVStorageObject API,
but calling RetrieveVStorageObject with an orphan ID results in a NotFound fault.
The disk.ls command would return an error and suggest: use 'disk.ls -R' to reconcile datastore inventory
The -R flag calls ReconcileDatastoreInventory, which normally cleans up such orphans, but we have seen cases where it does not.

This change ignores orphans by default. The new '-a' flag will list *all* orphans (if any) and includes the disk.ls -R suggestion.

Fixes vmware#3639

Signed-off-by: Doug MacEachern <dougm@broadcom.com>
  • Loading branch information
dougm committed Dec 19, 2024
1 parent 46d5d87 commit 1f91418
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
15 changes: 14 additions & 1 deletion cli/disk/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (

type ls struct {
*flags.DatastoreFlag
all bool
long bool
path bool
r bool
Expand All @@ -51,6 +52,7 @@ func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
cmd.DatastoreFlag, ctx = flags.NewDatastoreFlag(ctx)
cmd.DatastoreFlag.Register(ctx, f)

f.BoolVar(&cmd.all, "a", false, "List IDs with missing file backing")
f.BoolVar(&cmd.long, "l", false, "Long listing format")
f.BoolVar(&cmd.path, "L", false, "Print disk backing path instead of disk name")
f.BoolVar(&cmd.r, "R", false, "Reconcile the datastore inventory info")
Expand Down Expand Up @@ -167,7 +169,18 @@ func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
if err != nil {
if filterNotFound && fault.Is(err, &types.NotFound{}) {
// The case when an FCD is deleted by something other than DeleteVStorageObject_Task, such as VM destroy
return fmt.Errorf("%s not found: use 'disk.ls -R' to reconcile datastore inventory", id)
if cmd.all {
obj := VStorageObject{VStorageObject: types.VStorageObject{
Config: types.VStorageObjectConfigInfo{
BaseConfigInfo: types.BaseConfigInfo{
Id: types.ID{Id: id},
Name: "not found: use 'disk.ls -R' to reconcile datastore inventory",
},
},
}}
res.Objects = append(res.Objects, obj)
}
continue
}
return fmt.Errorf("retrieve %q: %s", id, err)
}
Expand Down
1 change: 1 addition & 0 deletions govc/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2150,6 +2150,7 @@ Options:
-L=false Print disk backing path instead of disk name
-R=false Reconcile the datastore inventory info
-T=false List attached tags
-a=false List IDs with missing file backing
-c= Query tag category
-ds= Datastore [GOVC_DATASTORE]
-l=false Long listing format
Expand Down
10 changes: 9 additions & 1 deletion govc/test/disk.bats
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,21 @@ load test_helper

run govc disk.ls
assert_success
[ ${#lines[@]} -eq 2 ]

path=$(govc disk.ls -json "$id" | jq -r .objects[].config.backing.filePath)
run govc datastore.rm "$path"
assert_success

# file backing was removed without using disk.rm, results in NotFound fault
run govc disk.ls
assert_failure # file backing was removed without using disk.rm, results in NotFound fault
assert_success
[ ${#lines[@]} -eq 1 ]

run govc disk.ls -a
assert_success
[ ${#lines[@]} -eq 2 ]
assert_matches "not found"

run govc disk.ls -R
assert_success
Expand Down

0 comments on commit 1f91418

Please sign in to comment.