一、KVM虚拟机模块化安装与管理

1.环境准备

yum -y install qemu-kvm libvirt libvirt-python libguestfs-tools virt-install virt-manager 
systemctl enable libvirtd && systemctl start libvirtd

ks.cfg 示例:

生成自定义密码:

[root@gbase8c-87 ~]# echo "123456" | openssl passwd -1 -stdin
$1$MZOp2vkd$r1mEtRWUhkU6CICTKw692.
#platform=x86, AMD64, aarch64 或 Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted $1$MZOp2vkd$r1mEtRWUhkU6CICTKw692.
# 系统语言
#lang zh_CN.UTF-8
lang en_US

# System authorization information
auth  --useshadow  --passalgo=sha512
# Use CDROM installation media
#cdrom

# Use text mode install
# 使用字符界面安装操作系统
text

# Do not configure the X Window System
# 不配置图形界面系统
skipx

# SELinux configuration
selinux --disabled

# Firewall configuration
firewall --disabled

# 设置动态获取 IP
network  --bootproto=dhcp --device=eth0 --onboot=on
network  --hostname=HOSNAME

# 设置静态 IP
# network  --bootproto=static --device=eth0 --gateway=192.168.122.1 --ip=192.168.122.100 --nameserver=192.168.122.1 --netmask=255.255.255.0 --activate

# 系统安装完成后 重启系统
#halt
reboot

# System timezone 系统时区
timezone Asia/Shanghai

# System services  系统启动时候自动启动的服务
services --enable="chronyd"

# System bootloader configuration
#系统启动引导方式 vda 是kvm 虚拟机时候的第块磁盘名称
bootloader --location=mbr --boot-drive=vda

# 清除主引导记录
zerombr

# 删除原来的分区和磁盘标签
clearpart --all --initlabel

# 磁盘分区信息,这里采用的是 lvm 自动分区
autopart --type=lvm

# 下面是自定义分区
#part /boot --fstype="xfs" --ondisk=vda --size=200
#part / --fstype="xfs" --ondisk=vda --size=10040
#part /var --fstype="xfs" --ondisk=vda --size=2048
#part /home --fstype="xfs" --ondisk=vda --size=2048

# 安装软件
%packages
@^minimal
@core
chrony
%end

# 禁用 kdump
%addon com_redhat_kdump --disable --reserve-mb='auto'

%end

# 安装系统完成后执行的命令脚本
%post --interpreter=/bin/bash
yum install -y epel-release bash-completion vim-enhanced wget
yum group install "Development Tools" -y
%end

# 用户密码策略
%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end

#!/bin/bash
##kvm管理脚本
##ShanHai

#source加载模块脚本
source /shanhai/install-virtualhost.sh #创建一个全新虚拟机的模块脚本
source /shanhai/mod.sh #使用虚拟机模板快速创建新虚拟机的模块脚本
source /shanhai/disk.sh #添加硬盘的模块脚本
source /shanhai/net.sh #添加网卡的模块脚本
source /shanhai/snap.sh #创建快照的模块脚本
source /shanhai/vm_del.sh #删除虚拟机硬件,快照的模块脚本

while :
do
cat <<-EOF
————————————————————————————————————————————————————————————————————————————————————————————————
    1.查看当前所有虚拟机
    2.创建一个全新的虚拟机
    3.使用模板快速创建虚拟机
    4.添加虚拟机硬件
    5.创建虚拟机快照
    6.删除虚拟机硬件、快照
    *.退出
———————————————————————————————————————————————————————————————————————————————————————————————
EOF

read -p "请输入要进行的操作:" kvm_num
case "$kvm_num" in
    1)
        virsh list --all
    ;;
    2)
        vm_install
    ;;
    3)
        vm_mod
    ;;
    4)
        read -p "(1)添加硬盘,(2)添加网卡,(Q/q)退出" vm_num
        case "$vm_num" in
            1)
                vm_disk
            ;;
            2)
                vm_net
            ;;
            Q|q)
                exit
        esac
    ;;
    5)
        vm_snap
    ;;
    6)
        read -p "(1)删除硬盘,(2)删除网卡,(3)删除快照,(Q/q):" del_num
        case "$del_num" in
            1)
                disk_del
            ;;
            2)
                net_del
            ;;
            3)
                snap_del
            ;;
            Q|q)
                exit
            ;;
        esac
    ;;
    *)
        exit
    ;;
esac

done

