筆記內容:CentOS7上安裝並配置KVM,以及經過KVM安裝CentOS系統
筆記日期:2018-03-07node
首先咱們須要一臺配置較高的CentOS7虛擬機,固然用物理機也能夠,我這裏用的是虛擬機,配置以下:
linux
硬盤須要新增一個,其中的新硬盤用於在KVM裏安裝操做系統時須要用到,內存的大小最好有4個G,最低2個G,由於我這裏物理機的內存有16G因此就給分了8G。具體的須要根據你物理機的配置進行分配,CPU的核心最少要給2個。vim
而後最好作一個快照,中途出現什麼問題方便重置:
centos
進入到系統後,關閉iptables或者firewalld,關閉selinux。而且檢查cpu參數是否支持虛擬化:bash
[root@localhost ~]# systemctl stop firewalld [root@localhost ~]# systemctl stop iptables [root@localhost ~]# systemctl disable firewalld [root@localhost ~]# systemctl disable iptables [root@localhost ~]# getenforce Disabled [root@localhost ~]# grep -Ei 'vmx|svm' /proc/cpuinfo
若是有過濾出vmx或svm關鍵字就表明支持虛擬化,vmx是Intel的CPU,svm是AMD的CPU。網絡
而後格式化新磁盤,掛載到/kvm_data,固然這個路徑能夠自定義,我這裏使用的是/kvm_data。app
[root@localhost ~]# fdisk -l |grep Disk Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 sectors Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors Disk label type: dos Disk identifier: 0x000f0eb3 Disk /dev/mapper/centos-root: 19.8 GB, 19826475008 bytes, 38723584 sectors Disk /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 sectors [root@localhost ~]# mkfs.ext4 /dev/sdb # 會有詢問,輸入y便可 [root@localhost ~]# blkid /dev/sdb # 檢查是否已格式化 /dev/sdb: UUID="5a7cf162-f8f3-4d02-ad72-17bde010efc8" TYPE="ext4" [root@localhost ~]# mkdir /kvm_data # 建立掛載目錄 [root@localhost ~]# mount /dev/sdb /kvm_data # 將新磁盤掛載到/kvm_data目錄下 [root@localhost ~]# vim /etc/fstab # 配置開機時掛載 /dev/sdb /kvm_data ext4 defaults 0 0
最後就是安裝kvm:dom
[root@localhost ~]# yum install -y virt-* libvirt bridge-utils qemu-img
安裝完KVM以後,須要配置一下網卡,增長一個橋接網卡:socket
[root@localhost ~]# cd /etc/sysconfig/network-scripts/ [root@localhost /etc/sysconfig/network-scripts]# cp ifcfg-eno16777728 ifcfg-br0 # 拷貝當前的網卡文件 [root@localhost /etc/sysconfig/network-scripts]# vim ifcfg-eno16777728 # 修改文件內容以下 TYPE=Ethernet BOOTPROTO=dhcp DEFROUTE=yes PEERDNS=yes PEERROUTES=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_PEERDNS=yes IPV6_PEERROUTES=yes IPV6_FAILURE_FATAL=no NAME=eno16777728 DEVICE=eno16777728 ONBOOT=yes BRIDGE=br0 [root@localhost /etc/sysconfig/network-scripts]# vim ifcfg-br0 # 修改文件內容以下 TYPE=Bridge BOOTPROTO=dhcp DEFROUTE=yes PEERDNS=yes PEERROUTES=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_PEERDNS=yes IPV6_PEERROUTES=yes IPV6_FAILURE_FATAL=no NAME=br0 DEVICE=br0 ONBOOT=yes [root@localhost /etc/sysconfig/network-scripts]# systemctl restart network # 重啓服務
重啓完服務後,執行ifconfig命令能夠看到此時eno16777728網卡的IP到br0網卡上了,這樣咱們的網卡就配置完成了:ide
[root@localhost /etc/sysconfig/network-scripts]# ifconfig br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.77.130 netmask 255.255.255.0 broadcast 192.168.77.255 inet6 fe80::20c:29ff:fef1:912c prefixlen 64 scopeid 0x20<link> ether 00:0c:29:f1:91:2c txqueuelen 0 (Ethernet) RX packets 51 bytes 8341 (8.1 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 27 bytes 2710 (2.6 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ether 00:0c:29:f1:91:2c txqueuelen 1000 (Ethernet) RX packets 147615 bytes 168580073 (160.7 MiB) RX errors 0 dropped 8 overruns 0 frame 0 TX packets 45008 bytes 3866579 (3.6 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 0 (Local Loopback) RX packets 2459 bytes 1125227 (1.0 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2459 bytes 1125227 (1.0 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@localhost /etc/sysconfig/network-scripts]#
接下來咱們就能夠啓動KVM的服務了:
[root@localhost ~]# lsmod |grep kvm # 檢查KVM模塊是否加載 kvm_intel 162153 0 kvm 525259 1 kvm_intel [root@localhost ~]# systemctl start libvirtd # 啓動libvirtd服務 [root@localhost ~]# ps aux |grep libvirtd # 檢查服務進程 root 5744 2.2 0.1 614840 14120 ? Ssl 23:02 0:00 /usr/sbin/libvirtd root 5872 0.0 0.0 112664 964 pts/1 R+ 23:02 0:00 grep --color=auto libvirtd [root@localhost ~]# brctl show # 能夠看到兩個網卡 bridge name bridge id STP enabled interfaces br0 8000.000c29f1912c no eno16777728 # 咱們配置的橋接網卡 virbr0 8000.525400240b50 yes virbr0-nic # NAT模式的網卡 [root@localhost ~]#
將服務成功啓動後,咱們就可使用KVM安裝虛擬機了,首先須要準備一個操做系統的鏡像文件,我這裏用的是CentOS7的鏡像文件:
[root@localhost ~]# cd /tmp/ [root@localhost /tmp]# ls |grep CentOS CentOS-7-x86_64-DVD-1511.iso [root@localhost /tmp]#
使用命令行安裝這個CentOS7鏡像文件:
[root@localhost ~]# virt-install --name=study01 --memory=512,maxmemory=1024 --vcpus=1,maxvcpus=2 --os-type=linux --os-variant=rhel7 --location=/tmp/CentOS-7-x86_64-DVD-1511.iso --disk path=/kvm_data/study01.img,size=10 --bridge=br0 --graphics=none --console=pty,target_type=serial --extra-args="console=tty0 console=ttyS0"
命令說明:
開始安裝後,正常的狀況下會進入到這樣一個界面:
這是讓你配置語言、時區、安裝源、網絡、密碼等信息,就和咱們在VMware裏安裝CentOS虛擬機是同樣的,只不過這個是命令行形式,而VMware裏是圖形界面罷了。
首先設置語言,按數字1,回車進入如下界面:
例如我要選Chinese就按數字68並回車便可,回車以後會讓你選擇是中文簡體仍是繁體,也是按下相應的數字並回車便可:
配置完成以後又會再次回到配置界面,此次咱們來設置時區:
設置系統安裝盤:
設置root用戶的密碼:
最後開始安裝系統:
安裝完成以後就會中止在如下這個界面,按一下回車便可,這時就會重啓安裝完的系統:
重啓成功以後就會入到登陸界面,能夠看到我這裏是成功登陸的:
這時咱們是處於一個虛擬終端的,由於安裝了虛擬機,若是要退出來的話,應該說是切出來,按 Ctrl + ]
便可。
切出虛擬機後,能夠看到/kvm_data/目錄下多了一個虛擬機的磁盤目錄:
[root@localhost ~]# ls /kvm_data/ lost+found study01.img [root@localhost ~]#
查看KVM進程:
[root@localhost ~]# ps axu |grep kvm root 880 0.0 0.0 0 0 ? S< Mar07 0:00 [kvm-irqfd-clean] qemu 6528 6.9 9.1 1568008 734216 ? Sl 00:15 0:40 /usr/libexec/qemu-kvm -name study01 -S -machine pc-i440fx-rhel7.0.0,accel=kvm,usb=off,dump-guest-core=off -cpu Haswell,-hle,-rtm -m 1024 -realtime mlock=off -smp 1,maxcpus=2,sockets=2,cores=1,threads=1 -uuid eeedcd47-1546-4e5f-ab2a-f62deb0838cf -display none -no-user-config -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/domain-2-study01/monitor.sock,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc,driftfix=slew -global kvm-pit.lost_tick_policy=delay -no-hpet -no-shutdown -global PIIX4_PM.disable_s3=1 -global PIIX4_PM.disable_s4=1 -boot strict=on -device ich9-usb-ehci1,id=usb,bus=pci.0,addr=0x4.0x7 -device ich9-usb-uhci1,masterbus=usb.0,firstport=0,bus=pci.0,multifunction=on,addr=0x4 -device ich9-usb-uhci2,masterbus=usb.0,firstport=2,bus=pci.0,addr=0x4.0x1 -device ich9-usb-uhci3,masterbus=usb.0,firstport=4,bus=pci.0,addr=0x4.0x2 -device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x5 -drive file=/kvm_data/study01.img,format=qcow2,if=none,id=drive-virtio-disk0 -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x6,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1 -drive if=none,id=drive-ide0-0-0,readonly=on -device ide-cd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0 -netdev tap,fd=25,id=hostnet0,vhost=on,vhostfd=27 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:65:d3:3f,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -chardev socket,id=charchannel0,path=/var/lib/libvirt/qemu/channel/target/domain-2-study01/org.qemu.guest_agent.0,server,nowait -device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=org.qemu.guest_agent.0 -device usb-tablet,id=input0,bus=usb.0,port=1 -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x7 -msg timestamp=on root 6534 0.0 0.0 0 0 ? S 00:15 0:00 [kvm-pit/6528] root 6687 0.0 0.0 112668 960 pts/1 S+ 00:25 0:00 grep --color=auto kvm [root@localhost ~]#
使用如下命令能夠列出當前有多少個虛擬機,以及其狀態:
[root@localhost ~]# virsh list Id Name State ---------------------------------------------------- 2 study01 running [root@localhost ~]#
以上這個命令沒法列出關機狀態的虛擬機,須要列出關機狀態的虛擬機須要加多一個--all參數:
[root@localhost ~]# virsh list --all Id Name State ---------------------------------------------------- 2 study01 running [root@localhost ~]#
查看虛擬機配置文件:
[root@localhost ~]# ls /etc/libvirt/qemu/ networks study01.xml [root@localhost ~]# ls /etc/libvirt/qemu/networks/ autostart default.xml [root@localhost ~]# ls /etc/libvirt/qemu/networks/autostart/ default.xml [root@localhost ~]#
如下介紹一下管理虛擬機的一些經常使用命令:
[root@localhost ~]# virsh console study01 # 進入指定的虛擬機,進入的時候還須要按一下回車 [root@localhost ~]# virsh start study01 # 啓動虛擬機 [root@localhost ~]# virsh shutdown study01 # 關閉虛擬機 [root@localhost ~]# virsh destroy study01 # 強制中止虛擬機 [root@localhost ~]# virsh undefine study01 # 完全銷燬虛擬機,會刪除虛擬機配置文件,但不會刪除虛擬磁盤 [root@localhost ~]# virsh autostart study01 # 設置宿主機開機時該虛擬機也開機 [root@localhost ~]# virsh autostart --disable study01 # 解除開機啓動 [root@localhost ~]# virsh suspend study01 # 掛起虛擬機 [root@localhost ~]# virsh resume study01 # 恢復掛起的虛擬機
進入到剛剛安裝的虛擬機裏配置一下IP:
[root@localhost ~]# virsh console study01 Connected to domain study01 Escape character is ^] [root@localhost ~]# ip addr # 此時尚未能聯網的IP 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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 52:54:00:65:d3:3f brd ff:ff:ff:ff:ff:ff [root@localhost ~]# dhclient eth0 # 獲取IP地址 [root@localhost ~]# ip addr # 此時能夠看到已經有IP地址了 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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 52:54:00:65:d3:3f brd ff:ff:ff:ff:ff:ff inet 192.168.77.140/24 brd 192.168.77.255 scope global dynamic eth0 valid_lft 1793sec preferred_lft 1793sec inet6 fe80::5054:ff:fe65:d33f/64 scope link valid_lft forever preferred_lft forever [root@localhost ~]# yum install -y net-tools # 獲取到IP能聯網後安裝網絡管理工具 [root@localhost ~]# ifconfig # 安裝完以後就可使用ifconfig命令了 eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.77.140 netmask 255.255.255.0 broadcast 192.168.77.255 inet6 fe80::5054:ff:fe65:d33f prefixlen 64 scopeid 0x20<link> ether 52:54:00:65:d3:3f txqueuelen 1000 (Ethernet) RX packets 4404 bytes 13614102 (12.9 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2480 bytes 139445 (136.1 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 0 (Local Loopback) 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 [root@localhost ~]# [root@localhost ~]# cd /etc/sysconfig/network-scripts/ [root@localhost network-scripts]# vi ifcfg-eth0 # 修改ONBOOT的值爲yes ONBOOT=yes [root@localhost network-scripts]# systemctl restart network # 重啓網卡服務
以上咱們就完成了虛擬機的安裝以及配置虛擬機的IP,這時就能夠像往常同樣使用這臺虛擬機了。