使用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>myslax</name>         
    <memory>1048576</memory>                 
    <currentMemory>524288</currentMemory>    
    <vcpu>1</vcpu>                                                     
    <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/bin/qemu-system-x86_64</emulator>
     <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>           
       <source file='/home/YC/image/slax.qcow2'/>         
       <target dev='hda' bus='ide'/>
     </disk>
     <disk type='file' device='cdrom'>
       <source file='/home/YC/image/slax.iso'/>            
       <target dev='hdb' bus='ide'/>
     </disk>
    <interface type='bridge'>                    
      <source bridge='virbr0'/>
      <mac address="00:16:3e:5d:aa:a8"/>   
    </interface>
    <input type='mouse' bus='ps2'/>
     <graphics type='vnc' port='-1' autoport='yes' listen = '0.0.0.0' keymap='en-us'/>
   </devices>
 </domain>



virsh define demo.xml //创建虚拟机
virsh start myslax //启动虚拟机
virsh vncdisplay myslax //查看虚拟机的vnc端口, 然后就可以通过vnc登录来完成虚拟机的安装


3. 使用刚才创建好的镜像来启动一个新的虚拟机
(1)创建启动配置文件,demo.xml如下。
<domain type='kvm'>
    <name>myslax</name>         
    <memory>1048576</memory>                 
    <currentMemory>524288</currentMemory>    
    <vcpu>1</vcpu>                                                     
    <os>
      <type arch='x86_64' machine='pc'>hvm</type>
      <boot dev='hd'/>      //中硬盘启动                                           
   </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/bin/qemu-system-x86_64</emulator>
     <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>           
       <source file='/home/YC/image/slax.qcow2'/>         
       <target dev='hda' bus='ide'/>
     </disk>
     <disk type='file' device='cdrom'>
       <source file='/home/YC/image/slax.iso'/>            
       <target dev='hdb' bus='ide'/>
     </disk>
    <interface type='bridge'>                    
      <source bridge='virbr0'/>
      <mac address="00:16:3e:5d:aa:a8"/>   
    </interface>
    <input type='mouse' bus='ps2'/>
     <graphics type='vnc' port='-1' autoport='yes' listen = '0.0.0.0' keymap='en-us'/>
   </devices>
 </domain>

virsh define demo.xml

virsh start myslax


4, 虚拟机管理
    虚拟机的管理即通过virsh命令来完成,具体命令的使用参见:http://libvirt.org/sources/virshcmdref/html-single/
本篇文章来源于 Linux公社网站(www.linuxidc.com)  原文链接:http://www.linuxidc.com/Linux/2012-06/62934.htm

你可能感兴趣的:(使用libvirt创建kvm虚拟机)