Skip to content

Commit 59cb29b

Browse files
committed
govc: Add vm.disk.promote command
Signed-off-by: Doug MacEachern <[email protected]>
1 parent 761efe9 commit 59cb29b

File tree

3 files changed

+127
-7
lines changed

3 files changed

+127
-7
lines changed

cli/device/info.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,22 @@ func match(p string, devices object.VirtualDeviceList) object.VirtualDeviceList
9090
return matches
9191
}
9292

93+
// Match returns devices where VirtualDeviceList.Name matches any of the strings in given args.
94+
// See also: path.Match, govc device.info, govc device.ls
95+
func Match(devices object.VirtualDeviceList, args []string) (object.VirtualDeviceList, error) {
96+
var found object.VirtualDeviceList
97+
98+
for _, name := range args {
99+
matches := match(name, devices)
100+
if len(matches) == 0 {
101+
return nil, fmt.Errorf("device '%s' not found", name)
102+
}
103+
found = append(found, matches...)
104+
}
105+
106+
return found, nil
107+
}
108+
93109
func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
94110
vm, err := cmd.VirtualMachine()
95111
if err != nil {
@@ -126,14 +142,12 @@ func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
126142
if f.NArg() == 0 {
127143
res.Devices = toInfoList(devices)
128144
} else {
129-
for _, name := range f.Args() {
130-
matches := match(name, devices)
131-
if len(matches) == 0 {
132-
return fmt.Errorf("device '%s' not found", name)
133-
}
134-
135-
res.Devices = append(res.Devices, toInfoList(matches)...)
145+
devices, err = Match(devices, f.Args())
146+
if err != nil {
147+
return err
136148
}
149+
150+
res.Devices = append(res.Devices, toInfoList(devices)...)
137151
}
138152

139153
return cmd.WriteResult(&res)

cli/vm/disk/promote.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// © Broadcom. All Rights Reserved.
2+
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
package disk
6+
7+
import (
8+
"context"
9+
"flag"
10+
11+
"github.com/vmware/govmomi/cli"
12+
"github.com/vmware/govmomi/cli/device"
13+
"github.com/vmware/govmomi/cli/flags"
14+
"github.com/vmware/govmomi/vim25/types"
15+
)
16+
17+
type promote struct {
18+
*flags.VirtualMachineFlag
19+
20+
unlink bool
21+
}
22+
23+
func init() {
24+
cli.Register("vm.disk.promote", &promote{})
25+
}
26+
27+
func (cmd *promote) Register(ctx context.Context, f *flag.FlagSet) {
28+
cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx)
29+
cmd.VirtualMachineFlag.Register(ctx, f)
30+
31+
f.BoolVar(&cmd.unlink, "unlink", true, "Unlink")
32+
}
33+
34+
func (cmd *promote) Usage() string {
35+
return "DISK..."
36+
}
37+
38+
func (cmd *promote) Description() string {
39+
return `Promote VM disk.
40+
41+
Examples:
42+
govc device.info -vm $name disk-*
43+
govc vm.disk.promote -vm $name disk-1000-0
44+
govc vm.disk.promote -vm $name disk-*`
45+
}
46+
47+
func (cmd *promote) Run(ctx context.Context, f *flag.FlagSet) error {
48+
vm, err := cmd.VirtualMachine()
49+
if err != nil {
50+
return err
51+
}
52+
53+
if vm == nil {
54+
return flag.ErrHelp
55+
}
56+
57+
if f.NArg() == 0 {
58+
return flag.ErrHelp
59+
}
60+
61+
devices, err := vm.Device(ctx)
62+
if err != nil {
63+
return err
64+
}
65+
66+
devices = devices.SelectByType((*types.VirtualDisk)(nil))
67+
68+
devices, err = device.Match(devices, f.Args())
69+
if err != nil {
70+
return err
71+
}
72+
73+
disks := make([]types.VirtualDisk, len(devices))
74+
for i := range devices {
75+
disks[i] = *(devices[i].(*types.VirtualDisk))
76+
}
77+
78+
task, err := vm.PromoteDisks(ctx, cmd.unlink, disks)
79+
if err != nil {
80+
return err
81+
}
82+
83+
logger := cmd.ProgressLogger("Promoting disks...")
84+
defer logger.Wait()
85+
86+
_, err = task.WaitForResult(ctx, logger)
87+
return err
88+
}

govc/USAGE.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ but appear via `govc $cmd -h`:
409409
- [vm.disk.attach](#vmdiskattach)
410410
- [vm.disk.change](#vmdiskchange)
411411
- [vm.disk.create](#vmdiskcreate)
412+
- [vm.disk.promote](#vmdiskpromote)
412413
- [vm.guest.tools](#vmguesttools)
413414
- [vm.info](#vminfo)
414415
- [vm.instantclone](#vminstantclone)
@@ -7201,6 +7202,23 @@ Options:
72017202
-vm= Virtual machine [GOVC_VM]
72027203
```
72037204

7205+
## vm.disk.promote
7206+
7207+
```
7208+
Usage: govc vm.disk.promote [OPTIONS] DISK...
7209+
7210+
Promote VM disk.
7211+
7212+
Examples:
7213+
govc device.info -vm $name disk-*
7214+
govc vm.disk.promote -vm $name disk-1000-0
7215+
govc vm.disk.promote -vm $name disk-*
7216+
7217+
Options:
7218+
-unlink=true Unlink
7219+
-vm= Virtual machine [GOVC_VM]
7220+
```
7221+
72047222
## vm.guest.tools
72057223

72067224
```

0 commit comments

Comments
 (0)