Personalize this online document.
Enter your machine login:
Enter the simple name of your machine:
$ sudo apt-get install kvm qemu bridge-utils libvirt-bin \ ubuntu-vm-builder virt-viewer virt-managerYou must add yourself to the libvirtd group to use some of the tools:
$ sudo adduser LOGIN libvirtdYou then have to log out and log back in to have this take effect. The inspiration for the steps outlined below is the Ubuntu Server Guide, in particular, the section which describes the minimal JeOS (pronounced "juice") installation using the Ubuntu vmbuilder tool. You can get information about how to use vmbuilder tool in two ways:
$ man vmbuilder $ vmbuilder kvm ubuntu --help | moreThe vmbuilder tool is undoubtedly the best approach for creating "production style" virtual machines whose primary goal is to provide some server function on a single server or within a so-called "server cloud", because vmbuilder is specifically geared for a virtual machine and only the software necessary is installed specific to that end. Our guest machines will be "attached" to the virtual bridge interface virbr0, which has been generated for you by the libvirt-bin installation. Check it out by:
$ ifconfig virbr0
$ mkdir -p ~/vm1Within this folder create the following file by copy/paste. Make sure you have your LOGIN set at the top of this script. The MIRROR variable is set to use for our Lab machines.
#!/bin/bash # choose mirror size MIRROR="http://us.archive.ubuntu.com/ubuntu" # choose sute (precise=12.04) SUITE=precise # choose architecture ARCH="amd64" #ARCH="i386" VMNAME="vm1" MYOCTET=200 ROOTSIZE=5000 SWAPSIZE=512 # login/password into VM # we will change the password immediately ADMIN="LOGIN" INITPWD="vmpass" vmbuilder kvm ubuntu \ --suite="$SUITE" \ --flavour=virtual \ --arch="$ARCH" \ --libvirt=qemu:///system \ --hostname="$VMNAME" \ --bridge=virbr0 \ --ip="192.168.122.$MYOCTET" \ --mask=255.255.255.0 \ --gw=192.168.122.1 \ --dns=192.168.122.1 \ --domain=localdomain \ --user="$ADMIN" \ --name=Administrator \ --pass="$INITPWD" \ --rootsize="$ROOTSIZE" \ --swapsize="$SWAPSIZE" \ --mem=512 \ --addpkg=acpid \ --addpkg=openssh-server \ --addpkg=nano \ --addpkg=rsync \ --install-mirror="$MIRROR" \ # leave at least one blank line after command # so any further options can easily be appendedselect
$ cd ~/vm1 $ time sudo bash create-vm1.shThe entire install completes in less than 10 minutes on the lab machines. Here are some points about this vmbuilder command usage:
- The kvm argument means to use KVM as the hypervisor.
- The --libvirt=qemu:///system option indicates to make the virtual machine accessible through the QEMU interface. This allows us to control the VM with many of the available software tools.
- The --hostname="$VMNAME" makes vm1 be the virtual machine hostname, and also the domain name, accessible through the QEMU system.
- The --bridge=virbr0 option means to use virbr0 as the guest's network interface.
- The --ip (IP address), --mask (netmask), --gw (gateway), --dns (resolver) and --domain define the network setup. The IP address is 192.168.122.200, has last octet set at 200. which could be anything other than 1.
- The --user, --name, and --pass set up the initial account on the guest.
- The --rootsize and --swapsize specifiy the virtual machine's virtual disk size being a 5000MB (~5GB) root and 512MB (~.5GB) swap.
- The RAM size is 512M introduced through the --mem option. This memory usage actually takes away from the memory available to the host machine. This memory size is configurable after creation.
- The --addpkg options specify packages to install in the initial machine.
The VM contentsAfter completion, the virtual machine you just built is represented by these:
~/vm1/ubuntu-kvm/ /etc/libvirt/qemu/vm1.xmlYou can see the file which represents the guest's storage as:
$ ls -s ~/vm1/ubuntu-kvm size-in-K ____.qcow2The "cow" here means copy-on-write. Note that the file size, around 550MB or so, is significantly less than 5.5BG maximum size. If you want to start over, delete both the directory and the XML configuration file and restart the virtual machine manager: libvirt-bin:
$ sudo service libvirt-bin restart
- virt-manager: The GUI tool, found in Gnome Classic Application menu as Virtual Machine Manager in System Tools.
- virsh: shell-based tool
192.168.122.200 vm1Start up virt-manager. Select vm1 from the list of virtual machines (the only one so far) and click the green triangle "run" button. If you double-click on the line, it will bring up a shell by which you can access the machine, but we'll access it via SSH. The VMs boot very quickly. You should be able to ping it very soon after starting it up:
$ ping vm1Then, open a Terminal and go in:
$ ssh vm1 LOGIN@vm1's password: vmpassSet the VM's password to your password. Use "sudo" to circumvent any password "deficiency":
LOGIN@vm1:~$ sudo passwd LOGIN [sudo] password for LOGIN: vmpass Enter new UNIX password: password-for-LOGIN-on-MACHINE Retype new UNIX password: password-for-LOGIN-on-MACHINE passwd: password updated successfullyGive cryptographic access to your virtual machine. Create the .ssh directory:
[vm1] $ mkdir .sshOpen another shell in the host and do this:
[MACHINE] $ rsync ~/.ssh/id_dsa.pub vm1:.ssh/authorized_keys2 LOGIN@vm1's password: password-for-LOGIN-on-MACHINEThen test password-less access:
[MACHINE] $ ssh vm1 ls
[vm1] $ sudo apt-get install nginx [vm1] $ sudo /etc/init.d/nginx startConfirm by accessing the VM web root:
ProxyPass /special http://vm1 ProxyPassReverse /special http://vm1
[vm1] $ sudo apt-get install aptitudeUpdate like this:
[vm1] $ sudo aptitude update [vm1] $ sudo aptitude upgrade