func (d *Dispatcher) configIso(conf *metadata.VirtualContainerHostConfigSpec, vm *vm.VirtualMachine) (object.VirtualDeviceList, error) { defer trace.End(trace.Begin("")) var devices object.VirtualDeviceList var err error vmDevices, err := vm.Device(d.ctx) if err != nil { log.Errorf("Failed to get vm devices for appliance: %s", err) return nil, err } ide, err := vmDevices.FindIDEController("") if err != nil { log.Errorf("Failed to find IDE controller for appliance: %s", err) return nil, err } cdrom, err := devices.CreateCdrom(ide) if err != nil { log.Errorf("Failed to create Cdrom device for appliance: %s", err) return nil, err } cdrom = devices.InsertIso(cdrom, fmt.Sprintf("[%s] %s/appliance.iso", conf.ImageStores[0].Host, d.vmPathName)) devices = append(devices, cdrom) return devices, nil }
func (d *Dispatcher) networkDevices(vmm *vm.VirtualMachine) ([]types.BaseVirtualDevice, error) { defer trace.End(trace.Begin("")) var err error vmDevices, err := vmm.Device(d.ctx) if err != nil { log.Errorf("Failed to get vm devices for appliance: %s", err) return nil, err } var devices []types.BaseVirtualDevice for _, device := range vmDevices { if _, ok := device.(types.BaseVirtualEthernetCard); ok { devices = append(devices, device) } } return devices, nil }
// Find the disk by name attached to the given vm. func findDisk(op trace.Operation, vm *vm.VirtualMachine, name string) (*types.VirtualDisk, error) { defer trace.End(trace.Begin(vm.String())) log.Debugf("Looking for attached disk matching filename %s", name) devices, err := vm.Device(op) if err != nil { return nil, fmt.Errorf("Failed to refresh devices for vm: %s", errors.ErrorStack(err)) } candidates := devices.Select(func(device types.BaseVirtualDevice) bool { db := device.GetVirtualDevice().Backing if db == nil { return false } backing, ok := device.GetVirtualDevice().Backing.(*types.VirtualDiskFlatVer2BackingInfo) if !ok { return false } log.Debugf("backing file name %s", backing.VirtualDeviceFileBackingInfo.FileName) match := strings.HasSuffix(backing.VirtualDeviceFileBackingInfo.FileName, name) if match { log.Debugf("Found candidate disk for %s at %s", name, backing.VirtualDeviceFileBackingInfo.FileName) } return match }) if len(candidates) == 0 { log.Warnf("No disks match name: %s", name) return nil, os.ErrNotExist } if len(candidates) > 1 { return nil, errors.Errorf("Too many disks match name: %s", name) } return candidates[0].(*types.VirtualDisk), nil }
// ensures that a paravirtual scsi controller is present and determines the // base path of disks attached to it returns a handle to the controller and a // format string, with a single decimal for the disk unit number which will // result in the /dev/disk/by-path path func verifyParavirtualScsiController(op trace.Operation, vm *vm.VirtualMachine) (*types.ParaVirtualSCSIController, string, error) { devices, err := vm.Device(op) if err != nil { log.Errorf("vmware driver failed to retrieve device list for VM %s: %s", vm, errors.ErrorStack(err)) return nil, "", errors.Trace(err) } controller, ok := devices.PickController((*types.ParaVirtualSCSIController)(nil)).(*types.ParaVirtualSCSIController) if controller == nil || !ok { err = errors.Errorf("vmware driver failed to find a paravirtual SCSI controller - ensure setup ran correctly") log.Error(err.Error()) return nil, "", errors.Trace(err) } // build the base path // first we determine which label we're looking for (requires VMW hardware version >=10) targetLabel := fmt.Sprintf("SCSI%d", controller.BusNumber) log.Debugf("Looking for scsi controller with label %s", targetLabel) pciBase := "/sys/bus/pci/devices" pciBus, err := os.Open(pciBase) if err != nil { log.Errorf("Failed to open %s for reading: %s", pciBase, errors.ErrorStack(err)) return controller, "", errors.Trace(err) } defer pciBus.Close() pciDevices, err := pciBus.Readdirnames(0) if err != nil { log.Errorf("Failed to read contents of %s: %s", pciBase, errors.ErrorStack(err)) return controller, "", errors.Trace(err) } var buf = make([]byte, len(targetLabel)) var controllerName string for _, n := range pciDevices { nlabel := fmt.Sprintf("%s/%s/label", pciBase, n) flabel, err := os.Open(nlabel) if err != nil { if !os.IsNotExist(err) { log.Errorf("Unable to read label from %s: %s", nlabel, errors.ErrorStack(err)) } continue } defer flabel.Close() _, err = flabel.Read(buf) if err != nil { log.Errorf("Unable to read label from %s: %s", nlabel, errors.ErrorStack(err)) continue } if targetLabel == string(buf) { // we've found our controller controllerName = n log.Debugf("Found pvscsi controller directory: %s", controllerName) break } } if controllerName == "" { err := errors.Errorf("Failed to locate pvscsi controller directory") log.Errorf(err.Error()) return controller, "", errors.Trace(err) } formatString := fmt.Sprintf("/dev/disk/by-path/pci-%s-scsi-0:0:%%d:0", controllerName) log.Debugf("Disk location format: %s", formatString) return controller, formatString, nil }