1/* 2Copyright (c) 2014-2016 VMware, Inc. All Rights Reserved. 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package scsi 18 19import ( 20 "context" 21 "flag" 22 "fmt" 23 "strings" 24 25 "github.com/vmware/govmomi/govc/cli" 26 "github.com/vmware/govmomi/govc/flags" 27 "github.com/vmware/govmomi/object" 28 "github.com/vmware/govmomi/vim25/types" 29) 30 31type add struct { 32 *flags.VirtualMachineFlag 33 34 controller string 35 sharedBus string 36 hotAddRemove bool 37} 38 39func init() { 40 cli.Register("device.scsi.add", &add{}) 41} 42 43func (cmd *add) Register(ctx context.Context, f *flag.FlagSet) { 44 cmd.VirtualMachineFlag, ctx = flags.NewVirtualMachineFlag(ctx) 45 cmd.VirtualMachineFlag.Register(ctx, f) 46 47 var ctypes []string 48 ct := object.SCSIControllerTypes() 49 for _, t := range ct { 50 ctypes = append(ctypes, ct.Type(t)) 51 } 52 f.StringVar(&cmd.controller, "type", ct.Type(ct[0]), 53 fmt.Sprintf("SCSI controller type (%s)", strings.Join(ctypes, "|"))) 54 f.StringVar(&cmd.sharedBus, "sharing", string(types.VirtualSCSISharingNoSharing), "SCSI sharing") 55 f.BoolVar(&cmd.hotAddRemove, "hot", false, "Enable hot-add/remove") 56} 57 58func (cmd *add) Description() string { 59 return `Add SCSI controller to VM. 60 61Examples: 62 govc device.scsi.add -vm $vm 63 govc device.scsi.add -vm $vm -type pvscsi 64 govc device.info -vm $vm {lsi,pv}*` 65} 66 67func (cmd *add) Process(ctx context.Context) error { 68 if err := cmd.VirtualMachineFlag.Process(ctx); err != nil { 69 return err 70 } 71 return nil 72} 73 74func (cmd *add) Run(ctx context.Context, f *flag.FlagSet) error { 75 vm, err := cmd.VirtualMachine() 76 if err != nil { 77 return err 78 } 79 80 if vm == nil { 81 return flag.ErrHelp 82 } 83 84 devices, err := vm.Device(ctx) 85 if err != nil { 86 return err 87 } 88 89 d, err := devices.CreateSCSIController(cmd.controller) 90 if err != nil { 91 return err 92 } 93 94 c := d.(types.BaseVirtualSCSIController).GetVirtualSCSIController() 95 c.HotAddRemove = &cmd.hotAddRemove 96 c.SharedBus = types.VirtualSCSISharing(cmd.sharedBus) 97 98 err = vm.AddDevice(ctx, d) 99 if err != nil { 100 return err 101 } 102 103 // output name of device we just created 104 devices, err = vm.Device(ctx) 105 if err != nil { 106 return err 107 } 108 109 devices = devices.SelectByType(d) 110 111 name := devices.Name(devices[len(devices)-1]) 112 113 fmt.Println(name) 114 115 return nil 116} 117