2.创建全新虚拟机的模块脚本

#!/usr/bin/env bash
##vm_install
##ShanHai
vm_install(){
set -ue
set -o pipefail

# 创建相关目录
ls /home/kvm/{ks,virtualhost,virtual-img} 1>/dev/null 2>&1 || mkdir -p /home/kvm/{ks,virtualhost,virtual-img}

# 此程序的变量
KVM_HOME=/home/kvm
KVM_ISO=${KVM_HOME}/iso/CentOS-7-x86_64-Minimal-1708.iso
KVM_KS_FILE=${KVM_HOME}/ks/ks.cfg
KVM_IMG_DIR=${KVM_HOME}/virtual-img

OS_TYPE="linux"
DEF_OS_VARIANT="rhel7"
DEF_VM_NAME="centos$(date +%Y%m%dT%H%M%S)"

# 设置默认内存大小 1024 M,这个单位是固定的 M,不支持其他单位
DEF_MEM_SIZE=1024
DEF_VCPUS=1
DEF_DISK_SIZE=10G

#  交互式安装
echo  "输入将要创建的KVM虚拟机名称,不是主机名哦"
read -p "默认值${DEF_VM_NAME}>>:" VM_NAME

echo  "输入将要创建的服务器主机名"
read -p "默认 localhost>>:"  HOST_NAME

read -p "输入虚拟机 CPU 核心数默认 1 示例:2>>:" VCPUS

echo "输入虚拟机内存大小,默认1024M,不支持其他单位"
read -p "只需要添数字即可>>:" MEM_SIZE

echo "输入虚拟机使用的磁盘容量"
read -p "默认 10G,[示例写法:15G]>>:"  DISK_SIZE

# 变量替换, 假如没有输入值, 就使用默认值
VM_NAME=${VM_NAME:-$DEF_VM_NAME}
HOST_NAME=${HOST_NAME:-localhost.localdomain}

VCPUS=${VCPUS:-$DEF_VCPUS}
MEM_SIZE=${MEM_SIZE:-$DEF_MEM_SIZE}
DISK_SIZE=${DISK_SIZE:-$DEF_DISK_SIZE}
OS_VARIANT=${OS_VARIANT:-$DEF_OS_VARIANT}

new_disk=${KVM_IMG_DIR}/${VM_NAME}.qcow2

if [[ ! "${DISK_SIZE}" =~ G$ ]] || [[ ! "${DISK_SIZE::-1}" =~ ^[0-9]+$ ]]
then
    echo "格式不正确,正确示例:20G"
    exit
fi

if [ ! -f "${new_disk}" ];then
    qemu-img create -f qcow2 ${new_disk} ${DISK_SIZE}
fi

# 开始创建
virt-install -v \
     --arch x86_64 --virt-type kvm \
     --name ${VM_NAME} \
     --memory ${MEM_SIZE} \
     --vcpus ${VCPUS} \
     --os-type ${OS_TYPE} \
     --location ${KVM_ISO} \
     --network default \
     --graphics none \
     --os-variant     ${OS_VARIANT} \
     --initrd-inject  "${KVM_KS_FILE}" \
     --extra-args     "ks=file:/ks.cfg  \
                       console=tty0 console=ttyS0,115200n8 \
                       hostname=${HOST_NAME}" \
     --disk ${new_disk},cache=writeback,io=threads,bus=virtio
}

3.使用现有虚拟机模板快速创建新虚拟机的模块脚本

配置模块:

#vim /etc/libvirt/qemu/centos7-mod.xml
[root@gbase8c-5-117 kvm]# cat centos7-mod.xml
<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
  virsh edit server-5-148
or other application using the libvirt API.
-->

