The main benefit for us is the ability to set up a "test bed" for trying out software features without tying up extra hardware. One can use this virtualization to install multiple versions of Linux in order to evaluate and compare them. From a security perspective, we can use virtual machines as "attack targets" for security software testing. In an enterprise setting a virtual machine can be a lightweight specialized "server" which is optimized to perform one service task.
There are a number of software tools which serve as the virtualization software including KVM, XEN, VmWare, VirtualBox, etc. Such a software tool is referred to as hypervisor. KVM is only possible if the hardware has sufficient capabilities, but if you can use it, it is extremely fast and Ubuntu supports it well.$ cat /proc/cpuinfo | grep ^flagsThe test for full virtualization support is the presence of these flags:
vmx (32-bit machine) svm (64-bit machine)A machine is 64-bit if the "lm" flag appears. Our lab machines are 32-bit. The "positive match" for us means that this command generates output:
$ cat /proc/cpuinfo | grep ^flags | grep vmx
$ sudo apt-get install kvm qemu bridge-utils libvirt-bin \
ubuntu-vm-builder virt-viewer virt-manager
Included in the installed packages are the MTA (Mail Transfer Agent)
postfix which you must configure in 2 screens.
First tab to OK, then Enter.
Then select No configuration,
tab to OK, then Enter.
$ ifconfig virbr0Create the folder vm0 in your home directory:
$ mkdir ~/vm0 $ cd ~/vm0Create the following two files vmbuilder.partition and create_vm0 It's best to simply copy and paste them into the shell.
vmbuilder kvm ubuntu \ --suite=jaunty \ --flavour=virtual \ --arch=i386 \ --libvirt=qemu:///system \ --hostname=vm0 \ --domain=localdomain \ --part=vmbuilder.partition \ --bridge=virbr0 \ --ip=192.168.122.2 \ --mask=255.255.255.0 \ --user=LOGIN \ --name=Administrator \ --pass=firstpass \ --addpkg=acpid \ --mem=512 \ --tmpfs=- \ -ovmbuilder.partition:
root 5000 swap 1000
$ sudo bash ./create_vm0
$ sudo virsh start vm0Eventually we'll be able to get into the machine through secure shell, but initially we need direct access via:
$ sudo virt-viewer vm0When you click on the "Virt Viewer" window, note the message at the top, because it's the "way out" of the virtual world. Log in to the virtual machine with the user name and password provided by the vmbuilder command, namely:
vm0 login: LOGIN Password: firstpassNow you're in, so do the things as you would for a "normal" machine:
$ sudo su # passwd LOGIN Enter new UNIX password: the-password-you-really-want Retype new UNIX password: the-password-you-really-wantWe want secure shell to give us more comfortable access to the guest:
# apt-get update # apt-get install openssh-server # exit $ exitNow close the "Virt Viewer" shell and open a regular shell (in your real machine). Create a host for vm0. Edit the file /etc/hosts. At the end of the file add the entry:
192.168.122.2 vm0Test ping:
$ ping vm0 Ctrl-C to stopThen go in (assuming your login on vm0 should be the same as on your machine):
$ ssh vm0When you're in you can verify the partitioning and memory allocation by doing:
[vm0] $ sudo fdisk -l /dev/sda [vm0] $ freeCreate a name for the host IP. Edit /etc/hosts in the virtual machine. Use either vim or nano, but first install:
[vm0] $ sudo apt-get install nano vim [vm0] $ sudo [ nano | vim ] /etc/hostsAdd the line:
192.168.122.1 hostSave, come back to the shell and try these:
[vm0] $ ping host [vm0] $ ssh host ----------- now you're going into back to your machine from the virtual guest!
$ sudo virsh shutdown vm0
$ cd ~/vm0 $ ls -s ubuntu-kvm size-in-K ... disk0.qcow2The "cow" here means copy-on-write. Note that the size in bytes, perhaps around 370K or so, is significantly less than 6GB. The 6GB figure actually represents the maximum size that the virtual machine can be. The other important configuration files are in the /etc/libvirt directory. See what's there by:
$ ls -lR /etc/libvirtThe bridge network interface, virbr0, is created by the file /etc/libvirt/qemu/networks/default.xml. This bridge network is controlled by the init file /etc/init.d/libvirt.bin. See what's in the network config file by doing:
$ cat /etc/libvirt/qemu/networks/default.xmlIn addition, the Ubuntu firewall is configured to use NAT (Network Address Translation) in a way which allows the virtual machine to communicate with the world though the bridge interface. The technique is referred to as masquerading and the arguments used for the iptables firewall operation can be seen by running the command:
$ sudo iptables-save -t natrevealing the iptables command arguments:
-A POSTROUTING -s 192.168.122.0/24 -d ! 192.168.122.0/24 -j MASQUERADEThe virtual machine is controlled by the virsh executable through the root-protected config file /etc/libvirt/qemu/vm0.xml. Take a look at its contents:
$ sudo cat /etc/libvirt/qemu/vm0.xml