Three Quick And Easy Home Made Qemu KVM VPS Recipes! — Bake Them Yourself On Almost Any Linux Distribution!

Introduction


KVM logo


You easily can launch your own additional KVM VPSes on your own existing Linux machine!


Do you want to try a different Linux distribution? Or try a different setup of your favorite distribution?


How about installing and testing something complex that you’re unsure of while keeping your familiar, already installed system safe and unchanged?


Maybe, like many of us, you do not have extra IPs. You easily can run your extra KVM VPSes on your existing network connection.


Are you working on a laptop, desktop, VPS, or dedicated server with Linux already installed?


Can you type or cut-and-paste a few lines of code into your terminal?


If you want all the above benefits, please read more.



Let’s bake one or a dozen of our very own Linux VPSes inside our already existing Linux installation! This ought to work on any fairly recent version of almost any Linux distribution.


Slirp


Neither additional IP addresses nor any special networking setup is required because we’re using slirp. Slirp provides qemu’s built-in default networking system.


Slirp seems underappreciated! Two issues usually are mentioned. First, slirp networking is described as “slow.” Second, for unprivileged users, slirp does not support the ICMP protocol, and so ping doesn’t work.


To me, slirp’s speed has seemed fine for normal use. Ping is great to have, and most of us can get ping with slirp. Most of us do have root on our own machines and VPSes. And, as root, we can start our own VPSes for our own use on our own machines without being overly concerned about security.


Three Easy VPS Recipes With Slirp


Here are three quick VPS recipes. First comes the plain and simple VPS which can be made by any unprivileged user. We will call our unprivileged user “leb-vps”. Next we add our user to the kvm group, which allows pouring delicious KVM sauce on the basic VPS to make it faster. Finally, we start the VPS as root, so we get ping to work.


Plain And Simple VPS


The plain and simple recipe works for an unprivileged user without root. This VPS will not have KVM, so it will be slower. Also, ping will not work.


First, get an image:


For Debian sid:


root@debian:~# wget https://cloud.debian.org/images/cloud/sid/daily/latest/debian-sid-nocloud-amd64-daily.qcow2


leb-vps@debian:~$ wget https://cloud.debian.org/images/cloud/sid/daily/latest/debian-sid-nocloud-amd64-daily.qcow2


If you prefer Red Hat, Fedora rawhide images are available at https://openqa.fedoraproject.org/nightlies.html.


Next, as our unprivileged user (“leb-vps”), go inside tmux:


leb-vps@debian:~$ tmux


Then, launch the VPS:


leb-vps@debian:~$ qemu-system-x86_64 -nographic -m 1G -hda

> /home/leb-vps/debian-sid-nocloud-amd64-daily.qcow2


You will see lots of lines of boot message text scroll by. There might be a pause of a minute or a little more. Eventually, you will see a login prompt. The login is “root”. No password required.


As for memory size, I also tried setting the “-m” flag to 256M and 0.5G. Both booted successfully.


This slirp VPS has built-in default networking capability. It uses the same IPs as the host, but ICMP is unsupported, so ping doesn’t work:


root@localhost:~# ping -c 2 www.google.com

PING www.google.com (142.251.45.68) 56(84) bytes of data.


— www.google.com ping statistics —

2 packets transmitted, 0 received, 100% packet loss, time 1022ms


root@localhost:~# curl icanhazip.com

66.11.114.203

root@localhost:~# curl -6 icanhazip.com

2602:ffc5:105:40f::203

root@localhost:~#


VPS with Delicious KVM Sauce!


Making a VPS with delicious KVM sauce requires root privileges or at least membership in a kvm group. The kvm group exists by default on Debian sid:


root@debian:~# grep kvm /etc/group

kvm:x:110:

root@debian:~# ls -l /dev | grep kvm

crw-rw---- 1 root kvm 10, 232 May 19 20:33 kvm

crw-rw---- 1 root kvm 10, 238 May 19 20:33 vhost-net

crw-rw---- 1 root kvm 10, 241 May 19 20:33 vhost-vsock

root@debian:~#


Let’s add user leb-kvm to the kvm group:


root@debian:~# usermod -a -G kvm leb-vps

root@debian:~#


Next we become leb-vps:


root@debian:~# su - leb-vps


You will see that this VPS boots faster because it has KVM. But ping still doesn’t work yet. Here’s a little script for user leb-vps to use to start the vps.


Don’t forget to launch the script inside tmux.


leb-vps@debian:~$ cat start-slirp-kvm-vps.sh

#!/bin/bash

# Start qemu and kvm with user mode "slirp" networking.

