Skip to content

Instantly share code, notes, and snippets.

@mrrooijen
Last active February 11, 2023 19:44
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save mrrooijen/3354956 to your computer and use it in GitHub Desktop.
Save mrrooijen/3354956 to your computer and use it in GitHub Desktop.
Setting up XEN on a Hetzner Dedicated Server

Setting up XEN on a Hetzner Dedicated Server

Author: Michael van Rooijen (@mrrooijen)

DISCLAIMER: I am a programmer, not a sysadmin in my day-to-day life. I provide this guide simply as a self-reference, and as a way to contribute to the community of developers. The main motivation for writing this guide is because of the lack of properly written guides/tutorials. They were either out-dated, inaccurate, in a non-English language or simply too vague to understand (at least for me, as a programmer and not a sysadmin).

I hope this guide helps getting you up and running with your own collection of VPS's on your own Dedicated Server over at Hetzner.de.

Requirements:

  • Dedicated Server at Hetzner
  • At least 1 additional IP
    • You need 1 IP per "VPS".
    • 1 EUR/month per additional IP.
    • 4 IP limit per server.
    • Purchase flexipack and subnet IPs if you need more IPs.

References:

  • When I refer to the host, I am referring to the dedicated server itself (also knows as dom0).
  • I use the keywords VM, VPS and Guest interchangeably. These all refer to the same thing, the virtual machines you get at traditional VPS providers.

Coverage:

This guide covers the basics of setting up XEN from scratch on a Debian Squeeze (6) instance on a Hetzner dedicated server. After reading this guide you should be able to:

  • Create new VM's (aka VPS's)
  • Destroy existing VM's
  • Clone existing VM's
  • Reconfigure (or resize) existing VM's (in terms of memory and virtual cpus)
  • Make these VM's accessible through a public IP address

Purchasing an additional IP

Open the Robot Panel and log in. Go to the the Servers Page. Select the desired server and click the "IPs" tab and click on "Ordering of additional IP, failover IP or subnet".

Next, select the desired IP you wish to purchase. Either a regular IP or a Subnet IP will do. (Note that these will be provided to you only during office hours, as they have to be processed manually.)

Installing Debian Squeeze (6)

From the Robot Panel select the desired server and click the "Rescue" tab. Now select the desired OS to boot in (pick Linux) and set the architecture (pick 64 bit). Hit "Active Rescue System". You will receive a new root password, take note of it.

SSH in to your dedicated server and reboot the system:

reboot

Wait for the server to reboot (takes a minute or two) and SSH back in. You should now be in "Rescue Mode". Now, simply run the following command:

installimage

This will pop up an interactive menu where you can select your Linux distribution. We're using Debian Squeeze (64bit) minimal in this example. Review the configuration that will be shown in a moment, if all is well, Quit the editor and the installation will continue. It will prompt to format your harddisks. Choose Yes.

The installation will now begin, it'll take a few minutes in install.

Once done, change your root password if you like with:

passwd

Finally, reboot the server once more to boot the newly installed Debian Squeeze distribution.

Installing and Configuring XEN with Hetzner

SSH in to your dedicated server as root.

Update package lists, upgrade installed packages, and install XEN and it's utilities:

apt-get update && apt-get upgrade -y
apt-get install -y xen-hypervisor xen-utils xenstore-utils xenwatch xen-tools xen-linux-system-2.6-xen-amd64

Open /etc/modules and change loop to:

loop max_loop=64

Open /etc/sysctl.conf and ensure the following is in place:

net.ipv4.conf.all.rp_filter=1
net.ipv4.icmp_echo_ignore_broadcasts=1
net.ipv4.ip_forward=1
net.ipv6.conf.all.forwarding=1
net.ipv4.conf.default.proxy_arp=1

Open /etc/xen/xend-config.sxp and ensure these lines are commented out or not in the file:

(network-script network-bridge)
(vif-script vif-bridge)

And ensure these lines are set in /etc/xen/xend-config.sxp:

(network-script network-route)
(vif-script vif-route)

Open /etc/network/interfaces and comment out the up route add -net line, like so:

