Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

Creating Virtual Machines For Xen, KVM, VMware Workstation 6, and VMware Server With vmbuilder On Ubuntu 8.10


vmbuilder is a tool (introduced on Ubuntu 8.10) that allows you to build virtual machines (with Ubuntu as the OS) for multiple virtualization techniques. Currently it supports Xen, KVM, VMware Workstation 6, and VMware Server. You can afterwards copy the virtual machines to another system (a Xen, KVM, VMware Workstation 6, or VMware Server host) and run them there.

I do not issue any guarantee that this will work for you!

 

1 Preliminary Note

I’m using a machine with the hostname server1.example.com and the IP address 192.168.0.100 here as my host system.

Because we will run all the steps from this tutorial with root privileges, we can either prepend all commands in this tutorial with the string sudo, or we become root right now by typing

sudo su

 

2 Installing vmbuilder

vmbuilder can be installed as follows:

apt-get install python-vm-builder

 

3 Installing apt-proxy

Whenever vmbuilder builds a new VM, it has to download all packages from an Ubuntu mirror which can take quite some time. To speed this up, we install apt-proxy

apt-get install apt-proxy

… to cache the downloaded packages so that subsequent VM installations will be a lot faster.

Now open /etc/apt-proxy/apt-proxy-v2.conf

vi /etc/apt-proxy/apt-proxy-v2.conf

