感谢支持
我们一直在努力

通过Shell、Python脚本管理KVM虚拟机

一个想法:

1 Python2.7

2 python的web框架 bottle

3 shell脚本管理kvm

组合在一起就可以了~

这段时间我会用bottle 简单的实现管理kvm 主机,开源出去的~ 性能烂的话,别怪我~

管理kvm主机完全可以在页面上完成,国外有个人用django写了这套程序,感觉还不错,只是稍复杂,bug有点多。。。。  个人觉得 自己开写一套简单的,python本身有kvm的库,大家要是觉得麻烦的话,也可以用os模块,调用virsh的命令来管理kvm主机,这完全是可行的~

https://www.webvirtmgr.net/docs/

上面是安装配置的教程

安装好需要的库之后,直接运行就好了

./manage.py runserver 10.96.60.61:8000

 

 

也可以利用这个小脚本,针对虚拟机开机,关机,重启~

用法:

  1. 查看虚拟机列表~
  2. # kvmctl.sh –list
  3. machine1
  4. machine2
  5. machineN
  6. 开启虚拟机
  7. # kvmctl.sh machine2 startd
  8. Starting up ‘machine2’ … (daemon mode)
  9. 虚拟机状态
  10. # kvmctl.sh –show
  11. MACHINE PID STIME TIME
  12. machine1 4856 Feb11 10:34:59
  13. machine2 11155 Mar12 00:05:45
  14. 关闭虚拟机
  15. # kvmctl.sh machine1 stop
  16. Shutting down ‘machine1’ …

 

Config file example:
KVM_M=1G
KVM_HDA=$BASEDIR/$MACHINE/root.qcow2
KVM_HDB=$BASEDIR/$MACHINE/swap.qcow2
KVM_HDC=$BASEDIR/.imgs/mysql.raw
KVM_HDD=$BASEDIR/.imgs/tmp.qcow2
KVM_NET=”nic,macaddr=52:54:00:00:02:52 -net tap”
KVM_PIDFILE=/var/run/192.168.0.249.pid
KVM_TCP_PORT=10000
KVM_MONITOR=”tcp:127.0.0.1:${KVM_TCP_PORT},server,nowait”

