BR Indice(depth=2 align=right)
Creazione di macchine virtuali
Una volta installato KVM, è possibile procedere alla creazione della VM. Ciò può essere fatto usando:
[http://virt-manager.et.redhat.com virt-manager]: uno strumento GUI.
[http://www.howtoforge.com/installing-kvm-guests-with-virt-install-on-ubuntu-8.10-server virt-install], uno script python sviluppato da Red Hat
[https://help.ubuntu.com/8.04/serverguide/C/ubuntu-vm-builder.html ubuntu-vm-builder], sviluppato da Canonical.
Ubuntu-vm-builder: un mezzo veloce e potente per creare VMs Ubuntu JEOS
[http://doc.ubuntu.com/ubuntu/serverguide/C/ubuntu-vm-builder.html Ubuntu-vm-builder] è probabilmente il miglior strumento per la creazione di VMs che eseguono Ubuntu JEOS 8.04, dato che l'installazione avviene interamente tramite script (molto veloce).
Questo strumento è pacchettizzato, e si trova nel repository universe:
sudo apt-get install ubuntu-vm-builder
Nota: a partire dalla 8.10 (intrepid): il pacchetto viene chiamato python-vm-builder, il comando vmbuilder ed è possibile trovare un [https://help.ubuntu.com/community/JeOSVMBuilder dettagliato tutorial sul wiki].*******Tradurre
Utilizzo elementare
Questo è un esempio elementare di come utilizzarlo:
sudo ubuntu-vm-builder kvm hardy
Ciò creerà una Ubuntu Hardy, con tutte le opzione impostate in modo predefinito.
Oppure è possibile usare questo URL in uno strumento per script java per generare la lunga serie di parametri disponibili per ubuntu-vm-builder:
Nota: se si preferisce usare Virsh per la gestione della macchina virtuale, come descritto più avanti, bisogna aggiungere questo parametro:
--libvirt qemu:///system
Sfortunatamente lo script java non contiene una opzione per farlo automaticamente.
Nota: utilizzando il parametro --libvirt, il file XML non viene archiviato localmente ma viene iniettato in libvirt. Utilizzare virsh -c qemu:///system oppure virt-manager -c qemu:///system per avviare la macchina o modificarne la configurazione.
Esempio più complesso
In seguito è riportato un esempio più complesso:
ubuntu-vm-builder kvm hardy \ --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 vim openssh-server \ --libvirt qemu:///system ;
Ciò creerà una nuova Ubuntu Hardy VM chiamata "newvm", l' hostname sarà impostato a "hostnameformyvm", la rete sarà configurata con un indirizzo IP statico ed il gateway all'indirizzo 192.168.0.1. il parametro --mirror richiederà allo script lo scaricamentodei pacchetti da un mirror locale di ubuntu piuttosto che dal server centrale (ciò diminuirà di molto il tempo necesario alla creazione della VM). L'argomento components abiliterà i repository main e universe predefiniti sulla VM, --addpkg vim installerà vim, ed infine l'ultimo argomento aggiungerà automaticamente la VM appena creata a KVM.
L'aggiunta di 'openssh-server' a --addpkg, consentirà di eseguire una sessione ssh sulla nuova macchina una volta avviata (Assicurarsi che i dispositivi di connessione siano funzionino correttamente).
Nota: Il manuale non fornisce spiegazioni dettagliate sull'utilizzo dell'opzione --exec, usata per lanciare uno script durante il processo dicostruzione della VM. Quindi se si specifica l'opzione --exec è necessario fornire l'indirizzo completo dallo script. In mancanza di ciò il programma d'installazione restituirà un errore relativo all'impossibilità di recuperare lo script.
Avviso bug: Sembra che il pacchetto 'linux-package' non venga trovato durante la costruzione della VM fino a che non viene aggiunta l'opzione 'restricted' nell'argomento component di ubuntu-vm-builder. se non specificato si otterrà un SO non funzionante che si avvierà solamente da grub., Quest linea risolve il problema:
--components 'main,universe,restricted'
Presumendo che ciò sia un bug di ubuntu-vm-builder.
Intallazione su un dispositivo a blocchi grezzo
Ubuntu-vm-builder non consente la creazione di VM su un dispositivo a blocchi grezzo (come ad esempio una partizione indipendente, oppure una condivisione iSCSI). È possibile usare ubuntu-vm-builder per creare l'immagine qcow2 e quindi spostare la VM sul dispositivo a blocchi con qemu-img; se /dev/sdb è il disco sul quale trasferire la macchina virtuale:
sudo qemu-img convert root.qcow2 -O raw /dev/sdb
Editare il file definizione XML per la VM in /etc/libvirt/qemu/, ed impostare il file sorgente per esere:
<source file='/dev/sdb'/>
Ridefinire la VM ed avviarla; così facendo funzionerà da /dev/sdb.
Ubuntu-vm-builder è uno strumento potentissimo - per avere un più dettagliato elenco delle sue capacità, utilizzare ubuntu-vm-builder --help.
Creazione di una VMs in grado di eseguire altri sistemi operativi: virt-install
Virt-install è redicalmente differente nel modo in cui funziona. Invece di creare automagicamente una nuova VM, consente di avviare un immagine iso, e quindi di installare qualsiasi sistema operativo. Per l'elenco dei sistemi operativi supportati, consultare la [http://www.linux-kvm.org/page/Guest_Support_Status pagina web ufficiale di KVM].
Di seguito viene descritto come installare Windows XPA. Durante la fase d'installazione, avverranno alcuni piccoli problemi, ma verrà spiegato come aggirarli.
Esempio di installazione di Windows
NB: La seguente descrizione è relativa a windows XP sotto KVM-62 (Predefinito in Hardy durante la stesura di questo testo). In questa versione è impossibile l'esecuzione di windows 2000 server in modalità accelerata, per cui occorre usare KVM-72 adattato per Hardy fornito da Soren nel suo PPA: https://launchpad.net/~soren/+archive.
Copiare la ISO del sistema da installare nella vostra directory funzionante: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.
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.