… and replace the default Ubuntu mirror with a mirror close to you (e.g. http://de.archive.ubuntu.com/ubuntu if you are in Germany):

[...]
[ubuntu]
;; Ubuntu archive
backends = http://de.archive.ubuntu.com/ubuntu
min_refresh_delay = 15m
[...]

Then we restart apt-proxy:

/etc/init.d/apt-proxy restart

apt-proxy listens on port 9999, so we can pass our local apt-proxy “mirror” as an argument to the vmbuilder script.

 

4 Using vmbuilder

Take a look at the following commands to learn how to use vmbuilder:

vmbuilder xen ubuntu –help

vmbuilder kvm ubuntu –help

vmbuilder vmw6 ubuntu –help

vmbuilder vmserver ubuntu –help

I will now create four virtual machines with the name vm7, one for Xen, one for KVM, one for VMware Workstation 6, and one for VMware Server. I’m assuming that you are using bridging for your virtual machines.

vmbuilder uses a template to create virtual machines – this template is located in the /etc/vmbuilder/libvirt/ directory. Because we must modify the template, we create a copy and modify that one:

mkdir -p ~/vm7/mytemplates/libvirt
cp /etc/vmbuilder/libvirt/* ~/vm7/mytemplates/libvirt/

Now we open ~/vm7/mytemplates/libvirt/libvirtxml.tmpl

vi ~/vm7/mytemplates/libvirt/libvirtxml.tmpl

… and change the network section from

[...]
    <interface type='network'>
      <source network='default'/>
    </interface>
[...]

to

[...]
    <interface type='bridge'>
      <source bridge='br0'/>
    </interface>
[...]

because we want the VMs to use bridging (if you use NAT instead of bridging, then don’t modify the template!).

Now we come to the partitioning of our VM. We create a file called vmbuilder.partition

vi ~/vm7/vmbuilder.partition

… and define the desired partitions as follows:

root 8000
swap 4000
/var 20000

This defines a root partition (/) with a size of 8000MB, a swap partition of 4000MB, and a /var partition of 20000MB. Of course, you are free to define whatever partitions you like (as long as you also define root and swap).

I want to install openssh-server in the VM. To make sure that each VM gets a unique OpenSSH key, we cannot install openssh-server when we create the VM. Therefore we create a script called boot.sh that will be executed when the VM is booted for the first time. It will install openssh-server (with a unique key) and also force the user (I will use the default username administrator for my VMs together with the default password Kreationnext) to change the password when he logs in for the first time:

vi ~/vm7/boot.sh

# This script will run the first time the virtual machine boots
# It is ran as root.

# Expire the user account
passwd -e administrator

# Install openssh-server
apt-get update
apt-get install -qqy --force-yes openssh-server

Make sure you replace the username administrator with your default login name.

(You can find more about this here: https://help.ubuntu.com/community/JeOSVMBuilder#First%20boot)

(You can also define a “first login” script as described here: https://help.ubuntu.com/community/JeOSVMBuilder#First%20login)

4.1 Create A Virtual Machine For KVM

To create a virtual machine for KVM, we could run the following command:

cd ~/vm7/
vmbuilder kvm ubuntu –suite=intrepid –flavour=virtual –arch=i386 –mirror=http://192.168.0.100:9999/ubuntu -o –tmpfs=- –libvirt=qemu:///system –ip=192.168.0.107 –part=vmbuilder.partition –templates=mytemplates –user=administrator –name=Administrator –pass=Kreationnext –addpkg=vim-nox –addpkg=unattended-upgrades –addpkg=acpid –firstboot=boot.sh –mem=256 –hostname=vm7

Most of the options are self-explanatory. –part specifies the file with the partitioning details, relative to our working directory (that’s why we had to go to our VM directory before running vmbuilder), –templates specifies the directory that holds the template file (again relative to our working directory), and –firstboot specifies the firstboot script. –libvirt=qemu:///system tells KVM to add this VM to the list of available virtual machines. –addpkg allows you to specify Ubuntu packages that you want to have installed during the VM creation (see above why you shouldn’t add openssh-server to that list and use the firstboot script instead).

In the –mirror line I have specified my local apt-proxy mirror (http://192.168.0.100:9999/ubuntu) – I have used my publically accessible IP address instead of localhost or 127.0.0.1 because this mirror will be used in the VM’s /etc/apt/sources.list file as well, and of course, the VM won’t be able to connect to 127.0.0.1 on the host. Of course, you can as well specify an official Ubuntu repository in –mirror, e.g. http://de.archive.ubuntu.com/ubuntu. If you leave out –mirror, then the default Ubuntu repository (http://archive.ubuntu.com/ubuntu) will be used. Don’t specify an apt-proxy mirror if you intend to transfer the VM to a host that has no access to the apt-proxy mirror (e.g. because it’s in a different network) – as I’ve already mentioned, the VM will use the mirror in its /etc/apt/sources.list that you specify for the vmbuilder command.

This will create an ubuntu-kvm/ subdirectory within the ~/vm7 directory that contains the disk image of the virtual machine:

ls -l ubuntu-kvm/

root@server1:~/vm7# ls -l ubuntu-kvm/
total 369184
-rw-r–r– 1 root root 377663488 2008-12-11 14:18 disk0.qcow2
-rwx—r-x 1 root root        49 2008-12-11 14:18 run.sh
root@server1:~/vm7#

The configuration file of the VM is located in the /etc/libvirt/qemu directory (/etc/libvirt/qemu/vm7.xml):

ls -l /etc/libvirt/qemu

root@server1:~/vm7# ls -l /etc/libvirt/qemu
total 32
drwxr-xr-x 3 root root 4096 2008-12-10 15:26 networks
-rw——- 1 root root  963 2008-12-10 16:29 vm1.xml
-rw——- 1 root root  959 2008-12-10 19:10 vm2.xml
-rw——- 1 root root  799 2008-12-10 18:25 vm3.xml
-rw——- 1 root root  799 2008-12-10 18:39 vm4.xml
-rw——- 1 root root  799 2008-12-10 19:25 vm5.xml
-rw——- 1 root root  805 2008-12-11 14:06 vm6.xml
-rw——- 1 root root  818 2008-12-11 14:54 vm7.xml
root@server1:~/vm7#

If you want to transfer the VM to another host, copy the ubuntu-kvm directory and the VM’s configuration file to the new host (e.g. using scp).

 

4.2 Create A Virtual Machine For Xen

The command to create a Xen VM is similar to the KVM command:

cd ~/vm7/
vmbuilder xen ubuntu –suite=intrepid –flavour=virtual –arch=i386 –mirror=http://192.168.0.100:9999/ubuntu -o –tmpfs=- –ip=192.168.0.107 –part=vmbuilder.partition –templates=mytemplates –user=administrator –name=Administrator –pass=Kreationnext –addpkg=vim-nox –addpkg=unattended-upgrades –addpkg=acpid –firstboot=boot.sh –mem=256 –hostname=vm7

This will create an ubuntu-xen/ subdirectory within the ~/vm7 directory that contains the disk image of the virtual machine and its Xen configuration file:

ls -l ubuntu-xen/

root@server1:~/vm7# ls -l ubuntu-xen/
total 303748
-rw-r–r– 1 root root 20971520000 2008-12-11 14:24 ____.img
-rw-r–r– 1 root root  8388608000 2008-12-11 14:23 root.img
-rw-r–r– 1 root root  4194304000 2008-12-11 14:23 swap.img
-rw-r–r– 1 root root         526 2008-12-11 14:24 xen.conf
root@server1:~/vm7#

If you want to transfer the VM to another host, copy the ubuntu-xen directory to the new host (e.g. using scp). You should place the Xen configuration file in the /etc/xen directory.

4.3 Create A Virtual Machine For VMware Workstation 6

The command to create a VM for VMware Workstation 6 is similar to the KVM command:

cd ~/vm7/
vmbuilder vmw6 ubuntu –suite=intrepid –flavour=virtual –arch=i386 –mirror=http://192.168.0.100:9999/ubuntu -o –tmpfs=- –ip=192.168.0.107 –part=vmbuilder.partition –templates=mytemplates –user=administrator –name=Administrator –pass=Kreationnext –addpkg=vim-nox –addpkg=unattended-upgrades –addpkg=acpid –firstboot=boot.sh –mem=256 –hostname=vm7

This will create an ubuntu-vmw6/ subdirectory within the ~/vm7 directory that contains the disk image of the virtual machine and its configuration file:

ls -l ubuntu-vmw6/

root@server1:~/vm7# ls -l ubuntu-vmw6/
total 368884
-rw-r–r– 1 root root 402325504 2008-12-11 14:27 disk0.vmdk
-rwx—r-x 1 root root       527 2008-12-11 14:27 vm7.vmx
root@server1:~/vm7#

If you want to transfer the VM to another host, copy the ubuntu-vmw6 directory to the new host (e.g. using scp).

 

4.4 Create A Virtual Machine For VMware Server

The command to create a VM for VMware Server is similar to the KVM command:

cd ~/vm7/
vmbuilder vmserver ubuntu –suite=intrepid –flavour=virtual –arch=i386 –mirror=http://192.168.0.100:9999/ubuntu -o –tmpfs=- –ip=192.168.0.107 –part=vmbuilder.partition –templates=mytemplates –user=administrator –name=Administrator –pass=Kreationnext –addpkg=vim-nox –addpkg=unattended-upgrades –addpkg=acpid –firstboot=boot.sh –mem=256 –hostname=vm7

This will create an ubuntu-vmserver/ subdirectory within the ~/vm7 directory that contains the disk image of the virtual machine and its configuration file:

ls -l ubuntu-vmserver/

root@server1:~/vm7# ls -l ubuntu-vmserver/
total 368828
-rw-r–r– 1 root root 402128896 2008-12-11 14:31 disk0.vmdk
-rwx—r-x 1 root root       531 2008-12-11 14:31 vm7.vmx
root@server1:~/vm7#

If you want to transfer the VM to another host, copy the ubuntu-vmserver directory to the new host (e.g. using scp).

 

  • vmbuilder: https://help.ubuntu.com/community/JeOSVMBuilder
  • JeOS and vmbuilder: http://doc.ubuntu.com/ubuntu/serverguide/C/jeos-and-vmbuilder.html
  • Ubuntu: http://www.ubuntu.com/

Comments

comments