[zz]kvm环境使用libvirt创建虚拟机

使用libvirt的xml配置文件创建虚拟机

kvm支持的镜像很多,常用的是原始镜像(*.img),还有支持动态大小扩张的qocw2格式(首选)。

更优的选择是系统盘如C盘用img格式,数据盘用qcow2格式以减少服务器磁盘闲置空间。

本文仅记录如何用ubuntu.iso制作系统镜像ubuntu.qcow2并创建启动虚拟机

一、制作虚拟机镜像

1、创建qcow2镜像,但是其实际占有磁盘大小仅为193K左右,而虚拟机内部显示磁盘大小为10G,也就是磁盘空间使用时才分配,即所谓动态扩张。

qemu-img create -f qcow2 ubuntu.qcow2 10G

2、下载并复制ubuntu的iso镜像到指定目录,本文将所有镜像及配置文件放到 /home/createvm 目录下

2.1 创建配置文件setup.xml,内容如下

<domain type='kvm'>

<name>test_ubuntu</name> //虚拟机名称

<memory>1048576</memory> //最大内存

<currentMemory>1048576</currentMemory> //可用内存

<vcpu>1</vcpu> //虚拟cpu个数

<os>

<type arch='x86_64' machine='pc'>hvm</type>

<boot dev='cdrom'/> //光盘启动

</os>

<features>

<acpi/>

<apic/>

<pae/>

</features>

<clock offset='localtime'/>

<on_poweroff>destroy</on_poweroff>

<on_reboot>restart</on_reboot>

<on_crash>destroy</on_crash>

<devices>

<emulator>/usr/libexec/qemu-kvm</emulator>

<disk type='file' device='disk'>

<driver name='qemu' type='qcow2'/> //此处关键,要求libvirt版本至少应该在0.9以上才能支持,libvirt版本升级http://blog.csdn.net/gg296231363/article/details/6891460

<source file='/home/createvm/ubuntu.qcow2'/> //目的镜像路径

<target dev='hda' bus='ide'/>

</disk>

<disk type='file' device='cdrom'>

<source file='/home/createvm/ubuntu.iso'/> //光盘镜像路径

<target dev='hdb' bus='ide'/>

</disk>

<interface type='bridge'> //虚拟机网络连接方式

<source bridge='br0'/>

<mac address="00:16:3e:5d:aa:a8"/> //为虚拟机分配mac地址,务必唯一,否则dhcp获得同样ip,引起冲突

</interface>

<input type='mouse' bus='ps2'/>

<graphics type='vnc' port='-1' autoport='yes' listen = '0.0.0.0' keymap='en-us'/>//vnc方式登录,端口号自动分配,自动加1

</devices>

</domain>

2.2 virsh define setup.xml //创建虚拟机 查看当前系统所有的虚拟机信息:virsh list --all

2.3 virsh start test_ubuntu //启动虚拟机

2.4 virsh vncdisplay test_ubuntu //查看虚拟机的vnc端口

2.5 使用vnc登录虚拟机,vnc安装配置http://blog.csdn.net/gg296231363/article/details/6899655

登录后能看到操作系统安装的初始界面,开始安装系统,安装完成即表示镜像制作完成(ubuntu.qcow2)。

二、使用制作好的虚拟机镜像启动虚拟机

1 创建文件start.xml,内容如下:

<domain type='kvm'>

<name>test_ubuntu</name>

<memory>1048576</memory>

<currentMemory>1048576</currentMemory>

<vcpu>1</vcpu>

<os>

<type arch='x86_64' machine='pc'>hvm</type>

<boot dev='hd'/> //即harddisk,从磁盘启动

</os>

<features>

<acpi/>

<apic/>

<pae/>

</features>

<clock offset='localtime'/>

<on_poweroff>destroy</on_poweroff>

<on_reboot>restart</on_reboot>

<on_crash>destroy</on_crash>

<devices>

<emulator>/usr/libexec/qemu-kvm</emulator>

<disk type='file' device='disk'>

<driver name='qemu' type='qcow2'/>

<source file='/home/createvm/ubuntu.qcow2'/> //目的镜像路径

<target dev='hda' bus='ide'/>

</disk>

<disk type='file' device='cdrom'>

<source file='/home/createvm/ubuntu.iso'/> //光盘镜像路径

<target dev='hdb' bus='ide'/>

</disk>

<interface type='bridge'>

<source bridge='br0'/>

<mac address="00:16:3e:5d:aa:a8"/>

</interface>

<input type='mouse' bus='ps2'/>

<graphics type='vnc' port='-1' autoport='yes' keymap='en-us'/>

</devices>

</domain>

2 同一台服务器上当然不能有两台同名的虚拟机,第一步创建的虚拟机为制作镜像使用,可以功成身退。

virsh shutdown test_ubuntu //关闭虚拟机,如果关闭不了,可以查看http://blog.csdn.net/gg296231363/article/details/6891380

virsh destory test_ubuntu //强制关闭虚拟机

virsh undefine test_ubuntu //删除虚拟机

3 使用制作好的镜像和start.xml配置文件来创建并启动虚拟机。

virsh define start.xml

virsh start test_ubuntu