deep-wine-runner/VM-source/buildvbox.cpp

116 lines
3.4 KiB
C++
Raw Normal View History

2023-02-07 22:12:05 +08:00
#include "buildvbox.h"
2023-02-19 14:51:29 +08:00
#include "vbox.h"
2023-02-07 22:12:05 +08:00
#include <QFile>
2023-02-19 14:51:29 +08:00
#include <QDir>
2023-02-07 22:12:05 +08:00
#include <QNetworkInterface>
#include <QMessageBox>
2023-02-19 14:51:29 +08:00
#include <QCoreApplication>
#include <infoutils.h>
2023-03-04 11:48:45 +08:00
// 懒得用 QThread 了(要继承)
#include <thread>
2023-02-07 22:12:05 +08:00
using namespace std;
// 清屏
void buildvbox::CleanScreen(){
if(QFile::exists("/etc/os-version")){
// Unix
system("clear");
return;
}
// Windows
system("cls");
}
QString buildvbox::GetNet(){
QList<QNetworkInterface> netList = QNetworkInterface::allInterfaces();
foreach(QNetworkInterface net, netList){
qDebug() << "Device:" << net.name();
QList<QNetworkAddressEntry> entryList = net.addressEntries();
foreach(QNetworkAddressEntry entry, entryList){
QString ip = entry.ip().toString();
qDebug() << "IP Address: " << ip;
2023-02-19 15:28:37 +08:00
if(ip != "127.0.0.1" && ip != "192.168.250.1" && ip != "::1" && net.name() != "lo"){
2023-02-07 22:12:05 +08:00
// 返回网卡名称
return net.name();
}
}
}
return "";
}
2023-03-04 11:48:45 +08:00
int buildvbox::Download(QString url, QString path, QString fileName){
return system(("aria2c -x 16 -s 16 -c " + url + " -d " + path + " -o " + fileName).toUtf8());
}
2023-02-19 15:28:37 +08:00
buildvbox::buildvbox(QString isoPath, int id){
2023-02-19 15:43:24 +08:00
/*QDir vboxPath(QDir::homePath() + "/VirtualBox VMs/Windows");
if(vboxPath.exists()){
qDebug("虚拟机存在,直接启动!");
vbox vm("Windows");
vm.Start();
return;
}*/
2023-02-19 14:51:29 +08:00
QString programPath = QCoreApplication::applicationDirPath();
2023-03-04 11:48:45 +08:00
/*if(!QFile::exists(programPath + "/a.iso")){
std::thread([=](){
while(1){
qDebug() << "a";
}
}).detach();
}*/
2023-02-07 22:12:05 +08:00
QString net = GetNet();
qDebug() << "使用网卡:" << net << endl;
2023-02-19 14:51:29 +08:00
//vbox *box = new vbox("Window");
vbox vm("Windows");
switch (id) {
case 0:
vm.Create("Windows7");
break;
case 1:
vm.Create("Windows7_64");
break;
vm.Create("WindowsNT_64");
}
2023-02-19 15:28:37 +08:00
vm.CreateDiskControl();
2023-03-04 11:48:45 +08:00
//vm.CreateDiskControl("storage_controller_2");
2023-02-19 15:28:37 +08:00
vm.CreateDisk(QDir::homePath() + "/VirtualBox VMs/Windows/Windows.vdi", 131072);
vm.MountDisk(QDir::homePath() + "/VirtualBox VMs/Windows/Windows.vdi");
2023-03-04 11:48:45 +08:00
vm.MountISO(isoPath, "storage_controller_1", 0, 1);
2023-02-19 15:28:37 +08:00
switch (id) {
case 0:
2023-03-04 11:48:45 +08:00
vm.MountISO(programPath + "/Windows7X86Auto.iso", "storage_controller_1", 1, 0);
2023-02-19 15:28:37 +08:00
break;
case 1:
2023-03-04 11:48:45 +08:00
vm.MountISO(programPath + "/Windows7X64Auto.iso", "storage_controller_1", 1, 0);
2023-02-19 15:28:37 +08:00
break;
}
2023-03-04 11:48:45 +08:00
// 判断 VirtualBox Guest ISO 是否存在
// 在的话直接挂载
if(QFile::exists("/usr/share/virtualbox/VBoxGuestAdditions.iso")){
vm.MountISO("/usr/share/virtualbox/VBoxGuestAdditions.iso", "storage_controller_1", 1, 1);
}
2023-02-19 14:51:29 +08:00
vm.SetCPU(1);
long memory = 0;
long memoryAll = 0;
long swap = 0;
long swapAll = 0;
infoUtils::memoryRate(memory, memoryAll, swap, swapAll);
//memoryRate(memory, memoryAll, swap, swapAll);
2023-02-19 15:28:37 +08:00
vm.SetMemory(memoryAll / 3 / 1024);
2023-02-19 14:51:29 +08:00
vm.SetDisplayMemory(32);
vm.SetNetBridge(net);
vm.EnabledAudio();
vm.EnabledClipboardMode();
vm.EnabledDraganddrop();
vm.SetVBoxSVGA();
vm.SetMousePS2();
vm.SetKeyboardPS2();
vm.OpenUSB();
vm.ShareFile("ROOT", "/");
vm.ShareFile("HOME", QDir::homePath());
vm.Start();
2023-02-07 22:12:05 +08:00
}