使用virsh創建kvm虛擬機

Using virsh to create a KVM

When you are finished creating or editing your XML definition file, create your KVM using the virsh tool.

Procedure

  1. Define your KVM by running:
    virsh define <Name of XML definition file>
    For example:
    # virsh define kvm4.xml

    In the background, a copy of the XML file is copied to the /etc/libvirt/qemu directory and the new KVM is officially defined.

  2. Start the KVM so that the installation of its operating system can begin:
    virsh start <Name of KVM>
    For example:
    # virsh start kvm4
    If your KVM does not start, complete one of the following to redefine the KVM:
    1. Edit the defined KVM definition by running the following command and revising the corresponding definition file:
      virsh edit <Name of KVM>
      For example:
      # virsh edit kvm4
    2. Undefine the KVM and then edit your copy of the file and redefine it as in Step 1. To undefine an existing KVM, run the following command:
      virsh undefine <Name of KVM>
      For example:
      # virsh undefine kvm4
  3. To check which KVM guests are running, run the following command:
    # virsh list    
    Id Name                 State   
    ----------------------------------     
    1 kvm1                 running     
    2 kvm2                 running     
    3 kvm3                 running     
    4 kvm4                 running 

 

Propagating your KVMs

With an understanding of how the underlying XML definition file works, you can manipulate and manage the KVMs in various ways. For example, if your task is to create 10 identical KVMs that have simple configurations, you can propagate existing KVMs rather than manually installing 10 identical KVMs.

Procedure

  1. Create one KVM using virt-install. For this example, the created KVM is named templateKVM.
  2. Finish the operating system installation and confirm that the installation was successful.
  3. Stop the KVM with the virsh tool:
    # virsh shutdown templateKVM
  4. Make copies of the KVM disk image by using the qemu-image command with the convert option:
    # qemu-img convert templateKVM.img -O raw NewKVM1.img
  5. Make copies of the templateKVM XML definition file. You can find it in the /etc/libvirt/qemu/ directory or you can run the following command to see the definition file.
    # virsh dumpxml templateKVM
  6. Edit the definition files and define the new KVMs as demonstrated in Using virsh to create a KVM. It is important that a unique MAC address is used in the definition file to make sure that the network functions without a problem.
  7. Start one KVM at a time and reconfigure its network so that a unique set of MAC and IP addresses is used.

Managing your KVM

You can use the virsh tool to perform several managing tasks on your KVM. Some commonly used virsh commands that are not mentioned previously in this Blueprint are described in this topic.

  • To shut down a running KVM gracefully:
    # virsh shutdown <Name of KVM>
  • To shut down a running KVM without notifying the guest operating system:
    # virsh destroy <Name of KVM>
  • To list all virtual networks:
    # virsh net-list 
    Name                 State      Autostart  
    -----------------------------------------  
    default              active     yes       
  • To get the XML definition of a particular network:
    # virsh net-dumpxml <Name of network>
    For example:
    # virsh net-dumpxml default 
    <network>  
     <name>default</name>  
     <uuid>ff2df37e-d22e-4bdd-8a04-4e5280488b29</uuid>  
     <forward mode='nat'/> 		
     <bridge name='virbr0' stp='on' forwardDelay='0'/>  
     <ip address='192.168.122.1' netmask='255.255.255.0'>     
        <dhcp>       
          <range start='192.168.122.2' end='192.168.122.254' />    
        </dhcp>  
      </ip> 
    </network>
  • To display information about the host:
    # virsh nodeinfo  
    CPU model:           x86_64  
    CPU(s):              8  
    CPU frequency:       1596 MHz  
    CPU socket(s):       2  
    Core(s) per socket:  4  
    Thread(s) per core:  1  
    NUMA cell(s):        1  
    Memory size:         8150212 kB
  • To set the running memory of a KVM:
    # virsh setmem <Name of KVM> <Amount of memory in KB>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章