The construction of KVM virtual machine under Linux

Source: Internet
Author: User
Tags uuid pscp

To install the KVM virtual machine tool:
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 #物理机迁移工具

Add a route forward on the real machine to facilitate later virtual machine access to the extranet
Vim/etc/sysctl.d/70-system.conf
Net.ipv4.ip_forward = 1

Configuring virtual network Adapter Parameters
cd/etc/libvirt/qemu/networks/autostart/
CP Default.xml/etc/libvirt/qemu/networks/vnet1.xml
CP Default.xml/etc/libvirt/qemu/networks/vnet2.xml
cd/etc/libvirt/qemu/networks/
Vim Vnet1.xml
Cases:
<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 Vnet2.xml
<network>
<name>veth2</name>
<forward mode= ' Nat '/>
<bridge name= ' veth2 '/>
<ip address= ' 192.168.2.254 ' netmask= ' 255.255.255.0 ' >
</ip>
</network>

Remove the real-computer firewall and turn off SELinux
Yum-y Remove Firewall
Setenforce 0
Edit SELinux Boot not enabled
Vim/etc/selinux/config

Edit Virtual network card settings
cd/etc/sysconfig/network-scripts/
Vim Ifcfg-veth1
CP Ifcfg-veth1 Ifcfg-veth2
Vim Ifcfg-veth2

Vim/etc/sysconfig/network-scripts/ifcfg-veth1
Cases:
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
Cases:
Type=ethernet
Proxy_method=none
Browser_only=no
Bootproto=none
Defroute=yes
Name=veth2
Device=veth2
Onboot=no
ipaddr=192.168.2.254
Prefix=24
gateway=192.168.2.254
dns1=114.114.114.114

Define the virtual network card and enable
In the case of determining to close the firewall and SELinux, if there are related errors, first restart the real machine
cd/etc/libvirt/qemu/networks/
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

Create a 20G (size look environment) template image file
cd/var/lib/libvirt/images/
Qemu-img create-f qcow2 Node.qcow2 20G

Virt-manager
Create a new virtual machine
System for loading CENTOS7
You can choose a network installation
Allocate space, select Custom, use previously created Node.qcow2
Partitions are divided into only one root, standard partitions
Language with default English
Close Kdump

Enter virtual machine to view IP address, veth1 previously defined as DHCP assigned IP address
Copy a real-machine yum source to a virtual machine

Install the required tools on the virtual machine
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"
Onboot=yes
Netboot=yes
Ipv6init=yes
Bootproto=dhcp
Type=ethernet
Device= "Eth0"

Vi/etc/sysconfig/grub
Add a row to facilitate the real-computer console character connection
grub_cmdline_linux= "Console=tty0 console=ttys0,115200n8"

Apply the configuration file:
Grub2-mkconfig-o/boot/grub2/grub.cfg

To shutdown the configuration template after mirroring is complete:
Halt-p

Cd/etc/libvirt/qemu
CP centos7.0.xml/root/
Vim/root/centos7.0.xml
9 <name>centos7.0</name>
Change to <name>node0</name> facilitate script execution
The following annotations are to be removed:
The following annotations are to be removed:
The following annotations are to be removed:

Ten <uuid>c04d4f3a-a3b9-4ddb-93ea-2361636d6e6f</uuid>
<mac address= ' 52:54:00:22:f4:de '/>

All <address type=....../>
All <controller type= ' USB ' .......</controller>

Since there is also a veth2 virtual network card, add the following statement:
<interface type= ' network ' >
<source network= ' veth2 '/>
<model type= ' Virtio '/>
</interface>

To delete a previously installed defined template
Virsh undefine centos7.0

Note: The following script uploads the blog after the SEQ statement will be missing characters, please add yourself
Note: The following script uploads the blog after the SEQ statement will be missing characters, please add yourself
Note: The following script uploads the blog after the SEQ statement will be missing characters, please add yourself

Ln-s/root/kvm.sh/sbin/kvm.sh
Ln-s/root/setip.sh/sbin/setip.sh

Attached: Virtual machine creation Script
Vim kvm.sh
#!/bin/bash
Read-p "Please enter the number of virtual machines to be created:" num
Read-p "Please enter the starting virtual machine number: node" x

For I inseq $num
Do
Cp/root/centos7.0.xml/var/lib/libvirt/qemu/node$[i-1+x].xml
Sed-i ' S/name>node. </name>node ' $[i-1+x] ' </'/var/lib/libvirt/qemu/node$[i-1+x].xml
Sed-i ' S/node.
Img/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

Attached: Character interface defines virtual machine IP script
Vim setip.sh
#!/bin/bash
Yum-y Install expect TCL
Read-p "Please enter the virtual machine start number (1~): Node" VM
Read-p "Please enter the starting IP address to be created (example: 192.168.2.5):" CIP
Read-p "Please enter the number of hosts that need to be created consecutively (1~
):" num
ip=echo $cip|awk -F. ‘{print $4}‘
net=echo $cip|awk -F. ‘{print $1"."$2"."$3}‘
Case $net in
192.168.1)
Cnet=eth0;;
192.168.2)
Cnet=eth1;;
*)
echo "Wrong IP address! "
Exit
Esac
#for I inseq $num
#do
#expect <<eof
#spawn Virsh start Node$[vm+i-1]
#expect "#" {send "\ n"}
#exit
#eof
#done

#sleep 18

For I inseq $num
Do
Echo $net. $[i+ip-1]>>/tmp/host
Expect <<eof
Spawn Virsh Console Node$[vm+i-1]
Expect "change code to ^" {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
Exit
Eof
Done
#yum-y Install Pssh

For I inseq $num
Do
Expect <<eof
Spawn Ssh-copy-id-o Stricthostkeychecking=no $net. $[ip+i-1]
Expect "Password:" {send "123456\n"}
Expect "#" {send "\ n"}
Exit
Eof
Done

Echo ' NMCLI connection show| awk ' "'/--/{print" ' $ ' "2} '" ' >/tmp/et.txt ' >/tmp/et.sh
echo for I in ' cat /tmp/et.txt >>/tmp/et.sh
echo Do >>/tmp/et.sh
echo NMCLI Connection Delete "$" I >>/tmp/et.sh
echo Done >>/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

The construction of KVM virtual machine under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.