web DevOps / qemu / kvm nat / kvm network / danei network

发布时间 2023-09-12 16:32:20作者: siemens800

s

[root@euler share]# rpm -qa | grep openssh              # 查看ssh 
openssh-8.8p1-21.oe2203sp2.x86_64
openssh-server-8.8p1-21.oe2203sp2.x86_64
openssh-clients-8.8p1-21.oe2203sp2.x86_64
openssh-askpass-8.8p1-21.oe2203sp2.x86_64
[root@euler share]# pgrep -l sshd                       # 检索正在运行的进程
1625 sshd
[root@euler share]# ps -elf | grep sshd                 # 查看ssh进程
4 S root        1625       1  0  80   0 -  3473 do_sel 08:24 ?        00:00:00 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
0 S root       43407   28302  0  80   0 -  5494 pipe_r 16:23 pts/7    00:00:00 grep --color=auto sshd

-  

[root@euler bin]# ll /usr/local/bin/           # 查看euler默认环境配置
总用量 30760
-rwxr-xr-x. 1 root root    64984  6月 29 01:23 blur_image
-rwxr-xr-x  1 root root  2322376  3月 18  2021 crashpad_handler
-rwxr-xr-x  1 root root 29108416  3月 18  2021 qq

-

[root@lindows ~]# /usr/local/bin/vm            # 定制化环境命令
vm {clone|remove|setip|completion} vm_name
[root@lindows ~]# more /usr/local/bin/vm       # 定制化脚本变量vm
#!/bin/bash
export LANG=C
. /etc/init.d/functions
CONF_DIR=/etc/libvirt/qemu
IMG_DIR=/var/lib/libvirt/images

