From 5c5ae77e2f2ab4942c12497f57907e25d9a6d45c Mon Sep 17 00:00:00 2001 From: QaidVoid <12017109+QaidVoid@users.noreply.github.com> Date: Fri, 13 Nov 2020 15:09:40 +0545 Subject: [PATCH] Deleted C++ files --- include/iommu.h | 11 ----- include/libvirt.h | 45 ------------------- include/shell.h | 8 ---- include/sysinfo.h | 10 ----- include/tools.h | 10 ----- include/vm.h | 5 --- src/iommu.cpp | 83 ----------------------------------- src/libvirt.cpp | 109 ---------------------------------------------- src/main.cpp | 28 ------------ src/shell.cpp | 38 ---------------- src/sysinfo.cpp | 43 ------------------ src/tools.cpp | 65 --------------------------- src/vm.cpp | 11 ----- 13 files changed, 466 deletions(-) delete mode 100644 include/iommu.h delete mode 100644 include/libvirt.h delete mode 100644 include/shell.h delete mode 100644 include/sysinfo.h delete mode 100644 include/tools.h delete mode 100644 include/vm.h delete mode 100644 src/iommu.cpp delete mode 100644 src/libvirt.cpp delete mode 100644 src/main.cpp delete mode 100644 src/shell.cpp delete mode 100644 src/sysinfo.cpp delete mode 100644 src/tools.cpp delete mode 100644 src/vm.cpp diff --git a/include/iommu.h b/include/iommu.h deleted file mode 100644 index 3739c54..0000000 --- a/include/iommu.h +++ /dev/null @@ -1,11 +0,0 @@ -#include -#include -#include -#include -#include -#include - -void check_vt_support(); -bool check_iommu(); -void enable_iommu(); -void configure_iommu(); \ No newline at end of file diff --git a/include/libvirt.h b/include/libvirt.h deleted file mode 100644 index 9388e15..0000000 --- a/include/libvirt.h +++ /dev/null @@ -1,45 +0,0 @@ -#include -#include -#include -#include -#include - -void libvirt_hook(); -void hook_begin(); -void hook_release(); -std::string begin_script(); -std::string end_script(); - -inline const char *hooks_path = "/etc/libvirt/hooks"; -inline const char *qemu_dir = "/etc/libvirt/hooks/qemu.d"; -inline const char *qemu_hook = "/etc/libvirt/hooks/qemu"; -inline const char *vm_path = "/etc/libvirt/hooks/qemu.d/win10"; - -inline const char *vm_prepare = "/etc/libvirt/hooks/qemu.d/win10/prepare"; -inline const char *vm_begin = "/etc/libvirt/hooks/qemu.d/win10/prepare/begin"; -inline const char *begin_sh = "/etc/libvirt/hooks/qemu.d/win10/prepare/begin/begin.sh"; - -inline const char *vm_release = "/etc/libvirt/hooks/qemu.d/win10/release"; -inline const char *vm_end = "/etc/libvirt/hooks/qemu.d/win10/release/end"; -inline const char *end_sh = "/etc/libvirt/hooks/qemu.d/win10/release/end/end.sh"; - -inline const std::string qemu_script = R"#(#!/bin/bash - -GUEST_NAME="$1" -HOOK_NAME="$2" -STATE_NAME="$3" -MISC="${@:4}" - -BASEDIR="$(dirname $0)" - -HOOKPATH="$BASEDIR/qemu.d/$GUEST_NAME/$HOOK_NAME/$STATE_NAME" -set -e # If a script exits with an error, we should as well. - -if [ -f "$HOOKPATH" ]; then - eval \""$HOOKPATH"\" "$@" -elif [ -d "$HOOKPATH" ]; then - while read file; do - eval \""$file"\" "$@" - done <<< "$(find -L "$HOOKPATH" -maxdepth 1 -type f -executable -print;)" -fi -)#"; \ No newline at end of file diff --git a/include/shell.h b/include/shell.h deleted file mode 100644 index f20cbeb..0000000 --- a/include/shell.h +++ /dev/null @@ -1,8 +0,0 @@ -#include -#include -#include - -std::string shell_cmd(char const *cmd); -void trim(std::string &s); -void rtrim(std::string &s); -void ltrim(std::string &s); \ No newline at end of file diff --git a/include/sysinfo.h b/include/sysinfo.h deleted file mode 100644 index f662a5e..0000000 --- a/include/sysinfo.h +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include -#include -#include -#include - -std::string get_cpu_vendor(); -std::string get_gpu_vendor(); -std::string get_vga_slot(); -std::string get_audio_slot(); \ No newline at end of file diff --git a/include/tools.h b/include/tools.h deleted file mode 100644 index 7e25963..0000000 --- a/include/tools.h +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include -#include -#include -#include - -void install_tools(); -void enable_services(); -void user_mod(); -std::string get_init(); \ No newline at end of file diff --git a/include/vm.h b/include/vm.h deleted file mode 100644 index 77bb53c..0000000 --- a/include/vm.h +++ /dev/null @@ -1,5 +0,0 @@ -#include -#include - -void create_vm(); -void create_vm_with_libvirt(); \ No newline at end of file diff --git a/src/iommu.cpp b/src/iommu.cpp deleted file mode 100644 index 0cf3f32..0000000 --- a/src/iommu.cpp +++ /dev/null @@ -1,83 +0,0 @@ -#include - -void configure_iommu() -{ - check_vt_support(); - enable_iommu(); -} - -void check_vt_support() -{ - std::cout << "\033[1;36mChecking system for virtualisation support \033[0m"; - std::string res = shell_cmd("lscpu | grep Virtualization"); - - if (res.empty()) - throw std::runtime_error("\n\033[1;31mVirtualisation is not enabled on your system!\033[0m"); - std::cout << "\033[1;32m\u2714\033[0m" << std::endl; -} - -bool check_iommu() -{ - std::cout << "\033[1;36mIs IOMMU enabled? \033[0m"; - std::string res = shell_cmd("(dmesg | grep 'IOMMU enabled') 2>&1"); - - if (res.find("IOMMU enabled") != std::string::npos) - std::cout << "\033[1;32m\u2714\033[0m" << std::endl; - else - { - std::cout << "\033[1;31m\u2718\033[0m" << std::endl; - return false; - } - return true; -} - -void enable_iommu() -{ - if (!check_iommu()) - { - std::ifstream ifs("/etc/default/grub"); - std::ofstream ofs("./tmp/grub"); - - if (ifs.fail()) - throw std::runtime_error("\033[1;31mGrubloader doesn't exist or I can't find it on your system.\033[0m"); - if (ofs.fail()) - throw std::runtime_error("\033[1;31mFailed to create temporary file.\033[0m"); - - std::string line; - while (getline(ifs, line)) - { - std::istringstream iss(line); - std::string key; - if (std::getline(iss, key, '=') && key.rfind("#", 0) != 0) - { - std::string value; - std::getline(iss, value); - if (key == "GRUB_CMDLINE_LINUX_DEFAULT") - { - if (get_cpu_vendor() == "intel") - { - if (value.find("intel_iommu=on") == std::string::npos) - value.insert(*value.cbegin(), " intel_iommu=on"); - } - else if (value.find("amd_iommu=on") == std::string::npos) - value.insert(*value.cbegin(), " amd_iommu=on"); - if (value.find("iommu=pt") == std::string::npos) - value.insert(*value.cbegin(), " iommu=pt"); - } - ofs << key << "=" << value << '\n'; - } - else - ofs << line << '\n'; - } - ifs.close(); - ofs.close(); - - std::string res = shell_cmd("(mv -f ./tmp/grub /etc/default/grub) 2>&1"); - system("grub-mkconfig -o /boot/grub/grub.cfg"); - - if (res.empty()) - throw std::runtime_error("\033[1;32mIOMMU enabled. Please reboot your system to continue.\033[0m"); - else - throw std::runtime_error("\033[1;31mFailed to enable IOMMU.\n" + res + "\033[0m"); - } -} \ No newline at end of file diff --git a/src/libvirt.cpp b/src/libvirt.cpp deleted file mode 100644 index 4bc0560..0000000 --- a/src/libvirt.cpp +++ /dev/null @@ -1,109 +0,0 @@ -#include - -void libvirt_hook() -{ - char cnf; - std::cout << "\033[1;34mSetting up libvirt hooks. If you have already setup libvirt hooks, they'll be replaced. Continue? (y/n) \033[0m"; - std::cin >> cnf; - if (tolower(cnf) != 'y') - throw std::runtime_error("\033[1;31mEXITING!\033[0m"); - - struct stat buf; - if (stat(hooks_path, &buf) != 0) - if (mkdir(hooks_path, 0755) == -1) - throw std::runtime_error(strerror(errno)); - if (stat(qemu_dir, &buf) != 0) - if (mkdir(qemu_dir, 0755) == -1) - throw std::runtime_error(strerror(errno)); - std::ofstream file(qemu_hook); - file << qemu_script; - file.close(); - if (chmod(qemu_hook, 0755) == -1) - throw std::runtime_error(strerror(errno)); - std::cout << "\033[1;32m/etc/libvirt/hooks/qemu \033[1;32m\u2714\033[0m \033[0m" << std::endl; - hook_begin(); - hook_release(); - system("systemctl restart libvirtd"); -} - -void hook_begin() -{ - struct stat buf; - if (stat(vm_path, &buf) != 0) - if (mkdir(vm_path, 0755) == -1) - throw std::runtime_error(strerror(errno)); - if (stat(vm_prepare, &buf) != 0) - if (mkdir(vm_prepare, 0755) == -1) - throw std::runtime_error(strerror(errno)); - if (stat(vm_begin, &buf) != 0) - if (mkdir(vm_begin, 0755) == -1) - throw std::runtime_error(strerror(errno)); - std::ofstream file(begin_sh); - file << begin_script(); - file.close(); - if (chmod(begin_sh, 0755) == -1) - throw std::runtime_error(strerror(errno)); - std::cout << "\033[1;32m/etc/libvirt/hooks/qemu.d/win10/prepare/begin/begin.sh \033[1;32m\u2714\033[0m \033[0m" << std::endl; -} - -void hook_release() -{ - struct stat buf; - if (stat(vm_path, &buf) != 0) - if (mkdir(vm_path, 0755) == -1) - throw std::runtime_error(strerror(errno)); - if (stat(vm_release, &buf) != 0) - if (mkdir(vm_release, 0755) == -1) - throw std::runtime_error(strerror(errno)); - if (stat(vm_end, &buf) != 0) - if (mkdir(vm_end, 0755) == -1) - throw std::runtime_error(strerror(errno)); - std::ofstream file(end_sh); - file << end_script(); - file.close(); - if (chmod(end_sh, 0755) == -1) - throw std::runtime_error(strerror(errno)); - std::cout << "\033[1;32m/etc/libvirt/hooks/qemu.d/win10/release/end/end.sh \033[1;32m\u2714\033[0m \033[0m" << std::endl; -} - -std::string begin_script() -{ - std::string gpu = get_gpu_vendor(); - - std::string modules; - modules = gpu == "nvidia" ? modules = "nvidia_drm nvidia_modeset nvidia_uvm nvidia" : modules = "amdgpu"; - std::string cont = R"(#!/bin/bash -set -x - -# Stop display manager -systemctl stop display-manager - -# Unbind EFI Framebuffer -echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/unbind)"; - cont += "\n\nmodprobe -r " + modules; - cont += "\nvirsh nodedev-detach " + get_vga_slot(); - cont += "\nvirsh nodedev-detach " + get_audio_slot(); - cont += "\n\n#Load vfio module\nmodprobe vfio-pci"; - return cont; -} - -std::string end_script() -{ - std::string gpu = get_gpu_vendor(); - - std::string modules = "amdgpu"; - if (gpu == "nvidia") - modules = "nvidia_drm nvidia_modeset nvidia_uvm nvidia"; - std::string cont = R"(#!/bin/bash -set -x - -# Unload vfio module -modprobe -r vfio-pci -)"; - cont += "\nvirsh nodedev-reattach " + get_vga_slot(); - cont += "\nvirsh nodedev-reattach " + get_audio_slot(); - cont += "\n\n#Unbind EFI Framebuffer\necho \"efi-framebuffer.0\" > /sys/bus/platform/drivers/efi-framebuffer/bind"; - cont += gpu == "nvidia" ? "\n\n# Load NVIDIA kernel modules\nmodprobe nvidia_drm\nmodprobe nvidia_modeset\nmodprobe nvidia_uvm\nmodprobe nvidia" : "\n\n# Load AMD kernel module\nmodprobe amdgpu"; - cont += "\n\n# Restart Display Manager\nsystemctl start display-manager"; - return cont; -} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp deleted file mode 100644 index 09e09c9..0000000 --- a/src/main.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include -#include - -int main() -{ - try - { - std::string user = shell_cmd("echo $(whoami)"); - if (user != "root") - throw std::runtime_error("\033[1;31mYou need to run the tool as root.\033[0m"); - - char cnf; - configure_iommu(); - - std::cout << "Do you want to install required packages automatically? (y/n) "; - std::cin >> cnf; - if (tolower(cnf) == 'y') - install_tools(); - enable_services(); - user_mod(); - libvirt_hook(); - } - catch (const std::runtime_error &err) - { - std::cerr << err.what() << std::endl; - } -} \ No newline at end of file diff --git a/src/shell.cpp b/src/shell.cpp deleted file mode 100644 index 32121fd..0000000 --- a/src/shell.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include - -std::string shell_cmd(char const *cmd) -{ - std::array buffer; - std::string resp; - std::unique_ptr pipe(popen(cmd, "r"), pclose); - if (!pipe) - throw std::runtime_error("\n\033[1;31mThat was unexpected!\033[0m"); - - while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) - { - resp += buffer.data(); - } - trim(resp); - return resp; -} - -void ltrim(std::string &s) -{ - s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { - return !std::isspace(ch); - })); -} - -void rtrim(std::string &s) -{ - s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { - return !std::isspace(ch); - }).base(), - s.end()); -} - -void trim(std::string &s) -{ - ltrim(s); - rtrim(s); -} \ No newline at end of file diff --git a/src/sysinfo.cpp b/src/sysinfo.cpp deleted file mode 100644 index 7cc0899..0000000 --- a/src/sysinfo.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include - -std::string get_cpu_vendor() -{ - std::string cpu = "intel"; - std::string res = shell_cmd("lspci | grep -i amd"); - - if (!res.empty()) - cpu = "amd"; - return cpu; -} - -std::string get_gpu_vendor() -{ - std::string gpu = "amd"; - std::string res = shell_cmd("lspci | grep -i nvidia"); - - if (!res.empty()) - gpu = "nvidia"; - return gpu; -} - -std::string get_vga_slot() -{ - std::string c = "lspci | grep -i vga.*" + get_gpu_vendor(); - std::string res = shell_cmd(c.c_str()); - - if (res.empty()) - throw std::runtime_error("\n\033[1;31mCouldn't fetch VGA PCI slot!\033[0m"); - std::replace(res.begin(), res.end(), ':', '_'); - return "pci_0000_" + res.substr(0, res.find(" ")); -} - -std::string get_audio_slot() -{ - std::string c = "lspci | grep -i audio.*" + get_gpu_vendor(); - std::string res = shell_cmd(c.c_str()); - - if (res.empty()) - throw std::runtime_error("\n\033[1;31mCouldn't fetch Audio PCI slot!\033[0m"); - std::replace(res.begin(), res.end(), ':', '_'); - return "pci_0000_" + res.substr(0, res.find(" ")); -} \ No newline at end of file diff --git a/src/tools.cpp b/src/tools.cpp deleted file mode 100644 index 208363d..0000000 --- a/src/tools.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include - -void install_tools() -{ - struct stat buf; - if (stat("/etc/arch-release", &buf) == 0) - system("pacman -S qemu libvirt edk2-ovmf virt-manager dnsmasq ebtables"); - else if (stat("/etc/gentoo-release", &buf) == 0) - system("emerge -a qemu libvirt virt-manager dnsmasq ebtables"); - else if (stat("/etc/debian_version", &buf) == 0) - system("apt install qemu-kvm libvirt-clients libvirt-daemon-system ovmf dnsmasq ebtables"); - else if (stat("/etc/fedora-release", &buf) == 0) - system("dnf install @virtualization"); - else - throw std::runtime_error("\n\033[1;31mUnsupported system. Install required tools manually.\033[0m"); -} - -void enable_services() -{ - std::string init_sys = get_init(); - std::cout << "\033[1;36mDetected " + init_sys + " init system. \033[0m"; - if (init_sys != "systemd") - throw std::runtime_error("\033[1;36mOnly systemd is supported at the moment. \033[0m"); - // TODO - if (init_sys == "systemd") - system("systemctl enable --now libvirtd"); - else // Other init systems? - std::cout << "Enable libvirtd service on your system manually..\n"; - std::string res = shell_cmd("virsh net-list --all | grep -i default"); - - if (res.empty()) - { - system("virsh net-start default"); - system("virsh net-autostart default"); - } -} - -std::string get_init() -{ - std::ifstream bin("/sbin/init", std::ios::binary); - std::string line; - while (std::getline(bin, line)) - { - if (line.find("/lib/systemd") != std::string::npos) - return "systemd"; - else if (line.find("sysvinit") != std::string::npos) - return "sysvinit"; - else if (line.find("upstart") != std::string::npos) - return "upstart"; - } - return ""; -} - -void user_mod() -{ - std::cout << "\n\033[1;36mAdding user to kvm, input & libvirt group \033[0m"; - std::string res = shell_cmd("usermod -aG kvm,input,libvirt $(logname)"); - if (!res.empty()) - { - std::cout << "\033[1;31m\u2718\033[0m" << std::endl; - std::string emsg = "\n\033[1;31m" + res + "\033[0m"; - throw std::runtime_error(emsg); - } - std::cout << "\033[1;32m\u2714\033[0m" << std::endl; -} \ No newline at end of file diff --git a/src/vm.cpp b/src/vm.cpp deleted file mode 100644 index 1c3bd1d..0000000 --- a/src/vm.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include - -void create_vm() -{ - // TODO -} - -void create_vm_with_libvirt() -{ - // TODO -} \ No newline at end of file