The Perfect Xen Setup For Debian And Ubuntu - Page 5

Want to support HowtoForge? Become a subscriber!
 
Submitted by falko (Contact Author) (Forums) on Wed, 2005-11-09 12:49. ::

4.2 Create And Start The First Virtual Machine

Now we create our first virtual machine, vm01, by making a copy of our template:

cp -pf /vserver/images/vm_base.img /vserver/images/vm01.img
cp -pf /vserver/images/vm_base-swap.img /vserver/images/vm01-swap.img

Then we create a Xen configuration file for vm01, /etc/xen/vm01-config.sxp:

name ="vm01"
kernel ="/boot/vmlinuz-2.6.11.12-xenU"
root ="/dev/hda1"
memory =128
disk = ['file:/vserver/images/vm01.img,hda1,w','file:/vserver/images/vm01-swap.img,hda2,w']

# network
nics=1
dhcp ="off"
ip="192.168.0.101"
netmask="255.255.255.0"
gateway="192.168.0.1"
hostname="vm01.example.com"

extra="3"

In memory you specify the RAM you want to allocate to that virtual machine (here: 128 MB). In disk you specify which images to use and how to mount them (i.e., under which partition, e.g. hda1). This must correspond to the settings in the image's /etc/fstab file! In the network settings we tell vm01 that its IP address is 192.168.0.101 (the main machine's (dom0) IP address is 192.168.0.100), and what hostname it has.

If you want vm01 to start automatically at the next boot of the system, then do this:

ln -s /etc/xen/vm01-config.sxp /etc/xen/auto

Now let's start vm01:

xm create -c /etc/xen/vm01-config.sxp

If nothing's wrong, vm01 should come up without problems, and you should be able to login. If you installed Xen from the sources, by running iptables -L you should see that iptables is available on vm01. To leave vm01's shell, type CTRL+] if you are at the console, or CTRL+5 if you're using PuTTY. From the outside you should be able to connect to 192.168.0.101 via SSH.

Back on dom0's shell, you can shutdown vm01 by running

xm shutdown vm01

Here are some other Xen commands:

xm create -c /path/to/config - Start a virtual machine.
xm shutdown <name>
- Stop a virtual machine.
xm list
- List all running systems.
xm console <name>
- Login on a virtual machine.
xm help
- List of all commands.

Now you can reboot the main system to see if vm01 comes up automatically (if you created the symlink in /etc/xen/auto):

shutdown -r now

4.3 Creating And Customizing Further Virtual Machines

You can create further virtual machines simply by copying the image template:

cp -pf /vserver/images/vm_base.img /vserver/images/vm02.img
cp -pf /vserver/images/vm_base-swap.img /vserver/images/vm02-swap.img

Then you have to create a Xen configuration file, e.g. /etc/xen/vm02-config.sxp:

name ="vm02"
kernel ="/boot/vmlinuz-2.6.11.12-xenU"
root ="/dev/hda1"
memory =64
disk = ['file:/vserver/images/vm02.img,hda1,w','file:/vserver/images/vm02-swap.img,hda2,w']

# network
nics=1
dhcp ="off"
ip="192.168.0.102"
netmask="255.255.255.0"
gateway="192.168.0.1"
hostname="vm02.example.com"

extra="3"

Start the machine:

xm create -c /etc/xen/vm02-config.sxp

and create a symlink, if you want to start the virtual machine at boot time:

ln -s /etc/xen/vm02-config.sxp /etc/xen/auto

Now you can log into each machine, e.g. via SSH, and configure it as if it was a normal system.

You can create as many virtual machines as you like. Your hardware's the limit!


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Anonymous (not registered) on Sun, 2005-12-18 19:10.

I had to create the hda2 entry under /dev in order to have the swap (I use the Xen 3.0 version):

mknod --mode=666 /dev/hda2 b 3 2 (for example)

Anyway, thanks a lot for this wonderful work!!