Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

govc: add 'device.clock.add' command #2837

Merged
merged 1 commit into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions govc/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

```
Expand Down
86 changes: 86 additions & 0 deletions govc/device/clock/add.go
Original file line number Diff line number Diff line change
@@ -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 {
embano1 marked this conversation as resolved.
Show resolved Hide resolved
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)
embano1 marked this conversation as resolved.
Show resolved Hide resolved

return nil
}
8 changes: 8 additions & 0 deletions govc/device/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down
1 change: 1 addition & 0 deletions govc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 16 additions & 0 deletions govc/test/device.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions object/virtual_device_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down