예제 #1
0
func addVNIC(ui packer.Ui, f *find.Finder, ctx context.Context, c *govmomi.Client, vm *object.VirtualMachine, network string, nwType string) error {

	ui.Say("Adding NIC")

	nets, err := f.NetworkList(ctx, network)
	if err != nil {
		return err
	}
	// TODO expose param for DVS
	net := nets[1]

	backing, err := net.EthernetCardBackingInfo(ctx)
	if err != nil {
		return err
	}
	device, err := object.EthernetCardTypes().CreateEthernetCard(nwType, backing)
	if err != nil {
		return err
	}
	err = vm.AddDevice(ctx, device)
	if err != nil {
		return err
	}
	ui.Say("Adding NIC Success")

	return nil
} //
// addHardDisk adds a new Hard Disk to the VirtualMachine.
func addHardDisk(vm *object.VirtualMachine, size, iops int64, diskType string, datastore *object.Datastore, diskPath string, controller_type string) error {
	devices, err := vm.Device(context.TODO())
	if err != nil {
		return err
	}
	log.Printf("[DEBUG] vm devices: %#v\n", devices)

	controller, err := devices.FindDiskController(controller_type)
	if err != nil {
		return err
	}
	log.Printf("[DEBUG] disk controller: %#v\n", controller)

	// TODO Check if diskPath & datastore exist
	// If diskPath is not specified, pass empty string to CreateDisk()
	if diskPath == "" {
		return fmt.Errorf("[ERROR] addHardDisk - No path proided")
	} else {
		// TODO Check if diskPath & datastore exist
		diskPath = fmt.Sprintf("[%v] %v", datastore.Name(), diskPath)
	}
	log.Printf("[DEBUG] addHardDisk - diskPath: %v", diskPath)
	disk := devices.CreateDisk(controller, datastore.Reference(), diskPath)

	existing := devices.SelectByBackingInfo(disk.Backing)
	log.Printf("[DEBUG] disk: %#v\n", disk)

	if len(existing) == 0 {
		disk.CapacityInKB = int64(size * 1024 * 1024)
		if iops != 0 {
			disk.StorageIOAllocation = &types.StorageIOAllocationInfo{
				Limit: iops,
			}
		}
		backing := disk.Backing.(*types.VirtualDiskFlatVer2BackingInfo)

		if diskType == "eager_zeroed" {
			// eager zeroed thick virtual disk
			backing.ThinProvisioned = types.NewBool(false)
			backing.EagerlyScrub = types.NewBool(true)
		} else if diskType == "thin" {
			// thin provisioned virtual disk
			backing.ThinProvisioned = types.NewBool(true)
		}

		log.Printf("[DEBUG] addHardDisk: %#v\n", disk)
		log.Printf("[DEBUG] addHardDisk capacity: %#v\n", disk.CapacityInKB)

		return vm.AddDevice(context.TODO(), disk)
	} else {
		log.Printf("[DEBUG] addHardDisk: Disk already present.\n")

		return nil
	}
}
예제 #3
0
파일: create.go 프로젝트: fdawg4l/govmomi
func (cmd *create) addDevices(vm *object.VirtualMachine) error {
	devices, err := vm.Device(context.TODO())
	if err != nil {
		return err
	}

	var add []types.BaseVirtualDevice

	if cmd.disk != "" {
		controller, err := devices.FindDiskController(cmd.controller)
		if err != nil {
			return err
		}

		disk := devices.CreateDisk(controller, cmd.Datastore.Path(cmd.disk))

		if cmd.link {
			disk = devices.ChildDisk(disk)
		}

		add = append(add, disk)
	}

	if cmd.iso != "" {
		ide, err := devices.FindIDEController("")
		if err != nil {
			return err
		}

		cdrom, err := devices.CreateCdrom(ide)
		if err != nil {
			return err
		}

		add = append(add, devices.InsertIso(cdrom, cmd.Datastore.Path(cmd.iso)))
	}

	netdev, err := cmd.NetworkFlag.Device()
	if err != nil {
		return err
	}

	add = append(add, netdev)

	return vm.AddDevice(context.TODO(), add...)
}
// addHardDisk adds a new Hard Disk to the VirtualMachine.
func addHardDisk(vm *object.VirtualMachine, size, iops int64, diskType string) error {
	devices, err := vm.Device(context.TODO())
	if err != nil {
		return err
	}
	log.Printf("[DEBUG] vm devices: %#v\n", devices)

	controller, err := devices.FindDiskController("scsi")
	if err != nil {
		return err
	}
	log.Printf("[DEBUG] disk controller: %#v\n", controller)

	disk := devices.CreateDisk(controller, "")
	existing := devices.SelectByBackingInfo(disk.Backing)
	log.Printf("[DEBUG] disk: %#v\n", disk)

	if len(existing) == 0 {
		disk.CapacityInKB = int64(size * 1024 * 1024)
		if iops != 0 {
			disk.StorageIOAllocation = &types.StorageIOAllocationInfo{
				Limit: iops,
			}
		}
		backing := disk.Backing.(*types.VirtualDiskFlatVer2BackingInfo)

		// if diskType == "eager_zeroed" {
		// 	// eager zeroed thick virtual disk
		// 	backing.ThinProvisioned = types.NewBool(false)
		// 	backing.EagerlyScrub = types.NewBool(true)
		// } else if diskType == "thin" {
		// 	// thin provisioned virtual disk
		// 	backing.ThinProvisioned = types.NewBool(true)
		// }
		backing.ThinProvisioned = types.NewBool(true)

		log.Printf("[DEBUG] addHardDisk: %#v\n", disk)
		log.Printf("[DEBUG] addHardDisk: %#v\n", disk.CapacityInKB)

		return vm.AddDevice(context.TODO(), disk)
	} else {
		log.Printf("[DEBUG] addHardDisk: Disk already present.\n")

		return nil
	}
}
// addCdrom adds a new virtual cdrom drive to the VirtualMachine and attaches an image (ISO) to it from a datastore path.
func addCdrom(vm *object.VirtualMachine, datastore, path string) error {
	devices, err := vm.Device(context.TODO())
	if err != nil {
		return err
	}
	log.Printf("[DEBUG] vm devices: %#v", devices)

	controller, err := devices.FindIDEController("")
	if err != nil {
		return err
	}
	log.Printf("[DEBUG] ide controller: %#v", controller)

	c, err := devices.CreateCdrom(controller)
	if err != nil {
		return err
	}

	c = devices.InsertIso(c, fmt.Sprintf("[%s] %s", datastore, path))
	log.Printf("[DEBUG] addCdrom: %#v", c)

	return vm.AddDevice(context.TODO(), c)
}
예제 #6
0
파일: vmomi.go 프로젝트: zhaog918/govmax
func (vmh *VMHost) AttachRDM(vm *object.VirtualMachine, deviceNAA string) (err error) {

	vmScsiDiskDeviceInfo, err := vmh.getVmScsiDiskDeviceInfo(vm)
	if err != nil {
		return err
	}

	// var scsiCtlrUnitNumber int
	//Build new Virtual Device to add to VM from list of avilable devices found from our query
	for _, ScsiDisk := range vmScsiDiskDeviceInfo {
		if !strings.Contains(ScsiDisk.Disk.CanonicalName, deviceNAA) {
			continue
		}

		var rdmBacking types.VirtualDiskRawDiskMappingVer1BackingInfo
		rdmBacking.FileName = ""
		rdmBacking.DiskMode = "independent_persistent"
		rdmBacking.CompatibilityMode = "physicalMode"
		rdmBacking.DeviceName = ScsiDisk.Disk.DeviceName
		for _, descriptor := range ScsiDisk.Disk.Descriptor {
			if string([]rune(descriptor.Id)[:4]) == "vml." {
				rdmBacking.LunUuid = descriptor.Id
				break
			}
		}
		var rdmDisk types.VirtualDisk
		rdmDisk.Backing = &rdmBacking
		rdmDisk.CapacityInKB = 1024

		controller, err := vmh.getAvailableSCSIController()
		if err != nil {
			return err
		}

		if controller == nil {
			controllers, err := vmh.getSCSIControllers()
			if err != nil {
				return err
			}

			if len(controllers) == 0 {
				return errors.New("no SCSI controllers found")
			}

			if len(controllers) == 4 {
				return errors.New("no more controllers can be added")
			}

			err = vmh.createController(&controllers[0])
			if err != nil {
				return err
			}

			controller, err = vmh.getAvailableSCSIController()
			if err != nil {
				return err
			}
		}

		rdmDisk.ControllerKey = controller.VirtualController.Key
		rdmDisk.UnitNumber = -1

		err = vm.AddDevice(vmh.Ctx, &rdmDisk)
		if err != nil {
			return errors.New(fmt.Sprintf("Error adding device %+v \n Logged Item:  %s", rdmDisk, err))
		}
		return nil

	}

	scsiLuns, err := vmh.GetSCSILuns()
	if err != nil {
		return goof.WithError("error getting existing LUNs", err)
	}

	for _, sl := range scsiLuns {
		if strings.Contains(sl.CanonicalName, deviceNAA) {
			return nil
		}
	}

	return errors.New("no device detected on VM host to add")
}