天天看點

linux下kvm虛拟機的搭建

安裝kvm虛拟機工具:

yum -y install

qemu-kvm #為kvm提供底層支援

libvirt-client #virsh等軟體

libvirt-daemon #libvirtd服務守護程序

libvirt-daemon-drive-qemu #virsh的qemu驅動

virt-install #系統安裝工具

virt-manager #圖形管理工具

virt-v2v #虛拟機遷移工具

virt-p2v #實體機遷移工具

在真機上添加一條路由轉發,以友善後期的虛拟機通路外網

vim /etc/sysctl.d/70-system.conf

net.ipv4.ip_forward = 1

配置虛拟網卡參數

cd /etc/libvirt/qemu/networks/autostart/

cp default.xml /etc/libvirt/qemu/networks/veth1.xml

cp default.xml /etc/libvirt/qemu/networks/veth2.xml

cd /etc/libvirt/qemu/networks/

vim veth1.xml

例:

<network>

<name>veth1</name>

<forward mode='nat'/>

<bridge name='veth1'/>

<ip address='192.168.1.254' netmask='255.255.255.0'>

<dhcp>

<range start='192.168.1.100' end='192.168.1.200'/>

</dhcp>

</ip>

</network>

vim veth2.xml

<name>veth2</name>

<bridge name='veth2'/>

<ip address='192.168.2.254' netmask='255.255.255.0'>

删除真機防火牆,關閉selinux

yum -y remove firewall

setenforce 0

編輯selinux開機不啟用

vim /etc/selinux/config

編輯虛拟網卡設定

cd /etc/sysconfig/network-scripts/

vim ifcfg-veth1

cp ifcfg-veth1 ifcfg-veth2

vim ifcfg-veth2

vim /etc/sysconfig/network-scripts/ifcfg-veth1

type=ethernet

proxy_method=none

browser_only=no

bootproto=none

defroute=yes

name=veth1

device=veth1

onboot=yes

ipaddr=192.168.1.254

prefix=24

gateway=192.168.1.254

dns1=114.114.114.114

vim /etc/sysconfig/network-scripts/ifcfg-veth2

name=veth2

device=veth2

onboot=no

ipaddr=192.168.2.254

gateway=192.168.2.254

定義虛拟網卡,并啟用

在确定以關閉防火牆及selinux情況下,如有相關報錯,先重新開機真機

virsh net-define veth1.xml

virsh net-define veth2.xml

virsh net-start veth1

virsh net-start veth2

virsh net-autostart veth1

virsh net-autostart veth2

建立一個20g(大小看環境)模闆鏡像檔案

cd /var/lib/libvirt/images/

qemu-img create -f qcow2 node.qcow2 20g

virt-manager

建立新虛拟機

裝centos7 的系統

可以選擇網絡安裝

配置設定空間,選擇自定義,使用之前建立的node.qcow2

分區隻分一個根, 标準分區

語言用預設英文

關閉kdump

進入虛拟機檢視ip位址,veth1之前定義是dhcp配置設定的ip位址

拷貝真機yum源至虛拟機

在虛拟機上安裝所需的工具

yum -y install net-tools vim-enhanced iproute bash-completion

vi /etc/sysconfig/network

nozeroconf="yes"

vi /etc/sysconfig/network-scripts/ifcfg-eth0

name="eth0"

netboot=yes

ipv6init=yes

bootproto=dhcp

device="eth0"

vi /etc/sysconfig/grub

添加一行,以友善真機console字元連接配接

grub_cmdline_linux="console=tty0 console=ttys0,115200n8"

應用該配置檔案:

grub2-mkconfig -o /boot/grub2/grub.cfg

配置模闆鏡像完畢後關機:

halt -p

cd /etc/libvirt/qemu

cp centos7.0.xml /root/

vim /root/centos7.0.xml

9 <name>centos7.0</name>

改成 <name>node0</name>友善執行腳本

以下标注内容均需删除:

10 <uuid>c04d4f3a-a3b9-4ddb-93ea-2361636d6e6f</uuid>

