Difference between revisions of "KVM: Membuat Mesin Guest"

From OnnoWiki
Jump to navigation Jump to search
(New page: Creating virtual machines Now that KVM is installed, let's see how we create our first VM. This can be done using: virt-manager: a GUI tool virt-install, a python script develop...)
 
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
Creating virtual machines
+
==Membuat mesin virtual==
  
Now that KVM is installed, let's see how we create our first VM. This can be done using:
+
Setelah KVM di instalasi, kita dapat membuat mesin virtual (VM). Pembuat mesin virtual dapat dilakukan dengan menggunakan:
  
    virt-manager: a GUI tool
+
* virt-manager: tool GUI
 +
* virt-install, python script dikembangkan Red Hat
 +
* ubuntu-vm-builder, dikembangkan oleh Canonical.
  
    virt-install, a python script developed by Red Hat
+
Ubuntu-vm-builder: tool yang paling cepat & powerfull untuk membuat Ubuntu JEOS VM
  
    ubuntu-vm-builder, developed by Canonical.  
+
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
  
Ubuntu-vm-builder: the fast & powerful way to create Ubuntu JEOS VMs
+
sudo apt-get install ubuntu-vm-builder
  
Ubuntu-vm-builder is probably the best tool to use if you want to create VMs running Ubuntu JEOS 8.04, as the install is entirely scripted (very fast).
+
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.
  
This tool is packaged, and in universe:
+
Catatan: pastikan kita mempunyai /tmp yang cukup besar (1GB atau lebih). Juga, beberapa locale akan membuat crash ubuntu-vm-builder; en_US cukup aman.
  
sudo apt-get install ubuntu-vm-builder
+
==Cara Penggunaan ubuntu-vm-builder==
  
Note: you will need to have grub installed for vmbuilder to work. If you're using lilo to boot the host you will have to install the grub package. You can still use lilo to boot the host OS, but the guests will boot with grub. The grub command needs to be install on the host for the guest image to build.
+
Cara paling sederhana untuk membuat sebuah mesin virtual adalah
  
Note for 8.10 (intrepid): the package is now named python-vm-builder, the command is now named vmbuilder and you can find a detailed tutorial on the wiki.
+
sudo ubuntu-vm-builder kvm hardy
 +
sudo ubuntu-vm-builder kvm trusty
  
Note: make sure you have big enough /tmp (1GB or more). Also, some locales will crash ubuntu-vm-builder; en_US is safe.
+
Ini akan membuat Ubuntu Trusty, dengan semua option default.
  
Basic use
+
Catatan: jika kita ingin menggunakan Virsh untuk memanage mesin virtual, kita perlu menambahkan parameter:
  
Here is a very basic example of how to use it:
+
--libvirt qemu:///system
  
sudo ubuntu-vm-builder kvm hardy
+
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.
  
This will create an Ubuntu Hardy, with all options set to default.
+
==Contoh yang lebih kompleks==
  
Or you can use this URL to a Javascript tool that generates the lengthy set of parameters available for ubuntu-vm-builder:
+
Berikut adalah contoh yang lumayan kompleks:
  
    http://thebwt.com/ubuntu-vm-builder.html (Outdated. Only offers options until Ubuntu 10.04)
+
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 ;
  
    http://thebwt.com/ubuntu-vm-builder-libvirt.html (With br0 option. Also outdated. Only offers options until Ubuntu 10.04)
+
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.
  
Note: if you want to use Virsh to manage your virtual machines as detailed below, you must add this parameter:
+
Dengan menambahkan 'openssh-server' melalui --addpkg, kita dapat melakukan ssh ke mesin yang kita buat.
  
--libvirt qemu:///system
+
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.
  
The javascript does not have an option for this itself, unfortunately.
+
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.
  
Note: when using --libvirt, the xml file is injected into libvirt and not stored locally. Use virsh -c qemu:///system or virt-manager -c qemu:///system to start your machine or modify its configuration.
+
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:
  
More complex example
+
--components 'main,universe,restricted'
  
Now here is a somewhat more complex example:
+
Saya berasumsi ini adalah bug di ubuntu-vm-builder.
  
ubuntu-vm-builder kvm hardy \
+
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".
                  --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 ;
 
  
This will create a new Ubuntu Hardy VM called "newvm", the hostname will be set to "hostnameformyvm", the network will be configured with a static IP address and a gateway at address 192.168.0.1. The --mirror will tell the script to download the packages from a local Ubuntu mirror instead of the default server (this may speed up by a lot the time necessary to create the VM). The components argument will enable main and universe by default on the VM, --addpkg vim will install vim, and finally the last argument will automatically add the newly created VM to KVM.
+
==Menjalankan Guest yang dibuat di "complex example"==
  
By adding 'openssh-server' with --addpkg, we will be able to ssh into our new machine once its started (provided the networking works correctly).
+
Setelah Guest Anda dibuat, Anda dapat melihatnya dengan mengetik berikut ini :
  
By adding 'avahi-daemon' the VM will register an MDNS record ('ubuntu.local' in this case), so you can ssh right in without bothering with DHCP assigned IP addresses.
+
virsh 'list --all'
  
Note: The manual isn't specific about the --exec option, used to execute a script file during the vm build process. If you specify the --exec option then you must provide the full path to the script you are executing. Not doing so will cause the installer to tell you that it can't find the script file.
+
Anda akan melihat :
  
Bug notice: it seems that the package 'linux-package' is not found during the machine building process unless 'restricted' is also specified as a component option to ubuntu-vm-builder. If not specified, you'll get a non-functioning system that boots into grub only. This line makes it work for me:
+
  Id Name                State
 +
----------------------------------
 +
  - YourGuestName        shut off
  
--components 'main,universe,restricted'
+
Untuk memulai server virtual Anda, Anda dapat mengetik:
  
I'm assuming this is a bug in ubuntu-vm-builder.
+
  virsh start YourGuestName
 
 
Package requirement notice: When managing your guests, the shutdown/reboot commands send by virsh are ACPI events. So in order for your machine to shutdown/reboot, you must have ACPI support. If the guest O/S is Ubuntu run "sudo apt-get install acpid".
 
 
 
Starting Guest created in "complex example"
 
 
 
Once your Guest is created, you can see it by typing the following :
 
 
 
virsh 'list --all'
 
 
 
You'll see :
 
 
 
  Id Name                State
 
----------------------------------
 
- YourGuestName        shut off
 
 
 
To start your virtual server, you can type :
 
 
 
virsh start YourGuestName
 
  
 
Accessing the new server remotely with SSH tunnel / VNC
 
Accessing the new server remotely with SSH tunnel / VNC
Line 111: Line 98:
 
From terminal on your local machine :
 
From terminal on your local machine :
  
ssh YOURUSER@YOURSERVER -L5900:127.0.0.1:5900
+
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.
 
Now go into Applications -> Internet -> Remote Desktop Viewer. Protocol : VNC Host : 127.0.0.1 Everything else is by default.
Line 117: Line 104:
 
This will connect you to the first guest.
 
This will connect you to the first guest.
  
Install on a raw block device
+
==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:
 
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:
Line 125: Line 112:
 
Edit the XML definition file for the VM in /etc/libvirt/qemu/, and set the source file to be:
 
Edit the XML definition file for the VM in /etc/libvirt/qemu/, and set the source file to be:
  
<source file='/dev/sdb'/>
+
<source file='/dev/sdb'/>
  
 
Redefine the VM and start it; it is now running from /dev/sdb.
 
Redefine the VM and start it; it is now running from /dev/sdb.
Line 131: Line 118:
 
Ubuntu-vm-builder is a very powerful tool - to get a more detailed list of its capabilities, use ubuntu-vm-builder --help.
 
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
+
==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.
 
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.
Line 139: Line 126:
 
Example Windows install
 
Example Windows install
  
http://waste.mandragor.org/windows-kvm.png
+
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.
 
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.
Line 145: Line 132:
 
Copy the ISO of the system you want to install to your working directory:
 
Copy the ISO of the system you want to install to your working directory:
  
yhamon@paris:~$ ls *.iso
+
yhamon@paris:~$ ls *.iso
ubuntu-8.04-server-i386.iso  windowsxpsp2.iso
+
ubuntu-8.04-server-i386.iso  windowsxpsp2.iso
  
 
or create an ISO from a cd/dvd:
 
or create an ISO from a cd/dvd:
  
dd if=/dev/dvd of=dvd.iso
+
dd if=/dev/dvd of=dvd.iso
  
 
Install virt-install:
 
Install virt-install:
  
sudo apt-get install python-virtinst
+
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.
 
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
+
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 8.10 (intrepid): to make above command work add --hvm option to enable fully virtualized guest.
Line 166: Line 153:
 
This will boot a new VM from the ISO. Connect to the new VM using virt-viewer:
 
This will boot a new VM from the ISO. Connect to the new VM using virt-viewer:
  
virt-viewer -c qemu:///system xpsp2
+
virt-viewer -c qemu:///system xpsp2
  
 
Or if KVM is running on a different server:
 
Or if KVM is running on a different server:
  
virt-viewer -c qemu+ssh://ip/system xpsp2
+
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:
 
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'>
+
<domain type='kvm'>
  [...]
+
  [...]
  <devices>
+
  <devices>
    [...]
+
    [...]
    <disk type='file' device='cdrom'>
+
    <disk type='file' device='cdrom'>
      <source file='//home/yhamon/windowsxpsp2.iso'/>
+
      <source file='//home/yhamon/windowsxpsp2.iso'/>
      <target dev='hdc' bus='ide'/>
+
      <target dev='hdc' bus='ide'/>
      <readonly/>
+
      <readonly/>
    </disk>
+
    </disk>
  </devices>
+
  </devices>
</domain>
+
</domain>
  
 
Redefine the VM in virsh (sudo virsh define /etc/libvirt/qemu/xpsp2.xml) and restart the VM - it should work fine.
 
Redefine the VM in virsh (sudo virsh define /etc/libvirt/qemu/xpsp2.xml) and restart the VM - it should work fine.
Line 190: Line 177:
 
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):
 
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
+
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.
 
