天天看點

CentOS7 安裝 KVM

轉自

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       

繼續閱讀