美文网首页
KVM脚本整理

KVM脚本整理

作者: david9 | 来源:发表于2020-06-07 15:09 被阅读0次
    克隆虚拟机

    vim /data/shll/clone.sh

    #!/bin/bash
    set -e
    kvm_path=$1
    original=$2
    target=$3
    format=$4
    if [ -z "${kvm_path}" ]; then
        echo "missing kvm path"
        exit 1
    fi
    if [ -z "${original}" ]; then
        echo "missing original"
        exit 1
    fi
    if [ -z "${target}" ]; then
        echo "missing target"
        exit 1
    fi
    if [ -z "${format}" ]; then
        echo "missing format"
        exit 1
    fi
    
    virt-clone -d -o ${original} -n ${target} -f ${kvm_path}/${target}.${format}
    

    使用说明:

    ./clone.sh 镜像目录 源 目标 镜像格式
    如:./clone.sh /data/vm centos-base centos-test-1 qcow2

    初始化虚拟机

    vim /data/shell/init.sh

    #!/bin/bash
    set -e
    eth0_uuid=$(uuidgen)
    date=$(date "+%Y_%m_%d_%H_%M_%S")
    random=$(cat /dev/urandom | head -n 10 | md5sum | head -c 10)
    mount_path="/mnt/kvm_${random}_${date}"
    
    kvm_path=$1
    image=$2
    format=$3
    ipaddr=$4
    if [ -z "${kvm_path}" ]; then
        echo "missing kvm path"
        exit 1
    fi
    if [ -z "${image}" ]; then
        echo "missing image"
        exit 1
    fi
    if [ ! -f "${kvm_path}/${image}.${format}" ]; then
        echo "no such image ${image}"
        exit 1
    fi
    if [ -z "${format}" ]; then
        echo "missing format"
        exit 1
    fi
    if [ -z "${ipaddr}" ]; then
        echo "missing ip addr"
        exit 1
    fi
    
    mkdir -p ${mount_path}
    echo "mounting image ..."
    guestmount -a ${kvm_path}/${image}.${format} -m /dev/centos/root ${mount_path}
    echo "mount image done"
    echo "creating network ..."
    if [ -f "${mount_path}/etc/sysconfig/network-scripts/ifcfg-eth0" ];then
      rm -rf ${mount_path}/etc/sysconfig/network-scripts/ifcfg-eth0
    fi
    cat >> ${mount_path}/etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF
    TYPE=Ethernet
    PROXY_METHOD=none
    BROWSER_ONLY=no
    BOOTPROTO=static
    DEFROUTE=yes
    IPV4_FAILURE_FATAL=no
    IPV6INIT=no
    IPV6_AUTOCONF=no
    IPV6_DEFROUTE=no
    IPV6_FAILURE_FATAL=no
    IPV6_ADDR_GEN_MODE=stable-privacy
    NAME=eth0
    DEVICE=eth0
    ONBOOT=yes
    NETMASK=255.255.255.0
    GATEWAY=10.88.36.1
    DNS1=8.8.8.8
    DNS2=223.5.5.5
    EOF
    echo "UUID=${eth0_uuid}" >> ${mount_path}/etc/sysconfig/network-scripts/ifcfg-eth0
    echo "IPADDR=${ipaddr}" >> ${mount_path}/etc/sysconfig/network-scripts/ifcfg-eth0
    echo "eth0 uuid ${eth0_uuid}"
    echo "eth0 ip ${ipaddr}"
    echo "create network done."
    echo "umounting image ..."
    umount ${mount_path}
    echo "umount image done."
    rm -rf ${mount_path}
    echo "all done."
    exit 0
    

    使用说明:

    ./init.sh 镜像目录 镜像名 镜像格式 初始化IP
    如:./init.sh /data/vm centos-test-1 qcow2 10.88.36.150

    检测网段IP占用情况
    #!/bin/bash
    network_number=192.168.1
    start_time=$(date +%s)
    echo "*********Running ...**********"
    for ((i = 0; i <= 255; i++)); do
    {
        ping ${network_number}.$i -c 2 | grep -q "ttl=" && echo "${network_number}.$i yes" >> ipyes.txt || echo "${network_number}.$i no" >> ipno.txt
    } &
    done
    wait
    
    end_time=$(date +%s)
    echo $end_time
    echo "*************Spent Time:$(expr $end_time - $start_time)**************"
    

    相关文章

      网友评论

          本文标题:KVM脚本整理

          本文链接:https://www.haomeiwen.com/subject/lesctktx.html