github.com/vmware/govmomi@v0.37.1/govc/device/pci/ls.go (about) 1 /* 2 Copyright (c) 2020-2023 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 pci 18 19 import ( 20 "context" 21 "flag" 22 "fmt" 23 "io" 24 "os" 25 "text/tabwriter" 26 27 "github.com/vmware/govmomi/govc/cli" 28 "github.com/vmware/govmomi/govc/flags" 29 "github.com/vmware/govmomi/object" 30 "github.com/vmware/govmomi/vim25/types" 31 ) 32 33 type ls struct { 34 *flags.VirtualMachineFlag 35 *flags.OutputFlag 36 } 37 38 func init() { 39 cli.Register("device.pci.ls", &ls{}) 40 } 41 42 func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) { 43 cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx) 44 cmd.VirtualMachineFlag.Register(ctx, f) 45 cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) 46 cmd.OutputFlag.Register(ctx, f) 47 } 48 49 func (cmd *ls) Description() string { 50 return `List allowed PCI passthrough devices that could be attach to VM. 51 52 Examples: 53 govc device.pci.ls -vm VM` 54 } 55 56 func (cmd *ls) Process(ctx context.Context) error { 57 if err := cmd.VirtualMachineFlag.Process(ctx); err != nil { 58 return err 59 } 60 if err := cmd.OutputFlag.Process(ctx); err != nil { 61 return err 62 } 63 return nil 64 } 65 66 func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error { 67 vm, err := cmd.VirtualMachine() 68 if err != nil { 69 return err 70 } 71 if vm == nil { 72 return flag.ErrHelp 73 } 74 75 vmConfigOptions, err := queryConfigTarget(ctx, vm) 76 if err != nil { 77 return err 78 } 79 80 return cmd.WriteResult(&infoResult{PciDevices: vmConfigOptions.PciPassthrough}) 81 } 82 83 type infoResult struct { 84 PciDevices []types.BaseVirtualMachinePciPassthroughInfo `json:"pciDevices"` 85 } 86 87 func (r *infoResult) Write(w io.Writer) error { 88 tw := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0) 89 fmt.Fprintf(tw, "System ID\tAddress\tDevice Name\n") 90 for _, d := range r.PciDevices { 91 info := d.GetVirtualMachinePciPassthroughInfo() 92 pd := info.PciDevice 93 fmt.Fprintf(tw, "%s\t%s\t%s\t%s\n", info.SystemId, pd.Id, pd.VendorName, pd.DeviceName) 94 } 95 return tw.Flush() 96 } 97 98 func queryConfigTarget(ctx context.Context, m *object.VirtualMachine) (*types.ConfigTarget, error) { 99 b, err := m.EnvironmentBrowser(ctx) 100 if err != nil { 101 return nil, err 102 } 103 return b.QueryConfigTarget(ctx, nil) 104 }