bool UIWizardNewVM::createVM() { /* Get VBox object: */ CVirtualBox vbox = vboxGlobal().virtualBox(); /* OS type: */ CGuestOSType type = field("type").value<CGuestOSType>(); QString strTypeId = type.GetId(); /* Create virtual machine: */ if (m_machine.isNull()) { QVector<QString> groups; if (!m_strGroup.isEmpty()) groups << m_strGroup; m_machine = vbox.CreateMachine(field("machineFilePath").toString(), field("machineBaseName").toString(), groups, strTypeId, QString()); if (!vbox.isOk()) { msgCenter().cannotCreateMachine(vbox, this); return false; } /* The First RUN Wizard is to be shown: * 1. if we don't attach any virtual hard-drive * 2. or attach a new (empty) one. * Usually we are assigning extra-data values through UIExtraDataManager, * but in that special case VM was not registered yet, so UIExtraDataManager is unaware of it. */ if (field("virtualDiskId").toString().isNull() || !field("virtualDisk").value<CMedium>().isNull()) m_machine.SetExtraData(GUI_FirstRun, "yes"); } /* RAM size: */ m_machine.SetMemorySize(field("ram").toInt()); /* VRAM size - select maximum between recommended and minimum for fullscreen: */ m_machine.SetVRAMSize(qMax(type.GetRecommendedVRAM(), (ULONG)(VBoxGlobal::requiredVideoMemory(strTypeId) / _1M))); /* Selecting recommended chipset type: */ m_machine.SetChipsetType(type.GetRecommendedChipset()); /* Selecting recommended Audio Controller: */ m_machine.GetAudioAdapter().SetAudioController(type.GetRecommendedAudioController()); /* And the Audio Codec: */ m_machine.GetAudioAdapter().SetAudioCodec(type.GetRecommendedAudioCodec()); /* Enabling audio by default: */ m_machine.GetAudioAdapter().SetEnabled(true); /* Enable the OHCI and EHCI controller by default for new VMs. (new in 2.2): */ CUSBDeviceFilters usbDeviceFilters = m_machine.GetUSBDeviceFilters(); bool fOhciEnabled = false; if (!usbDeviceFilters.isNull() && type.GetRecommendedUSB3() && m_machine.GetUSBProxyAvailable()) { /* USB 3.0 is only available if the proper ExtPack is installed. */ CExtPackManager manager = vboxGlobal().virtualBox().GetExtensionPackManager(); if (manager.IsExtPackUsable(GUI_ExtPackName)) { m_machine.AddUSBController("XHCI", KUSBControllerType_XHCI); /* xHci includes OHCI */ fOhciEnabled = true; } } if ( !fOhciEnabled && !usbDeviceFilters.isNull() && type.GetRecommendedUSB() && m_machine.GetUSBProxyAvailable()) { m_machine.AddUSBController("OHCI", KUSBControllerType_OHCI); fOhciEnabled = true; /* USB 2.0 is only available if the proper ExtPack is installed. * Note. Configuring EHCI here and providing messages about * the missing extpack isn't exactly clean, but it is a * necessary evil to patch over legacy compatability issues * introduced by the new distribution model. */ CExtPackManager manager = vboxGlobal().virtualBox().GetExtensionPackManager(); if (manager.IsExtPackUsable(GUI_ExtPackName)) m_machine.AddUSBController("EHCI", KUSBControllerType_EHCI); } /* Create a floppy controller if recommended: */ QString strFloppyName = getNextControllerName(KStorageBus_Floppy); if (type.GetRecommendedFloppy()) { m_machine.AddStorageController(strFloppyName, KStorageBus_Floppy); CStorageController flpCtr = m_machine.GetStorageControllerByName(strFloppyName); flpCtr.SetControllerType(KStorageControllerType_I82078); } /* Create recommended DVD storage controller: */ KStorageBus strDVDBus = type.GetRecommendedDVDStorageBus(); QString strDVDName = getNextControllerName(strDVDBus); m_machine.AddStorageController(strDVDName, strDVDBus); /* Set recommended DVD storage controller type: */ CStorageController dvdCtr = m_machine.GetStorageControllerByName(strDVDName); KStorageControllerType dvdStorageControllerType = type.GetRecommendedDVDStorageController(); dvdCtr.SetControllerType(dvdStorageControllerType); /* Create recommended HD storage controller if it's not the same as the DVD controller: */ KStorageBus ctrHDBus = type.GetRecommendedHDStorageBus(); KStorageControllerType hdStorageControllerType = type.GetRecommendedHDStorageController(); CStorageController hdCtr; QString strHDName; if (ctrHDBus != strDVDBus || hdStorageControllerType != dvdStorageControllerType) { strHDName = getNextControllerName(ctrHDBus); m_machine.AddStorageController(strHDName, ctrHDBus); hdCtr = m_machine.GetStorageControllerByName(strHDName); hdCtr.SetControllerType(hdStorageControllerType); } else { /* The HD controller is the same as DVD: */ hdCtr = dvdCtr; strHDName = strDVDName; } /* Liomit the AHCI port count if it's used because windows has trouble with too many ports and other guest (OS X in particular) may take extra long to boot: */ if (hdStorageControllerType == KStorageControllerType_IntelAhci) hdCtr.SetPortCount(1 + (dvdStorageControllerType == KStorageControllerType_IntelAhci)); else if (dvdStorageControllerType == KStorageControllerType_IntelAhci) dvdCtr.SetPortCount(1); /* Turn on PAE, if recommended: */ m_machine.SetCPUProperty(KCPUPropertyType_PAE, type.GetRecommendedPAE()); /* Set the recommended triple fault behavior: */ m_machine.SetCPUProperty(KCPUPropertyType_TripleFaultReset, type.GetRecommendedTFReset()); /* Set recommended firmware type: */ KFirmwareType fwType = type.GetRecommendedFirmware(); m_machine.SetFirmwareType(fwType); /* Set recommended human interface device types: */ if (type.GetRecommendedUSBHID()) { m_machine.SetKeyboardHIDType(KKeyboardHIDType_USBKeyboard); m_machine.SetPointingHIDType(KPointingHIDType_USBMouse); if (!fOhciEnabled && !usbDeviceFilters.isNull()) m_machine.AddUSBController("OHCI", KUSBControllerType_OHCI); } if (type.GetRecommendedUSBTablet()) { m_machine.SetPointingHIDType(KPointingHIDType_USBTablet); if (!fOhciEnabled && !usbDeviceFilters.isNull()) m_machine.AddUSBController("OHCI", KUSBControllerType_OHCI); } /* Set HPET flag: */ m_machine.SetHPETEnabled(type.GetRecommendedHPET()); /* Set UTC flags: */ m_machine.SetRTCUseUTC(type.GetRecommendedRTCUseUTC()); /* Set graphic bits: */ if (type.GetRecommended2DVideoAcceleration()) m_machine.SetAccelerate2DVideoEnabled(type.GetRecommended2DVideoAcceleration()); if (type.GetRecommended3DAcceleration()) m_machine.SetAccelerate3DEnabled(type.GetRecommended3DAcceleration()); /* Register the VM prior to attaching hard disks: */ vbox.RegisterMachine(m_machine); if (!vbox.isOk()) { msgCenter().cannotRegisterMachine(vbox, m_machine.GetName(), this); return false; } /* Attach default devices: */ { bool success = false; QString strMachineId = m_machine.GetId(); CSession session = vboxGlobal().openSession(strMachineId); if (!session.isNull()) { CMachine machine = session.GetMachine(); QString strId = field("virtualDiskId").toString(); /* Boot virtual hard drive: */ if (!strId.isNull()) { UIMedium vmedium = vboxGlobal().medium(strId); CMedium medium = vmedium.medium(); // @todo r=dj can this be cached somewhere? machine.AttachDevice(strHDName, 0, 0, KDeviceType_HardDisk, medium); if (!machine.isOk()) msgCenter().cannotAttachDevice(machine, UIMediumType_HardDisk, field("virtualDiskLocation").toString(), StorageSlot(ctrHDBus, 0, 0), this); } /* Attach empty optical drive: */ machine.AttachDevice(strDVDName, 1, 0, KDeviceType_DVD, CMedium()); if (!machine.isOk()) msgCenter().cannotAttachDevice(machine, UIMediumType_DVD, QString(), StorageSlot(strDVDBus, 1, 0), this); /* Attach an empty floppy drive if recommended */ if (type.GetRecommendedFloppy()) { machine.AttachDevice(strFloppyName, 0, 0, KDeviceType_Floppy, CMedium()); if (!machine.isOk()) msgCenter().cannotAttachDevice(machine, UIMediumType_Floppy, QString(), StorageSlot(KStorageBus_Floppy, 0, 0), this); } if (machine.isOk()) { machine.SaveSettings(); if (machine.isOk()) success = true; else msgCenter().cannotSaveMachineSettings(machine, this); } session.UnlockMachine(); } if (!success) { /* Unregister on failure */ QVector<CMedium> aMedia = m_machine.Unregister(KCleanupMode_UnregisterOnly); // @todo replace with DetachAllReturnHardDisksOnly once a progress dialog is in place below if (vbox.isOk()) { CProgress progress = m_machine.DeleteConfig(aMedia); progress.WaitForCompletion(-1); // @todo do this nicely with a progress dialog, this can delete lots of files } return false; } } /* Ensure we don't try to delete a newly created virtual hard drive on success: */ if (!field("virtualDisk").value<CMedium>().isNull()) field("virtualDisk").value<CMedium>().detach(); return true; }
bool UINewVMWzdPage5::constructMachine() { CVirtualBox vbox = vboxGlobal().virtualBox(); /* OS type */ CGuestOSType type = field("type").value<CGuestOSType>(); AssertMsg(!type.isNull(), ("GuestOSType must return non-null type")); QString typeId = type.GetId(); /* Create a machine with the default settings file location */ if (m_Machine.isNull()) { m_Machine = vbox.CreateMachine(QString::null, // auto-compose filename field("name").toString(), typeId, QString::null, // machine ID false); // forceOverwrite if (!vbox.isOk()) { msgCenter().cannotCreateMachine(vbox, this); return false; } /* The FirstRun wizard is to be shown only when we don't attach any hard disk or attach a new (empty) one. * Selecting an existing hard disk will cancel the wizard. */ if (field("hardDiskId").toString().isNull() || !field("hardDisk").value<CMedium>().isNull()) m_Machine.SetExtraData(VBoxDefs::GUI_FirstRun, "yes"); } /* RAM size */ m_Machine.SetMemorySize(field("ram").toInt()); /* VRAM size - select maximum between recommended and minimum for fullscreen */ m_Machine.SetVRAMSize (qMax (type.GetRecommendedVRAM(), (ULONG) (VBoxGlobal::requiredVideoMemory(typeId) / _1M))); /* Selecting recommended chipset type */ m_Machine.SetChipsetType(type.GetRecommendedChipset()); /* Selecting recommended Audio Controller */ m_Machine.GetAudioAdapter().SetAudioController(type.GetRecommendedAudioController()); /* Enabling audio by default */ m_Machine.GetAudioAdapter().SetEnabled(true); /* Enable the OHCI and EHCI controller by default for new VMs. (new in 2.2) */ CUSBController usbController = m_Machine.GetUSBController(); if ( !usbController.isNull() && usbController.GetProxyAvailable()) { usbController.SetEnabled(true); /* * USB 2.0 is only available if the proper ExtPack is installed. * * Note. Configuring EHCI here and providing messages about * the missing extpack isn't exactly clean, but it is a * necessary evil to patch over legacy compatability issues * introduced by the new distribution model. */ CExtPackManager manager = vboxGlobal().virtualBox().GetExtensionPackManager(); if (manager.IsExtPackUsable(UI_ExtPackName)) usbController.SetEnabledEhci(true); } /* Create recommended DVD storage controller */ KStorageBus ctrDvdBus = type.GetRecommendedDvdStorageBus(); QString ctrDvdName = getNextControllerName(ctrDvdBus); m_Machine.AddStorageController(ctrDvdName, ctrDvdBus); /* Set recommended DVD storage controller type */ CStorageController dvdCtr = m_Machine.GetStorageControllerByName(ctrDvdName); KStorageControllerType dvdStorageControllerType = type.GetRecommendedDvdStorageController(); dvdCtr.SetControllerType(dvdStorageControllerType); /* Create recommended HD storage controller if it's not the same as the DVD controller */ KStorageBus ctrHdBus = type.GetRecommendedHdStorageBus(); KStorageControllerType hdStorageControllerType = type.GetRecommendedHdStorageController(); CStorageController hdCtr; QString ctrHdName; if (ctrHdBus != ctrDvdBus || hdStorageControllerType != dvdStorageControllerType) { ctrHdName = getNextControllerName(ctrHdBus); m_Machine.AddStorageController(ctrHdName, ctrHdBus); hdCtr = m_Machine.GetStorageControllerByName(ctrHdName); hdCtr.SetControllerType(hdStorageControllerType); /* Set the port count to 1 if SATA is used. */ if (hdStorageControllerType == KStorageControllerType_IntelAhci) hdCtr.SetPortCount(1); } else { /* The HD controller is the same as DVD */ hdCtr = dvdCtr; ctrHdName = ctrDvdName; } /* Turn on PAE, if recommended */ m_Machine.SetCPUProperty(KCPUPropertyType_PAE, type.GetRecommendedPae()); /* Set recommended firmware type */ KFirmwareType fwType = type.GetRecommendedFirmware(); m_Machine.SetFirmwareType(fwType); /* Set recommended human interface device types */ if (type.GetRecommendedUsbHid()) { m_Machine.SetKeyboardHidType(KKeyboardHidType_USBKeyboard); m_Machine.SetPointingHidType(KPointingHidType_USBMouse); if (!usbController.isNull()) usbController.SetEnabled(true); } if (type.GetRecommendedUsbTablet()) { m_Machine.SetPointingHidType(KPointingHidType_USBTablet); if (!usbController.isNull()) usbController.SetEnabled(true); } /* Set HPET flag */ m_Machine.SetHpetEnabled(type.GetRecommendedHpet()); /* Set UTC flags */ m_Machine.SetRTCUseUTC(type.GetRecommendedRtcUseUtc()); /* Register the VM prior to attaching hard disks */ vbox.RegisterMachine(m_Machine); if (!vbox.isOk()) { msgCenter().cannotCreateMachine(vbox, m_Machine, this); return false; } /* Attach default devices */ { bool success = false; QString machineId = m_Machine.GetId(); CSession session = vboxGlobal().openSession(machineId); if (!session.isNull()) { CMachine m = session.GetMachine(); QString strId = field("hardDiskId").toString(); /* Boot hard disk */ if (!strId.isNull()) { VBoxMedium vmedium = vboxGlobal().findMedium(strId); CMedium medium = vmedium.medium(); // @todo r=dj can this be cached somewhere? m.AttachDevice(ctrHdName, 0, 0, KDeviceType_HardDisk, medium); if (!m.isOk()) msgCenter().cannotAttachDevice(m, VBoxDefs::MediumType_HardDisk, field("hardDiskLocation").toString(), StorageSlot(ctrHdBus, 0, 0), this); } /* Attach empty CD/DVD ROM Device */ m.AttachDevice(ctrDvdName, 1, 0, KDeviceType_DVD, CMedium()); if (!m.isOk()) msgCenter().cannotAttachDevice(m, VBoxDefs::MediumType_DVD, QString(), StorageSlot(ctrDvdBus, 1, 0), this); if (m.isOk()) { m.SaveSettings(); if (m.isOk()) success = true; else msgCenter().cannotSaveMachineSettings(m, this); } session.UnlockMachine(); } if (!success) { /* Unregister on failure */ QVector<CMedium> aMedia = m_Machine.Unregister(KCleanupMode_UnregisterOnly); // @todo replace with DetachAllReturnHardDisksOnly once a progress dialog is in place below if (vbox.isOk()) { CProgress progress = m_Machine.Delete(aMedia); progress.WaitForCompletion(-1); // @todo do this nicely with a progress dialog, this can delete lots of files } return false; } } /* Ensure we don't try to delete a newly created hard disk on success */ if (!field("hardDisk").value<CMedium>().isNull()) field("hardDisk").value<CMedium>().detach(); return true; }