<domain type='kvm'>
  <name>vm-name</name>
  <uuid>vm-uuid</uuid>
  <memory unit='KiB'>vm-mem</memory>
  <currentMemory unit='KiB'>vm-mem</currentMemory>
  <vcpu placement='static'>vm-cpu</vcpu>
  <os>
    <type arch='aarch64' machine='virt-rhel7.6.0'>hvm</type>
    <loader readonly='yes' type='pflash'>/usr/share/AAVMF/AAVMF_CODE.fd</loader>
    <nvram>/var/lib/libvirt/qemu/nvram/server-5-148_VARS.fd</nvram>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <gic version='3'/>
  </features>
  <cpu mode='host-passthrough' check='none'/>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2' cache='writeback' io='threads'/>
      <source file='vm-disk-path'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x05' slot='0x00' function='0x0'/>
    </disk>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <target dev='sda' bus='scsi'/>
      <readonly/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
    <controller type='usb' index='0' model='qemu-xhci' ports='8'>
      <address type='pci' domain='0x0000' bus='0x02' slot='0x00' function='0x0'/>
    </controller>
    <controller type='scsi' index='0' model='virtio-scsi'>
      <address type='pci' domain='0x0000' bus='0x03' slot='0x00' function='0x0'/>
    </controller>
    <controller type='pci' index='0' model='pcie-root'/>
    <controller type='pci' index='1' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='1' port='0x8'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x0' multifunction='on'/>
    </controller>
    <controller type='pci' index='2' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='2' port='0x9'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <controller type='pci' index='3' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='3' port='0xa'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
    </controller>
    <controller type='pci' index='4' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='4' port='0xb'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x3'/>
    </controller>
    <controller type='pci' index='5' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='5' port='0xc'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x4'/>
    </controller>
    <controller type='pci' index='6' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='6' port='0xd'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x5'/>
    </controller>
    <controller type='pci' index='7' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='7' port='0xe'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x6'/>
    </controller>
    <controller type='virtio-serial' index='0'>
      <address type='pci' domain='0x0000' bus='0x04' slot='0x00' function='0x0'/>
    </controller>
    <interface type='network'>
      <mac address='52:54:00:vm-mac'/>
      <source network='default'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x01' slot='0x00' function='0x0'/>
    </interface>
    <serial type='pty'>
      <target type='system-serial' port='0'>
        <model name='pl011'/>
      </target>
    </serial>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <channel type='unix'>
      <target type='virtio' name='org.qemu.guest_agent.0'/>
      <address type='virtio-serial' controller='0' bus='0' port='1'/>
    </channel>
    <rng model='virtio'>
      <backend model='random'>/dev/urandom</backend>
      <address type='pci' domain='0x0000' bus='0x06' slot='0x00' function='0x0'/>
    </rng>
  </devices>
</domain>

#!/usr/bin/bash
##MOD
##ShanHai
vm_mod(){
read -p "请输入创建的新虚拟机名称:" vm_name
vm_uuid=`uuidgen`
vm_mem=1048576
vm_mac=`openssl rand -hex 3 | sed -r 's/..\B/&:/g'`
#源镜像路径
image=/home/kvm/virtual-img/shanhai.qcow2
#源模板位置
mod=/etc/libvirt/qemu/centos7-mod.xml
#新镜像位置
new_image=/home/kvm/virtual-img/${vm_name}.qcow2
#新模板位置
new_mod=/etc/libvirt/qemu/${vm_name}.xml

cp $image $new_image
cp $mod $new_mod
sed -r \
-e  s%vm-name%$vm_name% \
-e  s%vm-uuid%$vm_uuid% \
-e  s%vm-mem%$vm_mem% \
-e  s%vm-disk-path%$new_image% \
-e  s%vm-mac%$vm_mac% \
$mod > $new_mod

echo "查看创建的虚拟机"
virsh define /etc/libvirt/qemu/${vm_name}.xml &>> /dev/null
systemctl restart libvirtd
virsh start $vm_name
virsh list --all | grep $vm_name
}

4.添加虚拟机硬盘的模块脚本

#!/bin/bash
##disk
##ShanHai

#添加虚拟硬盘
vm_disk(){
virsh list --all
read -p "请输入想要添加硬件的虚拟机:" vm_name
    for i in {a..z}
    do
        virsh domblklist ${vm_name} | grep "^vd${i}" > /dev/null
        if [ $? -ne 0 ];then
            break
        fi
    done
    disk=vd${i}
    
read -p "请输入创建的磁盘容量(如2G):" disk_size
disk_file=/shanhai/${vm_name}-${disk}.qcow2

qemu-img create -f qcow2 ${disk_file} ${disk_size}G
source /shanhai/disk.mod > /shanhai/disk.xml

virsh attach-device ${vm_name} /shanhai/disk.xml --persistent
echo "查看虚拟硬盘"
virsh domblklist ${vm_name}
}

5.添加虚拟机网卡的模块脚本

#!/bin/bash
##net
##ShanHai