function clone_vm(){
    local clone_IMG=${IMG_DIR}/${1};shift
    local clone_XML=${IMG_DIR}/${1};shift
    while ((${#} > 0));do
      if  [ -e ${IMG_DIR}/${1}.img ];then
          echo_warning
          echo "vm ${1}.img is exists"
          return 1
      else
          sudo -u qemu qemu-img create -b ${clone_IMG} -F qcow2 -f qcow2 ${IMG_DIR}/${1}.img 20G >/dev/null
          sed -e "s,node_base,${1}," ${clone_XML} |sudo tee ${CONF_DIR}/${1}.xml >/dev/null
          sudo virsh define ${CONF_DIR}/${1}.xml &>/dev/null
          msg=$(sudo virsh start ${1})
          echo_success
          echo ${msg}
      fi
      shift
    done
}
function remove_vm(){
    if $(sudo virsh list --all --name|grep -Pq "^${1}$");then
       img=$(sudo virsh domblklist $1 2>/dev/null |grep -Po "/var/lib/libvirt/images/.*")
       sudo virsh destroy  $1 &>/dev/null
       sudo virsh undefine $1 &>/dev/null
       sudo rm -f ${img}
       echo_success
       echo "vm ${1} delete"
    fi
}
function vm_setIP(){
    EXEC="sudo virsh qemu-agent-command $1"
    until $(${EXEC} '{"execute":"guest-ping"}' &>/dev/null);do sleep 1;done
    file_id=$(${EXEC} '{"execute":"guest-file-open",
              "arguments":{"path":"/etc/sysconfig/network-scripts/ifcfg-eth0","mode":"w"}}' |\
               python3 -c 'import json;print(json.loads(input())["return"])')
    body=$"# Generated by dracut initrd\nDEVICE=\"eth0\"\nONBOOT=\"yes\"\nNM_CONTROLLED=\"yes\"\nTYPE=\"Ethernet\"\nBOOTPROTO=\"static\"\nIPADDR=\"${2}\"\nPREFIX=24\nGATEWAY=\"${2%.*}.254\"\nDNS1=\"${2%.*}.254\""
    base64_body=$(echo -e "${body}"|base64 -w 0)
    ${EXEC} "{\"execute\":\"guest-file-write\",
              \"arguments\":{\"handle\":${file_id},\"buf-b64\":\"${base64_body}\"}}" &>/dev/null
    ${EXEC} "{\"execute\":\"guest-file-close\",\"arguments\":{\"handle\":${file_id}}}" &>/dev/null
    sudo virsh reboot ${1} &>/dev/null
}
function vm_completion(){
    cat <<"EOF"
__start_vm()
{
  COMPREPLY=()
  local cur
  cur="${COMP_WORDS[COMP_CWORD]}"

  if [[ "${COMP_WORDS[0]}" == "vm" ]] && [[ ${#COMP_WORDS[@]} -eq 2 ]];then
     COMPREPLY=($(compgen -W "clone remove setip" ${cur}))
  fi
  if [[ "${COMP_WORDS[1]}" == "remove" ]] && [[ ${#COMP_WORDS[@]} -gt 2 ]];then
     COMPREPLY=($(compgen -W "$(sudo virsh list --name --all)" ${cur}))
  fi
  if [[ "${COMP_WORDS[1]}" == "setip" ]] && [[ ${#COMP_WORDS[@]} -eq 3 ]];then
     COMPREPLY=($(compgen -W "$(sudo virsh list --name)" ${cur}))
  fi
}

if [[ $(type -t compopt) = "builtin" ]]; then
    complete -o default -F __start_vm vm
else
    complete -o default -o nospace -F __start_vm vm
fi
EOF
}

# main 
case "$1" in
    clone)
      shift
      _img=".Rocky.qcow2"
      _xml=".node_base.xml"
      clone_vm ${_img} ${_xml} ${@}
    ;;
    remove)
      while ((${#} > 1));do
        shift
        remove_vm ${1}
      done
    ;;
    setip)
      if (( ${#} == 3 )) && $(sudo virsh list --all --name|grep -Pq "^${2}$");then
         domid=$(sudo virsh domid $2)
         if [[ ${domid} != "-" ]] && $(grep -Pq "^((25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(25[0-5]|2[0-4]\d|1?\d?\d)$" <<<"${3}");then
            vm_setIP "${2}" "$3"
         fi
      else
         echo "${0##*/} setip vm_name ip.xx.xx.xx"
      fi
    ;;
    completion)
      vm_completion
    ;;
    *)
      echo "${0##*/} {clone|remove|setip|completion} vm_name"
    ;;
esac

exit $?

- [root@lindows ~]# more /etc/libvirt/qemu/networks/private1.xml  # 查看private1.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 net-edit private1                                                                                              
or other application using the libvirt API.                                                                            
-->                                                                                                                    
                                                                                                                       
<network>                                                                                                              
  <name>private1</name>                                                                                                
  <uuid>668f46ac-4153-4c0c-8bab-87a0fbd6a930</uuid>                                                                    
  <forward mode='nat'/>                                                                                                
  <bridge name='private1' stp='on' delay='0'/>                                                                         
  <mac address='52:54:00:1f:13:8c'/>                                                                                   
  <domain name='localhost' localOnly='no'/>                                                                            
  <ip address='192.168.88.254' netmask='255.255.255.0'>                                                                
    <dhcp>                                                                                                             
      <range start='192.168.88.128' end='192.168.88.200'/>                                                             
    </dhcp>                                                                                                            
  </ip>                                                                                                                
</network>

- [root@lindows ~]# more /etc/libvirt/qemu/networks/private2.xml  # 查看private2.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 net-edit private2                                                                                              
or other application using the libvirt API.                                                                            
-->                                                                                                                    
                                                                                                                       
<network>                                                                                                              
  <name>private2</name>                                                                                                
  <uuid>3bbe6f7c-f07a-4fe5-a062-f23954864614</uuid>                                                                    
  <bridge name='private2' stp='on' delay='0'/>                                                                         
  <mac address='52:54:00:c9:a6:0a'/>                                                                                   
  <ip address='192.168.99.254' netmask='255.255.255.0'>                                                                
    <dhcp>                                                                                                             
      <range start='192.168.99.128' end='192.168.99.200'/>                                                             
    </dhcp>                                                                                                            
  </ip>                                                                                                                
</network>

-

 

 

end