使用 libvirt创建和管理KVM虚拟机

字体大小: 中小 标准 ->行高大小: 标准

1. libvirt介绍
    Libvirt是一个软件集合,便于使用者管理虚拟机和其他虚拟化功能,比如存储和网络接口管理等等。Libvirt概括
起来包括一个API库、一个 daemon(libvirtd)和一个命令行工具(virsh)。
    Libvirt的主要目标是:提供一种单一的方式管理多种不同的虚拟化提供方式和 hypervisor。
    Libvirt的主要功能如下:
    (1)虚拟机管理
             包括不同的领域生命周期操作,比如:启动、停止、暂停、保存、恢复和迁移。
             支持多种设备类型的热插拔操作,包括:磁盘、网卡、内存和CPU。
    (2)远程机器支持
             只要机器上运行了libvirt daemon,包括远程机器,所有的libvirt功能就都可以访问和使用。
             支持多种网络远程传输,使用最简单的SSH,不需要额外配置工作。比如:example.com运行了libvirt,
             而且允许SSH访问,下面的命令行就可以在远程的主机上使用virsh命令行。                              virsh --connect qemu+ssh://root@example.com/system
    (3)存储管理
            任何运行了libvirt daemon的主机都可以用来管理不同类型的存储:创建不同格式的文件映像(qcow2、vmdk、raw等)、挂接NFS共享、列出现有的LVM卷组、创建新的LVM卷组和逻辑卷、对未处理过的磁盘设备分区、挂接iSCSI共享,等等等等。因为libvirt可以远程工作,所有这些都可以通过远程主机使用。
     (4)网络接口管理
           任何运行了libvirt daemon的主机都可以用来管理物理和逻辑的网络接口。可以列出现有的接口卡,配置、创建接口,以及桥接、vlan和关联设备等,通过netcf都可以支持。
     (5)虚拟NAT和基于路由的网络
             任何运行了libvirt daemon的主机都可以用来管理和创建虚拟网络。Libvirt虚拟网络使用防火墙规则作为路由器,让虚拟机可以透明访问主机的网络

2. 使用libvirt创建kvm虚拟机
(1)制作虚拟机镜像

    qemu-img create -f qcow2 test.qcow2 10G
(2)下载并复制iso镜像到指定目录,本文将所有镜像及配置文件放到/var/lib/libvirt/images/目录下,注意:有些系统因为SELinux的原因,限定了qemu的访问,所以,可以根据自己需求调整,默认放在/var/lib/libvirt/images/下。
(3)创建安装配置文件,demo.xml如下,可以根据自己需求更改。

    <domain type='kvm'>
            <name>test_Ubuntu</name> //虚拟机名称
            <memory>1048576</memory> //最大内存,单位k
            <currentMemory>1048576</currentMemory> //可用内存,单位k
            <vcpu>8</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'/>
               <source file='/var/lib/libvirt/images/test.qcow2'/> //目的镜像路径
               <target dev='hda' bus='ide'/>
             </disk>
             <disk type='file' device='cdrom'>
               <source file='/var/lib/libvirt/images/ubuntu.iso'/> //光盘镜像路径
               <target dev='hdb' bus='ide'/>
             </disk>
            <interface type='bridge'> //虚拟机网络连接方式
              <source bridge='kvmbr0'/> //当前主机网桥的名称
              <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,可以通过virsh vncdisplay来查询
           </devices>
         </domain>

    virsh define demo.xml //创建虚拟机
    virsh start test_ubuntu //启动虚拟机
    virsh vncdisplay test_ubuntu //查看虚拟机的vnc端口, 然后就可以通过vnc登录来完成虚拟机的安装
3. 使用刚才创建好的镜像来启动一个新的虚拟机
(1)创建启动配置文件,demo.xml如下。

    <domain type='kvm'>
    <name>myvm</name>  //创建名为myvm的虚拟机
    <memory>1048576</memory>
    <currentMemory>1048576</currentMemory>
    <vcpu>8</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='/var/lib/libvirt/images/test.qcow2'/> //目的镜像路径
    <target dev='hda' bus='ide'/>
    </disk>
    <disk type='file' device='cdrom'>
    <source file='/var/lib/libvirt/images/ubuntu.iso'/> //光盘镜像路径
    <target dev='hdb' bus='ide'/>
    </disk>
    <interface type='bridge'>
    <source bridge='kvmbr0'/>
    <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>
    virsh define demo.xml

    virsh start myvm
4, 虚拟机管理
    虚拟机的管理即通过virsh命令来完成,具体命令的使用参见:http://libvirt.org/sources/virshcmdref/html-single/

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/66622.html