Skip to content

Instantly share code, notes, and snippets.

@darkhypervisor
Forked from doomedraven/kvm-qemu.sh
Created May 14, 2020 20:58
Show Gist options
  • Save darkhypervisor/0ca11d02d91be64350fbc747afe153b5 to your computer and use it in GitHub Desktop.
Save darkhypervisor/0ca11d02d91be64350fbc747afe153b5 to your computer and use it in GitHub Desktop.
Linux - KVM + QEMU installer from sources :)
#!/bin/bash
# https://www.doomedraven.com/2016/05/kvm.html
# 09.09.2018 - ACPI fixes - huge thanks to @2sec4u for your patience and your time/help :P
# 05.09.2018 - libivrt 4.7 and virtlogd
# 19.08.2018 - Intel HAXM notes
# 14.08.2018 - QEMU 3 support tested on ubuntu 18.04
# 03.08.2018 - More anti-anti by Tim Shelton (redsand) @ HAWK (hawk.io) and @http_error_418
# 28.02.2018 - Support for qemu 2.12
# https://github.com/dylanaraps/pure-bash-bible
# ACPI tables related
# https://wiki.archlinux.org/index.php/DSDT
# Dump on linux
#  cat /sys/firmware/acpi/tables/DSDT > dsdt.dat
# Dump on Windows
#  https://acpica.org/downloads/binary-tools
# acpixtract -a acpi/4/acpi.dump
# Decompile: iasl -d dsdt.dat
# Recompile: iasl -tc dsdt.dsl
# 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 */
#https://www.qemu.org/download/#source or https://download.qemu.org/
qemu_version=3.0.0
# libvirt - https://libvirt.org/sources/
libvirt_version=4.7.0
# virt-manager - https://github.com/virt-manager/virt-manager/releases
virt_manager_version=1.5.0
# autofilled
OS=""
function usage() {
echo 'Usage: $0 <func_name>'
echo
echo 'Commands:'
echo ' All'
echo ' QEMU'
echo ' SeaBios'
echo ' KVM - this will install intel-HAXM if you on Mac'
echo ' HAXM - Mac Hardware Accelerated Execution Manager'
echo ' libvirt - install libvirt'
echo ' replace_qemu - only fix antivms in QEMU source'
echo ' replace_seabios <path> - only fix antivms in SeaBios source'
exit
}
function _check_brew() {
if [ ! -f /usr/local/bin/brew ]; then
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
fi
}
function install_haxm_mac() {
_check_brew
brew cask install intel-haxm
brew tap jeffreywildman/homebrew-virt-manager
brew cask install xquartz
brew install virt-manager virt-viewer
if [ `echo $SHELL` = "/bin/zsh" ]; then
echo "export LIBVIRT_DEFAULT_URI=qemu:///system" > $HOME/.zsh
else
echo "export LIBVIRT_DEFAULT_URI=qemu:///system" > $HOME/.bashrc
fi
}
function install_libvirt() {
cd /tmp
if [ -f ibvirt-$libvirt_version.tar.xz ]; then
rm -r libvirt-$libvirt_version
else
wget https://libvirt.org/sources/libvirt-$libvirt_version.tar.xz
fi
tar xvf libvirt-$libvirt_version.tar.xz
cd libvirt-$libvirt_version
if [ $OS = "Linux" ]; then
#sudo apt-get build-dep libvirt
./autogen.sh --system --prefix=/usr --localstatedir=/var --sysconfdir=/etc --with-qemu=yes --with-dtrace --with-numad --with-storage-rbd --disable-nls --with-openvz=no --with-vmware=no --with-phyp=no --with-xenapi=no --with-libxl=no --with-vbox=no --with-lxc=no --with-vz=no --with-esx=no --with-hyperv=no --with-yajl=yes
make -j4
make install
elif [ $OS = "Darwin" ]; then
./autogen.sh --system --prefix=/usr/local/ --localstatedir=/var --sysconfdir=/etc --with-qemu=yes --with-dtrace --disable-nls --with-openvz=no --with-vmware=no --with-phyp=no --with-xenapi=no --with-libxl=no --with-vbox=no --with-lxc=no --with-vz=no --with-esx=no --with-hyperv=no --with-wireshark-dissector=no --with-yajl=yes
fi
# https://wiki.archlinux.org/index.php/Libvirt#Using_polkit
#/etc/libvirt/libvirtd.conf
#unix_sock_group = "libvirt"
#unix_sock_ro_perms = "0777" # set to 0770 to deny non-group libvirt users
#unix_sock_rw_perms = "0770"
#auth_unix_ro = "none"
#auth_unix_rw = "none"
if [ -f /etc/libvirt/libvirtd.conf ]; then
path="/etc/libvirt/libvirtd.conf"
elif [ -f /usr/local/etc/libvirt/libvirtd.conf ]; then
path="/usr/local/etc/libvirt/libvirtd.conf"
fi
#sed -i 's/#unix_sock_group = "libvirt"/unix_sock_group = "libvirtd"/g' /etc/libvirt/libvirtd.conf
sed -i 's/#unix_sock_ro_perms = "0777"/unix_sock_ro_perms = "0770"/g' $path
sed -i 's/#unix_sock_rw_perms = "0770"/unix_sock_rw_perms = "0770"/g' $path
sed -i 's/#auth_unix_ro = "none"/auth_unix_ro = "none"/g' $path
sed -i 's/#auth_unix_r2 = "none"/auth_unix_ro = "none"/g' $path
cd /tmp
wget https://github.com/libvirt/libvirt-python/archive/v$libvirt_version.zip
unzip v$libvirt_version.zip
cd libvirt-python*
python setup.py build
sudo python setup.py install
}
function install_kvm_linux_apt() {
sed -i 's/# deb-src/deb-src/g' /etc/apt/sources.list
sudo apt-get update
sudo apt-get install build-essential numad python-pip gcc pkg-config cpu-checker glib-2.0 libglib2.0-dev libsdl1.2-dev libaio-dev libcap-dev libattr1-dev libpixman-1-dev libgtk2.0-bin libxml2-utils systemtap-sdt-dev# latest for ubuntu 16
sudo apt-get install gtk-update-icon-cache
# sudo apt-get build-dep qemu binfmt-support
# #qemu-kvm qemu-system libvirt-bin ubuntu-virt-server python-vm-builder ubuntu-vm-builder bridge-utils
sudo apt-get install lvm2
#DEPRICATED sudo apt-get install virt-viewer virt-manager virtinst # Virtual Machine Manager
sudo apt-get install debhelper ibusb-1.0-0-dev libxen-dev uuid-dev xfslibs-dev libjpeg-dev libusbredirparser-dev device-tree-compiler texinfo libbluetooth-dev libbrlapi-dev libcap-ng-dev libcurl4-gnutls-dev libfdt-dev gnutls-dev libiscsi-dev libncurses5-dev libnuma-dev libcacard-dev librados-dev librbd-dev libsasl2-dev libseccomp-dev libspice-server-dev
# WSL support
sudo apt-get install gcc make gnutls-bin
# remove old
sudo apt-get purge libvirt0 libvirt-bin
install_libvirt
# https://github.com/libvirt/libvirt/commit/e94979e901517af9fdde358d7b7c92cc055dd50c
groupname=""
if [ grep -q -E "^libvirtd:" /etc/group ]; then
groupname="libvirtd"
elif [ grep -q -E "^libvirt:" /etc/group ]; then
groupname="libvirt"
else
# create group if missed
groupname="libvirt"
sudo groupadd libvirt
fi
usermod -G $groupname -a cuckoo
usermod -G $groupname -a `whoami`
systemctl enable libvirtd.service
systemctl restart libvirtd.service
systemctl enable virtlogd.socket
systemctl restart virtlogd.socket
wget https://github.com/virt-manager/virt-manager/archive/v$virt_manager_version.zip
unzip v$virt_manager_version
cd virt-manager-$virt_manager_version
python setup.py build
python setup.py install
if [ `echo $SHELL` = "/bin/zsh" ]; then
echo "export LIBVIRT_DEFAULT_URI=qemu:///system" > $HOME/.zsh
else
echo "export LIBVIRT_DEFAULT_URI=qemu:///system" > $HOME/.bashrc
fi
#reboot me here
sudo kvm-ok
}
function replace_qemu_clues() {
echo '[+] Patching QEMU clues'
sed -i 's/QEMU HARDDISK/<WOOT> HARDDISK/g' qemu*/hw/ide/core.c
if [ $? -ne 0 ]; then
echo 'QEMU HARDDISK was not replaced in core.c'
fail=1
fi
sed -i 's/QEMU HARDDISK/<WOOT> HARDDISK/g' qemu*/hw/scsi/scsi-disk.c
if [ $? -ne 0 ]; then
echo 'QEMU HARDDISK was not replaced in scsi-disk.c'
fail=1
fi
sed -i 's/QEMU DVD-ROM/<WOOT> DVD-ROM/g' qemu*/hw/ide/core.c
if [ $? -ne 0 ]; then
echo 'QEMU DVD-ROM was not replaced in core.c'
fail=1
fi
sed -i 's/QEMU DVD-ROM/<WOOT> DVD-ROM/g' qemu*/hw/ide/atapi.c
if [ $? -ne 0 ]; then
echo 'QEMU DVD-ROM was not replaced in atapi.c'
fail=1
fi
sed -i 's/s->vendor = g_strdup("QEMU");/s->vendor = g_strdup("<WOOT>");/g' qemu*/hw/scsi/scsi-disk.c
if [ $? -ne 0 ]; then
echo 'Vendor string was not replaced in scsi-disk.c'
fail=1
fi
sed -i 's/QEMU CD-ROM/<WOOT> CD-ROM/g' qemu*/hw/scsi/scsi-disk.c
if [ $? -ne 0 ]; then
echo 'QEMU CD-ROM was not patched in scsi-disk.c'
fail=1
fi
sed -i 's/padstr8(buf + 8, 8, "QEMU");/padstr8(buf + 8, 8, "<WOOT>");/g' qemu*/hw/ide/atapi.c
if [ $? -ne 0 ]; then
echo 'padstr was not replaced in atapi.c'
fail=1
fi
sed -i 's/QEMU MICRODRIVE/<WOOT> MICRODRIVE/g' qemu*/hw/ide/core.c
if [ $? -ne 0 ]; then
echo 'QEMU MICRODRIVE was not replaced in core.c'
fail=1
fi
sed -i 's/KVMKVMKVM\\0\\0\\0/GenuineIntel/g' qemu*/target/i386/kvm.c
if [ $? -ne 0 ]; then
echo 'KVMKVMKVM was not replaced in kvm.c'
fail=1
fi
# by @http_error_418
sed -i 's/Microsoft Hv/GenuineIntel/g' qemu*/target/i386/kvm.c
if [ $? -ne 0 ]; then
echo 'Microsoft Hv was not replaced in target/i386/kvm.c'
fail=1
fi
sed -i 's/"bochs"/"hawks"/g' qemu*/block/bochs.c
if [ $? -ne 0 ]; then
echo 'BOCHS was not replaced in block/bochs.c'
fail=1
fi
# by Tim Shelton (redsand) @ HAWK (hawk.io)
sed -i 's/"BOCHS "/"ALASKA"/g' 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
# by Tim Shelton (redsand) @ HAWK (hawk.io)
sed -i 's/Bochs Pseudo/Intel RealTime/g' 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
# by Tim Shelton (redsand) @ HAWK (hawk.io)
sed -i 's/Bochs\/Plex86/<WOOT>\/FIRM64/g' qemu*/roms/vgabios/vbe.c
if [ $? -ne 0 ]; then
echo 'BOCHS was not replaced in roms/vgabios/vbe.c'
fail=1
fi
}
function replace_seabios_clues() {
echo "[+] deleting BOCHS APCI tables"
#rm src/fw/*.hex >/dev/null 2>&1
echo "[+] Generating SeaBios Kconfig"
./scripts/kconfig/merge_config.sh -o . >/dev/null 2>&1
sed -i 's/CONFIG_ACPI_DSDT=y/CONFIG_ACPI_DSDT=n/g' .config
sed -i 's/CONFIG_XEN=y/CONFIG_XEN=n/g' .config
echo "[+] Fixing SeaBios antivms"
sed -i 's/Bochs/<WOOT>/g' src/config.h
if [ $? -ne 0 ]; then
echo 'Bochs was not replaced in src/config.h'
fail=1
fi
sed -i 's/BOCHSCPU/<WOOT>/g' src/config.h
if [ $? -ne 0 ]; then
echo 'BOCHSCPU was not replaced in src/config.h'
fail=1
fi
sed -i 's/"BOCHS "/"<WOOT>"/g' src/config.h
if [ $? -ne 0 ]; then
echo 'BOCHS was not replaced in src/config.h'
fail=1
fi
sed -i 's/BXPC/<WOOT>/g' src/config.h
if [ $? -ne 0 ]; then
echo 'BXPC was not replaced in src/config.h'
fail=1
fi
sed -i 's/QEMU0001/<WOOT>/g' src/fw/ssdt-misc.dsl
if [ $? -ne 0 ]; then
echo 'QEMU0001 was not replaced in src/fw/ssdt-misc.dsl'
fail=1
fi
sed -i 's/QEMU\/Bochs/<WOOT>\/<WOOT>/g' vgasrc/Kconfig
if [ $? -ne 0 ]; then
echo 'QEMU\/Bochs was not replaced in vgasrc/Kconfig'
fail=1
fi
sed -i 's/qemu /<WOOT> /g' vgasrc/Kconfig
if [ $? -ne 0 ]; then
echo 'qemu was not replaced in vgasrc/Kconfig'
fail=1
fi
FILES=(
src/hw/blockcmd.c
src/fw/paravirt.c
)
for file in ${FILES[@]}; do
sed -i 's/"QEMU/"<WOOT>/g' $file;
if [ $? -ne 0 ]; then
echo 'QEMU was not replaced in' $file
fail=1
fi
done
sed -i 's/"QEMU"/"<WOOT>"/g' src/hw/blockcmd.c
if [ $? -ne 0 ]; then
echo '"QEMU" was not replaced in src/hw/blockcmd.c'
fail=1
fi
FILES=(
"src/fw/acpi-dsdt.dsl"
"src/fw/q35-acpi-dsdt.dsl"
)
for file in ${FILES[@]}; do
sed -i 's/"BXPC"/<WOOT>"/g' $file;
if [ $? -ne 0 ]; then
echo 'BXPC was not replaced in' $file
fail=1
fi
sed -i 's/"BXDSDT"/"<WOOT>"/g' $file;
if [ $? -ne 0 ]; then
echo 'BXDSDT was not replaced in' $file
fail=1
fi
done
sed -i 's/"BXPC"/"<WOOT>"/g' "src/fw/ssdt-pcihp.dsl";
if [ $? -ne 0 ]; then
echo 'BXPC was not replaced in src/fw/ssdt-pcihp.dsl'
fail=1
fi
sed -i 's/"BXDSDT"/"<WOOT>"/g' "src/fw/ssdt-pcihp.dsl";
if [ $? -ne 0 ]; then
echo 'BXDSDT was not replaced in src/fw/ssdt-pcihp.dsl'
fail=1
fi
sed -i 's/"BXPC"/"<WOOT>"/g' "src/fw/ssdt-proc.dsl";
if [ $? -ne 0 ]; then
echo 'BXPC was not replaced in "src/fw/ssdt-proc.dsl"'
fail=1
fi
sed -i 's/"BXSSDT"/"<WOOT>"/g' "src/fw/ssdt-proc.dsl";
if [ $? -ne 0 ]; then
echo 'BXSSDT was not replaced in "src/fw/ssdt-proc.dsl" '
fail=1
fi
sed -i 's/"BXPC"/"<WOOT>"/g' "src/fw/ssdt-misc.dsl" ;
if [ $? -ne 0 ]; then
echo 'BXPC was not replaced in "src/fw/ssdt-misc.dsl"'
fail=1
fi
sed -i 's/"BXSSDTSU"/"<WOOT>"/g' "src/fw/ssdt-misc.dsl" ;
if [ $? -ne 0 ]; then
echo 'BXDSDT was not replaced in "src/fw/ssdt-misc.dsl"'
fail=1
fi
sed -i 's/"BXSSDTSUSP"/"<WOOT>"/g' src/fw/ssdt-misc.dsl
if [ $? -ne 0 ]; then
echo 'BXSSDTSUSP was not replaced in src/fw/ssdt-misc.dsl'
fail=1
fi
sed -i 's/"BXSSDT"/"<WOOT>"/g' src/fw/ssdt-proc.dsl
if [ $? -ne 0 ]; then
echo 'BXSSDT was not replaced in src/fw/ssdt-proc.dsl'
fail=1
fi
sed -i 's/"BXSSDTPCIHP"/"<WOOT>"/g' src/fw/ssdt-pcihp.dsl
if [ $? -ne 0 ]; then
echo 'BXPC was not replaced in src/fw/ssdt-pcihp.dsl'
fail=1
fi
FILES=(
src/fw/q35-acpi-dsdt.dsl
src/fw/acpi-dsdt.dsl
src/fw/ssdt-misc.dsl
src/fw/ssdt-proc.dsl
src/fw/ssdt-pcihp.dsl
src/config.h
)
for file in ${FILES[@]}; do
sed -i 's/"BXPC"/"<WOOT>"/g' $file;
if [ $? -ne 0 ]; then
echo 'BXPC was not replaced in' $file
fail=1
fi
done
}
function qemu_func() {
echo '[+] Downloading QEMU source code'
if [ ! -f qemu-$qemu_version.tar.xz ]; then
wget https://download.qemu.org/qemu-$qemu_version.tar.xz
fi
tar xvJf qemu-$qemu_version.tar.xz
fail=0
# qemu deps
# Adding user libvirt-qemu to group libvirt-qemu
if [ $OS = "Linux" ]; then
sudo apt-get install checkinstall openbios-* libssh2-1-dev vde2 liblzo2-dev libghc-gtk3-dev libsnappy-dev libbz2-dev libxml2-dev google-perftools libgoogle-perftools-dev libvde-dev
elif [ $OS = "Darwin" ]; then
_check_brew
brew install pkg-config libtool jpeg gnutls glib ncurses pixman libpng vde gtk+3 libssh2 libssh2 libvirt snappy libcapn gperftools glib
fi
# WOOT
# some checks may be depricated, but keeping them for compatibility with old versions
if [ $? -eq 0 ]; then
cd qemu*/roms/seabios
replace_seabios_clues
cd -
replace_qemu_clues
if [ $fail -eq 0 ]; then
echo '[+] Starting compile it'
cd qemu-$qemu_version
# --enable-malloc-trim since we use tcmalloc
# add in future --enable-netmap https://sgros-students.blogspot.com/2016/05/installing-and-testing-netmap.html
# --target-list=i386-softmmu,x86_64-softmmu,i386-linux-user,x86_64-linux-user
if [ $OS = "Linux" ]; then
./configure --prefix=/usr --libexecdir=/usr/lib/qemu --localstatedir=/var --bindir=/usr/bin/ --enable-gnutls --enable-docs --enable-gtk --enable-vnc --enable-vnc-sasl --enable-vnc-png --enable-vnc-jpeg --enable-curl --enable-kvm --enable-linux-aio --enable-cap-ng --enable-vhost-net --enable-vhost-crypto --enable-spice --enable-usb-redir --enable-lzo --enable-snappy --enable-bzip2 --enable-coroutine-pool --enable-libssh2 --enable-libxml2 --enable-tcmalloc --enable-replication --enable-tools --enable-capstone
elif [ $OS = "Darwin" ]; then
# --enable-vhost-net --enable-vhost-crypto
./configure --prefix=/usr --libexecdir=/usr/lib/qemu --localstatedir=/var --bindir=/usr/bin/ --enable-gnutls --enable-docs --enable-vnc --enable-vnc-sasl --enable-vnc-png --enable-vnc-jpeg --enable-curl --enable-hax --enable-usb-redir --enable-lzo --enable-snappy --enable-bzip2 --enable-coroutine-pool --enable-libxml2 --enable-tcmalloc --enable-replication --enable-tools --enable-capstone
fi
if [ $? -eq 0 ]; then
echo '[+] Starting Install it'
#dpkg -i qemu*.deb
if [ -f /usr/share/qemu/qemu_logo_no_text.svg ]; then
rm /usr/share/qemu/qemu_logo_no_text.svg
fi
make -j4
if [ $OS = "Linux" ]; then
checkinstall -D --pkgname=$qemu_version
elif [ $OS = "Darwin" ]; then
make install
fi
# hack for libvirt/virt-manager
if [ ! -f /usr/bin/qemu-system-x86_64-spice ]; then
ln -s /usr/bin/qemu-system-x86_64 /usr/bin/qemu-system-x86_64-spice
fi
if [ ! -f /usr/bin/kvm-spice ]; then
ln -s /usr/bin/qemu-system-x86_64 /usr/bin/kvm-spice
fi
if [ ! -f /usr/bin/kvm ]; then
ln -s /usr/bin/qemu-system-x86_64 /usr/bin/kvm
fi
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
if [ $OS = "linux" ]; then
dpkg --get-selections | grep "qemu" | xargs sudo apt-mark hold
#sudo apt-mark unhold qemu
fi
}
function seabios_func() {
fail=0
echo '[+] Installign SeaBios dependencies'
apt-get install git iasl
if [ -d seabios ]; then
rm -r seabios
fi
git clone https://github.com/coreboot/seabios.git
if [ $? -eq 0 ]; then
cd seabios
replace_seabios_clues
cd -
if [ $fail -eq 0 ]; then
# sudo make help
# sudo make menuconfig -> BIOS tables -> disable Include default ACPI DSDT
make -j4
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
FILES=(
/usr/share/qemu/bios.bin
/usr/share/qemu/bios-256k.bin
)
for file in ${FILES[@]}; do
if [ -f $file ]; then
cp $file $file_back
cp out/bios.bin $file
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 previous errors'
fi
else
echo '[-] Check if git installed or network connection is OK'
fi
}
COMMAND=$1
if [ $# -eq 0 ]; then
usage
exit 0
fi
if [ $COMMAND = '-h' ]; then
usage
exit 0
fi
#check if start with root
if [ $EUID -ne 0 ]; then
echo 'This script must be run as root'
exit 1
fi
OS="$(uname -s)"
if [ "$1" = 'All' ]; then
qemu_func
seabios_func
if [ $OS = "Linux" ]; then
install_kvm_linux_apt
elif [ $OS = "Darwin" ]; then
install_haxm_mac
fi
fi
if [ $COMMAND = 'QEMU' ]; then
qemu_func
fi
if [ $COMMAND = 'SeaBios' ]; then
seabios_func
fi
if [ $COMMAND = 'KVM' ]; then
install_kvm_linux_apt
fi
if [ $COMMAND = 'HAXM' ]; then
install_haxm_mac
fi
if [ $COMMAND = "replace_qemu" ]; then
replace_qemu_clues
fi
if [ $COMMAND = "libvirt" ]; then
install_libvirt
fi
if [ $COMMAND = "replace_seabios" ]; then
if [ ! -d $2 ]; then
echo "[-] Pass the path to SeaBios folder"
exit 1
fi
cd $2
replace_seabios_clues
cd -
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment