创建虚拟机(kvm)

--virsh console virt_name

-- virsh nodeinfo                               # 查看kvm节点(服务器)信息

--virsh list [--all]                 # 列出虚拟机

--virsh net-list [--all]                # 查看虚拟网络

--virsh dominfo virt_name             # 查看指定虚拟机信息           

--virsh domiflist virt_name            # 查看虚拟机网卡

--virsh start | reboot | shutdown vitr_name   

--virsh destroy virt_name            # 强制关闭指定的虚拟机

--virsh autostart [--disable] virt_name        # 开机自启  

--virsh -c qemu+ssh://user@host_ip:port/syetem      # 远程连接

--qemu-img create -f format file_name size                  #  创建新的镜像盘文件

  qemu-img create -f qcow2 | raw disk.img 50G

--qemu-img info file_name            # 查看磁盘信息

--qemu-img snapshot 

--qemu-img create -b temp -f qcow2 file_name  # 使用后端模板创建

cd /var/lib/libvirt/images/

qemu-img create -b node.qcow2 -f qcow2 node1.img 

创建虚拟交换机

创建配置文件    /etc/libvirt/qemu/networks/vbr.xml

创建虚拟网络 virsh net-define /etc/libvirt/qemu/networks/vbr.xml                # net-undefine 删除

启动虚拟网络 virsh net-start vbr                   # net-destroy   停止 net-edit 修改

查看虚拟网络 virsh net-list 

设置开机自启 virsh net-autostart vbr                                                                       

<network>
  <name>vbr</name>                                              # virsh net-list --all 可见
  <bridge name='vbr' stp='on' delay='0'/>                       # ifconfig 可见
  <domain name='vbr'/>
  <forward mode="nat"/>                                         # 启用地址转换
  <ip address='192.168.1.254' netmask='255.255.255.0'>          # dhcp服务器
    <dhcp>                                                      # 在指定范围内随机分配ip
      <range start="192.168.1.100" end="192.168.1.200"/>
    </dhcp>
  </ip>
</network>

创建虚拟机

创建配置文件   /etc/libvirt/qemu/node1.xml$

创建虚拟机      virsh define /etc/libvirt/qemu/node1.xml

启动虚拟机      virsh start node1

登陆     virsh console node1

<domain type='kvm'>
  <name>node1</name>
  <memory unit='KB'>1524000</memory>                         # 最大可使用内存     
  <currentMemory unit='KB'>1524000</currentMemory>             # 当前最大内存
  <vcpu placement='static'>2</vcpu>                         # 虚拟的cpu个数     
  <os>
    <type arch='x86_64' machine='pc'>hvm</type>                  
    <boot dev='hd'/>                                 # 硬盘启动    
    <bootmenu enable='yes'/>    
    <bios useserial='yes'/>
  </os>
  <features>
    <acpi/>
    <apic/>
  </features>
  <cpu mode='host-passthrough'>
  </cpu>
  <clock offset='localtime'/>                            
  <on_poweroff>destroy</on_poweroff>                        
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>                        
      <driver name='qemu' type='qcow2'/>
      <source file='/var/lib/libvirt/images/node1.img'/>
      <target dev='vda' bus='virtio'/>
    </disk>
    <interface type='bridge'>                            
      <source bridge='vbr'/>                            # 连接的虚拟交换机    
      <model type='virtio'/>
    </interface>
    <channel type='unix'>
      <target type='virtio' name='org.qemu.guest_agent.0'/>            
    </channel>
    <serial type='pty'></serial>
    <console type='pty'>
      <target type='serial'/>
    </console>
    <memballoon model='virtio'></memballoon>
  </devices>
</domain>

脚本创建虚拟机

#!/bin/bash

#read -p '请输入交换机名称:' sw_name
read -p '请输入主机名称:' vm_name
read -p '请分配空间:(?G):' size
read -p '是否自启动?(yes/no):' auto_start

size=${size:-"20G"}
auto_start=${auto_start:-"yes"}

echo $size
echo $auto_start 

#sw_path="/etc/libvirt/qemu/networks"
img_path="/var/lib/libvirt/images"
xml_path="/etc/libvirt/qemu"

isExist=`find  $img_path/ -name $vm_name.img | wc -l`
if [ $isExist  -ne 0 ];then
    echo '镜像文件已经存在!'
    exit 1
fi


qemu-img create -b $img_path/node.qcow2 -f qcow2 $img_path/$vm_name.img $size

sed  "s/node/$vm_name/;/uuid/d"  $xml_path/node.xml >  $xml_path/$vm_name.xml
virsh define $xml_path/$vm_name.xml
virsh start $vm_name

if [ $auto_start == "yes" ];then
    virsh autostart $vm_name
fi

 

脚本删除虚拟机

#!/bin/bash

read -p '请输入要删除的主机名称:' vm_name

img_path="/var/lib/libvirt/images"
xml_path="/etc/libvirt/qemu"

virsh destroy $vm_name > /dev/null
virsh shutdown $vm_name > /dev/null
virsh undefine $vm_name > /dev/null
isExist=`find  $img_path/ -name $vm_name.img | wc -l`
if [ $isExist  -eq 0 ];then
    echo '镜像文件不存在!'
    exit 1
fi


rm -rf $img_path/$vm_name.img $xml_path/$vm_name.xml $xml_path/*-$vm_name > /dev/null
原文地址:https://www.cnblogs.com/ray-mmss/p/10370434.html