github.com/vmware/govmomi@v0.51.0/cli/device/pci/remove.go (about)

     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 pci
     6  
     7  import (
     8  	"context"
     9  	"flag"
    10  	"fmt"
    11  
    12  	"github.com/vmware/govmomi/cli"
    13  	"github.com/vmware/govmomi/cli/flags"
    14  	"github.com/vmware/govmomi/vim25/types"
    15  )
    16  
    17  type remove struct {
    18  	*flags.VirtualMachineFlag
    19  }
    20  
    21  func init() {
    22  	cli.Register("device.pci.remove", &remove{})
    23  }
    24  
    25  func (cmd *remove) Register(ctx context.Context, f *flag.FlagSet) {
    26  	cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx)
    27  	cmd.VirtualMachineFlag.Register(ctx, f)
    28  }
    29  
    30  func (cmd *remove) Usage() string {
    31  	return "<PCI ADDRESS>..."
    32  }
    33  
    34  func (cmd *remove) Description() string {
    35  	return `Remove PCI Passthrough device from VM.
    36  
    37  Examples:
    38    govc device.info -vm $vm
    39    govc device.pci.remove -vm $vm $pci_address
    40    govc device.info -vm $vm
    41  
    42  Assuming vm name is helloworld, device info command has below output
    43  
    44  $ govc device.info -vm helloworld
    45  ...
    46  Name:               pcipassthrough-13000
    47    Type:             VirtualPCIPassthrough
    48    Label:            PCI device 0
    49    Summary:
    50    Key:              13000
    51    Controller:       pci-100
    52    Unit number:      18
    53  Name:               pcipassthrough-13001
    54    Type:             VirtualPCIPassthrough
    55    Label:            PCI device 1
    56    Summary:
    57    Key:              13001
    58    Controller:       pci-100
    59    Unit number:      19
    60  
    61  To remove only 'pcipassthrough-13000', command should be as below. No output upon success.
    62  
    63  $ govc device.pci.remove -vm helloworld pcipassthrough-13000
    64  
    65  To remove both 'pcipassthrough-13000' and 'pcipassthrough-13001', command should be as below.
    66  No output upon success.
    67  
    68  $ govc device.pci.remove -vm helloworld pcipassthrough-13000 pcipassthrough-13001`
    69  }
    70  
    71  func (cmd *remove) Run(ctx context.Context, f *flag.FlagSet) error {
    72  	if len(f.Args()) == 0 {
    73  		return flag.ErrHelp
    74  	}
    75  
    76  	reqDevices := map[string]bool{}
    77  	for _, n := range f.Args() {
    78  		reqDevices[n] = false
    79  	}
    80  
    81  	vm, err := cmd.VirtualMachine()
    82  	if err != nil {
    83  		return err
    84  	}
    85  	if vm == nil {
    86  		return flag.ErrHelp
    87  	}
    88  
    89  	vmDevices, err := vm.Device(ctx)
    90  	if err != nil {
    91  		return err
    92  	}
    93  
    94  	rmDevices := []types.BaseVirtualDevice{}
    95  	for _, d := range vmDevices.SelectByType(&types.VirtualPCIPassthrough{}) {
    96  		name := vmDevices.Name(d)
    97  		_, ok := reqDevices[name]
    98  		if !ok {
    99  			continue
   100  		}
   101  		reqDevices[name] = true
   102  		rmDevices = append(rmDevices, d)
   103  	}
   104  
   105  	for id, found := range reqDevices {
   106  		if !found {
   107  			return fmt.Errorf("%s is not found, please check and try again", id)
   108  		}
   109  	}
   110  	return vm.RemoveDevice(ctx, false, rmDevices...)
   111  }