51 kvm技术基础使用qemu-kvm管理kvm虚拟机
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了51 kvm技术基础使用qemu-kvm管理kvm虚拟机相关的知识,希望对你有一定的参考价值。
01 kvm技术基础
配置环境:
node1 192.168.1.121 CentOS release 6.7
1、KVM安装
#确保CPU支持HVM
[[email protected] ~]# grep -E --color=auto "(vmx|svm)" /proc/cpuinfo
#装载模块
[[email protected] ~]# modprobe kvm
[[email protected] ~]# modprobe kvm-intel
[[email protected] ~]# yum grouplist | grep -i ‘virtualization‘
Virtualization
Virtualization Client
Virtualization Platform
Virtualization Tools
[[email protected] ~]# yum install qemu-kvm -y
[[email protected] ~]# ln -sv /usr/libexec/qemu-kvm /usr/bin/qemu-kvm
02 使用qemu-kvm管理kvm虚拟机
配置环境:
node1 192.168.1.131 CentOS Linux release 7.2
1、KVM安装
[[email protected] ~]# modinfo kvm
[[email protected] ~]# modinfo kvm-intel
[[email protected] ~]# modinfo kvm-amd
[[email protected] ~]# modprobe kvm
[[email protected] ~]# lsmod | grep kvm
kvm_intel 162153 0
kvm 525259 1 kvm_intel
#确保CPU支持HVM
[[email protected] ~]# grep -E "(vmx|svm)" /proc/cpuinfo
[[email protected] ~]# yum install qemu-kvm -y
[[email protected] ~]# ln -s /usr/libexec/qemu-kvm /usr/bin/
[[email protected] ~]# qemu-img info cirros-no_cloud-0.3.0-i386-disk.img
image: cirros-no_cloud-0.3.0-i386-disk.img
file format: qcow2
virtual size: 39M (41126400 bytes)
disk size: 11M
cluster_size: 65536
Format specific information:
compat: 0.10
#启动虚拟机
[[email protected] ~]# qemu-kvm -m 128 -smp 2 -name "test" -hda cirros-no_cloud-0.3.0-i386-disk.img
#安装vnc
[[email protected] ~]# yum install tigervnc
#连接至打开的虚拟机
[[email protected] ~]# vncviewer :5900
[[email protected] ~]# qemu-kvm -m 128 -cpu host -smp 2 -name "test" -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback
[[email protected] ~]# mkdir -p /images/windows
[[email protected] ~]# qemu-img -o ? -f qcow2 /images/windows/winxp.qcow2
[[email protected] ~]# qemu-img create -o size=20G,preallocation=metadata -f qcow2 /images/windows/winxp.qcow2
[[email protected] ~]# qemu-kvm -m 512 -smp 2 -cpu host -driver file=/images/windows/winxp.qcow2,media=disk -driver file=/root/winxp_ghost.iso media=cdrom order=dc,once=d
03 使用qemu-kvm管理kvm虚拟机
[[email protected] ~]# qemu-kvm -m 128 -cpu host -smp 2 -name "test" -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback -vnc 192.168.1.131:1,password -monitor stdio
[[email protected] ~]# qemu-kvm -m 128 -cpu host -smp 2 -name "test" -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback -nographic -monitor stdio
1、管理桥设备
#添加桥
[[email protected] ~]# brctl addbr br0
[[email protected] ~]# ifconfig -a
br0: flags=4098<BROADCAST,MULTICAST> mtu 1500
ether 32:5c:c1:7e:42:c7 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.131 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:fe50:27c4 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:50:27:c4 txqueuelen 1000 (Ethernet)
RX packets 17736 bytes 9576805 (9.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 13771 bytes 9831475 (9.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[[email protected] ~]# brctl stp br0 off
#显示桥
[[email protected] ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000000000000 no
#删除桥
[[email protected] ~]# brctl delbr br0
#激活桥
[[email protected] ~]# ifconfig br0 up
[[email protected] ~]# ip link set dev br0 down
[[email protected] ~]# ip link set dev br0 up
[[email protected] ~]# qemu-kvm -m 128 -cpu host -smp 2 -name "test" -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback -nographic -net nic -net tap,name=vif0.0,script=no
[[email protected] ~]# vim /etc/qemu-ifup
#!/bin/bash
#
bridge=br0
if [ -n "$1" ];then
ip link set $1 up
sleep 1
brctl addif $bridge $1
[ $? -eq 0 ] && exit 0 || exit 1
else
echo "Error: no interface specified."
exit 1
fi
[[email protected] ~]# chmod +x /etc/qemu-ifup
[[email protected] ~]# vim /etc/qemu-ifdown
#!/bin/bash
#
bridge=br0
if [ -n "$1" ];then
brctl delif $bridge $1
ip link set $1 down
exit 0
else
echo "Error: no interface specified."
exit 1
fi
[[email protected] ~]# chmod +x /etc/qemu-ifdown
[[email protected] ~]# qemu-kvm -m 128 -cpu host -smp 2 -name test -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback -nographic -net nic -net tap,ifname=vif0.0,script=/etc/qemu-ifup
[[email protected] ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.12188d08e408 no vif0.0
[[email protected] ~]# qemu-kvm -m 128 -cpu host -smp 2 -name test -drive file=cirros-no_cloud-0.3.0-i386-disk.img,if=virtio,media=disk,format=qcow2,cache=writeback -nographic -net nic -net tap,ifname=vif1.0,script=/etc/qemu-ifup
[[email protected] ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.12188d08e408 no vif0.0
vif1.0
[[email protected] ~]# ip link add veth1.0 type veth peer veth1.1
[[email protected] ~]# ip link set veth0 up
[[email protected] ~]# ip link set veth1.0 up
[[email protected] ~]# brctl addif br0 veth1.0
[[email protected] ~]# ifconfig veth0 172.16.100.67/24
04 使用qemu-kvm管理kvm虚拟机
[[email protected] ~]# mkdir /images/centos
[[email protected] ~]# qemu-img create /images/centos/centos6.img -o size=120G,preallocation=metadata -f qcow2
[[email protected] ~]# qemu-kvm -m 512 -smp 2 -name centos -drive file=/images/centos/centos6.img,media=disk -net nic,macaddr=52:54:00:55:32:19 -net tap,ifname=centos6.0,script=/etc/qemu-ifup -boot order=nc,once=n
[[email protected] ~]# qemu-kvm -m 512 -smp 2 -name centos -drive file=/images/centos/centos6.img,media=disk,if=virtio -net nic,model=virtio,macaddr=52:54:00:55:32:19 -net tap,ifname=centos6.0,script=/etc/qemu-ifup -boot order=nc,once=n
本文出自 “追梦” 博客,请务必保留此出处http://sihua.blog.51cto.com/377227/1887065
以上是关于51 kvm技术基础使用qemu-kvm管理kvm虚拟机的主要内容,如果未能解决你的问题,请参考以下文章