首页 > 代码库 > virsh 操作kvm虚拟机

virsh 操作kvm虚拟机

#查看你的硬件是否支持虚拟化。命令:

[root@VM_166_143 data]#egrep ‘(vmx|svm)‘ /proc/cpuinfo

#安装基础包

[root@VM_166_143 data]#yum -y install kvm libvirt bridge-utils acpid

#启动依赖服务及加载模块

[root@VM_166_143 data]#sed -i ‘/modprobe kvm/d‘ /etc/rc.local
[root@VM_166_143 data]#echo "modprobe kvm" >>/etc/rc.local
[root@VM_166_143 data]#/etc/init.d/libvirtd start
[root@VM_166_143 data]#/etc/init.d/acpid start 
#检查是否加载虚拟模块
[root@VM_166_143 data]#lsmod |grep kvm
kvm_intel              55496  0
kvm                   337772  1 kvm_intel

#网桥管理命令查看

#配置网桥
[root@VM_166_143 data]# brctl show
bridge name	bridge id		STP enabled	interfaces
virbr0		8000.525400671d5e	yes		virbr0-nic
[root@VM_166_143 data]# virsh iface-bridge eth0 br0
Created bridge br0 with attached device eth0
Bridge interface br0 started
[root@VM_166_143 data]# brctl show
bridge name	bridge id		STP enabled	interfaces
br0		8000.000c298a5fb4	yes		eth0
virbr0		8000.525400671d5e	yes		virbr0-nic
[root@VM_166_143 data]# ifconfig
br0       Link encap:Ethernet  HWaddr 00:0C:29:8A:5F:B4  
          inet addr:192.168.166.143  Bcast:192.168.166.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe8a:5fb4/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:32 errors:0 dropped:0 overruns:0 frame:0
          TX packets:31 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:2698 (2.6 KiB)  TX bytes:3518 (3.4 KiB)

eth0      Link encap:Ethernet  HWaddr 00:0C:29:8A:5F:B4  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:21519 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10302 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:29679394 (28.3 MiB)  TX bytes:1309503 (1.2 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:4 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:212 (212.0 b)  TX bytes:212 (212.0 b)

virbr0    Link encap:Ethernet  HWaddr 52:54:00:67:1D:5E  
          inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

#创建软链接,并创建虚拟机存放目录

[root@VM_166_143 data]# ln -sv  /usr/libexec/qemu-kvm  /usr/bin/qemu-kvm
`/usr/bin/qemu-kvm‘ -> `/usr/libexec/qemu-kvm‘
[root@VM_166_143 data]# mkdir -p /data/kvm/
[root@VM_166_143 data]# cd /data/kvm
[root@VM_166_143 data]# mkdir test;cd test

#创建虚拟机磁盘文件

[root@VM_166_143 test]# qemu-img create -o preallocation=metadata -f qcow2 linux.qcow2 50G

#创建xml模板文件

[root@VM_166_143 data]# cat test.xml
<domain type=‘kvm‘>
  <name>demo</name>  
  <memory>1048576</memory>
  <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/libexec/qemu-kvm</emulator>
    <disk type=‘file‘ device=‘disk‘> 
      <driver name=‘qemu‘ type=‘qcow2‘/>
      <source file=‘/data/kvm/test/linux.qcow2‘/>
      <target dev=‘hda‘ bus=‘ide‘/>
    </disk>
    <interface type=‘bridge‘>
      <source bridge=‘br0‘/>
    </interface>
    <input type =‘tablet‘ bus=‘usb‘/>
    <input type=‘mouse‘ bus=‘ps2‘/>
    <graphics type=‘vnc‘ port=‘-1‘ listen=‘0.0.0.0‘ keymap=‘en-us‘/>
    <serial type=‘pty‘>
     <target port=‘0‘/>
    </serial>
    <console type=‘pty‘>
      <target type=‘serial‘ port=‘0‘/>
    </console>
  </devices>
</domain>

#创建虚拟机,不建议使用virsh create,停机后,virsh list --all查看不到虚拟机信息,推荐使用virsh define+virsh start命令

[root@VM_166_143 data]# virsh create test.xml  
或者
[root@VM_166_143 data]# virsh define test.xml  
[root@VM_166_143 data]# virsh start test.xml

#常用命令  

#关闭虚拟机
virsh shutdown demo

#取消定义虚拟机
virsh undefine demo

#重启虚拟机
virsh reboot demo

#强制关闭虚拟机
virsh destroy demo

#列出所有虚拟机
virsh list --all

#常见问题 

#Escape character is ^] 
#error: internal error cannot find character device (null)
解决方法
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Virtualization_Host_Configuration_and_Guest_Installation_Guide/App_Domain_Console.html

  

 

  

  

  

  

  

  

  

virsh 操作kvm虚拟机