# Virtual Machines

Virtual machines or VMs proive a way to deploy software fast, easy and reliable (such as the geneious server or Microsoft-Office).

## Connect to a VM

### Connect to a Windows VM using spice-client on Debian

The [spice-client](https://packages.debian.org/jessie/spice-client/) available in the Debian repos, provides a way to connect to a virtual machine running Windows with a graphical interface. This allows the user to use Software like the Microsoft Office Suite. The software spice-gues-tools needs to be installed on Windows first. To do so connect via VNC the first time you start the VM. Then install [these tools](https://www.spice-space.org/download/windows/spice-guest-tools/spice-guest-tools-0.100.exe).

Then you can install the spice-client on any Debian machine using:

```
sudo aptitude install spice-client
```

You invoke the spice-client with the command `spicec`, additionally you have to provide an IP, a port and the password to connect to the VM. `spicec -h $server_IP -p $port -w $password`

A window will pop up you can control it with the following hot-keys:

| Key combination | Effect                            |
| --------------- | --------------------------------- |
| shift + F12     | Releases the catched mouse cursor |
| shift + F11     | Toggle Fullscreen                 |

If your VM is not running and spicec couldn't connect, start htop and search (F4) for qemu and look at the remaining processes and try to identify your VM. If your desired VM is not running jump to vm startup.

### Connect to a Linux VM using ssh

When starting the Linux vm with qemu you have to add a ssh-port forwarding rule with the parameter: `net user,hostfwd`. For example you add: `hostfwd=tcp::15000-:22` then you can connect to the vm with: `ssh user@IP -p 15000` See [the table](https://majuss.gitbooks.io/ecoevolpara/Documentation/vms.html#vm_table) for details.

### Connect to a Linux VM using a VNC-client (GUI)

Install a vnc client on your local machine for example xtightvncviewer (`aptitude install xtightvncviewer`).

## VM startup

To start a VM you should save the starting command somewhere (bash\_history is fine though). If you don't have the starting command you need to get it from the admin. If you have no access to the start command (look through the bash\_hiostory of the admin) you have to rebuild it. Look into next chapter for a starting command. A typical one for a simple Windows VM would be:

```
qemu-system-x86_64 -nodefconfig -machine accel=kvm -enable-kvm -m 4000M -k de -cpu host -smp cores=2,threads=1,sockets=1 -vga qxl -vnc :0 -hda $PATH_TO_QCOW2_IMAGE -net nic,model=e1000 -net user -spice port=$SPICE-PORT(something above 15000),addr=$SERVER_IP,password=$RANDOM_PASSPHRASE
```

For a detailed description of the startup parameter see the table beneath. When you got the starting-command start it with `sudo` inside a screen.

## VM-creation

### Create a VM using QEMU

QEMU is a virtualisation software which enables users to create virtual machines with different parameters.

You need to install QEMU: `sudo aptitude install qemu`.

and download an .iso file of an operating System. Thats all you need to get you started.

The usual QEMU-command we’ll use is `qemu-system-x86_64` (there are a lot of different one for other CPU-architectures).

A typical command to start a Windows-vm looks like that:

```
qemu-system-x86_64 -nodefconfig -machine accel=kvm -enable-kvm -m 2048M -k de -cpu host -smp cores=1,threads=1,sockets=1 -vga qxl -vnc :0 -hda /data/VMs/jessie.qcow2 -net nic,model=e1000 -net user,hostfwd=tcp::27001-:27001,hostfwd=tcp::3306-:3306,hostfwd=tcp::$vms_ssh_port-:22,hostfwd=tcp::49630-:49630 -spice port=15300,addr=$server_IP,password=$RND_PASS
```

|  Command                       | Meaning                                                                                                                                                    |
| ------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `nodefconfig`                  | No default configuration                                                                                                                                   |
| `machine accel=kvm`            | Sets the virtualization technology to KVM                                                                                                                  |
| `enable-kvm`                   | Enables the kvm acceleration                                                                                                                               |
| `m 2048M`                      | Amount of RAM you want to allocate to the VM                                                                                                               |
| `k de`                         | Keyboard layout                                                                                                                                            |
| `cpu host`                     | Passes the host-CPU identifier to the VM                                                                                                                   |
| `smp`                          | Enables smp (multicore processing)                                                                                                                         |
| `cores=1`                      | Number of CPU cores to allocate (never exceed the hosts physical core count)                                                                               |
| `vga qxl`                      | Graphics passthrough protocol (use `std` for setup)                                                                                                        |
| `vnc :0`                       | VNC port -> 5900                                                                                                                                           |
|                                |                                                                                                                                                            |
| `hda /home/horst/debian.qcow2` | Path to virtual disk                                                                                                                                       |
| `net nic,model=e1000`          | Model of virtualized networkadapter, usual e1000 for intel and `virtio` for fullspeed                                                                      |
| `net user,hostfwd`             | Forwarding ports from host to guest pe.: `hostfwd=tcp::15000-:22` the ssh port of the vm is now reacheable through the port 15000 (`ssh user@IP -p 15000`) |

\### Create a new Debian-stable VM

Connect to Beagle (or a different machine which should host the vm) and download the Debian-iso \[from this site]. Right click the link for the amd64 netinstall and copy the link. Then enter `wget`. paste the link and hit enter. This will download the install-image into the current working directory (`pwd`).

To create a virtual disk for the new vm type:

```
qemu-img create -f qcow2 /home/marius/debian_testing.qcow2 50G
```

To start the vm simply type the command:

```
qemu-system-x86_64 -nodefconfig -machine accel=kvm -enable-kvm -m 4000M -k de -cpu host -smp cores=4 -vga std -vnc :1 -hda /home/marius/debian_testing.qcow2 -cdrom /home/marius/debian-8.6.0-amd64-netinst.iso -net nic,model=virtio -net user,hostfwd=tcp::15351-:22
```

Connect with a VNC client for example xtightvncviewer. Open xtightvncviewer via terminal and enter the IP of the host and the VNC-port (`141...:5901`).

Then just follow this video how to setup the operating system.

[![Debian installation on YouTube](https://img.youtube.com/vi/PSH3hcGrqWw/0.jpg)](https://www.youtube.com/watch?v=PSH3hcGrqWw)

### Create a new Windows VM

Download the legal and free Windows 10 .iso from [this website](https://www.microsoft.com/de-de/evalcenter/evaluate-windows-10-enterprise). You have to enter some credentials then the download will start. You have to download the enterprise edition, because only this version will get activated by the HUs key management server.

Create a new virtual disk for the Windows-VM:

```
qemu-img create -f qcow2 Windows10_testing.qcow2 50G
```

Start the vm with the following command:

```
qemu-system-x86_64 -nodefconfig -machine accel=kvm -enable-kvm -m 4000M -k de -cpu host -smp cores=4 -vga std -vnc :1 -hda /home/marius/Windows10_testing.qcow2 -cdrom Path/To/Windows.iso -net nic,model=e1000 -net user -usbdevice tablet
```

Connect to the VM with a VNC-client. Then install the [spice-guest-tools](https://www.spice-space.org/download/windows/spice-guest-tools/spice-guest-tools-latest.exe) on the Windows VM.

After that you have to forward a spice port to the VM.

\[from this site]: [https://www.debian.org/CD/http-ftp/](https://www.debian.org/CD/http-ftp/)
