2023-04-05 16:09:58 +08:00
|
|
|
/*
|
2023-04-24 21:49:18 +08:00
|
|
|
* gfdgd xi
|
2023-04-05 16:09:58 +08:00
|
|
|
* 依照 GPLV3 开源
|
|
|
|
*/
|
2023-04-08 11:07:42 +08:00
|
|
|
#include <sys/sysinfo.h>
|
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-26 21:53:03 +08:00
|
|
|
#include "qemu.h"
|
2023-08-09 22:19:56 +08:00
|
|
|
#include <QProcess>
|
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");
|
|
|
|
}
|
|
|
|
|
2023-08-09 22:19:56 +08:00
|
|
|
// 获取 CPU 个数
|
|
|
|
int buildvbox::GetCPUSocket(){
|
|
|
|
// 获取命令返回值
|
|
|
|
QProcess process;
|
|
|
|
process.start("bash", QStringList() << "-c" << "cat /proc/cpuinfo | grep \"cpu cores\" | uniq | wc -l");
|
|
|
|
process.waitForStarted();
|
|
|
|
process.waitForFinished();
|
|
|
|
int value = process.readAllStandardOutput().toInt();
|
|
|
|
process.close();
|
|
|
|
// 判断异常值,例如没挂载 /proc
|
|
|
|
if(value <= 0){
|
|
|
|
value = 1;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取 CPU 核心数
|
|
|
|
int buildvbox::GetCPUCore(){
|
|
|
|
QProcess process;
|
|
|
|
process.start("bash", QStringList() << "-c" << "grep 'core id' /proc/cpuinfo | sort -u | wc -l");
|
|
|
|
process.waitForStarted();
|
|
|
|
process.waitForFinished();
|
|
|
|
int value = process.readAllStandardOutput().toInt();
|
|
|
|
process.close();
|
|
|
|
// 判断异常值,例如没挂载 /proc
|
|
|
|
if(value <= 0){
|
|
|
|
value = 1;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2023-02-07 22:12:05 +08:00
|
|
|
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-04-08 11:07:42 +08:00
|
|
|
buildvbox::buildvbox(QString isoPath, int id, int vm){
|
2023-02-19 14:51:29 +08:00
|
|
|
QString programPath = QCoreApplication::applicationDirPath();
|
2023-03-04 11:48:45 +08:00
|
|
|
|
2023-02-07 22:12:05 +08:00
|
|
|
QString net = GetNet();
|
|
|
|
qDebug() << "使用网卡:" << net << endl;
|
2023-04-08 11:07:42 +08:00
|
|
|
if(vm == 0){
|
2023-11-11 20:53:27 +08:00
|
|
|
// Qemu
|
2023-04-08 11:07:42 +08:00
|
|
|
qemu vm("Windows");
|
2023-08-16 09:53:59 +08:00
|
|
|
bool setISOAlready = 0;
|
2023-04-08 11:07:42 +08:00
|
|
|
switch (id) {
|
|
|
|
case 0:
|
|
|
|
vm.Create("Windows7");
|
2023-08-13 21:05:18 +08:00
|
|
|
vm.SetDisplayMemory(32);
|
2023-04-08 11:07:42 +08:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
vm.Create("Windows7_64");
|
2023-08-13 21:05:18 +08:00
|
|
|
vm.SetDisplayMemory(32);
|
2023-04-08 11:07:42 +08:00
|
|
|
break;
|
2023-08-09 22:57:14 +08:00
|
|
|
case 2:
|
2023-08-09 20:19:05 +08:00
|
|
|
vm.Create("WindowsNT_64");
|
2023-08-13 21:05:18 +08:00
|
|
|
vm.SetDisplayMemory(32);
|
2023-08-09 20:19:05 +08:00
|
|
|
break;
|
2023-08-09 22:57:14 +08:00
|
|
|
case 3:
|
|
|
|
vm.Create("WindowsNT_64");
|
2023-08-13 21:05:18 +08:00
|
|
|
vm.SetDisplayMemory(32);
|
|
|
|
vm.EnabledUEFI(true);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
vm.Create("Windows11_64");
|
|
|
|
vm.SetDisplayMemory(128);
|
2023-08-09 22:57:14 +08:00
|
|
|
vm.EnabledUEFI(true);
|
2023-08-16 09:53:59 +08:00
|
|
|
setISOAlready = 1;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
vm.Create("WindowsXP_32");
|
|
|
|
vm.SetDisplayMemory(32);
|
|
|
|
setISOAlready = 1;
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
vm.Create("WindowsNT_64");
|
|
|
|
vm.SetDisplayMemory(32);
|
|
|
|
setISOAlready = 1;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
vm.Create("WindowsNT_64");
|
|
|
|
vm.SetDisplayMemory(32);
|
|
|
|
vm.EnabledUEFI(true);
|
|
|
|
setISOAlready = 1;
|
2023-08-09 22:57:14 +08:00
|
|
|
break;
|
2023-11-11 20:53:27 +08:00
|
|
|
case 8:
|
|
|
|
vm.Create("WindowsNT_64");
|
|
|
|
vm.SetDisplayMemory(32);
|
|
|
|
vm.UseArmhfEFI();
|
|
|
|
setISOAlready = 1;
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
vm.Create("WindowsNT_64");
|
|
|
|
vm.SetDisplayMemory(32);
|
|
|
|
vm.UseAarch64EFI();
|
|
|
|
setISOAlready = 1;
|
|
|
|
break;
|
2023-11-11 19:44:28 +08:00
|
|
|
|
2023-04-08 11:07:42 +08:00
|
|
|
}
|
|
|
|
vm.CreateDiskControl();
|
|
|
|
//vm.CreateDiskControl("storage_controller_2");
|
2024-04-27 15:42:25 +08:00
|
|
|
if(id == 0 || id == 1) {
|
|
|
|
vm.CreateDisk(QDir::homePath() + "/Qemu/Windows/Windows.qcow2", 131072);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
vm.CreateDisk(QDir::homePath() + "/Qemu/Windows/Windows.qcow2", 131072 * 5);
|
|
|
|
}
|
|
|
|
|
2023-11-11 20:53:27 +08:00
|
|
|
//vm.MountDisk(QDir::homePath() + "/Qemu/Windows/Windows.qcow2");
|
|
|
|
vm.MountMainDisk(QDir::homePath() + "/Qemu/Windows/Windows.qcow2");
|
2024-06-29 10:51:16 +08:00
|
|
|
if(QFile::exists("/opt/apps/deepin-wine-runner-qemu-system-extra/files/resources/virtio-win.iso")) {
|
|
|
|
vm.MountISO("/opt/apps/deepin-wine-runner-qemu-system-extra/files/resources/virtio-win.iso", "storage_controller_2", 1, 3);
|
|
|
|
}
|
2023-08-16 09:53:59 +08:00
|
|
|
if(!setISOAlready){
|
|
|
|
vm.MountISO(isoPath, "storage_controller_1", 0, 1);
|
|
|
|
switch (id) {
|
|
|
|
case 0:
|
2024-04-27 15:42:25 +08:00
|
|
|
if(QFile::exists(programPath + "/Windows7X86Auto.iso")) {
|
|
|
|
vm.MountISO(programPath + "/Windows7X86Auto.iso", "storage_controller_1", 1, 2);
|
|
|
|
}
|
2023-08-16 09:53:59 +08:00
|
|
|
break;
|
|
|
|
case 1:
|
2024-04-27 15:42:25 +08:00
|
|
|
if(QFile::exists(programPath + "/Windows7X64Auto.iso")) {
|
|
|
|
vm.MountISO(programPath + "/Windows7X64Auto.iso", "storage_controller_1", 1, 2);
|
|
|
|
}
|
2023-08-16 09:53:59 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
2023-11-11 21:37:49 +08:00
|
|
|
//vm.AutoInstall(isoPath);
|
2023-11-11 21:41:46 +08:00
|
|
|
if(isoPath != ""){
|
|
|
|
vm.MountMainISO(isoPath);
|
|
|
|
}
|
|
|
|
|
2023-04-08 11:07:42 +08:00
|
|
|
}
|
|
|
|
|
2023-08-09 22:19:56 +08:00
|
|
|
vm.SetCPU(get_nprocs(), GetCPUSocket(), GetCPUCore());
|
2023-04-08 11:07:42 +08:00
|
|
|
long memory = 0;
|
|
|
|
long memoryAll = 0;
|
|
|
|
long swap = 0;
|
|
|
|
long swapAll = 0;
|
|
|
|
infoUtils::memoryRate(memory, memoryAll, swap, swapAll);
|
|
|
|
vm.SetMemory(memoryAll / 3 / 1024);
|
|
|
|
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());
|
2023-11-11 20:53:27 +08:00
|
|
|
switch(id){
|
|
|
|
case 8:
|
|
|
|
vm.StartArmhf();
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
vm.StartAarch64();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
vm.Start();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-19 14:51:29 +08:00
|
|
|
}
|
2023-04-08 11:07:42 +08:00
|
|
|
else if(vm == 1){
|
2023-11-11 20:53:27 +08:00
|
|
|
// VirtualBox
|
2023-04-08 11:07:42 +08:00
|
|
|
vbox vm("Windows");
|
2023-08-16 09:53:59 +08:00
|
|
|
bool setISOAlready = 1;
|
2023-04-08 11:07:42 +08:00
|
|
|
switch (id) {
|
|
|
|
case 0:
|
|
|
|
vm.Create("Windows7");
|
2023-08-13 21:05:18 +08:00
|
|
|
vm.SetDisplayMemory(32);
|
2023-04-08 11:07:42 +08:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
vm.Create("Windows7_64");
|
2023-08-13 21:05:18 +08:00
|
|
|
vm.SetDisplayMemory(32);
|
2023-04-08 11:07:42 +08:00
|
|
|
break;
|
2023-08-09 22:57:14 +08:00
|
|
|
case 2:
|
|
|
|
vm.Create("WindowsNT_64");
|
2023-08-13 21:05:18 +08:00
|
|
|
vm.SetDisplayMemory(32);
|
2023-08-09 22:57:14 +08:00
|
|
|
break;
|
|
|
|
case 3:
|
2023-08-09 20:19:05 +08:00
|
|
|
vm.Create("WindowsNT_64");
|
2023-08-09 22:57:14 +08:00
|
|
|
vm.EnabledUEFI(true);
|
2023-08-13 21:05:18 +08:00
|
|
|
vm.SetDisplayMemory(32);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
vm.Create("Windows11_64");
|
|
|
|
vm.SetDisplayMemory(128);
|
|
|
|
vm.EnabledUEFI(true);
|
2023-08-16 09:53:59 +08:00
|
|
|
setISOAlready = 1;
|
2023-08-09 20:19:05 +08:00
|
|
|
break;
|
2023-08-16 09:53:59 +08:00
|
|
|
case 5:
|
|
|
|
vm.Create("WindowsXP_32");
|
|
|
|
vm.SetDisplayMemory(32);
|
|
|
|
setISOAlready = 1;
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
vm.Create("WindowsNT_64");
|
|
|
|
vm.SetDisplayMemory(32);
|
|
|
|
setISOAlready = 1;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
vm.Create("WindowsNT_64");
|
|
|
|
vm.SetDisplayMemory(32);
|
|
|
|
vm.EnabledUEFI(true);
|
|
|
|
setISOAlready = 1;
|
|
|
|
break;
|
|
|
|
}
|
2023-04-08 11:07:42 +08:00
|
|
|
QDir dir("/home/gfdgd_xi/Qemu/Windows/");
|
|
|
|
dir.mkpath("/home/gfdgd_xi/Qemu/Windows/");
|
|
|
|
vm.CreateDiskControl();
|
|
|
|
//vm.CreateDiskControl("storage_controller_2");
|
2024-04-27 15:42:25 +08:00
|
|
|
if(id == 0 || id == 1) {
|
|
|
|
vm.CreateDisk(QDir::homePath() + "/Qemu/Windows/Windows.qcow2", 131072);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
vm.CreateDisk(QDir::homePath() + "/Qemu/Windows/Windows.qcow2", 131072 * 5);
|
|
|
|
}
|
2023-04-08 11:07:42 +08:00
|
|
|
vm.MountDisk(QDir::homePath() + "/VirtualBox VMs/Windows/Windows.vdi");
|
2023-08-16 09:53:59 +08:00
|
|
|
if(!setISOAlready){
|
|
|
|
vm.MountISO(isoPath, "storage_controller_1", 0, 1);
|
|
|
|
switch (id) {
|
|
|
|
case 0:
|
2024-04-27 15:42:25 +08:00
|
|
|
if(QFile::exists(programPath + "/Windows7X86Auto.iso")) {
|
|
|
|
vm.MountISO(programPath + "/Windows7X86Auto.iso", "storage_controller_1", 1, 0);
|
|
|
|
}
|
2023-08-16 09:53:59 +08:00
|
|
|
break;
|
|
|
|
case 1:
|
2024-04-27 15:42:25 +08:00
|
|
|
if(QFile::exists(programPath + "/Windows7X64Auto.iso")) {
|
|
|
|
vm.MountISO(programPath + "/Windows7X64Auto.iso", "storage_controller_1", 1, 0);
|
|
|
|
}
|
2023-08-16 09:53:59 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
vm.AutoInstall(isoPath);
|
2023-04-08 11:07:42 +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-08-13 21:05:18 +08:00
|
|
|
// VirtualBox 的 CPU 数量设置方法和 Qemu 不一样
|
|
|
|
vm.SetCPU(GetCPUCore(), GetCPUSocket(), GetCPUCore());
|
2023-04-08 11:07:42 +08:00
|
|
|
long memory = 0;
|
|
|
|
long memoryAll = 0;
|
|
|
|
long swap = 0;
|
|
|
|
long swapAll = 0;
|
|
|
|
infoUtils::memoryRate(memory, memoryAll, swap, swapAll);
|
|
|
|
//memoryRate(memory, memoryAll, swap, swapAll);
|
|
|
|
vm.SetMemory(memoryAll / 3 / 1024);
|
|
|
|
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-03-26 21:53:03 +08:00
|
|
|
}
|
2023-03-04 11:48:45 +08:00
|
|
|
|
2023-02-07 22:12:05 +08:00
|
|
|
}
|