Overwrite the qcow2 file created by virt-install by this new file, reboot your VM and you are ready to go.
  
Example Ubuntu install
+
==Example Ubuntu install==
  
 
You can perform the following to install Ubuntu Hardy:
 
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
+
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:
 
The '--network' option sets up the machine to use the default libvirt network. You can see available libvirt networks with:
  
virsh net-list --all
+
virsh net-list --all
  
Other Operating Systems
+
==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.
 
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.
Line 210: Line 197:
 
Warning: this method fails with Fedora LiveCD installations and possibly others; see the bug here: https://bugs.launchpad.net/ubuntu/+source/kvm/+bug/220463
 
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
+
==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.
 
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.
Line 216: Line 203:
 
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.
 
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
+
$ virt-clone --connect=qemu:///system -o srchost -n newhost -f /path/to/newhost.qcow2
  
 
Create a virtual machine from pre-existing image
 
Create a virtual machine from pre-existing image
Line 233: Line 220:
 
1. Create the hard drive image with qcow2 format:
 
1. Create the hard drive image with qcow2 format:
  
$ qemu-img create -f qcow2 <image name>.qcow2
+
$ qemu-img create -f qcow2 <image name>.qcow2
  
 
2. Install the virtual machine using virt-manager:
 
2. Install the virtual machine using virt-manager:
Line 241: Line 228:
 
