#!/bin/bash
# www.doomedraven.com/2016/05/kvm.html
# strs[0] = "KVMKVMKVM\0\0\0"; /* KVM */
# strs[1] = "Microsoft Hv"; /* Microsoft Hyper-V or Windows Virtual PC */
# strs[2] = "VMwareVMware"; /* VMware */
# strs[3] = "XenVMMXenVMM"; /* Xen */
# strs[4] = "prl hyperv "; /* Parallels */
# strs[5] = "VBoxVBoxVBox"; /* VirtualBox */
function usage() {
echo 'Usage: $0 '
echo
echo 'Func:'
echo ' All'
echo ' KVM'
echo ' QEMU'
echo ' SeaBios'
exit
}
function install_kvm() {
# already done?
echo ""
}
function qemu_func() {
#Download code
echo '[+] Downloading QEMU source code'
curl 'http://vault.centos.org/centos/7/virt/Source/kvm-common/qemu-kvm-ev-2.10.0-21.el7_5.3.1.src.rpm' > qemu-kvm-ev-2.10.0-21.el7_5.3.1.src.rpm
fail=0
if [ $? -eq 0 ]; then
rm -rf ~/rpmbuild/BUILD/*
rpm -vhi ./qemu-kvm-ev-2.10*.src.rpm >/dev/null
rpmbuild -bp ~/rpmbuild/SPECS/qemu-kvm.spec
sed -i 's/QEMU Virtual CPU/HAWK Realtap CPU/g' ~/rpmbuild/BUILD/qemu*/target/i386/cpu.c
if [ $? -ne 0 ]; then
echo 'QEMU was not replaced in cpu.c'
fail=1
fi
sed -i 's/"QEMU/"HAWK/g' ~/rpmbuild/BUILD/qemu*/target/i386/cpu.c
if [ $? -ne 0 ]; then
echo 'QEMU was not replaced in cpu.c'
fail=1
fi
sed -i 's/"QEMU/"HAWK/g' ~/rpmbuild/BUILD/qemu*/target/i386/arch_dump.c
if [ $? -ne 0 ]; then
echo 'QEMU was not replaced in arch_dump.c'
fail=1
fi
sed -i 's/KVMKVMKVM\\0\\0\\0/GenuineIntel/g' ~/rpmbuild/BUILD/qemu*/target/i386/kvm.c
if [ $? -ne 0 ]; then
echo 'KVMKVMKVM was not replaced in kvm.c'
fail=1
fi
sed -i 's/Microsoft Hv/GenuineIntel/g' ~/rpmbuild/BUILD/qemu*/target/i386/kvm.c
if [ $? -ne 0 ]; then
echo 'Microsoft Hv was not replaced in kvm.c'
fail=1
fi
sed -i 's/"bochs"/"hawks"/g' ~/rpmbuild/BUILD/qemu*/block/bochs.c
if [ $? -ne 0 ]; then
echo 'BOCHS was not replaced in block/bochs.c'
fail=1
fi
sed -i 's/"BOCHS "/"HAWKS "/g' ~/rpmbuild/BUILD/qemu*/include/hw/acpi/aml-build.h
if [ $? -ne 0 ]; then
echo 'bochs was not replaced in include/hw/acpi/aml-build.h'
fail=1
fi
sed -i 's/Bochs Pseudo/Intel RealTime/g' ~/rpmbuild/BUILD/qemu*/roms/ipxe/src/drivers/net/pnic.c
if [ $? -ne 0 ]; then
echo 'Bochs Pseudo was not replaced in roms/ipxe/src/drivers/net/pnic.c'
fail=1
fi
sed -i 's/Bochs\/Plex86/HAWKS\/FIRM64/g' ~/rpmbuild/BUILD/qemu*/roms/vgabios/vbe.c
if [ $? -ne 0 ]; then
echo 'BOCHS was not replaced in roms/vgabios/vbe.c'
fail=1
fi
if [ $fail -eq 0 ]; then
echo '[+] Starting compile it'
# not make sense compile if was not patched
rpmbuild --short-circuit -bc ~/rpmbuild/SPECS/qemu-kvm.spec
rpmbuild --short-circuit -bi ~/rpmbuild/SPECS/qemu-kvm.spec
rpmbuild --short-circuit -bb ~/rpmbuild/SPECS/qemu-kvm.spec
if [ $? -eq 0 ]; then
#rpm -vhU --nodeps --force ~/rpmbuild/RPMS/x86_64/qemu*.rpm
if [ $? -eq 0 ]; then
echo '[+] Patched, compiled and installed'
else
echo '[-] Install failed'
fi
else
echo '[-] Compilling failed'
fi
else
echo '[-] Check previous output'
exit
fi
else
echo '[-] Download QEMU source was not possible'
fi
}
function seabios_func {
echo '[+] Installign SeaBios dependencies'
yum -y install git iasl
git clone git://git.seabios.org/seabios.git
if [ $? -eq 0 ]; then
cd seabios
sed -i 's/Bochs//g' src/config.h
sed -i 's/BOCHSCPU/CPU/g' src/config.h
sed -i 's/BOCHS//g' src/config.h
sed -i 's/BXPC/S/g' src/config.h
make
if [ $? -eq 0 ]; then
echo '[+] Compiled SeaBios, bios file located in -> out/bios.bin'
echo '[+] Replacing old bios.bin to new one, with backup'
bios=0
echo '[+] Original bios.bin file backuped and replaced'
if [ -f /usr/share/seabios/bios.bin ]; then
cp /usr/share/seabios/bios.bin /usr/share/seabios/bios.bin_back
cp out/bios.bin /usr/share/seabios/bios.bin
bios=1
fi
if [ -f /usr/share/qemu/bios.bin ]; then
cp /usr/share/qemu/bios.bin /usr/share/qemu/bios.bin_back
cp out/bios.bin /usr/share/qemu/bios.bin
bios=1
fi
if [ $bios -eq 1 ]; then
echo '[+] Patched bios.bin placed correctly'
else
echo '[-] Bios patching failed'
fi
else
echo '[-] Bios compilation failed'
fi
else
echo '[-] Check if git installed or network connection is OK'
fi
}
#check if start with root
if [ $EUID -ne 0 ]; then
echo 'This script must be run as root'
exit 1
fi
if [ $# -eq 0 ]; then
usage
fi
if [ "$1" = '-h' ]; then
usage
fi
if [ "$1" = 'All' ]; then
install_kvm
qemu_func
seabios_func
fi
if [ "$1" = 'QEMU' ]; then
qemu_func
fi
if [ "$1" = 'SeaBios' ]; then
seabios_func
fi
if [ "$1" = 'KVM' ]; then
install_kvm
fi