@@ -22,6 +22,7 @@ import (
22
22
"fmt"
23
23
"io"
24
24
"log"
25
+ "strings"
25
26
"text/tabwriter"
26
27
27
28
"github.com/vmware/govmomi/cli"
@@ -41,6 +42,7 @@ type ls struct {
41
42
long bool
42
43
id bool
43
44
disk bool
45
+ back bool
44
46
}
45
47
46
48
func init () {
@@ -60,6 +62,7 @@ func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
60
62
f .BoolVar (& cmd .long , "l" , false , "Long listing format" )
61
63
f .BoolVar (& cmd .id , "i" , false , "List volume ID only" )
62
64
f .BoolVar (& cmd .disk , "L" , false , "List volume disk or file backing ID only" )
65
+ f .BoolVar (& cmd .back , "b" , false , "List file backing path" )
63
66
}
64
67
65
68
func (cmd * ls ) Process (ctx context.Context ) error {
@@ -84,14 +87,24 @@ Examples:
84
87
govc volume.ls -l
85
88
govc volume.ls -ds vsanDatastore
86
89
govc volume.ls df86393b-5ae0-4fca-87d0-b692dbc67d45
90
+ govc volume.ls -json $id | jq -r .volume[].backingObjectDetails.backingDiskPath
91
+ govc volume.ls -b $id # verify backingDiskPath exists
87
92
govc disk.ls -l $(govc volume.ls -L pvc-9744a4ff-07f4-43c4-b8ed-48ea7a528734)`
88
93
}
89
94
90
95
type lsWriter struct {
91
- Volume []types.CnsVolume `json:"volume"`
96
+ Volume []types.CnsVolume `json:"volume"`
97
+ Info []types.BaseCnsVolumeOperationResult `json:"info,omitempty"`
92
98
cmd * ls
93
99
}
94
100
101
+ func (r * lsWriter ) Dump () any {
102
+ if len (r .Info ) != 0 {
103
+ return r .Info
104
+ }
105
+ return r .Volume
106
+ }
107
+
95
108
func (r * lsWriter ) Write (w io.Writer ) error {
96
109
if r .cmd .id {
97
110
for _ , volume := range r .Volume {
@@ -123,6 +136,9 @@ func (r *lsWriter) Write(w io.Writer) error {
123
136
124
137
for _ , volume := range r .Volume {
125
138
fmt .Printf ("%s\t %s" , volume .VolumeId .Id , volume .Name )
139
+ if r .cmd .back {
140
+ fmt .Printf ("\t %s" , r .backing (volume .VolumeId ))
141
+ }
126
142
if r .cmd .long {
127
143
capacity := volume .BackingObjectDetails .GetCnsBackingObjectDetails ().CapacityInMb
128
144
c := volume .Metadata .ContainerCluster
@@ -134,6 +150,34 @@ func (r *lsWriter) Write(w io.Writer) error {
134
150
return tw .Flush ()
135
151
}
136
152
153
+ func (r * lsWriter ) backing (id types.CnsVolumeId ) string {
154
+ for _ , info := range r .Info {
155
+ res , ok := info .(* types.CnsQueryVolumeInfoResult )
156
+ if ! ok {
157
+ continue
158
+ }
159
+
160
+ switch vol := res .VolumeInfo .(type ) {
161
+ case * types.CnsBlockVolumeInfo :
162
+ if vol .VStorageObject .Config .Id .Id == id .Id {
163
+ switch backing := vol .VStorageObject .Config .BaseConfigInfo .Backing .(type ) {
164
+ case * vim.BaseConfigInfoDiskFileBackingInfo :
165
+ return backing .FilePath
166
+ }
167
+ }
168
+ }
169
+
170
+ if fault := res .Fault ; fault != nil {
171
+ if f , ok := fault .Fault .(types.CnsFault ); ok {
172
+ if strings .Contains (f .Reason , id .Id ) {
173
+ return f .Reason
174
+ }
175
+ }
176
+ }
177
+ }
178
+ return "???"
179
+ }
180
+
137
181
func (cmd * ls ) Run (ctx context.Context , f * flag.FlagSet ) error {
138
182
ds , err := cmd .DatastoreIfSpecified ()
139
183
if err != nil {
@@ -154,6 +198,7 @@ func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
154
198
}
155
199
156
200
var volumes []types.CnsVolume
201
+ var info []types.BaseCnsVolumeOperationResult
157
202
158
203
for {
159
204
res , err := c .QueryVolume (ctx , cmd .CnsQueryFilter )
@@ -170,5 +215,26 @@ func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
170
215
cmd .Cursor = & res .Cursor
171
216
}
172
217
173
- return cmd .WriteResult (& lsWriter {volumes , cmd })
218
+ if cmd .back {
219
+ ids := make ([]types.CnsVolumeId , len (volumes ))
220
+ for i := range volumes {
221
+ ids [i ] = volumes [i ].VolumeId
222
+ }
223
+
224
+ task , err := c .QueryVolumeInfo (ctx , ids )
225
+ if err != nil {
226
+ return err
227
+ }
228
+
229
+ res , err := task .WaitForResult (ctx , nil )
230
+ if err != nil {
231
+ return err
232
+ }
233
+
234
+ if batch , ok := res .Result .(types.CnsVolumeOperationBatchResult ); ok {
235
+ info = batch .VolumeResults
236
+ }
237
+ }
238
+
239
+ return cmd .WriteResult (& lsWriter {volumes , info , cmd })
174
240
}
0 commit comments