虚拟机操作脚本

虚拟机操作脚本

  • 手动添加磁盘以及磁盘配置

virsh attach-disk vm8 /vmdata22/vm8_2.qcow2 sde --wwn 0x50011731`openssl rand  -base64 0 | md5sum | cut -c1-8` --subdriver qcow2 --config --live

### 详情 ###
<< 'INFO'
  www: 磁盘wwn
  subdriver: 磁盘文件的格式
INFO
  • 手动添加物理磁盘virt-manager

    #!/usr/bash
    
    set -x
    
    
    function add_remove_disk()
    {
        IFS=","
        vm_name=$1
        vm_disk=($2)
        IFS=" "
        if [ "$3" == "remove" ];then
            echo "Remove disk: ${vm_disk[*]}"
            for disk in ${vm_disk[*]};do
                virt-xml ${vm_name} --remove-device --update --disk ${disk};
            done
        else
            echo "Attach disk: ${vm_disk[*]}"
            for disk in ${vm_disk[*]};do
                virt-xml ${vm_name} --add-device --update --disk ${disk},cache=none,bus=scsi,device='lun';
            done
        fi
    }
    
    flag=${1}
    # sdi, sdh, sdg
    vm_name="vm5"
    vm_disk1="/dev/disk/by-id/wwn-0x50014ee0596e2504,/dev/disk/by-id/wwn-0x50014ee00419471e,/dev/disk/by-id/wwn-0x5001173101672bd0"
    
    add_remove_disk ${vm_name} ${vm_disk1} ${flag}
    
    ## sdd sde sdf
    vm_name="vm6"
    vm_disk1="/dev/disk/by-id/wwn-0x50014ee6b0d37088,/dev/disk/by-id/wwn-0x50014ee0aec3dc0b" #,/dev/disk/by-id/wwn-0x50014ee0596e6130"
    
    add_remove_disk ${vm_name} ${vm_disk1} ${flag}
    
    # sdf
    vm_name="vm7"
    vm_disk1="/dev/disk/by-id/wwn-0x50014ee0596e6130"
    
    add_remove_disk ${vm_name} ${vm_disk1} ${flag}
    
  • 虚拟机模拟物理磁盘

    # config 配置下次重启生效; live实时生效,重启失效
    virsh attach-disk vm8 /vmdata22/vm8_2.qcow2 sde --wwn 0x50011731`openssl rand  -base64 8 | md5sum | cut -c1-8` --subdriver qcow2 --config --live
  • 模拟ssd,nvme磁盘

    # nvme 需要 version > 4.2.0; ssd配置scsi设置
    <domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>  
      <qemu:commandline>
        <qemu:arg value='-set'/>
        <qemu:arg value='device.scsi0-0-0-0.rotation_rate=1'/>
        <qemu:arg value='-drive'/>
        <qemu:arg value='file=/vmdata22/centos8_4_ssd_t.qcow2,format=qcow2,if=none,id=NVME1'/>
        <qemu:arg value='-device'/>
        <qemu:arg value='nvme,drive=NVME1,serial=nvme-1'/>
      </qemu:commandline>
      
    # Command
    virt-xml centos8.4 --edit --confirm --qemu-commandline='-set device.scsi0-0-0-0.rotation_rate=1'
    
    virt-xml centos8.4 --edit --confirm --qemu-commandline='-drive file=/vmdata22/centos8_4_ssd_t.qcow2,format=qcow2,if=none,id=NVME1'
    
    virt-xml centos8.4 --edit --confirm --qemu-commandline='-device nvme,drive=NVME1,serial=nvme-1'
    
  • 故障解决

    【问题现象】
    Starting install… ERROR ‘virConnect’ object has no attribute ‘baselineHypervisorCPU’ Domain installation does not appear to have been successful. If it was, you can restart your domain by running: virsh --connect qemu:///system start kvm-test otherwise, please restart your installation.
    【解决办法】
    升级 libvirt-python-3.2.0-3 版本至 libvirt-python-4.5.0-1 
    
上一篇:3389服务器远程端口修改怎么操作


下一篇:宝塔面板+javaweb项目