个人工具

UbuntuHelp:KVM/CreateGuests/zh

来自Ubuntu中文

跳转至: 导航, 搜索

创建虚拟机

目前 KVM 已经安装好,我们如何创建我们的第一个 VM (虚拟机)。可以参考下面的网址:

Ubuntu-vm-builder: 快速且强大的创建 Ubuntu JEOS VMs 的工具

如果您想要创建运行Ubuntu JEOS 8.04的虚拟机,那么Ubuntu-vm-builder 是最好的工具,因为整个创建过程全部使用脚本(非常快)。这个工具可以在 universe 中找到安装包:

sudo apt-get install ubuntu-vm-builder

Note: 为了使vmbuilder能工作,您可能还需要安装grub。如果您使用lilo来启动host(宿主机),您还需要安装grub包。您仍然可以使用lilo来启动宿主机的OS,但是guests(客户机)将使用grub来启动。 The grub command needs to be install on the host for the guest image to build. 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

这个命令会使用所有的缺省选项来创建Ubuntu Hardy虚拟机。 Or you can use this URL to a Javascript tool that generates the lengthy set of parameters available for ubuntu-vm-builder:

Note: 如果您希望使用Virsh来管理您的虚拟机(后面详细讲述),您还要添加参数:

--libvirt qemu:///system

The javascript does not have an option for this itself, unfortunately. Note: 当使用--libvirt选项,xml文件会注入到libvirt而不是在本地保存。使用virsh -c qemu:///system或者virt-manager -c qemu:///system 来启动虚拟机或者修改虚拟机配置。

More complex example

Now here is a somewhat more complex example:

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 ;

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. By adding 'openssh-server' with --addpkg, we will be able to ssh into our new machine once its started (provided the networking works correctly). 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. 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:

--components 'main,universe,restricted'

I'm assuming this is a bug in ubuntu-vm-builder.

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

从已存在的镜像文件创建虚拟机

假设:

  • Virt-manager被用来管理虚拟机。
  • 您希望添加已存在的镜像文件到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.

过程:

1. 创建qcow2格式的硬盘镜像文件:

$ qemu-img create -f qcow2 <image name>.qcow2

2.使用virt-manager安装虚拟机:

为了不让virt-manager自动创建新的磁盘镜像,可以命令它使用您刚刚创建的镜像。

3. 创建overlay:

$ qemu-img create -f qcow2 -b <image name>.qcow2 <image name>.ovl

4. 创建虚拟镜像的XML描述文件:

首先阅读:

$ man virt-image
$ man 5 virt-image

复制上面第2步创建的XML文件:

$ cp ~/.libvirt/qemu/<vm name>.xml overlay.xml

打开并编辑复制的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.

5. 通过镜像描述文件创建新的虚拟机:

$ virt-image --vnc overlay.xml

6. 运行和测试基于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. <<Include(KVM/Header)>>