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
  • Select an option

  • Save darkhypervisor/0ca11d02d91be64350fbc747afe153b5 to your computer and use it in GitHub Desktop.

Select an option

Save darkhypervisor/0ca11d02d91be64350fbc747afe153b5 to your computer and use it in GitHub Desktop.
Linux - KVM + QEMU installer from sources :)
#!/bin/bash
# https://doomedraven.github.io/2016/01/23/KVM-QEMU.html
# 28.02.2018 - Support for qemu 2.12
# 03.08.2018 - More anti-anti by Tim Shelton (redsand) @ HAWK (hawk.io) and @http_error_418
# 14.08.2018 - QEMU 3 support tested on ubuntu 18.04
# 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 */
# KVM should be installed last
function usage() {
echo 'Usage: $0 <func_name>'
echo
echo 'Func:'
echo ' All'
echo ' QEMU'
echo ' SeaBios'
echo ' KVM'
exit
}
function install_kvm() {
sed -i 's/# deb-src/deb-src/g' /etc/apt/sources.list
sudo apt-get update
sudo apt-get install unzip build-essential make 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 gtk-update-icon-cache
sudo apt-get build-dep qemu binfmt-support
sudo apt-get install lvm2 bridge-utils #python-vm-builder ubuntu-vm-builder qemu-kvm qemu-system libvirt-bin ubuntu-virt-server
#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
# libvirt - https://libvirt.org/sources/
version="4.6.0"
wget https://libvirt.org/sources/libvirt-$version.tar.xz
tar xvf libvirt-$version.tar.xz
cd libvirt-$version
# sudo apt-get build-dep libvirt - list belove extracted from here removing qemu
sudo apt-get install attr augeas-lenses augeas-tools dh-apparmor dwarves gir1.2-harfbuzz-0.0 gir1.2-polkit-1.0 glusterfs-common icu-devtools keyutils libacl1-dev libapparmor-dev libaudit-dev libaugeas0 libavahi-client-dev libavahi-client3 libavahi-common-data libavahi-common-dev libavahi-common3 libblkid-dev libc-ares2 libcfg6 libcorosync-common4 libcpg4 libdbus-1-dev libdevmapper-dev libdw1 libfcgi0ldbl libfuse-dev libgcrypt20-dev libgpg-error-dev libgraphite2-3 libgraphite2-dev libharfbuzz-dev libharfbuzz-gobject0 libharfbuzz-icu0 libharfbuzz0b libicu-dev libicu-le-hb-dev libicu-le-hb0 libiculx60 liblua5.2-0 libnetcf-dev libnetcf1 libnfsidmap2 libnl-3-dev libnl-route-3-dev libparted-dev libparted-fs-resize0 libpcap0.8-dev libpciaccess-dev libpolkit-gobject-1-dev libqb0 libreadline-dev libsanlock-client1 libsanlock-dev libsanlock1 libsbc1 libselinux1-dev libsepol1-dev libsmi2ldbl libsnappy1v5 libspandsp2 libtirpc1 libudev-dev liburcu6 libwireshark-data libwireshark-dev libwireshark10 libwiretap7 libwscodecs1 libwsutil-dev libwsutil8 libxml2-dev libxml2-utils libyajl-dev libzookeeper-mt2 nfs-common numad python-certifi python-chardet python-jwt python-prettytable python-requests python-urllib3 radvd rpcbind sanlock sheepdog systemtap-sdt-dev xsltproc
./autogen.sh --system --with-qemu=yes
make -j4
make install
# https://wiki.archlinux.org/index.php/Libvirt#Using_polkit
sed -i 's/#unix_sock_ro_perms = "0777"/unix_sock_ro_perms = "0770"/g' /etc/libvirt/libvirtd.conf
sed -i 's/#unix_sock_rw_perms = "0770"/unix_sock_rw_perms = "0770"/g' /etc/libvirt/libvirtd.conf
sed -i 's/#auth_unix_ro = "none"/auth_unix_ro = "none"/g' /etc/libvirt/libvirtd.conf
sed -i 's/#auth_unix_r2 = "none"/auth_unix_ro = "none"/g' /etc/libvirt/libvirtd.conf
systemctl stop libvirtd.service
systemctl enable libvirtd.service
systemctl start libvirtd.service
# virt-manager - https://github.com/virt-manager/virt-manager/releases
version="1.5.0"
wget https://github.com/virt-manager/virt-manager/archive/v$version.zip
unzip v$version
cd virt-manager-$version
sudo apt-get build-dep virt-manager
./setup.py install
pip install libvirt-python
#reboot me here
sudo kvm-ok
}
function qemu_func() {
#Download code
echo '[+] Downloading QEMU source code'
# new version can be found here https://www.qemu.org/download/#source or https://download.qemu.org/
version=3.0.0
wget https://download.qemu.org/qemu-$version.tar.xz
tar xvJf qemu-$version.tar.xz
fail=0
# qemu deps
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
# WOOT
fail=0
if [ $? -eq 0 ]; then
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 "/"<WOOT> "/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
if [ $fail -eq 0 ]; then
echo '[+] Starting compile it'
# not make sense compile if was not patched
#apt-get source --compile qemu
cd 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
# for x32 and x64 only add --target-list=i386-softmmu,x86_64-softmmu,i386-linux-user,x86_64-linux-user
./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
if [ $? -eq 0 ]; then
echo '[+] Starting Install it'
# we need to clean old qemu packages if exists
dpkg -l | grep qemu | cut -d " " -f 3 | xargs dpkg --remove
make -j4 && checkinstall -D --pkgname=qemu-$version
if [ $? -eq 0 ]; then
echo '[+] failed make -j4 && checkinstall -D --pkgname=qemu-$version'
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 [ $? -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'
apt-get install git iasl
git clone git://git.seabios.org/seabios.git
if [ $? -eq 0 ]; then
cd seabios
sed -i 's/Bochs/<WOOT>/g' src/config.h
sed -i 's/BOCHSCPU/<WOOT>CPU/g' src/config.h
sed -i 's/BOCHS/<WOOT>/g' src/config.h
sed -i 's/BXPC/<WOOT>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'
cp /usr/share/qemu/bios.bin /usr/share/qemu/bios.bin_back
if [ $? == 0 ]; then
echo '[+] Original bios.bin file backuped to /usr/share/qemu/bios.bin_back'
cp out/bios.bin /usr/share/qemu/bios.bin
if [ $? -eq 0 ]; then
echo '[+] Patched bios.bin placed correctly'
else:
echo '[-] Bios patching failed'
fi
else
echo '[-] Bios backup failed'
fi
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
qemu_func
seabios_func
install_kvm
fi
if [ "$1" = 'QEMU' ]; then
qemu_func
fi
if [ "$1" = 'SeaBios' ]; then
seabios_func
fi
if [ "$1" = 'KVM' ]; then
install_kvm
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment