diff --git a/StartVM.sh b/StartVM.sh index b7d899b..38fdc88 100755 --- a/StartVM.sh +++ b/StartVM.sh @@ -22,6 +22,10 @@ if [[ $? == 0 ]] && [[ -f "$HOME/Qemu/Windows/Windows.qcow2" ]]; then python3 ./VM/StartQemu.py exit fi + # 查看CPU个数 + CpuSocketNum=`cat /proc/cpuinfo | grep "cpu cores" | uniq | wc -l` + # 查看CPU核心数 + CpuCoreNum=`grep 'core id' /proc/cpuinfo | sort -u | wc -l` # 查看逻辑CPU的个数 CpuCount=`cat /proc/cpuinfo| grep "processor"| wc -l` @@ -30,11 +34,11 @@ if [[ $? == 0 ]] && [[ -f "$HOME/Qemu/Windows/Windows.qcow2" ]]; then use=$(echo "scale=4; $MemTotal / 3" | bc) if [[ `arch` == "x86_64" ]]; then echo X86 架构,使用 kvm 加速 - kvm --hda "$HOME/Qemu/Windows/Windows.qcow2" -soundhw all -smp $CpuCount -m ${use}G -net user,hostfwd=tcp::3389-:3389 -display vnc=:5 -display gtk -usb -nic model=rtl8139 + kvm -cpu host --hda "$HOME/Qemu/Windows/Windows.qcow2" -soundhw all -smp $CpuCount,sockets=$CpuSocketNum,cores=$(($CpuCoreNum / $CpuCount)),threads=$(($CpuSocketNum / $CpuCoreNum / $CpuCount)) -m ${use}G -net user,hostfwd=tcp::3389-:3389 -display vnc=:5 -display gtk -usb -nic model=rtl8139 exit fi echo 非 X86 架构,不使用 kvm 加速 - qemu-system-x86_64 --hda "$HOME/Qemu/Windows/Windows.qcow2" -soundhw all -smp $CpuCount -m ${use}G -net user,hostfwd=tcp::3389-:3389 -display vnc=:5 -display gtk -usb -nic model=rtl8139 + qemu-system-x86_64 --hda "$HOME/Qemu/Windows/Windows.qcow2" -soundhw all -smp $CpuCount,sockets=$CpuSocketNum,cores=$(($CpuCoreNum / $CpuCount)),threads=$(($CpuSocketNum / $CpuCoreNum / $CpuCount)) -m ${use}G -net user,hostfwd=tcp::3389-:3389 -display vnc=:5 -display gtk -usb -nic model=rtl8139 exit fi zenity --question --no-wrap --text="检查到您未创建所指定的虚拟机,是否创建虚拟机并继续?\n如果不创建将无法使用" diff --git a/VM-source/buildvbox.cpp b/VM-source/buildvbox.cpp index a2ffa7e..05e7230 100644 --- a/VM-source/buildvbox.cpp +++ b/VM-source/buildvbox.cpp @@ -12,6 +12,7 @@ #include <QCoreApplication> #include <infoutils.h> #include "qemu.h" +#include <QProcess> // 懒得用 QThread 了(要继承) #include <thread> using namespace std; @@ -27,6 +28,37 @@ void buildvbox::CleanScreen(){ system("cls"); } +// 获取 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; +} + QString buildvbox::GetNet(){ QList<QNetworkInterface> netList = QNetworkInterface::allInterfaces(); foreach(QNetworkInterface net, netList){ @@ -114,7 +146,7 @@ buildvbox::buildvbox(QString isoPath, int id, int vm){ vm.MountISO("/usr/share/virtualbox/VBoxGuestAdditions.iso", "storage_controller_1", 1, 1); }*/ - vm.SetCPU(get_nprocs()); + vm.SetCPU(get_nprocs(), GetCPUSocket(), GetCPUCore()); long memory = 0; long memoryAll = 0; long swap = 0; @@ -174,7 +206,7 @@ buildvbox::buildvbox(QString isoPath, int id, int vm){ vm.MountISO("/usr/share/virtualbox/VBoxGuestAdditions.iso", "storage_controller_1", 1, 1); } - vm.SetCPU(get_nprocs_conf()); + vm.SetCPU(get_nprocs(), GetCPUSocket(), GetCPUCore()); long memory = 0; long memoryAll = 0; long swap = 0; diff --git a/VM-source/buildvbox.h b/VM-source/buildvbox.h index 7f90c14..e4753d0 100644 --- a/VM-source/buildvbox.h +++ b/VM-source/buildvbox.h @@ -14,6 +14,8 @@ public: void CleanScreen(); QString GetNet(); int Download(QString url, QString path, QString fileName); + int GetCPUSocket(); + int GetCPUCore(); }; #endif // BUILDVBOX_H diff --git a/VM-source/qemu.cpp b/VM-source/qemu.cpp index 1811f8d..e3ee134 100644 --- a/VM-source/qemu.cpp +++ b/VM-source/qemu.cpp @@ -53,8 +53,14 @@ int qemu::BootFirst(QString bootDrive){ int qemu::SetNetBridge(QString netDriver){ return 0; } -int qemu::SetCPU(int number){ - commandOption += "-smp " + QString::number(number) + " "; +int qemu::SetCPU(int number, int cpuNum, int coreNum){ + // commandOption += "-smp " + QString::number(number) + " "; + // 调整调用方法 + //qDebug() << number << " " << cpuNum << " " << coreNum; + qDebug() << "Socket: " << cpuNum; + qDebug() << "Core: " << coreNum; + qDebug() << "Threads: " << number; + commandOption += "-smp " + QString::number(number) + ",sockets=" + QString::number(cpuNum) + ",cores=" + QString::number(coreNum / cpuNum) + ",threads=" + QString::number(number / cpuNum / coreNum) + " "; return 0; } int qemu::SetMemory(int memory){ @@ -70,7 +76,7 @@ int qemu::SetRemoteConnectSetting(int port){ int qemu::Start(bool unShown){ qDebug() << commandOption; if(Command().GetCommand("arch").replace("\n", "").replace(" ", "") == "x86_64"){ - return system(("kvm " + commandOption + " &").toLatin1()); + return system(("kvm -cpu host " + commandOption + " &").toLatin1()); } return system(("qemu-system-x86_64 -nic model=rtl8139 " + commandOption + " &").toLatin1()); } @@ -113,3 +119,6 @@ int qemu::SetKeyboardPS2(){ int qemu::OpenUSB(){ return 0; } +int EnabledUEFI(bool status){ + +} diff --git a/VM-source/qemu.h b/VM-source/qemu.h index 58f3e72..fa6773c 100644 --- a/VM-source/qemu.h +++ b/VM-source/qemu.h @@ -21,7 +21,7 @@ public: int MountISO(QString isoPath, QString controlName="storage_controller_1", int port=1, int device=0); int BootFirst(QString bootDrive); int SetNetBridge(QString netDriver); - int SetCPU(int number); + int SetCPU(int number, int cpuNum, int coreNum); int SetMemory(int memory); int SetRemote(bool setting); int SetRemoteConnectSetting(int port=5540); @@ -38,6 +38,7 @@ public: int SetMousePS2(); int SetKeyboardPS2(); int OpenUSB(); + int EnabledUEFI(bool status); private: QString commandOption; diff --git a/VM-source/vbox.cpp b/VM-source/vbox.cpp index e33f2b8..4f3894e 100644 --- a/VM-source/vbox.cpp +++ b/VM-source/vbox.cpp @@ -44,7 +44,7 @@ int vbox::SetNetBridge(QString netDriver){ return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --nic1 bridged --cableconnected1 on --nictype1 82540EM --bridgeadapter1 \"" + netDriver + "\" --intnet1 brigh1 --macaddress1 auto").toLatin1()); } -int vbox::SetCPU(int number){ +int vbox::SetCPU(int number, int cpuNum, int coreNum){ return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --cpus " + QString::number(number)).toLatin1()); } int vbox::SetMemory(int memory){ @@ -101,3 +101,10 @@ int vbox::SetKeyboardPS2(){ int vbox::OpenUSB(){ return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --usbohci on").toLatin1()); } + +int vbox::EnabledUEFI(bool status){ + if(status){ + return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --firmware=efi").toLatin1()); + } + return system(("\"" + managerPath + "\" modifyvm \"" + name + "\" --firmware=bios").toLatin1()); +} diff --git a/VM-source/vbox.h b/VM-source/vbox.h index 94a021c..cc02198 100644 --- a/VM-source/vbox.h +++ b/VM-source/vbox.h @@ -21,7 +21,7 @@ public: int MountISO(QString isoPath, QString controlName="storage_controller_1", int port=1, int device=0); int BootFirst(QString bootDrive); int SetNetBridge(QString netDriver); - int SetCPU(int number); + int SetCPU(int number, int cpuNum, int coreNum); int SetMemory(int memory); int SetRemote(bool setting); int SetRemoteConnectSetting(int port=5540); @@ -38,6 +38,7 @@ public: int SetMousePS2(); int SetKeyboardPS2(); int OpenUSB(); + int EnabledUEFI(bool status); private: diff --git a/其他Windows无法正常创建虚拟机的问题 b/其他Windows无法正常创建虚拟机的问题 new file mode 100644 index 0000000..0ee5eac --- /dev/null +++ b/其他Windows无法正常创建虚拟机的问题 @@ -0,0 +1,3 @@ +[main acff44e] 修复选择vbox- + 2 files changed, 1198 insertions(+), 2 deletions(-) + create mode 100644 VM-source/.qtc_clangd/compile_commands.json