#添加虚拟网卡
vm_net(){
echo "添加虚拟网卡"
s=`openssl rand -hex 3 | sed -r 's/..\B/&:/g'`
source /shanhai/net.mod > /shanhai/net.xml
virsh attach-device ${vm_name} net.xml --persistent
echo "查看虚拟网卡"
virsh domiflist ${vm_name}
}
#cat net.mod
echo "<interface type='network'>
      <mac address='52:54:00:$s'/>
      <source network='default'/>
      <model type='virtio'/>
</interface>"

6.创建虚拟机快照的模块脚本

#!/bin/bash
##snap
##ShanHai

#创建快照
vm_snap(){
    virsh list --all
    read -p "请输入想要创建快照的虚拟机名称:" vm_name
    vm_stat=`virsh list --all | grep $vm_name | awk '{print $3" "$4}'`
    qemu-img info /home/kvm/virtual-img/${vm_name}.qcow2 &>>/dev/null
    disk_type=`qemu-img info /home/kvm/virtual-img/${vm_name}.qcow2 | grep file | awk '{print $3}'`
    echo $disk_type
    exit
    echo "当前虚拟机磁盘类型为:$disk_type"
    echo "当前虚拟机状态为:$vm_stat"
    read -p "是否继续创建快照?[Y/n]:" snap_num
    case "$snap_num" in
        Y|y)
            if [ "$disk_type" = "qcow2" ];then
                read -p "请输入创建的快照名称:" snap_name
                virsh snapshot-create-as $vm_name ${vm_name}.${snap_name}
                echo "查看指定虚拟机的快照:"
                virsh snapshot-list $vm_name
            else
                echo "当前磁盘类型不支持创建快照"
            fi
        ;;
        N|n)
            echo "正在退出..."
        ;;
    esac
}

7.删除虚拟机硬件,快照的模块脚本

#!/usr/bin/bash
##vm_del
##ShanHai

#删除虚拟机硬盘
disk_del(){
    virsh list --all
    read -p "请输入想要删除硬盘的虚拟机:" vm_name
    echo "查看当前虚拟机硬盘信息:"
    virsh domblklist $vm_name
    read -p "请输入想要删除的硬盘:" vm_disk
    virsh detach-disk ${vm_name} ${vm_disk} --persistent
}

#删除虚拟机网卡
net_del(){
    virsh list --all
    read -p "请输入想要删除网卡的虚拟机名称:" vm_name
    echo "查看当前虚拟机网卡信息:"
    virsh domiflist ${vm_name}
    read -p "请输入想要删除的网卡mac名称:" net_name
    virsh detach-interface ${vm_name} network ${net_name} --persistent
}

#删除虚拟机快照
snap_del(){
    virsh list --all
    read -p "输入想要删除快照的虚拟机名称:" vm_name
    echo "查看当前虚拟机所有快照:"
    virsh snapshot-list ${vm_name}
    read -p "输入想要删除的快照名称:" snap_name
    virsh snapshot-delete ${vm_name} --snapshotname ${snap_name}
}

8.virsh工具管理虚拟机

下文中 vm-name 表示一个虚拟机的名称

1.列出当前宿主机中正在运行的虚拟机

virsh list

2.列出所有的虚拟机

virsh list  --all

3.查看指定虚拟机的元数据信息

virsh  dominfo  vm-name

4.查看指定虚拟机的 IP 地址

virsh  domifaddr  vm-name

5.启动虚拟机

virsh  start  vm-name

6.正常关闭虚拟机

virsh  shutdown  vm-name

7.强制关闭虚拟机,相当于拔掉电源

virsh  destroy  vm-name

8.重启虚拟机

virsh  reboot  vm-name

9.从一个 xml 文件定义一个虚拟机

virsh  define  vmn.xml

vmn.xml 是一个 xml 文件路径名称

10.取消定义一个虚拟机

就是删除一台虚拟机,但是虚拟机的磁盘文件会保留

virsh  undefine  vm-name  --nvram


二、执行效果

1.查看所有虚拟机

在这里插入图片描述

2.创建新的虚拟机

在这里插入图片描述

3.模板快速创建虚拟机

在这里插入图片描述

4.虚拟机硬盘添加

在这里插入图片描述

5.虚拟机网卡添加

在这里插入图片描述

6.创建虚拟机快照

在这里插入图片描述
在这里插入图片描述

7.删除虚拟机硬件,快照

删除虚拟机硬盘
在这里插入图片描述
删除虚拟机网卡
在这里插入图片描述
删除虚拟机快照
在这里插入图片描述

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