Linux 虚拟机--KVM安装和配置

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux 虚拟机--KVM安装和配置相关的知识,希望对你有一定的参考价值。

一、配置环境

软件包组:

virtualization            提供虚拟机的环境,主要包含qemu-kvm

virtualization-client      管理和安装虚拟机实例的客户端,主要有Python-virtinst,virt-manager,virt-viewer

virtualization-platform    提供访问和控制虚拟客户端的接口,主要有libvirt,libvirt-client

virtualization-tools      管理离线虚拟机镜像的工具,主要有libguestfs

 

# cat /proc/cpuinfo |grep flags    需要全虚拟化支持为vmx(Inter)或svm(AMD)

 

二、配置本地yum源

[[email protected] yum.repos.d]# cat local.repo 
[rhel_6_iso]

name=local iso

baseurl=file:///media

gpgcheck=1

gpgkey=file:///media/RPM-GPG-KEY-redhat-release

[HighAvailability]
name=HighAvailability
baseurl=file:///media/HighAvailability
gpgcheck=1
gpgkey=file:///media/RPM-GPG-KEY-redhat-release


[LoadBalancer]
name=LoadBalancer
baseurl=file:///media/LoadBalancer
gpgcheck=1
gpgkey=file:///media/RPM-GPG-KEY-redhat-release


[ResilientStorage]
name=ResilientStorage
baseurl=file:///media/ResilientStorage
gpgcheck=1
gpgkey=file:///media/RPM-GPG-KEY-redhat-release


[ScalableFileSystem]
name=ScalableFileSystem
baseurl=file:///media/ScalableFileSystem
gpgcheck=1
gpgkey=file:///media/RPM-GPG-KEY-redhat-release

 

 

三、安装KVM软件包

# yum groupinstall "Virtualization" "Virtualization Client" "Virtualzation Platform" -y

 

 

四、安装虚拟机

1、配置网卡桥接

由于KVM创建虚拟机时,网络默认使用nat模式,将使得虚拟机能访问外部网络,外部网络无法访问虚拟机;为了外部网络能与虚拟机正常通信,在此配置网卡桥接,创建虚拟机是使用桥接网络。

# cd /etc/sysconfig/network-scripts/
# cp ifcfg-eth0 ifcfg-br0
# cat ifcfg-eth0 
DEVICE="eth0"
BOOTPROTO="static"
#DNS1="192.168.62.110"
#HWADDR="00:50:56:25:AE:19"
#IPADDR="192.168.62.201"
#IPV6INIT="yes"
#NETMASK="255.255.255.0"
#NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Ethernet"
BRIDGE=br0

# cat ifcfg-br0 
DEVICE="br0"
BOOTPROTO="static"
DNS1="192.168.62.110"
IPADDR="192.168.62.201"
IPV6INIT="yes"
NETMASK="255.255.255.0"
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Bridge"

 

重启网卡后可以看到eth0与br0建立桥接

# /etc/init.d/network restart
Shutting down interface eth0:  bridge br0 does not exist!
                                                           [  OK  ]
Shutting down interface eth1:                              [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth0:                                [  OK  ]
Bringing up interface eth1:  
Determining IP information for eth1... done.
                                                           [  OK  ]
Bringing up interface br0:  Determining if ip address 192.168.62.201 is already in use for device br0...
                                                           [  OK  ]
[[email protected] network-scripts]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:50:56:25:ae:19 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::250:56ff:fe25:ae19/64 scope link 
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:68:3b:ef brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.63/24 brd 192.168.1.255 scope global eth1
    inet6 fe80::20c:29ff:fe68:3bef/64 scope link 
       valid_lft forever preferred_lft forever
4: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN 
    link/ether 00:50:56:25:ae:19 brd ff:ff:ff:ff:ff:ff
    inet 192.168.62.201/24 brd 192.168.62.255 scope global br0
    inet6 fe80::250:56ff:fe25:ae19/64 scope link 
       valid_lft forever preferred_lft forever
[[email protected] network-scripts]# brctl show
bridge name    bridge id        STP enabled    interfaces
br0        8000.00505625ae19    no        eth0

 

2、创建虚拟机

# virt-install --name vm01 --boot network,cdrom,menu=on --ram 512 --vcpus=1 --os-variant=rhel6 --accelerate --cdrom=/dev/sr0 --disk path=/opt/kvmimg/vm01.img,size=5,bus=virtio --bridge=br0,model=virtio --autostart --vnc --vncport=5900 --vnclisten=0.0.0.0
ERROR    internal error Unable to locate libvirtd daemon in /usr/sbin (to override, set $LIBVIRTD_PATH to the name of the libvirtd binary)

此时提示错误,是由于没有安装libvirt,

# yum install libvirt* -y
# /etc/init.d/libvirtd start
Starting libvirtd daemon:                                  [  OK  ]

安装完成后并启动,否则提示错误ERROR    Failed to connect socket to ‘/var/run/libvirt/libvirt-sock‘: No such file or directory

 

重新创建虚拟机

# virt-install --name vm01 --boot network,cdrom,menu=on --ram 512 --vcpus=1 --os-variant=rhel6 --accelerate --cdrom=/dev/sr0 --disk path=/opt/kvmimg/vm01.img,size=5,bus=virtio --bridge=br0,model=virtio --autostart --vnc --vncport=5900 --vnclisten=0.0.0.0

Starting install...
Creating storage file vm01.img                                                                                                                                                                                                                         | 5.0 GB     00:00     
Creating domain...                                                                                                                                                                                                                                     |    0 B     00:00     
Cannot open display: 
Run virt-viewer --help to see a full list of available command line options
Domain installation still in progress. You can reconnect to 
the console to complete the installation process.

关于virt-intall详细参数请查看《kvm virt-install命令选项参数说明

 

此时就可以用vnc客户端连接<宿主机ip>:5900即可使用图形界面安装系统。安装过程忽略

以上是关于Linux 虚拟机--KVM安装和配置的主要内容,如果未能解决你的问题,请参考以下文章

基于Linux命令行KVM虚拟机的安装配置与基本使用

Linux KVM 安装使用手册

Linux之部署KVM虚拟化平台

Linux系统下KVM虚拟机的基本管理和操作

Linux实战之KVM虚拟机安装爱快软路由系统

centos7虚拟机怎么安装kvm