From 97b761067b1fa23f0a6158b536bd8d1de1000212 Mon Sep 17 00:00:00 2001 From: gfdgd_xi <3025613752@qq.com> Date: Sun, 5 Nov 2023 14:36:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Ekvm=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- VM-source/kvm-ok | 132 +++++++++++++++++++++++++++++++++++++++ VM-source/mainwindow.cpp | 27 ++++++++ VM-source/mainwindow.h | 2 + VM-source/mainwindow.ui | 7 +++ VM/kvm-ok | 132 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 300 insertions(+) create mode 100755 VM-source/kvm-ok create mode 100755 VM/kvm-ok diff --git a/VM-source/kvm-ok b/VM-source/kvm-ok new file mode 100755 index 0000000..a8b7e6d --- /dev/null +++ b/VM-source/kvm-ok @@ -0,0 +1,132 @@ +#!/bin/sh +# +# kvm-ok - check whether the CPU we're running on supports KVM acceleration +# Copyright (C) 2008-2010 Canonical Ltd. +# +# Authors: +# Dustin Kirkland +# Kees Cook +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 3, +# as published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +set -e + +assert_root() { + if [ "$(id -u)" != "0" ]; then + echo "INFO: For more detailed results, you should run this as root" + echo "HINT: sudo $0" + exit 1 + fi +} + +verdict() { + # Print verdict + if [ "$1" = "0" ]; then + echo "KVM acceleration can be used" + exit 0 + else + echo "KVM acceleration can NOT be used" + exit 1 + fi +} + +ARM_CPU_PART_CORTEX_A15="0xc0f" # + +# check cpu flags for capability +case "$(uname -m)" in + armv7l) + if egrep -m1 -w '^CPU part[[:blank:]]*:' /proc/cpuinfo | \ + egrep -wq "$ARM_CPU_PART_CORTEX_A15"; then + virt="ARM" + kvm_mod="kvm" + fi + ;; + aarch64) + virt="ARM" + kvm_mod="kvm" + ;; + ppc64le|ppc64|s390x) + # FIXME: Assume that all POWER and z/Systems are KVM capable + virt="generic" + kvm_mod="kvm" + ;; + *) + virt=$(egrep -m1 -w '^flags[[:blank:]]*:' /proc/cpuinfo | egrep -wo '(vmx|svm)') || true + [ "$virt" = "vmx" ] && kvm_mod="kvm_intel" + [ "$virt" = "svm" ] && kvm_mod="kvm_amd" + ;; +esac + +if [ -z "$virt" ]; then + echo "INFO: Your CPU does not support KVM extensions" + assert_root + verdict 1 +fi + +# Now, check that the device exists +if [ -e /dev/kvm ]; then + echo "INFO: /dev/kvm exists" + verdict 0 +else + echo "INFO: /dev/kvm does not exist" + echo "HINT: sudo modprobe $kvm_mod" +fi + +assert_root + +# Prepare MSR access +msr="/dev/cpu/0/msr" +if [ ! -r "$msr" ]; then + modprobe msr +fi +if [ -e "$msr" -a ! -r "$msr" ]; then + echo "You must be root to run this check." >&2 + exit 2 +fi + +echo "INFO: Your CPU supports KVM extensions" + +disabled=0 +# check brand-specific registers +if [ "$virt" = "vmx" ]; then + BIT=$(rdmsr --bitfield 0:0 0x3a 2>/dev/null || true) + if [ "$BIT" = "1" ]; then + # and FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX clear (no tboot) + BIT=$(rdmsr --bitfield 2:2 0x3a 2>/dev/null || true) + if [ "$BIT" = "0" ]; then + disabled=1 + fi + fi + +elif [ "$virt" = "svm" ]; then + BIT=$(rdmsr --bitfield 4:4 0xc0010114 2>/dev/null || true) + if [ "$BIT" = "1" ]; then + disabled=1 + fi +elif [ "$virt" = "ARM" ]; then + # Should also test that we booted in HYP mode, if detectable + : +elif [ "$virt" = "generic" ]; then + : +else + echo "FAIL: Unknown virtualization extension: $virt" + verdict 1 +fi + +if [ "$disabled" -eq 1 ]; then + echo "INFO: KVM ($virt) is disabled by your BIOS" + echo "HINT: Enter your BIOS setup and enable Virtualization Technology (VT)," + echo " and then hard poweroff/poweron your system" + verdict 1 +fi + +verdict 0 diff --git a/VM-source/mainwindow.cpp b/VM-source/mainwindow.cpp index 2bc9d99..16052a4 100755 --- a/VM-source/mainwindow.cpp +++ b/VM-source/mainwindow.cpp @@ -301,3 +301,30 @@ void MainWindow::on_delQemuDiskButton_clicked() } QMessageBox::information(this, "提示", "移除成功"); } + +void MainWindow::on_kvmTest_clicked() +{ + if(system("which kvm-ok")&& !QFile::exists(QCoreApplication::applicationDirPath() + "/kvm-ok")){ + QMessageBox::critical(this, "错误", "未识别到命令 kvm-ok\n可以使用命令 sudo apt install cpu-checker 安装"); + return; + } + QString kvm_ok_path = "kvm-ok"; + if(!system("which kvm-ok")){ + kvm_ok_path = "kvm-ok"; + } + else if(QFile::exists(QCoreApplication::applicationDirPath() + "/kvm-ok")){ + kvm_ok_path = QCoreApplication::applicationDirPath() + "/kvm-ok"; + } + qDebug() << "使用" << kvm_ok_path; + QProcess process; + process.start(kvm_ok_path); + process.waitForStarted(); + process.waitForFinished(); + if(process.exitCode()){ + QMessageBox::critical(this, "错误", "您的系统不支持使用 kvm:\n" + process.readAll()); + return; + } + QMessageBox::information(this, "提示", "您的系统支持使用 kvm:\n" + process.readAll()); + +} + diff --git a/VM-source/mainwindow.h b/VM-source/mainwindow.h index ad5c725..5ca13b9 100755 --- a/VM-source/mainwindow.h +++ b/VM-source/mainwindow.h @@ -44,6 +44,8 @@ private slots: void on_delQemuDiskButton_clicked(); + void on_kvmTest_clicked(); + private: Ui::MainWindow *ui; long m_cpuAll; diff --git a/VM-source/mainwindow.ui b/VM-source/mainwindow.ui index f29d7d8..5c206cb 100755 --- a/VM-source/mainwindow.ui +++ b/VM-source/mainwindow.ui @@ -174,6 +174,13 @@ + + + + kvm 测试 + + + diff --git a/VM/kvm-ok b/VM/kvm-ok new file mode 100755 index 0000000..a8b7e6d --- /dev/null +++ b/VM/kvm-ok @@ -0,0 +1,132 @@ +#!/bin/sh +# +# kvm-ok - check whether the CPU we're running on supports KVM acceleration +# Copyright (C) 2008-2010 Canonical Ltd. +# +# Authors: +# Dustin Kirkland +# Kees Cook +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 3, +# as published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +set -e + +assert_root() { + if [ "$(id -u)" != "0" ]; then + echo "INFO: For more detailed results, you should run this as root" + echo "HINT: sudo $0" + exit 1 + fi +} + +verdict() { + # Print verdict + if [ "$1" = "0" ]; then + echo "KVM acceleration can be used" + exit 0 + else + echo "KVM acceleration can NOT be used" + exit 1 + fi +} + +ARM_CPU_PART_CORTEX_A15="0xc0f" # + +# check cpu flags for capability +case "$(uname -m)" in + armv7l) + if egrep -m1 -w '^CPU part[[:blank:]]*:' /proc/cpuinfo | \ + egrep -wq "$ARM_CPU_PART_CORTEX_A15"; then + virt="ARM" + kvm_mod="kvm" + fi + ;; + aarch64) + virt="ARM" + kvm_mod="kvm" + ;; + ppc64le|ppc64|s390x) + # FIXME: Assume that all POWER and z/Systems are KVM capable + virt="generic" + kvm_mod="kvm" + ;; + *) + virt=$(egrep -m1 -w '^flags[[:blank:]]*:' /proc/cpuinfo | egrep -wo '(vmx|svm)') || true + [ "$virt" = "vmx" ] && kvm_mod="kvm_intel" + [ "$virt" = "svm" ] && kvm_mod="kvm_amd" + ;; +esac + +if [ -z "$virt" ]; then + echo "INFO: Your CPU does not support KVM extensions" + assert_root + verdict 1 +fi + +# Now, check that the device exists +if [ -e /dev/kvm ]; then + echo "INFO: /dev/kvm exists" + verdict 0 +else + echo "INFO: /dev/kvm does not exist" + echo "HINT: sudo modprobe $kvm_mod" +fi + +assert_root + +# Prepare MSR access +msr="/dev/cpu/0/msr" +if [ ! -r "$msr" ]; then + modprobe msr +fi +if [ -e "$msr" -a ! -r "$msr" ]; then + echo "You must be root to run this check." >&2 + exit 2 +fi + +echo "INFO: Your CPU supports KVM extensions" + +disabled=0 +# check brand-specific registers +if [ "$virt" = "vmx" ]; then + BIT=$(rdmsr --bitfield 0:0 0x3a 2>/dev/null || true) + if [ "$BIT" = "1" ]; then + # and FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX clear (no tboot) + BIT=$(rdmsr --bitfield 2:2 0x3a 2>/dev/null || true) + if [ "$BIT" = "0" ]; then + disabled=1 + fi + fi + +elif [ "$virt" = "svm" ]; then + BIT=$(rdmsr --bitfield 4:4 0xc0010114 2>/dev/null || true) + if [ "$BIT" = "1" ]; then + disabled=1 + fi +elif [ "$virt" = "ARM" ]; then + # Should also test that we booted in HYP mode, if detectable + : +elif [ "$virt" = "generic" ]; then + : +else + echo "FAIL: Unknown virtualization extension: $virt" + verdict 1 +fi + +if [ "$disabled" -eq 1 ]; then + echo "INFO: KVM ($virt) is disabled by your BIOS" + echo "HINT: Enter your BIOS setup and enable Virtualization Technology (VT)," + echo " and then hard poweroff/poweron your system" + verdict 1 +fi + +verdict 0