Getting started with Xen - setting up virtual machines

At work we had a spare dev machine which we wanted to set up to run a Gitlab runner to run our Continuous Integration (CI) for our repositories. Rather than use the whole machine, we decided to create a VM to run it - allowing it its own little space.

Virtual Machines are exactly just that. Programs running inside a VM have no idea they are in a VM - they think they are in a physical machine. Many "cloud" servers today are VMs hosted on mega servers.

We previously had this set up with Vagrant, but that was using a lot of processing power and space for something no-one at work understood. We have an existing Xen server setup, so I took it upon myself to reconfigure the server with this.

These instructions are for Debian, and they worked for me. It is recommended you know command line and at least a little about command line and servers

Install Xen #

Xen play nicely with Vagrant (as they do the same thing), so if you are replacing Vagrant with Xen, ensure you have the data from your Vagrant Virtual Machines backed up. Vagrant won't boot once Xen is installed.

All of the instructions are on the Debian Wiki

$ apt-get install xen-system

Note: Ensure you follow the instructions under the Networking heading on the Wiki page. A Bridge is required for the VMs to connect to the internet - this details setting it up.

Use Xen #

There are plenty of comments for Xen, which all need to be run as root. These can be seen with xl help.

Create a VM #

To create a new image, you use the xen-create-image command. It can take several arguments, but a common command to create a VM with 8gb "hard-drive" and 1gb RAM would be the following:

$ xen-create-image --hostname=mikevm --dhcp --size=8G --memory=1G

Here I have made a vm with the imaginative name of mikevm - keep a note of what you put here as it is used in the following commands.

A recommended additional parameter is the --password= one - this allows you to set a root password for accessing the machine. If you omit one, it will be output on the command line once the machine has been created

This command makes a file in /etc/xen/mikevm.cfg - feel free to have a look at this file to see what is in it.

Boot a VM #

Once your VM has been created, you can boot it up with:

$ xl create /etc/xen/mikevm.cfg

This now creates the VM and brings it "online". This can be verified by using xl list to see which VMs are running

Access the VM #

By default, the latest Debian image comes with one user - root. root has remote access disabled, so you will need to access the VM from the host machine and create a new user for yourself.

$ xl console mikevm

This will replace your current terminal with that of the VM. You can now use it as though you were accessing the computer. To exit the terminal, use

ctrl + ]

The login is root and either the autogenerated password or one you specified in the command.

Shutdown the VM #

Shutting down, rebooting or destroying the VM follows a similar pattern to the commands above. Replace shutdown in the command below with reboot or destroy as required:

$ xl shutdown mikevm

Once your VM is up and running, you may wish to set it up as a server or for something else.

Mike Street

Written by Mike Street

Mike is a front-end developer from Brighton, UK. He spends his time writing, cycling and coding. You can find Mike on Twitter.