# Please see http://www.linux-kvm.org/page/Networking

# Default login "root". No password needed.


qemu-system-x86_64

-nographic

-cpu host -enable-kvm

-m 1G

-hda /home/leb-vps/debian-sid-nocloud-amd64-daily.qcow2

leb-vps@debian:~$


As expected, ping does not work. But we do have network capability on our host system’s IP addresses.


root@localhost:~# ping -c 2 www.google.com

PING www.google.com (142.250.138.104) 56(84) bytes of data.


— www.google.com ping statistics —

2 packets transmitted, 0 received, 100% packet loss, time 1013ms


root@localhost:~# curl icanhazip.com

66.11.114.203

root@localhost:~# curl -6 icanhazip.com

2602:ffc5:105:40f::203

root@localhost:~#


As Root For Working Ping


Here’s a little script that root can use for starting a slirp KVM VPS with working ping:


root@debian:~# cat start-qemu-kvm-slirp.sh

#!/bin/bash

# Start qemu with user mode "slirp" networking.

# Please see http://www.linux-kvm.org/page/Networking

# Default login "root". No password needed.


qemu-system-x86_64

-nographic

-cpu host -enable-kvm

-m 1G

-hda /root/debian-sid-nocloud-amd64-daily.qcow2

root@debian:~#


To launch the VPS, start tmux, then run the script from inside tumx.


root@debian:~# tmux


root@debian:~# /root/start-qemu-kvm-slirp.sh


This time ICMP works:


root@localhost:~# ping -c 2 www.google.com

PING www.google.com (142.251.45.68) 56(84) bytes of data.

64 bytes from dfw25s48-in-f4.1e100.net (142.251.45.68): icmp_seq=1 ttl=255 time=s

64 bytes from dfw25s48-in-f4.1e100.net (142.251.45.68): icmp_seq=2 ttl=255 time=s


— www.google.com ping statistics —

2 packets transmitted, 2 received, 0% packet loss, time 1002ms

rtt min/avg/max/mdev = 2.251/2.352/2.453/0.101 ms

root@localhost:~# curl -4 icanhazip.com

66.11.114.203

root@localhost:~# curl -6 icanhazip.com

2602:ffc5:105:40f::203

root@localhost:~#


Hardware Support For Virtualization


If we have trouble making our VMs, one reason might be lack of hardware support for virtualization. Running the following script will tell us whether hardware support for virtualization is enabled or disabled.


root@debian:~# cat cpu-virt.sh

#!/bin/bash

# From https://github.com/masonr/yet-another-bench-script/blob/master/yabs.sh

# Lines 210-212

# Thanks to Mason and yabs developers!


CPU_VIRT=$(cat /proc/cpuinfo | grep ‘vmx|svm’)

[[ -z “$CPU_VIRT” ]] && CPU_VIRT=”xE2x9Dx8C Disabled” || CPU_VIRT=”xE2x9Cx94 Enabled”

echo -e “VM-x/AMD-V : $CPU_VIRT”

root@debian:~#


Installing Qemu


You might need to install tmux and qemu.


apt-get update && apt-get upgrade -y

apt-get install tmux qemu-system


Image Verification


The checksums for the Debian cloud images can be found at https://cloud.debian.org/images/cloud/sid/daily/latest/SHA512SUMS.


leb-vps@debian:~$ wget -q https://cloud.debian.org/images/cloud/sid/daily/latest/SHA512SUMS

leb-vps@debian:~$ sha512sum --ignore-missing -c SHA512SUMS

debian-sid-nocloud-amd64-daily.qcow2: OK

leb-vps@debian:~$


Please verifiy prior to booting and running an image since booting and running an image changes it.


How To Quit


To exit tmux while leaving the VPS running, type “Ctrl-b” then “d”.

To re-enter tmux and the already running VPS:


tmux attach


To stop the VPS and quit tmux:


shutdown -h now

Type “exit” when the command prompt returns following VPS shutdown.


To return to root after using su - leb-vps to become an unprivileged user, type “exit”.


Additional Resources


A really helpful resource for me when I was first trying KVM tricks was Linux-KVM.org’s Network Configuration page.


Maybe the world’s most thorough qemu tutorial is from Dongli Zhang.


Conclusion


We have available three ways to use qemu’s default networking tool, slirp. We can make user level qemu virtual machines without KVM, KVM group enabled user level machines with KVM, and root launched KVM servers with working ICMP.


The post Three Quick And Easy Home Made Qemu KVM VPS Recipes! — Bake Them Yourself On Almost Any Linux Distribution! appeared first on LowEndBox.