Deleted C++ files

pull/15/head
QaidVoid 4 years ago
parent dd9130a331
commit 5c5ae77e2f

@ -1,11 +0,0 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <memory>
#include <sysinfo.h>
#include <shell.h>
void check_vt_support();
bool check_iommu();
void enable_iommu();
void configure_iommu();

@ -1,45 +0,0 @@
#include <sys/stat.h>
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
#include <sysinfo.h>
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
)#";

@ -1,8 +0,0 @@
#include <memory>
#include <iostream>
#include <algorithm>
std::string shell_cmd(char const *cmd);
void trim(std::string &s);
void rtrim(std::string &s);
void ltrim(std::string &s);

@ -1,10 +0,0 @@
#include <iostream>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <shell.h>
std::string get_cpu_vendor();
std::string get_gpu_vendor();
std::string get_vga_slot();
std::string get_audio_slot();

@ -1,10 +0,0 @@
#include <iostream>
#include <fstream>
#include <sys/stat.h>
#include <memory>
#include <shell.h>
void install_tools();
void enable_services();
void user_mod();
std::string get_init();

@ -1,5 +0,0 @@
#include <iostream>
#include <shell.h>
void create_vm();
void create_vm_with_libvirt();

@ -1,83 +0,0 @@
#include <iommu.h>
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");
}
}

@ -1,109 +0,0 @@
#include <libvirt.h>
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;
}

@ -1,28 +0,0 @@
#include <iommu.h>
#include <tools.h>
#include <libvirt.h>
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;
}
}

@ -1,38 +0,0 @@
#include <shell.h>
std::string shell_cmd(char const *cmd)
{
std::array<char, 128> buffer;
std::string resp;
std::unique_ptr<FILE, decltype(&pclose)> 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);
}

@ -1,43 +0,0 @@
#include <sysinfo.h>
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(" "));
}

@ -1,65 +0,0 @@
#include <tools.h>
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;
}

@ -1,11 +0,0 @@
#include <vm.h>
void create_vm()
{
// TODO
}
void create_vm_with_libvirt()
{
// TODO
}
Loading…
Cancel
Save