KVM_EXTRA=

 

  1. #!/bin/bash
  2. BASEDIR=“/var/local/kvm”
  3. KVM=“/usr/bin/kvm”
  4. KVM_CONFIG=“settings.cfg”
  5. do_help()
  6. {
  7. PROGRAM_NAME=`basename $0`
  8. echo “Usage:”
  9. echo “$PROGRAM_NAME machine (start|startd|stop|monitor|status|settings|edit)”
  10. echo “$PROGRAM_NAME (–help|–list|–show)”
  11. }
  12. if [ $# -eq 0 ]; then
  13. do_help
  14. exit 1
  15. fi
  16. if [ “$1” == “–help” ]; then
  17. do_help
  18. exit 0
  19. fi
  20. if [ “$1” == “–list” ]; then
  21. ls –1 $BASEDIR
  22. exit 0
  23. fi
  24. if [ “$1” == “–show” ]; then
  25. ps -ef | awk -v format=“%-19s %-7s %-7s %s\n” ‘\
  26. BEGIN { \
  27. printf(format,“MACHINE”, “PID”, “STIME”, “TIME”) \
  28. } \
  29. /kvm -name/&&!/awk/ { \
  30. printf(format, $10, $2, $5, $7) \
  31. }’
  32. exit 0
  33. fi
  34. MACHINE=$1
  35. MACHINEDIR=$BASEDIR/$MACHINE
  36. if [ ! -d $MACHINEDIR ]; then
  37. echo -e “Error. (Machine not found at $BASEDIR/)\n”
  38. do_help
  39. exit 1
  40. fi
  41. do_load_settings()
  42. {
  43. if [ -f “$MACHINEDIR/$KVM_CONFIG” ]; then
  44. source $MACHINEDIR/$KVM_CONFIG
  45. else
  46. echo -e “Error. ($MACHINEDIR/$KVM_CONFIG not found)\n”
  47. echo “Example $KVM_CONFIG:”
  48. echo ‘KVM_M=512M’
  49. echo ‘KVM_HDA=$BASEDIR/$MACHINE/root.qcow2’
  50. echo ‘KVM_HDB=$BASEDIR/.imgs/swap.qcow2’
  51. echo ‘KVM_HDC=$BASEDIR/.imgs/home.qcow2’
  52. echo ‘KVM_HDD=’
  53. echo ‘KVM_NET=”nic,macaddr=52:54:00:00:02:53 -net tap”‘
  54. echo ‘KVM_TCP_PORT=10001’
  55. echo ‘KVM_PIDFILE=/var/run/192.168.0.253.pid’
  56. echo ‘KVM_MONITOR=”tcp:127.0.0.1:${KVM_TCP_PORT},server,nowait”‘
  57. echo ‘KVM_EXTRA=’
  58. exit 1
  59. fi
  60. }
  61. do_check_tcp_port()
  62. {
  63. if [ -z “$1” ]; then
  64. echo “Error. (KVM_TCP_PORT is not set)”
  65. exit 1
  66. fi
  67. }
  68. case “$2”in
  69. start|startd)
  70. do_load_settings
  71. PARAMS=“-name $MACHINE”
  72. [ “$2” == “startd” ] \
  73. && PARAMS=“$PARAMS -vnc none -daemonize” \
  74. && MSG=“(daemon mode)”
  75. [ -n “$KVM_M” ] && PARAMS=“$PARAMS -m $KVM_M”
  76. [ -n “$KVM_HDA” ] && PARAMS=“$PARAMS -hda $KVM_HDA”
  77. [ -n “$KVM_HDB” ] && PARAMS=“$PARAMS -hdb $KVM_HDB”
  78. [ -n “$KVM_HDC” ] && PARAMS=“$PARAMS -hdc $KVM_HDC”
  79. [ -n “$KVM_HDD” ] && PARAMS=“$PARAMS -hdd $KVM_HDD”
  80. [ -n “$KVM_NET” ] && PARAMS=“$PARAMS -net $KVM_NET”
  81. [ -n “$KVM_PIDFILE” ] && PARAMS=“$PARAMS -pidfile $KVM_PIDFILE”
  82. [ -n “$KVM_MONITOR” ] && PARAMS=“$PARAMS -monitor $KVM_MONITOR”
  83. [ -n “$KVM_EXTRA” ] && PARAMS=“$PARAMS $KVM_EXTRA”
  84. echo “Starting up ‘$MACHINE’ … $MSG”
  85. $KVM $PARAMS
  86. [ $? -ne 0 ] \
  87. && echo “Error. (Couldn’t run KVM)”
  88. ;;
  89. stop)
  90. do_load_settings
  91. do_check_tcp_port $KVM_TCP_PORT
  92. echo “Shutting down ‘$MACHINE’ …”
  93. echo “system_powerdown” | nc 127.0.0.1 $KVM_TCP_PORT &>/dev/null
  94. if [ $? -ne 0 ]; then
  95. echo “Error. (Couldn’t connect. Is the machine up?)”
  96. exit 1
  97. fi
  98. ;;
  99. monitor)
  100. do_load_settings
  101. do_check_tcp_port $KVM_TCP_PORT
  102. echo “Starting Monitor for ‘$MACHINE’… (Ctrl+C to exit)”
  103. nc 127.0.0.1 $KVM_TCP_PORT
  104. ;;
  105. status)
  106. do_load_settings
  107. do_check_tcp_port $KVM_TCP_PORT
  108. INFO=“info status\ninfo kvm\ninfo network”
  109. echo -e $INFO | nc -w 1127.0.0.1 $KVM_TCP_PORT \
  110. && echo “”
  111. if [ $? -ne 0 ]; then
  112. echo “Error. (Couldn’t connect. Is the machine up?)”
  113. exit 1
  114. fi
  115. ;;
  116. settings)
  117. do_load_settings
  118. cat $MACHINEDIR/$KVM_CONFIG
  119. ;;
  120. edit)
  121. if [ -z “$EDITOR” ]; then
  122. echo “Error. (EDITOR is not set)”
  123. exit 1
  124. fi
  125. do_load_settings
  126. if [ -n “$KVM_PIDFILE” -a -f “$KVM_PIDFILE” ]; then
  127. echo -n “Warning! ‘$MACHINE’ is running. Continue? (y/N) “
  128. read CONTINUE
  129. [ “$CONTINUE” != “y” -a “$CONTINUE” != “Y” ] \
  130. && exit 1
  131. fi
  132. $EDITOR $MACHINEDIR/$KVM_CONFIG
  133. ;;
  134. *)
  135. do_help
  136. exit 1
  137. ;;
  138. esac
  139. exit 0
赞(0) 打赏
转载请注明出处:服务器评测 » 通过Shell、Python脚本管理KVM虚拟机
分享到: 更多 (0)

听说打赏我的人,都进福布斯排行榜啦!

支付宝扫一扫打赏

微信扫一扫打赏