Centos 7 最小化kvm部署

时间:2024-05-22 16:34:08

1、检查CPU是否支持虚拟化

sh-4.2# grep -E '(vmx|svm)' /proc/cpuinfo
# 若是无任何显示,则表示CPU不支持kvm虚拟化

2、关闭selinux

sh-4.2# sed -i '1,$ s/enforcing/disabled/g' /etc/selinux/config
sh-4.2# reboot

3、卸载防火墙

sh-4.2# rpm -qa | grep "iptables" | xargs rpm -e --nodeps
sh-4.2# rpm -qa | grep "firewall" | xargs rpm -e --nodeps

4、配置本地源

sh-4.2# cat /etc/yum.repos.d/GuGe.repo
[GuGe]
name=GuGe
baseurl=ftp://192.168.1.125
gpgcheck=
enable=
# 这里仅使用了iso镜像源

5、安装软件

sh-4.2# yum install qemu-kvm libvirt virt-install bridge-utils -y

# 检查安装是否成功,如果出现下列显示,则表示成功
sh-4.2# lsmod | grep kvm
kvm_intel
kvm kvm_intel
irqbypass kvm

6、启动程序

sh-4.2# systemctl start libvirtd
sh-4.2# systemctl enable libvirtd

7、配置程序

# 配置桥接网卡
sh-4.2# vi /etc/sysconfig/network-scripts/ifcfg-br0
sh-4.2# cat /etc/sysconfig/network-scripts/ifcfg-br0
BOOTPROTO=static
DEVICE=br0
TYPE=Bridge
NM_CONTROLLED=no
IPADDR=192.168.1.125
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8 sh-4.2# cp /etc/sysconfig/network-scripts/ifcfg-enp7s0 /etc/sysconfig/network-scripts/ifcfg-enp7s0.bak
sh-4.2# vi /etc/sysconfig/network-scripts/ifcfg-enp7s0
sh-4.2# cat /etc/sysconfig/network-scripts/ifcfg-enp7s0
BOOTPROTO=none
DEVICE=enp7s0
NM_CONTROLLED=no
ONBOOT=yes
BRIDGE=br0
sh-4.2# systemctl restart network.service sh-4.2# ifconfig br0
br0: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu
inet 192.168.1.125 netmask 255.255.255.0 broadcast 192.168.1.255
ether :e0:2b::0a:ad txqueuelen (Ethernet)
RX packets bytes (5.4 KiB)
RX errors dropped overruns frame
TX packets bytes (6.2 KiB)
TX errors dropped overruns carrier collisions # 配置kvm虚拟机存放位置,这里就做过多描述了;看后续操作即可明白 sh-4.2# parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mkpart
Error: /dev/sdb: unrecognised disk label
(parted) mk
mklabel mkpart mktable
(parted) mklabel
New disk label type? gpt
(parted) mkpart /dev/sdb
File system type? [ext2]? xfs
Start?
End? 2T
(parted) quit
Information: You may need to update /etc/fstab. sh-4.2# partprobe
sh-4.2# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1 xfs 3abc71fb-6c9d-4fe6-a432-b5a48643f8bb /boot
├─sda2 xfs fd28aad6-4cc8-48a7-beed-91624ea06746 /
├─sda3 swap 76b8f6e8-33b3-4fc0-90fc-b243c1d69cfb [SWAP]
└─sda4 xfs b34c005c-15dd-4ade-b6d4-3cda14127116 /FTP
sdb
└─sdb1 sh-4.2# mkfs -t xfs /dev/sdb1
meta-data=/dev/sdb1 isize= agcount=, agsize= blks
= sectsz= attr=, projid32bit=
= crc= finobt=, sparse=
data = bsize= blocks=, imaxpct=
= sunit= swidth= blks
naming =version bsize= ascii-ci= ftype=
log =internal log bsize= blocks=, version=
= sectsz= sunit= blks, lazy-count=
realtime =none extsz= blocks=, rtextents= sh-4.2# vi /etc/fstab
sh-4.2# grep -E -v '^$|^#' /etc/fstab
UUID=fd28aad6-4cc8-48a7-beed-91624ea06746 / xfs defaults
UUID=3abc71fb-6c9d-4fe6-a432-b5a48643f8bb /boot xfs defaults
UUID=76b8f6e8-33b3-4fc0-90fc-b243c1d69cfb swap swap defaults
UUID=b34c005c-15dd-4ade-b6d4-3cda14127116 /FTP xfs defaults
UUID=eb08ac4c-9b16-491b-b735-9f5cecfd8ba6 /disk xfs defaults sh-4.2# mkdir /disk
sh-4.2# mount -a
sh-4.2# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1 xfs 3abc71fb-6c9d-4fe6-a432-b5a48643f8bb /boot
├─sda2 xfs fd28aad6-4cc8-48a7-beed-91624ea06746 /
├─sda3 swap 76b8f6e8-33b3-4fc0-90fc-b243c1d69cfb [SWAP]
└─sda4 xfs b34c005c-15dd-4ade-b6d4-3cda14127116 /FTP
sdb
└─sdb1 xfs eb08ac4c-9b16-491b-b735-9f5cecfd8ba6 /disk # 我这里已经准备好了ISO镜像,可以开始创建虚拟机了;我一般是用vnc操作虚拟机,也可以通过字符界面直接操作,我嫌太麻烦,就直接vnc操作了
# vnc下载地址:https://www.realvnc.com/en/connect/download/viewer/ sh-4.2# virt-install -n Centos7- -r --disk /disk/Centos7-.img,format=qcow2,size= --vcpus= --network bridge=br0 --os-type=linux \
sh-4.2# --cdrom /FTP/ftp/镜像软件/iso/linux/Centos/7.4/CentOS-7-x86_64-Everything-1708.iso --vnc --vncport=5910 --vnclisten=0.0.0.0

8、开启宿主机远程连接虚拟机权限(可选操作)

sh-4.2# vim  /etc/default/grub
用如下内容覆盖原有内容:
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL="console serial"
GRUB_SERIAL_COMMAND="serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"
GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb"
GRUB_CMDLINE_LINUX_DEFAULT="console=tty1 console=ttyS0,115200"
GRUB_DISABLE_RECOVERY="true" sh-4.2# grub2-mkconfig -o /boot/grub2/grub.cfg
sh-4.2# reboot sh-4.2# yum install -y acpid(客户端开启电源管理功能,这样就能在宿主机中设置虚拟机随宿主机启动而启动)

9、常用命令

virsh console vmname                在服务器端连接虚拟机
virsh shutdown vmname 关闭虚拟机
virsh list --all 显示所有虚拟机及状态
virsh reboot vmname 重启虚拟机
virsh start vmname 启动虚拟机
virsh console vmname 连接虚拟机
ctrl + ] 关闭连接
virsh suspend vmname 挂起虚拟机
virsh resume vmname 恢复虚拟机
virsh dumpxml vmname 查看虚拟机配置详情
virsh autostart vmname 设置虚拟机随宿主机启动而启动
virsh autostart --disable vmname 取消虚拟机随宿主机启动而启动 virsh destroy vmname 强制关机
virsh undefine vmname 解除虚拟机,然后删除虚拟机文件(*.img);这样才能彻底删除虚拟机,并且释放空间。 virsh start vmname --console 启动并远程连接
qemu-img create -f qcow2 /var/lib/libvirt/images/kvm01-.img 20G 创建一个20G的磁盘文件,格式qcow2
virsh attach-disk vmname /var/lib/libvirt/images/kvm01-.img vdb --cache=none --subdriver=qcow2 在线添加硬盘
virt-clone -o vmname1 -n vmname2 -f /var/lib/libvirt/images/vmname02.img 克隆一个虚拟机 虚拟机信息的修改
virsh shutdown name
cd /etc/libvirt/qemu
virsh dumpxml old_name > new_name.xml
# 直接更改new_name.xml,更换想要更换的信息
virsh undefine old_name
virsh define new_name.xml
virsh list --all

10、参考地址

https://www.linuxidc.com/Linux/2017-01/140007.htm
http://blog.chinaunix.net/uid-30022178-id-5749329.html
https://blog.****.net/zxj1988/article/details/8530414
http://www.361way.com/virt-install/2721.html
https://blog.****.net/qq_37594711/article/details/61917902
https://www.cnblogs.com/createyuan/p/3741479.html
https://blog.****.net/wh211212/article/details/74908390
https://www.cnblogs.com/chenjiahe/p/5919426.html
https://www.linuxidc.com/Linux/2017-01/140009.htm?utm_source=tuicool&utm_medium=referral