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

356 lines
11 KiB
C++
Raw Normal View History

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
*/
2023-03-26 21:53:03 +08:00
#include "qemu.h"
#include <QDir>
#include <QFile>
#include <QCoreApplication>
#include "command.h"
#include <QMessageBox>
#include <QProcess>
#include <QDebug>
#include <iostream>
using namespace std;
2023-03-26 21:53:03 +08:00
2024-04-27 15:42:25 +08:00
qemu::qemu(QString name, QString managerPath)
{
2023-03-26 21:53:03 +08:00
if(!QFile::exists(name)){
this->name = QDir::homePath() + "/Qemu/" + name;
}
else{
this->name = name;
}
this->managerPath = managerPath;
//Command command = Command();
2024-06-17 22:38:45 +08:00
QString qemuPath = "qemu-system-i386";
if(QFile::exists("/opt/apps/deepin-wine-runner-qemu-system-extra/files/run.sh")) {
// 如果存在拓展 Qemu则调用此
qemuPath = "/opt/apps/deepin-wine-runner-qemu-system-extra/files/run.sh qemu-system-i386";
}
this->vboxVersion = Command().GetCommand(qemuPath + " --version");
2023-03-26 21:53:03 +08:00
}
2024-04-27 15:42:25 +08:00
int qemu::Create(QString type)
{
2023-03-26 21:53:03 +08:00
if(!QFile::exists(this->name)){
QDir dir(this->name);
dir.mkpath(this->name);
}
return 0;
}
2024-04-27 15:42:25 +08:00
int qemu::CreateDisk(QString path, int size)
{
2023-03-26 21:53:03 +08:00
if(QFile::exists(path)){
return 0;
}
2023-04-05 16:09:58 +08:00
return system(("qemu-img create -f qcow2 '" + path + "' " + QString::number(size) + "M").toLatin1());
2023-03-26 21:53:03 +08:00
}
2024-04-27 15:42:25 +08:00
int qemu::CreateDiskControl(QString controlName)
{
2023-03-26 21:53:03 +08:00
return 0;
}
2024-04-27 15:42:25 +08:00
int qemu::MountDisk(QString diskPath, QString controlName, int port, int device)
{
2023-03-26 21:53:03 +08:00
commandOption += "-drive 'file=" + diskPath + ",if=ide,index=" + QString::number(device) + "' ";
return 0;
}
2024-04-27 15:42:25 +08:00
int qemu::MountISO(QString isoPath, QString controlName, int port, int device)
{
2023-03-26 21:53:03 +08:00
commandOption += "-drive 'media=cdrom,file=" + isoPath + ",if=ide,index=" + QString::number(device) + "' ";
return 0;
}
2024-04-27 15:42:25 +08:00
int qemu::BootFirst(QString bootDrive)
{
2023-03-26 21:53:03 +08:00
commandOption += "-boot '" + bootDrive + "' ";
return 0;
}
2024-04-27 15:42:25 +08:00
int qemu::SetNetBridge(QString netDriver)
{
2023-03-26 21:53:03 +08:00
return 0;
}
2024-04-27 15:42:25 +08:00
int qemu::SetCPU(int number, int cpuNum, int coreNum)
{
2023-08-09 22:19:56 +08:00
// 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) + " ";
2023-03-26 21:53:03 +08:00
return 0;
}
2024-04-27 15:42:25 +08:00
int qemu::SetMemory(int memory)
{
2023-03-26 21:53:03 +08:00
commandOption += "-m " + QString::number(memory) + "M ";
return 0;
}
2024-04-27 15:42:25 +08:00
int qemu::SetRemote(bool setting)
{
2023-03-26 21:53:03 +08:00
return 0;
}
2024-04-27 15:42:25 +08:00
int qemu::SetRemoteConnectSetting(int port)
{
2023-03-26 21:53:03 +08:00
return 0;
}
2024-04-27 15:42:25 +08:00
int qemu::MountMainDisk(QString diskPath)
{
commandOption += " --hda '" + diskPath + "' ";
return 0;
}
2024-04-27 15:42:25 +08:00
int qemu::StartArmhf()
{
qDebug() << commandOption;
2024-06-17 22:38:45 +08:00
QString qemuPath = "qemu-system-arm";
if(QFile::exists("/opt/apps/deepin-wine-runner-qemu-system-extra/files/run.sh")) {
// 如果存在拓展 Qemu则调用此
qemuPath = "/opt/apps/deepin-wine-runner-qemu-system-extra/files/run.sh qemu-system-arm";
}
if(Command().GetCommand("arch").replace("\n", "").replace(" ", "") == "aarch64" && !system((QCoreApplication::applicationDirPath() + "/kvm-ok").toUtf8())){
2024-06-17 22:38:45 +08:00
return system((qemuPath + " --boot 'splash=" + GetBootLogoPath() + ",order=d,menu=on,splash-time=2000' -display vnc=:5 -display gtk --enable-kvm -cpu host -M virt " + commandOption + " -device virtio-gpu-pci -device nec-usb-xhci,id=xhci,addr=0x1b -device usb-tablet,id=tablet,bus=xhci.0,port=1 -device usb-kbd,id=keyboard,bus=xhci.0,port=2 > /tmp/windows-virtual-machine-installer-for-wine-runner-install.log 2>&1 &").toLatin1());
}
2024-06-17 22:38:45 +08:00
return system((qemuPath + " --boot 'splash=" + GetBootLogoPath() + ",order=d,menu=on,splash-time=2000' -display vnc=:5 -display gtk -cpu max -M virt " + commandOption + " -device virtio-gpu-pci -device nec-usb-xhci,id=xhci,addr=0x1b -device usb-tablet,id=tablet,bus=xhci.0,port=1 -device usb-kbd,id=keyboard,bus=xhci.0,port=2 > /tmp/windows-virtual-machine-installer-for-wine-runner-install.log 2>&1 &").toLatin1());
}
2024-04-27 15:42:25 +08:00
int qemu::StartAarch64()
{
2024-06-01 09:55:22 +08:00
QString bootScreenLogo = "";
2024-06-17 22:38:45 +08:00
QString qemuPath = "qemu-system-aarch64";
2024-06-01 09:55:22 +08:00
// 判断 boot 文件是否存在
if(QFile::exists(QCoreApplication::applicationDirPath() + "/boot.jpg")) {
bootScreenLogo = QCoreApplication::applicationDirPath() + "/boot.jpg";
}
else {
// 写入 logo
QFile::copy(":/boot.jpg", "/tmp/deep-wine-runner-boot.jpg");
bootScreenLogo = "/tmp/deep-wine-runner-boot.jpg";
}
2024-06-17 22:38:45 +08:00
if(QFile::exists("/opt/apps/deepin-wine-runner-qemu-system-extra/files/run.sh")) {
// 如果存在拓展 Qemu则调用此
qemuPath = "/opt/apps/deepin-wine-runner-qemu-system-extra/files/run.sh qemu-system-aarch64";
}
qDebug() << commandOption;
if(Command().GetCommand("arch").replace("\n", "").replace(" ", "") == "aarch64" && !system((QCoreApplication::applicationDirPath() + "/kvm-ok").toUtf8())){
2024-06-17 22:38:45 +08:00
return system((qemuPath + " --boot 'splash=" + GetBootLogoPath() + ",order=d,menu=on,splash-time=2000' -display vnc=:5 -display gtk --enable-kvm -cpu host -M virt " + commandOption + " -device virtio-gpu-pci -device nec-usb-xhci,id=xhci,addr=0x1b -device usb-tablet,id=tablet,bus=xhci.0,port=1 -device usb-kbd,id=keyboard,bus=xhci.0,port=2 > /tmp/windows-virtual-machine-installer-for-wine-runner-install.log 2>&1 &").toLatin1());
}
2024-06-17 22:38:45 +08:00
return system((qemuPath + " --boot 'splash=" + GetBootLogoPath() + ",order=d,menu=on,splash-time=2000' -display vnc=:5 -display gtk -cpu max -M virt " + commandOption + " -device virtio-gpu-pci -device nec-usb-xhci,id=xhci,addr=0x1b -device usb-tablet,id=tablet,bus=xhci.0,port=1 -device usb-kbd,id=keyboard,bus=xhci.0,port=2 > /tmp/windows-virtual-machine-installer-for-wine-runner-install.log 2>&1 &").toLatin1());
}
2024-04-27 15:42:25 +08:00
int qemu::StartLoong64()
{
2024-06-01 09:55:22 +08:00
return 1;
2023-03-26 21:53:03 +08:00
}
2024-04-27 15:42:25 +08:00
int qemu::Start(bool unShown)
{
2024-06-01 08:56:18 +08:00
QString newCommandOption = commandOption;
2024-06-01 13:07:04 +08:00
QString qemuPath = "qemu-system-x86_64";
2024-06-01 09:55:22 +08:00
qDebug() << GetBootLogoPath();
2024-06-01 08:56:18 +08:00
if(isUEFI) {
newCommandOption += " -vga none -device virtio-gpu-pci -device nec-usb-xhci,id=xhci,addr=0x1b -device usb-tablet,id=tablet,bus=xhci.0,port=1 -device usb-kbd,id=keyboard,bus=xhci.0,port=2 ";
}
else {
2024-06-01 10:37:06 +08:00
newCommandOption += " -vga virtio -device nec-usb-xhci,id=xhci,addr=0x1b -device usb-tablet,id=tablet,bus=xhci.0,port=1 ";
2024-06-01 08:56:18 +08:00
}
2024-06-01 13:07:04 +08:00
// UOS 3a4000 使用程序自带的 qemu
QString info = SystemInfo().toLower();
2024-06-01 15:08:20 +08:00
if(info.contains("uos") || info.contains("unio")) {
2024-06-01 13:07:04 +08:00
// 判断架构
QString arch = GetArch();
2024-06-01 15:08:20 +08:00
if(arch == "mips64" || arch == "mipsel64") {
qemuPath = "bwrap --dev-bind / / --bind '" + QCoreApplication::applicationDirPath() + "/MipsQemu/usr/lib/mips64el-linux-gnuabi64/qemu/ui-gtk.so' /usr/lib/mips64el-linux-gnuabi64/qemu/ui-gtk.so '" + QCoreApplication::applicationDirPath() + "/MipsQemu/usr/bin/qemu-system-x86_64' ";
2024-06-01 13:07:04 +08:00
}
}
2024-06-17 22:38:45 +08:00
if(QFile::exists("/opt/apps/deepin-wine-runner-qemu-system-extra/files/run.sh")) {
// 如果存在拓展 Qemu则调用此
qemuPath = "/opt/apps/deepin-wine-runner-qemu-system-extra/files/run.sh qemu-system-x86_64";
}
2023-03-26 21:53:03 +08:00
qDebug() << commandOption;
if(Command().GetCommand("arch").replace("\n", "").replace(" ", "") == "x86_64" && !system((QCoreApplication::applicationDirPath() + "/kvm-ok").toUtf8())){
2024-06-01 13:07:04 +08:00
return system((qemuPath + " --boot 'splash=" + GetBootLogoPath() + ",order=d,menu=on,splash-time=2000' -display vnc=:5 -display gtk --enable-kvm -cpu host " + newCommandOption + " > /tmp/windows-virtual-machine-installer-for-wine-runner-install.log 2>&1 &").toLatin1());
2023-03-26 21:53:03 +08:00
}
2024-06-01 13:07:04 +08:00
return system((qemuPath + " --boot 'splash=" + GetBootLogoPath() + ",order=d,menu=on,splash-time=2000' -display vnc=:5 -display gtk -nic model=rtl8139 " + newCommandOption + " > /tmp/windows-virtual-machine-installer-for-wine-runner-install.log 2>&1 &").toLatin1());
2023-03-26 21:53:03 +08:00
}
2024-04-27 15:42:25 +08:00
int qemu::Stop()
{
2023-03-26 21:53:03 +08:00
system("killall qemu-system-x86_64 -9");
system("killall qemu-system-aarch64 -9");
system("killall qemu-system-arm -9");
2023-03-26 21:53:03 +08:00
system("killall kvm -9");
return 0;
}
2024-04-27 15:42:25 +08:00
int qemu::Delete()
{
2023-03-26 21:53:03 +08:00
return system(("rm -rfv '" + name + "'").toLatin1());
}
2024-04-27 15:42:25 +08:00
int qemu::SetDisplayMemory(int memory)
{
2023-03-26 21:53:03 +08:00
return 0;
}
2024-04-27 15:42:25 +08:00
int qemu::InstallGuessAdditions(QString controlName, int port, int device)
{
2023-03-26 21:53:03 +08:00
return 0;
}
2024-04-27 15:42:25 +08:00
int qemu::EnabledAudio()
{
commandOption += "-device AC97 -device ES1370 -device intel-hda -device hda-duplex ";
2023-03-26 21:53:03 +08:00
return 0;
}
2024-04-27 15:42:25 +08:00
int qemu::EnabledClipboardMode()
{
2023-03-26 21:53:03 +08:00
return 0;
}
2024-04-27 15:42:25 +08:00
int qemu::EnabledDraganddrop()
{
2023-03-26 21:53:03 +08:00
return 0;
}
2024-04-27 15:42:25 +08:00
int qemu::ShareFile(QString name, QString path)
{
2023-03-26 21:53:03 +08:00
return 0;
}
2024-04-27 15:42:25 +08:00
int qemu::SetVBoxSVGA()
{
2023-03-26 21:53:03 +08:00
return 0;
}
2024-04-27 15:42:25 +08:00
int qemu::SetMousePS2()
{
2023-03-26 21:53:03 +08:00
return 0;
}
2024-04-27 15:42:25 +08:00
int qemu::SetKeyboardPS2()
{
2023-03-26 21:53:03 +08:00
return 0;
}
2024-04-27 15:42:25 +08:00
int qemu::OpenUSB()
{
2023-03-26 21:53:03 +08:00
return 0;
2023-11-11 19:51:31 +08:00
}
2024-04-27 15:42:25 +08:00
int qemu::UseAarch64EFI()
{
if(QFile::exists("/usr/share/qemu-efi-aarch64/QEMU_EFI.fd")){
commandOption += "--bios /usr/share/qemu-efi-aarch64/QEMU_EFI.fd ";
return 0;
}
if(QFile::exists(QCoreApplication::applicationDirPath() + "/QEMU_AARCH64_EFI.fd")){
commandOption += "--bios '" + QCoreApplication::applicationDirPath() + "/QEMU_AARCH64_EFI.fd' ";
return 0;
}
return 1;
2023-11-11 19:51:31 +08:00
}
2024-04-27 15:42:25 +08:00
int qemu::UseArmhfEFI()
{
if(QFile::exists("/usr/share/AAVMF/AAVMF32_CODE.fd")){
commandOption += "--bios /usr/share/AAVMF/AAVMF32_CODE.fd ";
return 0;
}
if(QFile::exists(QCoreApplication::applicationDirPath() + "/AAVMF32_CODE.fd")){
commandOption += "--bios '" + QCoreApplication::applicationDirPath() + "/AAVMF32_CODE.fd' ";
return 0;
}
return 1;
2023-11-11 19:51:31 +08:00
}
2024-04-27 15:42:25 +08:00
int qemu::UseLoongarch64EFI()
{
2023-11-11 19:51:31 +08:00
}
2024-04-27 15:42:25 +08:00
int qemu::UseOtherEFI(QString fdFilePath)
{
2023-11-11 19:51:31 +08:00
2023-03-26 21:53:03 +08:00
}
2024-04-27 15:42:25 +08:00
int qemu::EnabledUEFI(bool status)
{
2024-06-01 10:37:06 +08:00
isUEFI = status;
2023-08-10 14:36:41 +08:00
if(!status){
return 0;
}
if(QFile::exists("/usr/share/qemu/OVMF.fd")){
2023-08-09 22:57:14 +08:00
commandOption += "--bios /usr/share/qemu/OVMF.fd ";
2023-08-10 14:36:41 +08:00
return 0;
2023-08-09 22:57:14 +08:00
}
2023-08-10 14:36:41 +08:00
if(QFile::exists(QCoreApplication::applicationDirPath() + "/OVMF.fd")){
commandOption += "--bios '" + QCoreApplication::applicationDirPath() + "/OVMF.fd' ";
return 0;
}
return 1;
2023-08-09 22:19:56 +08:00
}
2024-04-27 15:42:25 +08:00
int qemu::MountMainISO(QString isoPath)
{
commandOption += "--cdrom '" + isoPath + "' ";
2023-11-11 21:52:13 +08:00
return 0;
}
2024-04-27 15:42:25 +08:00
int qemu::AutoInstall(QString iso)
{
2023-08-16 09:53:59 +08:00
return 0;
}
2024-04-27 15:42:25 +08:00
int qemu::AddDiskSpace(QString path, double data)
{
return system(("qemu-img resize '" + path + "' +" + QString::number(data) + "G").toUtf8());
}
2024-06-01 09:55:22 +08:00
QString qemu::GetBootLogoPath()
{
QString bootScreenLogo = "";
// 判断 boot 文件是否存在
if(QFile::exists(QCoreApplication::applicationDirPath() + "/boot.jpg")) {
bootScreenLogo = QCoreApplication::applicationDirPath() + "/boot.jpg";
}
else {
// 写入 logo
if(QFile::exists("/tmp/deep-wine-runner-boot.jpg")) {
QFile::remove("/tmp/deep-wine-runner-boot.jpg");
}
QFile::copy(":/boot.jpg", "/tmp/deep-wine-runner-boot.jpg");
bootScreenLogo = "/tmp/deep-wine-runner-boot.jpg";
}
return bootScreenLogo;
}
2024-06-01 13:07:04 +08:00
QString qemu::SystemInfo()
{
QFile file("/etc/os-version");
file.open(QFile::ReadOnly);
QString data = file.readAll();
file.close();
return data;
}
QString qemu::GetArch()
{
QProcess process;
process.start("uname", QStringList() << "-m");
process.waitForStarted();
process.waitForFinished();
2024-06-07 12:37:07 +08:00
QString data = process.readAllStandardOutput();
2024-06-01 13:07:04 +08:00
process.close();
return data.replace("\n", "").replace(" ", "");
}