知乎专栏 |
目录
http://wiki.centos.org/HowTos/KVM
确认处理器是否支持KVM
egrep 'vmx|svm' /proc/cpuinfo
对当前系统做一个全面升级
sudo yum update sudo yum upgrade
Installing
如果你不想安装Virtualization组,想单独安装需要的软件,可是使用下面命令
# yum install qemu-kvm libvirt virt-install bridge-utils
确认kvm已经安装
lsmod | grep kvm# lsmod | grep kvm kvm_intel 138567 0 kvm 441119 1 kvm_intel
Create the disk image
qemu-img create -f qcow2 disk.img 5Gor
dd if=/dev/zero of=disk.img bs=1G count=5# qemu-img create -f qcow2 disk.img 5G Formatting 'disk.img', fmt=qcow2, size=5242880 kB # dd if=/dev/zero of=disk.img bs=1G count=5 5+0 records in 5+0 records out 5368709120 bytes (5.4 GB) copied, 61.0353 seconds, 88.0 MB/s
Creating a virtual machine
/usr/libexec/qemu-kvm -hda disk.img -cdrom archlinux-2009.08-core-x86_64.iso -m 512 -boot d
如果你不在localhost上安装OS,你需要指定vnc,这样你可以远程连接到kvm
[root@scientific ~]# /usr/libexec/qemu-kvm disk.img -cdrom rhel-server-5.6-x86_64-dvd.iso -m 8000 -boot d -vnc :1
[root@scientific ~]# yum install -y virt-manager virt-top virt-v2v virt-viewer or [root@scientific ~]# yum groupinstall 'Virtualization'
[root@scientific ~]# yum install -y tunctl
DHCP
brctl addbr br0 ifconfig eth0 0.0.0.0 brctl addif br0 eth0 dhclient br0 tunctl -b -u root ifconfig tap0 up brctl addif br0 tap0
STATIC IP Address
brctl addbr br0 ifconfig eth0 0.0.0.0 brctl addif br0 eth0 ifconfig br0 up tunctl -b -u root ifconfig tap0 up brctl addif br0 tap0 ifconfig br0 192.168.1.120 netmask 255.255.255.0 up ip route add default via 192.168.3.1 dev br0
[root@scientific ~]# ip route 192.168.3.0/24 dev br0 proto kernel scope link src 192.168.3.43 192.168.3.0/24 dev tap0 proto kernel scope link src 192.168.3.21 default via 192.168.3.1 dev br0 [root@scientific ~]# brctl show bridge name bridge id STP enabled interfaces br0 8000.4ea7e4cf4633 no eth0 tap0 br06499 8000.000000000000 no
启动KVM
指定网络参数 -net nic -net tap,ifname=tap0,script=no/usr/libexec/qemu-kvm -hda disk.img -m 8000 -net nic -net tap,ifname=tap0,script=no -vnc :1 /usr/libexec/qemu-kvm -hda disk.img -m 8000 -net nic -net tap,ifname=tap0,script=no -nographic -daemonize
yum install -y libvirt python-virtinst virt-manager
命令行安装
sudo virt-install --connect qemu:///system -n Ubuntu32 -r 512 --vcpus=1 -f /dev/sda3 -s 9 -c Desktop/ubuntu-10.10-desktop-i386.iso --vnc --noautoconsole --os-type linux --os-variant generic26 --accelerate --network=bridge:virbr0 --hvm sudo virt-install --connect qemu:///system -n Ubuntu32 -r 512 --vcpus=1 -f ~/ubuntu32.qcow2 -s 12 -c esktop/ubuntu-10.10-desktop-i386.iso --vnc --noautoconsole --os-type linux --os-variant generic26 --accelerate --network=bridge:br0 --hvm
进入GUI工具
virsh -c qemu:///system list sudo virt-manager
确认你的CPU是否支持KVM
egrep '(vmx|svm)' –color=always /proc/cpuinfo
sudo apt-get install kvm libvirt-bin ubuntu-vm-builder bridge-utils kvm-pxe uml-utilities
kvm gui
sudo apt-get install ubuntu-virt-server ubuntu-virt-mgmt ubuntu-vm-builder python-vm-builder kvm-pxe
# yum groupinstall Virtualization # yum groupinstall "Virtualization Client" # yum groupinstall "Virtualization Platform" # /etc/init.d/libvirtd start Starting libvirtd daemon: [ OK ]
[root@scientific ~]# yum groupinstall 'Virtualization' 'Virtualization Client' 'Virtualization Platform' 'Virtualization Tools'
$ sudo virsh -c qemu:///system list Id Name State ---------------------------------- 1 Ubuntu running 2 Ubuntu-Server running # virsh list Id Name State ---------------------------------------------------- 1 Ubuntu running 2 CentOS6.4 running
# virsh 显示虚拟机列表: virsh # list --all 启动虚拟机: virsh # start [name] 关闭虚拟机: virsh # shutdown [name] 重启虚拟机: virsh # reboot [name] 指定虚拟机开机自动启动: virsh # autostart [name]
例 115.1. virsh
virsh # list --all Id Name State ---------------------------------------------------- - CentOS6.4 shut off - FreeBSD shut off - Test shut off - Ubuntu shut off - www shut off virsh # start Ubuntu Domain Ubuntu started virsh # list --all Id Name State ---------------------------------------------------- 1 Ubuntu running - CentOS6.4 shut off - FreeBSD shut off - Test shut off - www shut off virsh # quit
# virsh list Id Name State ---------------------------------------------------- 2 monitor running # virsh console monitor Connected to domain monitor Escape character is ^]
Ctrl + ] 推出 console
Error: No hypervisor options were found for this connection
[root@r910 etc]# grep kvm /var/log/messages Jun 21 15:28:05 r910 udevd[803]: specified group 'kvm' unknown Jun 21 15:28:05 r910 udevd[803]: specified group 'kvm' unknown Jun 21 15:28:07 r910 kernel: kvm: disabled by bios Jun 21 15:28:07 r910 yum: Installed: 2:qemu-kvm-0.12.1.2-2.1 13.el6_0.8.x86_64 Jun 21 15:58:27 r910 kernel: kvm: disabled by bios Jun 21 16:48:08 r910 kernel: kvm: disabled by bios Jun 21 17:15:42 r910 yum: Erased: qemu-kvm Jun 21 17:20:00 r910 kernel: kvm: disabled by bios Jun 21 17:20:00 r910 yum: Installed: 2:qemu-kvm-0.12.1.2-2.1 13.el6_0.8.x86_64
进入BIOS启用虚拟化
# lspci 00:00.0 Host bridge: Intel Corporation 440BX/ZX/DX - 82443BX/ZX/DX Host bridge (rev 01) 00:01.0 PCI bridge: Intel Corporation 440BX/ZX/DX - 82443BX/ZX/DX AGP bridge (rev 01) 00:07.0 ISA bridge: Intel Corporation 82371AB/EB/MB PIIX4 ISA (rev 08) 00:07.1 IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01) 00:07.3 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 08) 00:07.7 System peripheral: VMware Virtual Machine Communication Interface (rev 10) 00:0f.0 VGA compatible controller: VMware SVGA II Adapter 00:10.0 SCSI storage controller: LSI Logic / Symbios Logic 53c1030 PCI-X Fusion-MPT Dual Ultra320 SCSI (rev 01) 00:11.0 PCI bridge: VMware PCI bridge (rev 02) 00:15.0 PCI bridge: VMware PCI Express Root Port (rev 01) 00:15.1 PCI bridge: VMware PCI Express Root Port (rev 01) 00:15.2 PCI bridge: VMware PCI Express Root Port (rev 01) 00:15.3 PCI bridge: VMware PCI Express Root Port (rev 01) 00:15.4 PCI bridge: VMware PCI Express Root Port (rev 01) 00:15.5 PCI bridge: VMware PCI Express Root Port (rev 01) 00:15.6 PCI bridge: VMware PCI Express Root Port (rev 01) 00:15.7 PCI bridge: VMware PCI Express Root Port (rev 01) 00:16.0 PCI bridge: VMware PCI Express Root Port (rev 01) 00:16.1 PCI bridge: VMware PCI Express Root Port (rev 01) 00:16.2 PCI bridge: VMware PCI Express Root Port (rev 01) 00:16.3 PCI bridge: VMware PCI Express Root Port (rev 01) 00:16.4 PCI bridge: VMware PCI Express Root Port (rev 01) 00:16.5 PCI bridge: VMware PCI Express Root Port (rev 01) 00:16.6 PCI bridge: VMware PCI Express Root Port (rev 01) 00:16.7 PCI bridge: VMware PCI Express Root Port (rev 01) 00:17.0 PCI bridge: VMware PCI Express Root Port (rev 01) 00:17.1 PCI bridge: VMware PCI Express Root Port (rev 01) 00:17.2 PCI bridge: VMware PCI Express Root Port (rev 01) 00:17.3 PCI bridge: VMware PCI Express Root Port (rev 01) 00:17.4 PCI bridge: VMware PCI Express Root Port (rev 01) 00:17.5 PCI bridge: VMware PCI Express Root Port (rev 01) 00:17.6 PCI bridge: VMware PCI Express Root Port (rev 01) 00:17.7 PCI bridge: VMware PCI Express Root Port (rev 01) 00:18.0 PCI bridge: VMware PCI Express Root Port (rev 01) 00:18.1 PCI bridge: VMware PCI Express Root Port (rev 01) 00:18.2 PCI bridge: VMware PCI Express Root Port (rev 01) 00:18.3 PCI bridge: VMware PCI Express Root Port (rev 01) 00:18.4 PCI bridge: VMware PCI Express Root Port (rev 01) 00:18.5 PCI bridge: VMware PCI Express Root Port (rev 01) 00:18.6 PCI bridge: VMware PCI Express Root Port (rev 01) 00:18.7 PCI bridge: VMware PCI Express Root Port (rev 01) 03:00.0 Ethernet controller: VMware VMXNET3 Ethernet Controller (rev 01)
# dmesg | grep vm kvm-clock: Using msrs 4b564d01 and 4b564d00 kvm-clock: cpu 0, msr 0:1c28841, boot clock kvm-clock: cpu 0, msr 0:2216841, primary cpu clock kvm-stealtime: cpu 0, msr 220e880 kvm-clock: cpu 1, msr 0:2316841, secondary cpu clock kvm-stealtime: cpu 1, msr 230e880 sizeof(vma)=200 bytes Switching to clocksource kvm-clock