虚拟机添加功能测试正常

This commit is contained in:
gfdgd xi 2023-02-19 15:28:37 +08:00
parent 3d49e8dd7b
commit 4632417414
8 changed files with 45 additions and 48 deletions

Binary file not shown.

View File

@ -27,7 +27,7 @@ QString buildvbox::GetNet(){
foreach(QNetworkAddressEntry entry, entryList){
QString ip = entry.ip().toString();
qDebug() << "IP Address: " << ip;
if(ip != "127.0.0.1" && ip != "192.168.250.1"){
if(ip != "127.0.0.1" && ip != "192.168.250.1" && ip != "::1" && net.name() != "lo"){
// 返回网卡名称
return net.name();
}
@ -36,8 +36,7 @@ QString buildvbox::GetNet(){
return "";
}
buildvbox::buildvbox(int id)
{
buildvbox::buildvbox(QString isoPath, int id){
QString programPath = QCoreApplication::applicationDirPath();
QString net = GetNet();
qDebug() << "使用网卡:" << net << endl;
@ -46,14 +45,24 @@ buildvbox::buildvbox(int id)
switch (id) {
case 0:
vm.Create("Windows7");
vm.MountISO(programPath + "/Windows7X86Auto.iso", "storage_controller_1", 1);
break;
case 1:
vm.Create("Windows7_64");
vm.MountISO(programPath + "/Windows7X64Auto.iso", "storage_controller_1", 1);
break;
vm.Create("WindowsNT_64");
}
vm.CreateDiskControl();
vm.CreateDisk(QDir::homePath() + "/VirtualBox VMs/Windows/Windows.vdi", 131072);
vm.MountDisk(QDir::homePath() + "/VirtualBox VMs/Windows/Windows.vdi");
vm.MountISO(isoPath);
switch (id) {
case 0:
vm.MountISO(programPath + "/Windows7X86Auto.iso", "storage_controller_1", 1);
break;
case 1:
vm.MountISO(programPath + "/Windows7X64Auto.iso", "storage_controller_1", 1);
break;
}
vm.SetCPU(1);
long memory = 0;
long memoryAll = 0;
@ -61,7 +70,7 @@ buildvbox::buildvbox(int id)
long swapAll = 0;
infoUtils::memoryRate(memory, memoryAll, swap, swapAll);
//memoryRate(memory, memoryAll, swap, swapAll);
vm.SetMemory(memoryAll / 1024 / 1024 / 3);
vm.SetMemory(memoryAll / 3 / 1024);
vm.SetDisplayMemory(32);
vm.SetNetBridge(net);
vm.EnabledAudio();

View File

@ -6,7 +6,7 @@ class buildvbox
{
// Q_OBJECT
public:
buildvbox(int id=0);
buildvbox(QString isoPath, int id=0);
void CleanScreen();
QString GetNet();
};

Binary file not shown.

View File

@ -39,20 +39,6 @@ void MainWindow::on_browser_clicked()
void MainWindow::on_install_clicked()
{
/*long memory = 0;
long memoryAll = 0;
long swap = 0;
long swapAll = 0;
infoUtils::memoryRate(memory, memoryAll, swap, swapAll);
QMessageBox::information(this, "", QString::number(memoryAll));
return;*/
buildvbox();
buildvbox(ui->isoPath->text(), ui->systemVersion->currentIndex());
return;
//
/*QProcess progress;
QStringList list;
list << ui->isoPath->text() << QString::number(ui->systemVersion->currentIndex());
qDebug() << QCoreApplication::applicationDirPath() + QString("/run.py");
progress.startDetached(QCoreApplication::applicationDirPath() + QString("/run.py"), list);
ui->tabWidget->setCurrentIndex(1);*/
}

Binary file not shown.

View File

@ -1,5 +1,6 @@
#include "vbox.h"
#include "command.h"
#include <QMessageBox>
vbox::vbox(QString name, QString managerPath) {
this->name = name;
@ -11,85 +12,86 @@ vbox::vbox(QString name, QString managerPath) {
int vbox::Create(QString type){
return system(("\"" + managerPath + "\" createvm --name \""
+ name + "\" --ostype \"" + type +
"\" --register").toUtf8());
"\" --register").toLatin1());
}
int vbox::CreateDisk(QString path, int size){
return system(("\"" + managerPath + "\" createvdi --filename \"" + path + "\" --size \"" + size + "\"").toUtf8());
return system(("\"" + managerPath + "\" createvdi --filename \"" + path + "\" --size \"" + QString::number(size) + "\"").toLatin1());
}
int vbox::CreateDiskControl(QString controlName){
return system(("\"" + managerPath + "\" storagectl \"" + name + "\" --name \"" + controlName + "\" --add ide").toUtf8());
return system(("\"" + managerPath + "\" storagectl \"" + name + "\" --name \"" + controlName + "\" --add ide").toLatin1());
}
int vbox::MountDisk(QString diskPath, QString controlName, int port, int device){
return system(("\"" + managerPath + "\" storageattach \"" + name +
"\" --storagectl \"" + controlName + "\" --type hdd --port "
+ port + " --device " + device + " --medium \"" + diskPath + "\"").toUtf8());
+ QString::number(port) + " --device " + QString::number(device) + " --medium \"" + diskPath + "\"").toLatin1());
}
int vbox::MountISO(QString isoPath, QString controlName, int port, int device){
return system(("\"" + managerPath + "\" storageattach \"" + name + "\" --storagectl \"" +
controlName + "\" --type dvddrive --port " + port + " --device " + device
+ " --medium \"{isoPath}\"").toUtf8());
controlName + "\" --type dvddrive --port " + QString::number(port) + " --device " + QString::number(device)
+ " --medium \"" + isoPath + "\"").toLatin1());
}
int vbox::BootFirst(QString bootDrive){
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --boot1 " + bootDrive).toUtf8());
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --boot1 " + bootDrive).toLatin1());
}
int vbox::SetNetBridge(QString netDriver){
return system(("\"" + managerPath + "\" modifyvm \"" + name +
"\" --nic1 bridged --cableconnected1 on --nictype1 82540EM --bridgeadapter1 \"" + netDriver + "\" --intnet1 brigh1 --macaddress1 auto").toUtf8());
"\" --nic1 bridged --cableconnected1 on --nictype1 82540EM --bridgeadapter1 \"" + netDriver + "\" --intnet1 brigh1 --macaddress1 auto").toLatin1());
}
int vbox::SetCPU(int number){
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --cpus " + number).toUtf8());
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --cpus " + QString::number(number)).toLatin1());
}
int vbox::SetMemory(int memory){
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --memory " + memory).toUtf8());
QMessageBox::information(NULL, "", ("\"" + managerPath + "\" modifyvm \"" + name + "\" --memory " + QString::number(memory)).toLatin1());
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --memory " + QString::number(memory)).toLatin1());
}
int vbox::SetRemote(bool setting){
if(setting){
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --vrde on").toUtf8());
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --vrde on").toLatin1());
}
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --vrde off").toUtf8());
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --vrde off").toLatin1());
}
int vbox::SetRemoteConnectSetting(int port){
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --vrdeport " + port + " --vrdeaddress """).toUtf8());
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --vrdeport " + QString::number(port) + " --vrdeaddress """).toLatin1());
}
int vbox::Start(bool unShown){
if(unShown){
return system(("\"" + managerPath + "\"").toUtf8());
return system(("\"" + managerPath + "\"").toLatin1());
}
return system(("\"" + managerPath + "\" startvm \"" + name + "\"").toUtf8());
return system(("\"" + managerPath + "\" startvm \"" + name + "\"").toLatin1());
}
int vbox::Stop(){
return system(("\"" + managerPath + "\" controlvm \"" + name + "\" poweroff").toUtf8());
return system(("\"" + managerPath + "\" controlvm \"" + name + "\" poweroff").toLatin1());
}
int vbox::Delete(){
return system(("\"" + managerPath + "\" unregistervm --delete \"" + name + "\"").toUtf8());
return system(("\"" + managerPath + "\" unregistervm --delete \"" + name + "\"").toLatin1());
}
int vbox::SetDisplayMemory(int memory){
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --vram " + memory).toUtf8());
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --vram " + QString::number(memory)).toLatin1());
}
int vbox::InstallGuessAdditions(QString controlName, int port, int device){
return MountISO("/usr/share/virtualbox/VBoxGuestAdditions.iso", controlName, port, device);
}
int vbox::EnabledAudio(){
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --audio pulse --audiocontroller hda --audioin on --audioout on").toUtf8());
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --audio-driver pulse --audiocontroller hda --audioin on --audioout on").toLatin1());
}
int vbox::EnabledClipboardMode(){
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --clipboard-mode bidirectional").toUtf8());
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --clipboard-mode bidirectional").toLatin1());
}
int vbox::EnabledDraganddrop(){
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --draganddrop bidirectional").toUtf8());
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --draganddrop bidirectional").toLatin1());
}
int vbox::ShareFile(QString name, QString path){
return system(("\"" + managerPath + "\" sharedfolder add \"" + this->name + "\" -name \"" + name + "\" -hostpath \"" + path + "\"").toUtf8());
return system(("\"" + managerPath + "\" sharedfolder add \"" + this->name + "\" -name \"" + name + "\" -hostpath \"" + path + "\"").toLatin1());
}
int vbox::SetVBoxSVGA(){
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --graphicscontroller vboxsvga").toUtf8());
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --graphicscontroller vboxsvga").toLatin1());
}
int vbox::SetMousePS2(){
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --mouse usb").toUtf8());
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --mouse usb").toLatin1());
}
int vbox::SetKeyboardPS2(){
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --keyboard usb").toUtf8());
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --keyboard usb").toLatin1());
}
int vbox::OpenUSB(){
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --usbohci on").toUtf8());
return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --usbohci on").toLatin1());
}

Binary file not shown.