Skip to content

Instantly share code, notes, and snippets.

@aviatrix
Forked from mrrooijen/README.md
Last active November 26, 2017 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aviatrix/9ad87e2ce330cd70b8e63f4b62195c2b to your computer and use it in GitHub Desktop.
Save aviatrix/9ad87e2ce330cd70b8e63f4b62195c2b 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 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-4.8-amd64 xen-utils-4.8 xenstore-utils xenwatch xen-tools xen-linux-system-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)
(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 xl 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:

xl list

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

xl create /etc/xen/testvm.cfg

Confirm it's booted, check the VM list:

xl list

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

xl 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 xl shutdown testvm (wait for it to shut down, run xl list to see if it's gone). Then run xl 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 (xl list). This means that when you create the VM again with xl 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 xl shutdown testvm and not xl 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 xl 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:

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

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

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