# up route add -net 188.40.103.64 netmask 255.255.255.192 gw 188.40.103.65 eth0

Open /etc/xen-tools/xen-tools.conf and modify it. My setup looks like this:

dir              = /home/xen
install-method   = debootstrap
size             = 20Gb           # Disk size
vcpus            = 4              # Virtual CPU quantity
memory           = 128Mb          # Memory size
swap             = 128Mb          # Swap size
fs               = ext3           # use the EXT3 filesystem for the disk image.
gateway          = 192.168.0.1    # replace with your host's ip (NOT gateway) [1]
netmask          = 255.255.255.0  # replace with your netmask                 [2]
broadcast        = 192.168.0.255  # replace with your broadcast               [3]
passwd           = 1
kernel           = /boot/vmlinuz-`uname -r`
initrd           = /boot/initrd.img-`uname -r`
arch             = amd64
dist             = lucid
image            = sparse
mirror           = http://archive.ubuntu.com/ubuntu
boot             = 1
serial_device    = hvc0
disk_device      = xvda
ext3_options     = noatime,nodiratime,errors=remount-ro
ext2_options     = noatime,nodiratime,errors=remount-ro
xfs_options      = defaults
reiserfs_options = defaults
btrfs_options    = defaults

This setup works for me, so I basically replace all the contents of the configuration file with this. But you should review the initially generated configuration yourself. It includes a lot of comments that explain a bunch of things that you may want to know, and it shows some of the available options for certain settings.

For example: By default it does a few things differently that I changed, such as boot = 1 is normally disabled, this will tell it to boot the VM as soon as I create the image. The mirror list was pointing at the Debian distro instead of the Ubuntu distro and the dist was set to squeeze which I changed to lucid. You might want to set the arch to i386 if you're running a 32bit architecture instead of a 64 architechture, etc. Make sure you do fill in the proper gateway, netmask, and broadcast since these will definitely differ from my machine.

  • [1] Find your host's (ip) address in /etc/network/interfaces. (Yes, fill in your host's IP in to the gateway)
  • [2] Find your netmask config in /etc/network/interfaces
  • [3] Find your broadcast config in /etc/network/interfaces

Ensure the /home/xen is available, XEN will store swap- and disks images in here for each "VPS".

mkdir /home/xen

Now reboot for all the changes thus far take effect:

reboot

SSH in again and run the following to check if xen installed properly:

uname -r

If it did, then you should see something like this: 2.6.32-5-xen-amd64 (notice xen in the name).

Now, try running xm dmesg, if you see errors like this:

WARNING!  Can't find hypervisor information in sysfs!
Error: Unable to connect to xend: No such file or directory. Is xend running?

Then run the following:

mv /etc/grub.d/10_linux /etc/grub.d/50_linux
update-grub2
reboot

SSH in again and run mx dmesg and now the error should be gone (you should now see a lot of output when running the command, if it works) and xend should be in your process list.

Creating and running XEN images

We are now ready to create and run some XEN images.

Run the following command to create a new XEN image:

xen-create-image --hostname=testvm --ip=<your additional ip>
  • Mandatory Options:

    • --hostname, the hostname to use, doesn't really matter, just think of it as an identifier.
    • --ip, the additional IP you purchased from the Hetzner Robot Panel.
  • Common Additional Options (These override the defaults specified in /etc/xen-tools/xen-tools.cfg):

    • --vcpus, the amount of virtual processing units to provide the VM with. (e.g. --vcpus=4)
    • --memory, the amount of memory the VM may consume. (e.g. --memory=4096Mb)
    • --swap, the amount of swap the VM may have. (e.g. --swap=1024Mb)
    • --size, the amount of disk storage for the VM. (e.g. --size=50Gb)
    • --arch, set it to amd64 if you're running 64bit architecture, i386 if 32bit. (e.g. --arch=amd64)

Consult man xen-create-image for more info.

This will generate:

  • a disk image in /home/xen/domains/testvm/disk.img.
  • a swap image in /home/xen/domains/testvm/swap.img.
  • a config file in /etc/xen/testvm.cfg.

