在CentOS中使用KVM创建虚拟机

0 Comments

一、关闭防火墙和SELINUX

关闭防火墙

[root@CentOS110 ~]# systemctl stop firewalld

[root@CentOS110 ~]# systemctl disable firewalld

关闭SELINUX

[root@CentOS110 ~]# vi /etc/selinux/config

将第七行  SELINUX=enforcing改为SELINUX=disabled

重启服务器

[root@CentOS110 ~]# reboot

二、配置本地YUM源(已配置请跳过此步)

切换到/etc/yum.repos.d/

cd /etc/yum.repos.d/

备份原有的repo文件

mkdir backup

mv *po backup/

配置本地YUM源

[root@CentOS110 yum.repos.d]# cat << EOF > CentOS-Media.repo

[CentOS7]

name=CentOS7

baseurl=file:///media/

enabled=1

gkgcheck=1

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

EOF

挂载光盘并写入fstab

[root@CentOS112 etc]# echo “/dev/cdrom              /media                  iso9660 defaults        0 0” >> /etc/fstab

[root@CentOS112 etc]# mount -a

mount: /dev/sr0 is write-protected, mounting read-only

重新生成缓存

[root@CentOS110 yum.repos.d]# yum clean all

[root@CentOS110 yum.repos.d]# yum makecache

三、安装KVM、启动服务

安装KVM

[root@CentOS112 ~]#yum -y groupinstall “Virtualization Host”

yum -y install qemu-kvm qemu-kvm-tools qemu-img virt-manager libvirt libvirt-python libvirt-client bridge-utils virt-viewer virt-install

检查模块是否已经加载

[root@CentOS112 ~]# lsmod | grep kvm

配置桥接网卡

[root@CentOS112 ~]# cd /etc/sysconfig/network-scripts/

备份网卡配置文件

[root@CentOS112 network-scripts]# cp ifcfg-ens33 ifcfg-ens33.bak

创建网卡桥接文件

[root@CentOS112 network-scripts]# cp ifcfg-ens33 ifcfg-br0

将TYPE=”Ethernet”修改为TYPE=”Bridge”

将NAME=”ens33″修改为NAME=”br0″

将UUID删除

将DEVICE=”ens33″修改为DEVICE=”br0″

修改原网卡配置

[root@CentOS112 network-scripts]# cat ifcfg-ens33

TYPE=”Ethernet”

NAME=”ens33″

DEVICE=”ens33″

ONBOOT=”yes”

BRIDGE=”br0″

重启网卡

[root@CentOS112 ~]# systemctl restart network

启动服务、设置开机启动

[root@CentOS112 ~]# systemctl start libvirtd

[root@CentOS112 ~]# systemctl enable libvirtd

四、添加一块硬盘,完成分区、格式化、挂载

硬盘分区

格式化硬盘

挂载硬盘

[root@CentOS112 ~]# vim /etc/fstab

[root@CentOS112 ~]# mkdir /kvm_data

[root@CentOS112 ~]# mount -a

查看是否挂载成功

[root@CentOS112 ~]# df -h

五、创建虚拟机

[root@CentOS112 ~]# virt-install –name CentOS7 –ram 1024 –vcpus=1 –disk path=/kvm_data/CentOS7.img,size=10 –network bridge=br0 –cdrom /iso/CentOS-7-x86_64-DVD-1810.iso –vnclisten=192.168.16.112 –vncport=6900 –vnc

打开VNC,安装系统

发表评论

电子邮件地址不会被公开。 必填项已用*标注