github.com/vmware/govmomi@v0.37.2/govc/device/pci/remove.go (about)

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