If you've set boot = 1 in the /etc/xen-tools/xen-tools.conf then the VM should have booted up right after the images were created. Confirm that it's running by checking the VM list:

xm list

If it isn't running, you can manually boot this image with the following command:

xm create /etc/xen/testvm.cfg

Confirm it's booted, check the VM list:

xm list

If it's there it should be running. You can now long in via console:

xm console testvm

Or via SSH:

ssh root@your-vm-ip

For a full list of xm commands, simply run xm without any arguments.

If you wish to make modifications to your VM later, to, for example, change the memory or vcpus values, you can edit the /etc/xen/testvm.cfg config file and run xm shutdown testvm (wait for it to shut down, run xm list to see if it's gone). Then run xm create /etc/xen/testvm.cfg to create the VM again. Although this looks destructive, the actual disk/swap images (located in: /home/xen/domains/testvm/) are not destroyed. Only the running VM will be destroyed and removed from the managed XEN list (xm list). This means that when you create the VM again with xm create /etc/xen/testvm.cfg, all data will be loaded/available in to the new VM from the disk/swap images and your "VPS" data should still be there.

Note: Use xm shutdown testvm and not xm destroy testvm to cleanly shutdown a VM's OS. Otherwise data in the VM might get corrupted due to improper termination.

That's it. Now you can create as many images as you want with different hostnames and ips the same way you created this image.

Bonus: Cloning an image

Note: I am not 100% sure that this is the proper way of doing it, but I don't see anything wrong with it and have done it a few times now without fail. I couldn't find any built-in utilities to do this.

If you wish to clone a VPS, you could simply shutdown the VPS first with xm shutdown testvm, then make a copy of /home/xen/domains/testvm to for example /home/xen/domains/testvmclone. Then copy the original configuration file /etc/xen/testvm.cfg to /etc/xen/testvmclone.cfg and change all the testvm specific data to testvmclone (like the path to the disk/swap images, and the hostname and ip).

Now you can simply boot both VMs back up with:

xm create /etc/xen/testvm.cfg
xm create /etc/xen/testvmclone.cfg

Now you should have properly cloned the testvm disk/swap images. Confirm with xm list again.

@LaraEzarik
Copy link

This guide works upto a point you can ping and ssh from the host to the vm and you can ping and ssh from the vm to the host but the vm can't ping anything external or be pinged by external machine.

@netorb
Copy link

netorb commented Aug 8, 2013

Thank you Meskyanichi for this useful post. I too have an EX series server from Hetzner. A year ago, I had a friend set up VM for me on it. He used OpenBSD and since I'm a complete novice with servers, he installed Citrix Xenserver (free version) for me. I see that your tutorial allows me to control the VMs without recourse to Citrix, whose free version have limitations anyhow on what they allow one to do.

Please I have a couple of questions, because I'm in the process of formatting the HDD to redo this process using your guide.

  1. In your opinion, what are the advantages of using a VM, instead of simply using "Virtualhosts"?
  2. I have 4 different IPs from hetzner. If I use the "Virtualhost" option, will I be able to assign different IPs to such virtual hosts?

Many thanks!

@kourindouhime
Copy link

Thanks for manual bro, that worked for me

@jprayner
Copy link

Really useful guide - thanks!

@turboyz
Copy link

turboyz commented Jan 7, 2016

great job! Thanks for sharing Michael.
a couple of questions:

Did you experience lower perfomance having virtualized a machine instead of using directly the machine resources?
Do you think it would be OK to run an Ubuntu 14.04 distro with LEMP running symfonyII framework?
On this web app there is an average of 150K pageviews/month

Actually I fit very unconfortable without a disk cloneimage of my webapp... this means that in case of disaster recovery I must deploy everything as new... it takes more less 1.30 hour...

@sshine
Copy link

sshine commented Jul 29, 2021

I noticed that for Debian 10.10, the packages seem to be called

xen-hypervisor-4.11-amd64 xen-utils-4.11 xenstore-utils xenwatch xen-tools xen-system-amd64

That is, most of the packages got versioned, and the "system" package got shortened.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment