diff --git a/govc/USAGE.md b/govc/USAGE.md index 6189f28eb..7bdc5c88e 100644 --- a/govc/USAGE.md +++ b/govc/USAGE.md @@ -86,6 +86,7 @@ but appear via `govc $cmd -h`: - [device.cdrom.add](#devicecdromadd) - [device.cdrom.eject](#devicecdromeject) - [device.cdrom.insert](#devicecdrominsert) + - [device.clock.add](#deviceclockadd) - [device.connect](#deviceconnect) - [device.disconnect](#devicedisconnect) - [device.floppy.add](#devicefloppyadd) @@ -1275,6 +1276,21 @@ Options: -vm= Virtual machine [GOVC_VM] ``` +## device.clock.add + +``` +Usage: govc device.clock.add [OPTIONS] + +Add precision clock device to VM. + +Examples: + govc device.clock.add -vm $vm + govc device.info clock-* + +Options: + -vm= Virtual machine [GOVC_VM] +``` + ## device.connect ``` diff --git a/govc/device/clock/add.go b/govc/device/clock/add.go new file mode 100644 index 000000000..c984d5f32 --- /dev/null +++ b/govc/device/clock/add.go @@ -0,0 +1,86 @@ +/* +Copyright (c) 2022 VMware, Inc. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package floppy + +import ( + "context" + "flag" + "fmt" + + "github.com/vmware/govmomi/govc/cli" + "github.com/vmware/govmomi/govc/flags" + "github.com/vmware/govmomi/vim25/types" +) + +type add struct { + *flags.VirtualMachineFlag +} + +func init() { + cli.Register("device.clock.add", &add{}) +} + +func (cmd *add) Register(ctx context.Context, f *flag.FlagSet) { + cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx) + cmd.VirtualMachineFlag.Register(ctx, f) +} + +func (cmd *add) Description() string { + return `Add precision clock device to VM. + +Examples: + govc device.clock.add -vm $vm + govc device.info clock-*` +} + +func (cmd *add) Run(ctx context.Context, f *flag.FlagSet) error { + vm, err := cmd.VirtualMachine() + if err != nil { + return err + } + + if vm == nil { + return flag.ErrHelp + } + + d := &types.VirtualPrecisionClock{ + VirtualDevice: types.VirtualDevice{ + Backing: &types.VirtualPrecisionClockSystemClockBackingInfo{ + Protocol: string(types.HostDateTimeInfoProtocolPtp), // TODO: ntp option + }, + }, + } + + err = vm.AddDevice(ctx, d) + if err != nil { + return err + } + + // output name of device we just created + devices, err := vm.Device(ctx) + if err != nil { + return err + } + + devices = devices.SelectByType(d) + + name := devices.Name(devices[len(devices)-1]) + + fmt.Println(name) + + return nil +} diff --git a/govc/device/info.go b/govc/device/info.go index bf561bc86..4c7d4a08a 100644 --- a/govc/device/info.go +++ b/govc/device/info.go @@ -243,6 +243,14 @@ func (r *infoResult) Write(w io.Writer) error { fmt.Fprintf(tw, " Service URI:\t%s\n", b.ServiceURI) fmt.Fprintf(tw, " Proxy URI:\t%s\n", b.ProxyURI) } + case *types.VirtualPrecisionClock: + if b, ok := md.Backing.(*types.VirtualPrecisionClockSystemClockBackingInfo); ok { + proto := b.Protocol + if proto == "" { + proto = string(types.HostDateTimeInfoProtocolPtp) + } + fmt.Fprintf(tw, " Protocol:\t%s\n", proto) + } } } diff --git a/govc/main.go b/govc/main.go index ec91ae7d9..2493c24c4 100644 --- a/govc/main.go +++ b/govc/main.go @@ -34,6 +34,7 @@ import ( _ "github.com/vmware/govmomi/govc/datastore/vsan" _ "github.com/vmware/govmomi/govc/device" _ "github.com/vmware/govmomi/govc/device/cdrom" + _ "github.com/vmware/govmomi/govc/device/clock" _ "github.com/vmware/govmomi/govc/device/floppy" _ "github.com/vmware/govmomi/govc/device/pci" _ "github.com/vmware/govmomi/govc/device/scsi" diff --git a/govc/test/device.bats b/govc/test/device.bats index b064563ce..629285854 100755 --- a/govc/test/device.bats +++ b/govc/test/device.bats @@ -279,6 +279,22 @@ load test_helper [ $result -eq 1 ] } +@test "device.clock" { + vcsim_env + + vm=$(new_empty_vm) + + result=$(govc device.ls -vm "$vm" | grep clock | wc -l) + [ "$result" -eq 0 ] + + run govc device.clock.add -vm "$vm" + assert_success + id=$output + + result=$(govc device.ls -vm "$vm" | grep "$id" | wc -l) + [ "$result" -eq 1 ] +} + @test "device.scsi slots" { vcsim_env diff --git a/object/virtual_device_list.go b/object/virtual_device_list.go index 30bd17054..edf071c54 100644 --- a/object/virtual_device_list.go +++ b/object/virtual_device_list.go @@ -894,6 +894,8 @@ func (l VirtualDeviceList) Type(device types.BaseVirtualDevice) string { return "lsilogic-sas" case *types.VirtualNVMEController: return "nvme" + case *types.VirtualPrecisionClock: + return "clock" default: return l.deviceName(device) }