3. Create the overlay:
 
3. Create the overlay:
  
$ qemu-img create -f qcow2 -b <image name>.qcow2 <image name>.ovl
+
$ qemu-img create -f qcow2 -b <image name>.qcow2 <image name>.ovl
  
 
4. Create virtual image XML descriptor.
 
4. Create virtual image XML descriptor.
Line 247: Line 234:
 
Read first:
 
Read first:
  
$ man virt-image
+
$ man virt-image
$ man 5 virt-image
+
$ man 5 virt-image
  
 
Copy the XML file created in step 2 above:
 
Copy the XML file created in step 2 above:
  
$ cp ~/.libvirt/qemu/<vm name>.xml overlay.xml
+
$ cp ~/.libvirt/qemu/<vm name>.xml overlay.xml
  
 
Then using your favorite editor, open and edit the copied XML file:
 
Then using your favorite editor, open and edit the copied XML file:
  
$ <editor> overlay.xml
+
$ <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.
 
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.
Line 262: Line 249:
 
5. Create the new virtual machine from the image descriptor:
 
5. Create the new virtual machine from the image descriptor:
  
$ virt-image --vnc overlay.xml
+
$ virt-image --vnc overlay.xml
  
 
6. Run and test the new virtual machine based on the overlay.
 
6. Run and test the new virtual machine based on the overlay.
Line 268: Line 255:
 
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.
 
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
+
==See also==
  
 
The system specific adjustment recommended in the FAQ.
 
The system specific adjustment recommended in the FAQ.

Latest revision as of 15:13, 15 April 2023

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.



Referensi