Building a KVM Virtual Machine is easy in Ubuntu. Ubuntu has a prepackaged Virtual Machine building application available to it. This application is a script that executes "libvirtd" and allows for the easy creation of virtual machines. In this article I will be describing how to build a VM after the KVM system has been setup and then how to build VM's fast and efficiently without the need to remember huge strings of code.
The beauty of KVM is that almost all Operating Systems have the ability to be run under KVM. KVM is a production ready powerful Virtual Machine building appliance, that is free. Now the big players, VMware, VirtualBox, and ZEN, are all good Hyper-visors but for those of you who don't have thousands of dollars to setup a VMware server farm, or the time to workout all of the bugs in VirtualBox, KVM is a great solution. With KVM you are free from big brother and you can stay FOSS (Full Open Source Software).
Building a KVM Machine
Building is easy. There is no fancy GUI or conveluted command structure that is nessisary for you to build and run a KVM.
There are two main steps and then the rest is all in the customization of the VM build to suit your needs.
First step is to get to know the basic commands. The "list" "Start" and "shutdown" commands are the ones that you will use the most.
This command shows a list of registered KVM Machines. It also shows you the status of each of the machines.
virsh -c qemu:///system list
This command starts the registered KVM Machines.
virsh -c qemu:///system start NAME-OF-VM
This command Shuts Down the registered KVM Machines.
virsh -c qemu:///system shutdown NAME-OF-VM
These are not the only commands there are many more. To see a list of available commands enter
virsh -c qemu:///system --help
Now you need to build your machine. Shown here is a basic VM build that will be explained in detail. But this build is by no means limited. You have the freedom to customize the build to better suit your needs.
sudo vmbuilder kvm ubuntu --suite maverick \ --flavour virtual \ --arch i386 \ --hostname NewServer \ --ip 10.10.10.10 \ --mask 255.0.0.0 \ --gw 10.10.10.1 \ --libvirt qemu:///system \ --rootsize=5000 \ --swapsize=512 \ --user kevin \ --pass temp1234 \ --addpkg=openssh-server \ --addpkg=wget \ --addpkg=nano \ --dest=/KVM/KVM-newserver
Now to explain all of this so that it makes sense
vmbuilder kvm ubuntu This is the call to the Ubuntu VMbuilder Application. This command also tells ubuntu that we are building a KVM machine using ubuntu as the target Operating System
--suite maverick This specifies the build type of Ubuntu. Valid options are...
maverick, lucid, karmic, jaunty, intrepid, hardy, gutsy, feisty, dapper
This is the virtual Machines Kernel, Operating System software. You can specify a Server Desktop or any other, the choses available depend on the suit you picked and the architecture that you will pick. The flavour that I picked is a virtual Kernel. The virtual Kernel is a Kernel that has been optimised for a Virtual Machine.
--arch i386 This is the processor type that you want your VM to run. It is important to note that you have to pick a processor mode that your computer supports. I.E. If you have a 32bit operating system do not attempt to run a 64 bit virtual machine.
--hostname NewServer This is the name of the server. You should pick a unique name.
--ip 10.10.10.10 This is the IP address that you have given the VM to use
--mask 255.0.0.0 This is the Subnet Mask that the VM will use.
--gw 10.10.10.1 This is the Gateway that the VM will use to connect to the internet.
--libvirt qemu:///system This is the "Libvirt" command to automatically add the Virtual machine to the list of available Virtual Machines. Earlier I specified how to list the available Virtual Machines as well as the status of the Virtual Machines.
--rootsize=5000 This command specifies the size of the ROOT partition is a Virtual Machine. In this instence I am making a Virtual Partition of 5 GB.
--swapsize=512 This command specifies the size of the SWAP partition is a Virtual Machine. Conventional wisdom states that the SWAP size should be twice the size of the RAM, I choose not to listen to conventional wisdom and I generally do a 512 swap size as I tend to give my VM's plenty of available RAM. Ultimately the choice is yours.
--user kevin This command specifies the Username for the Virtual Machine.
--pass temp1234 This command specifies the password for the user that you just created.
This section is optional
--addpkg=openssh-server This adds the ability to SSH to the new VM.
--addpkg=wget This adds the ability to use wget to the new VM
--addpkg=nano This adds the ability to use a standard text editor in
the new VM
--dest=/KVM/KVM-newserver This specifies the destination of the new VM that you are building. In this example I am making the assumption that you are going to place your VM's in a folder on the ROOT partition called /KVM
Now that you have created the VM and waited for the VM to build, which can take more than 30 minutes, are are ready to check to see that the VM has been registered with "libvert"
Execute the command that I specified earlier.
virsh -c qemu:///system list
If you see the VM that you just built you are now ready to start the VM
To start the VM you have to execute the start command. In the case of this example you will execute the command
virsh -c qemu:///system start NewServer
Because we specified the SSH Server to be installed from the start, VM-Server management is simple. You can now SSH to the server IP address, enter the username and password that you specified and you are in. Manage the VM just like you would a regular Computer.