Create a kvm virtual machine in the CentOS7 environment, build a bridge environment, and use the command line to control the virtual machine

Host network bridge configuration

We require that the virtual machine created can share the network segment with the host. The topology diagram is as follows:

figure 1

The network architecture of the host in the bridge environment is as follows:

figure 2

This is the case when the host has two network cards, if there are no two network cards (as shown above without eth1), it is fine, as long as the physical network card (eth0) is used as the external interface, and the bridge device (br0) is also normal as the host network card. communication.

host bridge tool

yum -y install bridge-utils 
# Note: Many Centos systems already come with them, so you don't need to install them. You can view them with the command (rpm -qa | grep birdge).

Host network configuration

# Modify the network interface (eth0 in Figure 2) 
cat /etc/sysconfig/network-scripts/ifcfg-eth0 
DEVICE = "eth0" 
ONBOOT = yes 
BRIDGE = br0 
# Note: The above three items are required, as for HWADDR, NAME , . . . Wait, I don't know if I have to write it, try it myself. 
​#
 Modifications to the bridge device (br0 in Figure 2) 
cat /etc/sysconfig/network-scripts/ifcfg-br0 
DEVICE = "br0" 
TYPE = Bridge 
BOOTPROTO = dhcp 
ONBOOT = yes 
# Description: This device is mostly No, you need to create it yourself. At the same time, this device can have a website and use it as the host's network card (if the host does not have eth1 in Figure 2, then eth0 is used as the interface, and this br0 is used as the original eth0) .

Then restart the network card

host kvm installation

Tool installation
yum install -y qemu-kvm qemu-kvm-tools libvirt
yum install -y virt-install net-tools
systemctl enable libvirtd
systemctl start libvirtd
Creation of virtual machines
# Creation in Centos7 environment
 qemu-img create -f raw disk name.raw disk size 
virt-install --virt-type kvm --name virtual machine name --ram memory size --vcpus = number of CPU cores --cdrom = Mirror path --disk  path = disk path --network  bridge = bridge device (br0 in Figure 2) , model = virtio --graphics vnc , listen = 0.0.0.0 --noautoconsole 
# Create
 qemu- in 
Centos6 environment img create -f raw disk name.raw disk size virt-install --name virtual machine name--ram memory size --vcpus = number of CPU cores --disk  path = mirror path   --network  bridge = bridge device (br0 in Figure 2)   --cdrom mirror path --vnclisten = 0.0.0.0 --vncport = 5900 --vnc  
# There is a problem with this creation under Centos6, that is, the vnc listening port is fixed and will not be automatically modified, and the configuration file needs to be modified. The operation is as follows:
 virsh edit virtual machine name 
. . . . . 
<graphics type = 'vnc'  port = '-1'  autoport = 'yes'  listen = '0.0.0.0' >
     <listen type = 'address' address = '0.0.0.0' /> 
</graphics> 
. . . . 
# Change the autoport option to yes 
# If it is not processed, only one virtual machine can be started during batch cloning.

After the creation is completed, it needs to be installed. Use the built-in virt-manager graphical interface and vncviewer graphical interface to install. Note that the port of the vnc link is the vnclisten and port in the above figure, that is, 0.0.0.0:5900. Of course, this port is only the first one you create. If you create a lot of virtual machines, the port will be automatically assigned. You can check the port by yourself, as follows:

# netstat -tunlp    //查看端口
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name   
tcp        0      0 0.0.0.0:5900            0.0.0.0:*               LISTEN      4878/qemu-kvm       
tcp        0      0 0.0.0.0:5901            0.0.0.0:*               LISTEN      5051/qemu-kvm       
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:20048           0.0.0.0:*               LISTEN      1590/rpc.mountd     
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      2821/dnsmasq      
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1474/sshd           
tcp        0      0 0.0.0.0:46200           0.0.0.0:*               LISTEN      -                   
.........
# vncviewer     //After startup, according to the port link icon, there are two virtual machines 0.0.0.0:5900 and 0.0.0.0:5901# 
......... //Modify the file after successful installation according to your own requirements     

If you want to use the command line to control the virtual machine, there are two methods, one is the ssh service, the virtual machine needs to build a network environment, and both the host and the virtual machine have openssh software installed; the other is the virsh console your_vm_names command, but this The instructions require some modifications inside the virtual machine as follows:

# For CentOS7 grubby
 --update -kernel = ALL --args = "console=ttyS0"
 reboot 
# For other linux 
# first add ttyS0 in sercuretty 
echo  "ttyS0" >> /etc/sercuretty 
# second in grub.conf specific Line added console=ttyS0 
cat /etc/grub.conf | grep  console = ttyS0 
kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root = /dev/mapper/VolGroup-lv_root rd_NO_LUKS LANG = en_US.UTF-8 rd_NO_MD rd_LVM_LV = VolGroup/lv_swap SYSFONT = latarcyrheb-sun16 crashkernel =128M rd_LVM_LV = VolGroup/lv_root   KEYBOARDTYPE = pc KEYTABLE = us rd_NO_DM rhgb quiet console = ttyS0 
# Finally in inittab add S0:12345:respawn:/sbin/agetty ttyS0 115200 
echo  "S0:12345:respawn:/sbin/agetty ttyS0 115200" >> /etc/inittab 
reboot 
# If it doesn't operate, it will stop at the following location
 [root@h97 myCentos] # virsh console virtual machine name
 Connected to domain centos7 
Escape character is ^]

This allows you to control the virtual machine from the command line

If you want to use script operation, you need to use expect script to process the login interface. An example of mine is as follows

# cat connect.exp 
set timeout 3s 
set ip [lindex $argv  0 ] 
set setip [lindex $argv  1 ] 
spawn ssh  $ip
 expect "password:"
 send "123456\n"
 expect "#"
 send "cd /home/\ n"
 expect "#"
 send "./vmset.sh $setip \n"
 expect eof 
exit 
# timeout refers to the interaction delay, and the script will automatically end if it exceeds. 
# ip and setip are the same incoming values ​​as the shell script, it is worth Note that the value of the expect script starts from $argv 0 
# spawn is followed by the instruction to be executed 
# Then if a keyword (ie expect "keyword") is not captured, send what you want to send (ie send "content sent")


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324728116&siteId=291194637