Lampros - Weird Bricks

Creating a FreeBSD 10 guest in KVM on a CentOS 7 Host

21 September, 2014 | KVM

Continuing from my last post on installing KVM on CentOS 7, we'll look into installing a non-linux Operating System on CentOS 7 inside KVM. In this example I'll install FreeBSD 10.

Before we install any guest we need to get the list of supported guests - to get that list type:

virt-install --os-variant list

The output is too long to include here, but in the output we can see: freebsd8 which is what we're going to use.

Since I've assigned most HDD space under /home, I'll add a Linux user first and add him to the wheel group so he can be a sudoer and use that space:

useradd lampros -G wheel

In order to make the user a sudoer, we'll run visudo and comment out this line:

%wheel ALL=(ALL)       NOPASSWD: ALL

Make sure this line is commented out:

##%wheel        ALL=(ALL)       ALL

Check with:

grep wheel /etc/sudoers

Your output should look like this:

## Allows people in group wheel to run all commands
##%wheel        ALL=(ALL)       ALL
 %wheel ALL=(ALL)       NOPASSWD: ALL

Now become that user:

su - lampros

Now we're going to need the ISO for the OS - download it with curl: (It's 622MB so it might take a while!)

curl -O ftp://ftp.freebsd.org/pub/FreeBSD/releases/amd64/amd64/ISO-IMAGES/10.0/FreeBSD-10.0-RELEASE-amd64-disc1.iso

Note that we downloaded the AMD64 version if FreeBSD - that will work since our host supports the VT extensions.

Now I save the following script to /home/lampros/create-kvm.sh

#!/bin/sh

NAME=
MEMORYMB=
NUMCORES=
OSVARIANT=
ISOFILENAME=
#if you've followed my previous post NIC needs to be bridge0
NIC=bridge0 
DISKFILENAME=/home/lampros/freebsd10.img
DISKSIZEGB=30

virt-install -n $NAME -r $MEMORYMB --vcpus=$NUMCORES --os-variant=$OSVARIANT --accelerate -v -c $ISOFILENAME -w bridge:$NIC --vnc --disk path=$DISKFILENAME,size=$DISKSIZEGB

After I fill in the variables with the options I need (amount of memory, number of cores, ISO filename and OS variant) the completed script now looks like this:

#!/bin/sh

NAME=freebsd
MEMORYMB=512
NUMCORES=1
OSVARIANT=freebsd8
ISOFILENAME=/home/lampros/FreeBSD-10.0-RELEASE-amd64-disc1.iso
#if you've followed my previous post NIC needs to be bridge0
NIC=bridge0
DISKFILENAME=/home/lampros/freebsd10.img
DISKSIZEGB=30

virt-install -n $NAME -r $MEMORYMB --vcpus=$NUMCORES --os-variant=$OSVARIANT --accelerate -v -c $ISOFILENAME -w bridge:$NIC --vnc --disk path=$DISKFILENAME,size=$DISKSIZEGB

Make the script executable:

chmod +x /home/lampros/create-kvm.sh

Run it with:

./create-kvm.sh

I got an error:

./create-kvm.sh

Starting install...
Creating storage file freebsd10.img                                          |  30 GB  00:00:00
ERROR    failed to retrieve file descriptor for interface: Permission denied
Domain installation does not appear to have been successful.
If it was, you can restart your domain by running:
  virsh --connect qemu:///session start freebsd
otherwise, please restart your installation.

If you get the above check if SELinux is Enforcing or Permissive - check with the getenforce command:

getenforce

The output I got is:

Enforcing

Set to permissive:

sudo setenforce Permissive

Change it permanently by editing the /etc/selinux/config file and making sure that the line:

SELINUX=enforcing

is changed to:

SELINUX=permissive

you can do this by running:

sudo sed -i 's/SELINUX=enforcing/SELINUX=permissive/' /etc/selinux/config

Then you also need to edit the qemu.conf file, this should be under: /etc/libvirt/qemu.conf

#clear_emulator_capabilities = 1

change to:

clear_emulator_capabilities = 0
#user = "root"

change to:

user = "root"
#group = "root"

change to:

group = "root"

then uncomment this entire block:

#cgroup_device_acl = [
#    "/dev/null", "/dev/full", "/dev/zero",
#    "/dev/random", "/dev/urandom",
#    "/dev/ptmx", "/dev/kvm", "/dev/kqemu",
#    "/dev/rtc","/dev/hpet", "/dev/vfio/vfio"
#]

should now look like:

cgroup_device_acl = [
    "/dev/null", "/dev/full", "/dev/zero",
    "/dev/random", "/dev/urandom",
    "/dev/ptmx", "/dev/kvm", "/dev/kqemu",
    "/dev/rtc","/dev/hpet", "/dev/vfio/vfio"
]

Make sure to exit and save Then restart the libvirtd service so that the changes take effect:

sudo service libvirtd restart

Run the script again:

sudo ./create-kvm.sh

and you should get:

Starting install...
Creating domain...                                                          |    0 B  00:00:00
WARNING  Unable to connect to graphical console: virt-viewer not installed. Please install the 'virt-viewer' package.
Domain installation still in progress. You can reconnect to
the console to complete the installation process.

KVM automatically starts up a VNC session - if you want to connect to it you'll need a tunnel as it's only listening to local (127.0.0.1) connections:

sudo netstat -ntlp | grep qemu-kvm

The output I got was:

tcp        0      0 127.0.0.1:5900          0.0.0.0:*               LISTEN      8720/qemu-kvm

View active VMs:

sudo virsh list --all

In my case I get this output:

sudo virsh list --all
 Id    Name                           State
----------------------------------------------------
 2     freebsd                        running

Destroy/stop a VM: (does not delete any files!)

sudo virsh destroy 2

Delete a VM: (permanently deletes files!)

sudo virsh undefine freebsd

To connect from Windows Using VNC you'll first need to establish an SSH tunnel - I do this with cygwin like this:

ssh -f root@192.168.2.100 -L 5900:127.0.0.1:5900 -N

To check if the port is open, on the Windows side you can check with: (Note, this will not work in cygwin, but it will in Windows Command shell or PowerShell)

netstat -na | find "5900"

The output I got was:

 TCP    127.0.0.1:5900         0.0.0.0:0              LISTENING
 TCP    [::1]:5900             [::]:0                 LISTENING

Now on Windows connect using something like TightVNC - since we're using an SSH tunnel set the remote host to 127.0.0.1

Now go ahead with a regular FreeBSD installation!

Sample output from VNC from Windows using TightVNC:

Once the installation is done the system will be shut down - check it's state with:

sudo virsh list --all

You should get something like:

Id    Name                           State
----------------------------------------------------
 -     freebsd                        shut off

Start it up:

suvo virsh start freebsd

Check that it started:

sudo virsh list --all
 Id    Name                           State
----------------------------------------------------
 3     freebsd                        running

That covers the basics of installing a new operating system inside KVM.

LINKS/REFERENCES:

Cyberciti - KVM virt-install: Install FreeBSD / CentOS As Guest Operating System

Cyberciti - KVM: Starting / Stopping Guest Operating Systems With virsh Command

RedHat - B.12. Guest is unable to start with error: warning: could not open /dev/net/tun

Ubuntu Forums - How do I delete a kvm guest?

TechRepublic - List open ports and listening services