转自
http://1.chaoxu.sinaapp.com/archives/1739
前言
看过网上的很多安装教程,基本都是一条命令安装后,便直接创建虚拟机。其实有很多的坑要么是不能创建,要么就是各种失败。所以,在这里,我给出自己的完整笔记,尽量避免大家去跳坑。
1、安装
# yum -y install qemu-kvm libvirt virt-install bridge-utils
2、配置
# vim /etc/libvirt/qemu.conf # Some examples of valid values are: # # user = "qemu" # A user named "qemu" # user = "+0" # Super user (uid=0) # user = "100" # A user named "100" or a user with uid=100 # user = "root" # The group for QEMU processes run by the system instance. It can be # specified in a similar way to user. group = "root" # Whether libvirt should dynamically change file ownership # to match the configured user/group above. Defaults to 1. # Set to 0 to disable file ownership changes. dynamic_ownership = 0 重启服务 # systemctl restart libvirtd
2、加载kvm
# lsmod | grep kvm # systemctl start libvirtd # systemctl enable libvirtd # ip a # nmcli c add type bridge autoconnect yes con-name br0 ifname br0
说明
nmcli是网络管理工具NetworkManager包的命令行工具。
3、编辑网络配置文件
# cd /etc/sysconfig/network-scripts/ # vim ifcfg-br0 DEVICE=br0 TYPE=Bridge BOOTPROTO=none NAME=br0 ONBOOT=yes IPADDR=192.168.1.133 GATEWAY=192.168.1.1 NETMASK=255.255.255.0 DNS1=192.168.1.1 # vim ifcfg-eno16777736 TYPE=Ethernet BRIDGE=br0 BOOTPROTO=none DEFROUTE=yes PEERDNS=yes PEERROUTES=yes NAME=eno16777736 UUID=d250bd30-01e7-4147-abb1-83382d7a7eff DEVICE=eno16777736 ONBOOT=yes
重启网络
# systemctl restart network
4、最后,查看网络
# ip addr 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 valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP qlen 1000 link/ether 00:0c:29:ca:b9:21 brd ff:ff:ff:ff:ff:ff 3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN link/ether 52:54:00:8b:52:5c brd ff:ff:ff:ff:ff:ff inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0 valid_lft forever preferred_lft forever 4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 500 link/ether 52:54:00:8b:52:5c brd ff:ff:ff:ff:ff:ff 6: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP link/ether 00:0c:29:ca:b9:21 brd ff:ff:ff:ff:ff:ff inet 192.168.1.133/24 brd 192.168.1.255 scope global br0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:feca:b921/64 scope link valid_lft forever preferred_lft forever
注意,centos 7默认命令的输出格式,确实不好看,建议安装net-tools工具,使用ifconfig命令查看。
5、一个安装列子
# mkdir -p /var/kvm/images # create a new Storage Pool # virt-install \ --name centos7 \ --ram 4096 \ --disk path=/var/kvm/images/centos7.img,size=30 \ --vcpus 2 \ --os-type linux \ --os-variant rhel7 \ --network bridge=br0 \ --graphics none \ --console pty,target_type=serial \ --location=/data/centos7.iso \ --extra-args 'console=ttyS0,115200n8 serial' Starting install...# start installation
6、、安装spice服务
为了后续使用Spice协议,连接KVM创建的虚拟机,我们需要安装spice服务。
# yum -y install spice-server spice-protocol
7、一个使用Spice的案例
# virsh edit centos7 <domain type='kvm'> <name>centos7</name> <uuid>b38a50ca-a1ae-4d37-ba10-caf1e05b43ce</uuid> <memory unit='KiB'>4194304</memory> <currentMemory unit='KiB'>4194304</currentMemory> <vcpu placement='static'>2</vcpu> . . . # add follows # set any password for "passwd=***" section # specify a uniq number for "sound" section "slot='0x06'" # the "slot='0x02'" in video section is fixed number for graphics <graphics type='spice' port='5900' autoport='no' listen='0.0.0.0' passwd='password'> <listen type='address' address='0.0.0.0'/> </graphics> <sound model='ac97'> <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/> </sound> <video> <model type='qxl' ram='65536' vram='32768' heads='1'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/> </video> <memballoon model='virtio'> <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/> </memballoon> </devices> </domain> # virsh start centos7