65 <mac address='52:54:00:22:f4:de'/>

所有<address type=....../>

所有<controller type='usb'.......</controller>

因為之前還有一塊veth2虛拟網卡,故添加以下語句:

<interface type='network'>

<source network='veth2'/>

<model type='virtio'/>

</interface>

删除之前安裝定義的模闆

virsh undefine centos7.0

注:以下腳本上傳部落格後seq語句會缺失字元,請自行添加

ln -s /root/kvm.sh /sbin/kvm.sh

ln -s /root/setip.sh /sbin/setip.sh

附:虛拟機建立腳本

vim kvm.sh

#!/bin/bash

read -p "請輸入需要建立的虛拟機數量:" num

read -p "請輸入起始的虛拟機編号:node" x

for i in <code>seq $num</code>

do

cp /root/centos7.0.xml /var/lib/libvirt/qemu/node$[i-1+x].xml

sed -i 's/name&gt;node.&lt;/name&gt;node'$[i-1+x]'&lt;/' /var/lib/libvirt/qemu/node$[i-1+x].xml

sed -i 's/node..qcow2/node'$[i-1+x]'.img/' /var/lib/libvirt/qemu/node$[i-1+x].xml

virsh define /var/lib/libvirt/qemu/node$[i-1+x].xml

qemu-img create -b /var/lib/libvirt/images/node.qcow2 -f qcow2 /var/lib/libvirt/images/node$[i-1+x].img 20g

done

附:字元界面定義虛拟機ip腳本

vim setip.sh

yum -y install expect tcl

read -p "請輸入虛拟機起始編号(1~): node" vm

read -p "請輸入需要建立起始ip位址(例:192.168.2.5):" cip

read -p "請輸入需要建立連續的主機數(1~):" num

ip=<code>echo $cip|awk -f. '{print $4}'</code>

net=<code>echo $cip|awk -f. '{print $1"."$2"."$3}'</code>

case $net in

192.168.1)

cnet=eth0;;

192.168.2)

cnet=eth1;;

*)

echo "錯誤的ip位址!"

exit

esac

#for i in <code>seq $num</code>

#do

#expect &lt;&lt;eof

#spawn virsh start node$[vm+i-1]

#expect "#" {send "\n"}

#exit

#eof

#done

#sleep 18

echo $net.$[i+ip-1]&gt;&gt;/tmp/host

expect &lt;&lt;eof

spawn virsh console node$[vm+i-1]

expect "換碼符為 ^]" {send "\n"}

expect "login:" {send "root\n"}

expect "password:" {send "123456\n"}

expect "]#" {send "nmcli connection add con-name $cnet ifname $cnet type ethernet\n"}

expect "]#" {send "nmcli connection modify $cnet ipv4.method manual ipv4.addresses $net.$[i+ip-1]/24 ipv4.gatew

ay $net.254 ipv4.dns 114.114.114.114 connection.autoconnect yes\n"}expect "]#" {send "nmcli connection up $cnet\n"}

expect "]#" {send "hostnamectl set-hostname node$[i+ip-1].cn\n"}

interact

eof

#yum -y install pssh

spawn ssh-copy-id -o stricthostkeychecking=no $net.$[ip+i-1]

expect "#" {send "\n"}

echo 'nmcli connection show| awk '"'/--/{print "'$'"2}'"'&gt;/tmp/et.txt'&gt;/tmp/et.sh

echo for i in '<code>cat /tmp/et.txt</code>'&gt;&gt;/tmp/et.sh

echo do &gt;&gt; /tmp/et.sh

echo nmcli connection delete "$"i &gt;&gt;/tmp/et.sh

echo done &gt;&gt; /tmp/et.sh

chmod +x /tmp/et.sh

pscp.pssh -h /tmp/host /new/gao.repo /etc/yum.repos.d/gao.repo

pscp.pssh -h /tmp/host /tmp/et.sh /tmp/et.sh

pssh -h /tmp/host bash /tmp/et.sh

rm -rf /tmp/host

繼續閱讀