Skip to content

Commit fc17df0

Browse files
committed
govc: add 'device.clock.add' command
Closes #2834
1 parent ac1eba3 commit fc17df0

File tree

6 files changed

+129
-0
lines changed

6 files changed

+129
-0
lines changed

govc/USAGE.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ but appear via `govc $cmd -h`:
8686
- [device.cdrom.add](#devicecdromadd)
8787
- [device.cdrom.eject](#devicecdromeject)
8888
- [device.cdrom.insert](#devicecdrominsert)
89+
- [device.clock.add](#deviceclockadd)
8990
- [device.connect](#deviceconnect)
9091
- [device.disconnect](#devicedisconnect)
9192
- [device.floppy.add](#devicefloppyadd)
@@ -1275,6 +1276,21 @@ Options:
12751276
-vm= Virtual machine [GOVC_VM]
12761277
```
12771278

1279+
## device.clock.add
1280+
1281+
```
1282+
Usage: govc device.clock.add [OPTIONS]
1283+
1284+
Add precision clock device to VM.
1285+
1286+
Examples:
1287+
govc device.clock.add -vm $vm
1288+
govc device.info clock-*
1289+
1290+
Options:
1291+
-vm= Virtual machine [GOVC_VM]
1292+
```
1293+
12781294
## device.connect
12791295

12801296
```

govc/device/clock/add.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
Copyright (c) 2022 VMware, Inc. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package floppy
18+
19+
import (
20+
"context"
21+
"flag"
22+
"fmt"
23+
24+
"github.com/vmware/govmomi/govc/cli"
25+
"github.com/vmware/govmomi/govc/flags"
26+
"github.com/vmware/govmomi/vim25/types"
27+
)
28+
29+
type add struct {
30+
*flags.VirtualMachineFlag
31+
}
32+
33+
func init() {
34+
cli.Register("device.clock.add", &add{})
35+
}
36+
37+
func (cmd *add) Register(ctx context.Context, f *flag.FlagSet) {
38+
cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx)
39+
cmd.VirtualMachineFlag.Register(ctx, f)
40+
}
41+
42+
func (cmd *add) Description() string {
43+
return `Add precision clock device to VM.
44+
45+
Examples:
46+
govc device.clock.add -vm $vm
47+
govc device.info clock-*`
48+
}
49+
50+
func (cmd *add) Run(ctx context.Context, f *flag.FlagSet) error {
51+
vm, err := cmd.VirtualMachine()
52+
if err != nil {
53+
return err
54+
}
55+
56+
if vm == nil {
57+
return flag.ErrHelp
58+
}
59+
60+
d := &types.VirtualPrecisionClock{
61+
VirtualDevice: types.VirtualDevice{
62+
Backing: &types.VirtualPrecisionClockSystemClockBackingInfo{
63+
Protocol: string(types.HostDateTimeInfoProtocolPtp), // TODO: ntp option
64+
},
65+
},
66+
}
67+
68+
err = vm.AddDevice(ctx, d)
69+
if err != nil {
70+
return err
71+
}
72+
73+
// output name of device we just created
74+
devices, err := vm.Device(ctx)
75+
if err != nil {
76+
return err
77+
}
78+
79+
devices = devices.SelectByType(d)
80+
81+
name := devices.Name(devices[len(devices)-1])
82+
83+
fmt.Println(name)
84+
85+
return nil
86+
}

govc/device/info.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,14 @@ func (r *infoResult) Write(w io.Writer) error {
243243
fmt.Fprintf(tw, " Service URI:\t%s\n", b.ServiceURI)
244244
fmt.Fprintf(tw, " Proxy URI:\t%s\n", b.ProxyURI)
245245
}
246+
case *types.VirtualPrecisionClock:
247+
if b, ok := md.Backing.(*types.VirtualPrecisionClockSystemClockBackingInfo); ok {
248+
proto := b.Protocol
249+
if proto == "" {
250+
proto = string(types.HostDateTimeInfoProtocolPtp)
251+
}
252+
fmt.Fprintf(tw, " Protocol:\t%s\n", proto)
253+
}
246254
}
247255
}
248256

govc/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
_ "github.com/vmware/govmomi/govc/datastore/vsan"
3535
_ "github.com/vmware/govmomi/govc/device"
3636
_ "github.com/vmware/govmomi/govc/device/cdrom"
37+
_ "github.com/vmware/govmomi/govc/device/clock"
3738
_ "github.com/vmware/govmomi/govc/device/floppy"
3839
_ "github.com/vmware/govmomi/govc/device/pci"
3940
_ "github.com/vmware/govmomi/govc/device/scsi"

govc/test/device.bats

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,22 @@ load test_helper
279279
[ $result -eq 1 ]
280280
}
281281

282+
@test "device.clock" {
283+
vcsim_env
284+
285+
vm=$(new_empty_vm)
286+
287+
result=$(govc device.ls -vm "$vm" | grep clock | wc -l)
288+
[ "$result" -eq 0 ]
289+
290+
run govc device.clock.add -vm "$vm"
291+
assert_success
292+
id=$output
293+
294+
result=$(govc device.ls -vm "$vm" | grep "$id" | wc -l)
295+
[ "$result" -eq 1 ]
296+
}
297+
282298
@test "device.scsi slots" {
283299
vcsim_env
284300

object/virtual_device_list.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,8 @@ func (l VirtualDeviceList) Type(device types.BaseVirtualDevice) string {
894894
return "lsilogic-sas"
895895
case *types.VirtualNVMEController:
896896
return "nvme"
897+
case *types.VirtualPrecisionClock:
898+
return "clock"
897899
default:
898900
return l.deviceName(device)
899901
}

0 commit comments

Comments
 (0)