The Perfect Xen 3.0.3 Setup For Debian Sarge - Page 8
5.2.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
In the binary Xen install we cannot specify our virtual machine's hostname and network configuration in a Xen configuration file, we must specify these details directly in the virtual machine. Therefore we must mount the image now and edit a few files:
mount -o loop /vserver/images/vm01.img /vserver/vm_base
chroot /vserver/vm_base
The hostname of our first virtual machine is vm01.example.com, therefore we do this:
echo "vm01.example.com" > /etc/hostname
Then we edit /etc/network/interfaces and put in our network configuration (IP address 192.168.0.101, gateway 192.168.0.1):
vi /etc/network/interfaces
auto lo iface lo inet loopback address 127.0.0.1 netmask 255.0.0.0 # The primary network interface auto eth0 iface eth0 inet static address 192.168.0.101 netmask 255.255.255.0 network 192.168.0.0 broadcast 192.168.0.255 gateway 192.168.0.1 |
Then we leave the chroot environment and unmount the image:
exit
umount /vserver/vm_base
Next we create a Xen configuration file for vm01, /etc/xen/vm01-config.sxp:
vi /etc/xen/vm01-config.sxp
name="vm01" kernel="/boot/vmlinuz-2.6-xen" root="/dev/hda1" memory=32 disk=['file:/vserver/images/vm01.img,hda1,w','file:/vserver/images/vm01-swap.img,hda2,w'] # network vif=[ '' ] extra="3" |
In memory you specify the RAM you want to allocate to that virtual machine (here: 32 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!
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. 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 destroy <name> - Stop a virtual machine immediately without shutting it down. It's as if you switch off the power button.
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
5.2.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
Again, we must specify our network configuration like this:
mount -o loop /vserver/images/vm02.img /vserver/vm_base
chroot /vserver/vm_base
Now our hostname is vm02.example.com for example, therefore we do this:
echo "vm02.example.com" > /etc/hostname
Then we edit /etc/network/interfaces and put in our network configuration (e.g. IP address 192.168.0.102, gateway 192.168.0.1):
vi /etc/network/interfaces
auto lo iface lo inet loopback address 127.0.0.1 netmask 255.0.0.0 # The primary network interface auto eth0 iface eth0 inet static address 192.168.0.102 netmask 255.255.255.0 network 192.168.0.0 broadcast 192.168.0.255 gateway 192.168.0.1 |
Then we leave the chroot environment and unmount the image:
exit
umount /vserver/vm_base
Then you have to create a Xen configuration file, e.g. /etc/xen/vm02-config.sxp:
vi /etc/xen/vm02-config.sxp
name="vm02" kernel="/boot/vmlinuz-2.6-xen" root="/dev/hda1" memory=32 disk=['file:/vserver/images/vm02.img,hda1,w','file:/vserver/images/vm02-swap.img,hda2,w'] # network vif=[ '' ] extra="3" |
Start the machine:
xm create -c /etc/xen/vm02-config.sxp
If you get an error like this:
Using config file "/etc/xen/vm02-config.sxp".
Error: Error creating domain: The privileged domain did not balloon!
then this means that the virtual machine tried to use more memory than is available. Edit the configuration file of the virtual machine and decrease the value of memory and try to start it again.
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!
6 Links
- Xen: http://www.xensource.com/xen/
- Debian: http://www.debian.org/