KVM: Membuat Mesin Guest
Membuat mesin virtual
Setelah KVM di instalasi, kita dapat membuat mesin virtual (VM). Pembuat mesin virtual dapat dilakukan dengan menggunakan:
- virt-manager: tool GUI
- virt-install, python script dikembangkan Red Hat
- ubuntu-vm-builder, dikembangkan oleh Canonical.
Ubuntu-vm-builder: tool yang paling cepat & powerfull untuk membuat Ubuntu JEOS VM
Ubuntu-vm-builder adalah tool terbaik yang dapat digunakan jika kita ingin membuat VM menjalankan Ubuntu JEOS 8.04, karena seluruh proses instalasi sudah dibuat dalam bentuk script (sangat cepat). Instalasi tool ini dapat menggunakan perintah
sudo apt-get install ubuntu-vm-builder
Catatan: kita akan membutuhkan grub agar vmbuilder dapat bekerja. Jika kita menggunakan lilo untuk boot host maka kita perlu menginstalasi paket grub. Kita tetap dapat menggunakan lilo untuk boot OS host, tapi guest akan boot dengan grub. Perintah grub perlu di install di host untuk dapat build guest image.
Catatan: pastikan kita mempunyai /tmp yang cukup besar (1GB atau lebih). Juga, beberapa locale akan membuat crash ubuntu-vm-builder; en_US cukup aman.
Cara Penggunaan ubuntu-vm-builder
Cara paling sederhana untuk membuat sebuah mesin virtual adalah
sudo ubuntu-vm-builder kvm hardy sudo ubuntu-vm-builder kvm trusty
Ini akan membuat Ubuntu Trusty, dengan semua option default.
Catatan: jika kita ingin menggunakan Virsh untuk memanage mesin virtual, kita perlu menambahkan parameter:
--libvirt qemu:///system
Catatan: jika menggunakan --libvirt, file xml file di inject ke libvirt dan tidak di simpan secara local. Gunakan virsh -c qemu:///system atau virt-manager -c qemu:///system untuk menjalankan mesin kita atau memodifikasi konfigurasinya.
Contoh yang lebih kompleks
Berikut adalah contoh yang lumayan kompleks:
ubuntu-vm-builder kvm trusty \ --domain newvm \ --dest newvm \ --arch i386 \ --hostname hostnameformyvm \ --mem 256 \ --user john \ --pass doe \ --ip 192.168.0.12 \ --mask 255.255.255.0 \ --net 192.168.0.0 \ --bcast 192.168.0.255 \ --gw 192.168.0.1 \ --dns 192.168.0.1 \ --mirror http://archive.localubuntumirror.net/ubuntu \ --components main,universe \ --addpkg acpid \ --addpkg vim \ --addpkg openssh-server \ --addpkg avahi-daemon \ --libvirt qemu:///system ;
Ini akan membuat mesin virtual (VM) Ubuntu Trusty dengan nama "newvm", hostname akan di set "hostnameformyvm", network akan di konfigurasi dengan IP address statik dengan gateway 192.168.0.1. Switch --mirror akan mengatur agar proses download paket aplikasi di arahkan ke repository local Ubuntu mirror bukan dari repository di Internet (ini akan mempercepat waktu yang dibutuhkan untuk membuat VM). Argumen components akan meng-enable main dan universe secara default di VM, --addpkg vim akan menginstalasi vim, dan argumen terakhir akan secara automatis menambahkan VM yang baru dibuat ke KVM.
Dengan menambahkan 'openssh-server' melalui --addpkg, kita dapat melakukan ssh ke mesin yang kita buat.
Dengan menambahkan 'avahi-daemon' VM akan mendaftarkan record MDNS ('ubuntu.local' dalam hal ini), sehingga kita dapat ssh tanpa perlu pusing dengan IP address DHCP yang di peroleh VM.
Catatan: Manual ini tidak spesifik tentang opsi --exec, yang digunakan untuk mengeksekusi file skrip selama proses pembuatan vm. Jika Anda menentukan opsi --exec maka Anda harus memberikan path lengkap ke skrip yang Anda jalankan. Tidak melakukannya akan menyebabkan penginstal memberi tahu Anda bahwa file skrip tidak dapat ditemukan.
Pemberitahuan bug: tampaknya paket 'linux-package' tidak ditemukan selama proses pembuatan mesin kecuali 'dibatasi' juga ditentukan sebagai opsi komponen untuk ubuntu-vm-builder. Jika tidak ditentukan, Anda akan mendapatkan sistem yang tidak berfungsi yang melakukan booting hanya ke grub. Baris ini membuatnya bekerja untuk saya:
--components 'main,universe,restricted'
Saya berasumsi ini adalah bug di ubuntu-vm-builder.
Pemberitahuan persyaratan paket: Saat mengelola tamu Anda, perintah shutdown/reboot yang dikirim oleh virsh adalah peristiwa ACPI. Jadi agar mesin Anda mati/reboot, Anda harus memiliki dukungan ACPI. Jika tamu O/S adalah Ubuntu, jalankan "sudo apt-get install acpid".
Menjalankan Guest yang dibuat di "complex example"
Setelah Guest Anda dibuat, Anda dapat melihatnya dengan mengetik berikut ini :
virsh 'list --all'
Anda akan melihat :
Id Name State ---------------------------------- - YourGuestName shut off
Untuk memulai server virtual Anda, Anda dapat mengetik:
virsh start YourGuestName
Accessing the new server remotely with SSH tunnel / VNC
If your server is remote, you can use the two following commands from your local machine {
The first guest is accessible, by default, on vnc:127.0.0.1:5900. If you are remotely accessing the server, you can setup an ssh tunnel and vnc onto the virtual server.
From terminal on your local machine :
ssh YOURUSER@YOURSERVER -L5900:127.0.0.1:5900
Now go into Applications -> Internet -> Remote Desktop Viewer. Protocol : VNC Host : 127.0.0.1 Everything else is by default.
This will connect you to the first guest.
Install on a raw block device
Ubuntu-vm-builder doesn't allow you to create the VM on a raw block device yet (like a standalone partition, or a iSCSI share). You can use ubuntu-vm-builder to create the qcow2 image and then move the VM to the block device with qemu-img though; if /dev/sdb is the disk device on which you want to move the virtual machine:
sudo qemu-img convert root.qcow2 -O raw /dev/sdb
Edit the XML definition file for the VM in /etc/libvirt/qemu/, and set the source file to be:
<source file='/dev/sdb'/>
Redefine the VM and start it; it is now running from /dev/sdb.
Ubuntu-vm-builder is a very powerful tool - to get a more detailed list of its capabilities, use ubuntu-vm-builder --help.
Create VMs running other operating systems: virt-install
Virt-install is radically different in the way it works. Instead of creating automagically a new VM, it will allow you to boot on an iso, and therefore to install almost any operating system. For a list of supported operating system, consult kvm's official webpage.
As you need virt-install to setup Windows guests, we will here describe how to install windows XP. There are a couple of small issues during the install, so I will also describe the workarounds.
Example Windows install
http://waste.mandragor.org/windows-kvm.png
NB: The following install is for Windows XP under KVM-62 (default in Hardy as time of writing). I have been unable to run windows 2000 server in accelerated mode on it. To run windows 2000 server, I use the KVM-72 backport for Hardy provided by Soren in his PPA: https://launchpad.net/~soren/+archive.
Copy the ISO of the system you want to install to your working directory:
yhamon@paris:~$ ls *.iso ubuntu-8.04-server-i386.iso windowsxpsp2.iso
or create an ISO from a cd/dvd:
dd if=/dev/dvd of=dvd.iso
Install virt-install:
sudo apt-get install python-virtinst
Then, you need to run virt-install. On Ubuntu Hardy, with KVM-62, the install of windows XP doesn't work when accelerated (ie, using --accelerate), so run the install without that argument, and replace qemu by kvm in the XML defintiion file (in /etc/libvirt/qemu) after the first reboot.
sudo virt-install --connect qemu:///system -n xpsp2 -r 512 -f windows.qcow2 -s 12 -c windowsxpsp2.iso --vnc --noautoconsole --os-type windows --os-variant winxp
Note for 8.10 (intrepid): to make above command work add --hvm option to enable fully virtualized guest.
Note for 9.10 (karmic): if virtual machine just freezes after 1 - 2 minutes after beginning of windows xp installation just use --accelerate option instead of --hvm
This will boot a new VM from the ISO. Connect to the new VM using virt-viewer:
virt-viewer -c qemu:///system xpsp2
Or if KVM is running on a different server:
virt-viewer -c qemu+ssh://ip/system xpsp2
You can now start the install. Another problem is that after the first reboot, in the process of the installation, the windows installer will complain that it can not see the disk anymore. In the XML definition of the VM again, add this to the list of devices:
<domain type='kvm'> [...] <devices> [...] <disk type='file' device='cdrom'> <source file='//home/yhamon/windowsxpsp2.iso'/> <target dev='hdc' bus='ide'/> <readonly/> </disk> </devices> </domain>
Redefine the VM in virsh (sudo virsh define /etc/libvirt/qemu/xpsp2.xml) and restart the VM - it should work fine.
Another problem with virt-install in combination with Windows is that the created disk image is corrupted. At first the installation seems to work, but then after a reboot it stops with the message 'A read error occured'. This can be easily fixed by creating a new disk image (prior to installation):
qemu-img create -f qcow2 disk0.qcow2 12G
Overwrite the qcow2 file created by virt-install by this new file, reboot your VM and you are ready to go.
Example Ubuntu install
You can perform the following to install Ubuntu Hardy:
sudo virt-install --connect qemu:///system -n hardy -r 512 -f hardy.qcow2 -s 12 -c hardy-server-amd64.iso --vnc --noautoconsole --os-type linux --os-variant ubuntuHardy --accelerate --network=network:default
The '--network' option sets up the machine to use the default libvirt network. You can see available libvirt networks with:
virsh net-list --all
Other Operating Systems
Other Operating Systems can be installed using the same method as above. See 'man virt-install' for more information on different options for your virtual machine and supported OS types and OS variants.
Warning: this method fails with Fedora LiveCD installations and possibly others; see the bug here: https://bugs.launchpad.net/ubuntu/+source/kvm/+bug/220463
Cloning a virtual machine
You can clone an existing virtual machine using the virt-clone tool. This duplicates the disk image and sets up the virtual machine domain configuration.
If you wish to clone a virtual machine named srchost to a new machine newhost, ensure that the virtual machine srchost is not running and execute the following command.
$ virt-clone --connect=qemu:///system -o srchost -n newhost -f /path/to/newhost.qcow2
Create a virtual machine from pre-existing image
Assuming:
Virt-manager is being used to manage virtual machines. You wish to add a pre-existing image to virt-manager.
Use case:
Using qcow2 with overlays, you can experiment heavily with your virtual machines without having to perform complete reinstalls if you manage to corrupt them. A side effect of this method in comparison to cloning, is that less disk space is used.
Procedure:
1. Create the hard drive image with qcow2 format:
$ qemu-img create -f qcow2 <image name>.qcow2
2. Install the virtual machine using virt-manager:
Instead of allowing virt-manager to create a new drive image, simply direct it to use the image you created in the previous step.
3. Create the overlay:
$ qemu-img create -f qcow2 -b <image name>.qcow2 <image name>.ovl
4. Create virtual image XML descriptor.
Read first:
$ man virt-image $ man 5 virt-image
Copy the XML file created in step 2 above:
$ cp ~/.libvirt/qemu/<vm name>.xml overlay.xml
Then using your favorite editor, open and edit the copied XML file:
$ <editor> overlay.xml
Create the virtual image XML descriptor based on the virt-image(5) manpage. I simply commented out the original text, and used it as a guide when creating the new descriptor.
5. Create the new virtual machine from the image descriptor:
$ virt-image --vnc overlay.xml
6. Run and test the new virtual machine based on the overlay.
Open the new virtual machine in virt-manager and test to your hearts content. If you 'break' the virtual machine, simply destroy and delete it. Then recreate the overlay image and recreate the overlay virtual machine as described above. You can commit the overlay changes to the base image if desired, see the qemu-img manpage for details.
See also
The system specific adjustment recommended in the FAQ.
apt-cacher-ng for mirroring. You can reduce setup time to 3-4 minutes using ubuntu-vm-builder on a reasonable machine.