CentOS7上安装并配置KVM,以及通过KVM安装CentOS系统
- ssh免密码
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.0.2
- 修改静态IP地址
vim /etc/sysconfig/network-scripts/ifcfg-enp0s25
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.0.2
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
- 停止防火墙
systemctl stop firewalld
systemctl disable firewalld
- 停止selinux
setenforce 0
vim /etc/selinux/config
SELINUX=disabled
- 安装常用工具
yum install git vim net-tools wget
- 安装KVM
yum install -y virt-* libvirt bridge-utils qemu-img
- 配置网桥
cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cat ifcfg-enp0s25
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s25
UUID=2d353bf4-691e-44a9-b6e2-0e6c1d9fc082
DEVICE=enp0s25
ONBOOT=yes
IPADDR=192.168.0.2
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
BRIDGE=br0
[root@localhost network-scripts]# cat ifcfg-br0
NAME="br0"
DEVICE="br0"
TYPE=Bridge
ONBOOT=yes
IPADDR=192.168.0.2
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
BOOTPROTO=static
[root@localhost network-scripts]# systemctl restart network
启动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 ~]#
创建虚拟机安装CentOS7
[root@localhost ~]# virt-install --name=study01 --memory=512,maxmemory=1024 --vcpus=1,maxvcpus=2 --os-type=linux --os-variant=rhel7 --location=/kvm/iso/CentOS-7-x86_64-Minimal-1810.iso --disk path=/kvm/disk/study01.img,size=10 --bridge=br0 --graphics=none --console=pty,target_type=serial --extra-args="console=tty0 console=ttyS0"
虚拟机管理
[root@localhost ~]# virsh list
[root@localhost ~]# virsh list --all
[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 # 恢复挂起的虚拟机
网友评论