Advertisement
Guest User

Untitled

a guest
Mar 30th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.42 KB | None | 0 0
  1. [kvasilev@darkstar win10]$ cat win10.sh
  2. #!/bin/bash
  3.  
  4. DIR=/srv/vm/
  5. ISO_DIR=/srv/iso
  6.  
  7. VM_ID=01 # Change if new VM is created;
  8. VM_NAME=win10 # Change if new VM is created;
  9. VM_DIR=${DIR}${VM_NAME}
  10.  
  11. VM_PID_FILE=${VM_DIR}/run/${VM_NAME}.pid
  12. VM_PID_ID="0"
  13.  
  14. VM_MONITOR_FILE=${VM_DIR}/run/${VM_NAME}.mon
  15. VM_MON_ID="0"
  16.  
  17. NET_DEV=${VM_NAME}
  18. NET_MAC=00:00:00:00:00:${VM_ID}
  19.  
  20. # Set the VM pid:
  21. set_vm_pid() {
  22. if [ -f ${VM_PID_FILE} ]; then
  23. VM_PID_ID=`cat ${VM_PID_FILE}`
  24. fi
  25. }
  26.  
  27. # Set if monitor:
  28. set_vm_monitor() {
  29. if [ -e ${VM_MONITOR_FILE} ]; then
  30. VM_MON_ID="1"
  31. fi
  32. }
  33.  
  34. # Check if VM pid is set:
  35. check_vm_pid() {
  36. if [ ${VM_PID_ID} == 0 ]; then
  37. echo "ERROR: VM not running. ( PID is ${VM_PID_ID} )."
  38. exit 1
  39. fi
  40. }
  41.  
  42. # Check if monitor is set:
  43. check_vm_monitor() {
  44. if [ ${VM_MON_ID} == 0 ]; then
  45. echo "ERROR: QEMU monitor file \"${VM_MONITOR_FILE}\" not found."
  46. exit 1
  47. fi
  48. }
  49.  
  50. # Send a command to qemu console viq the monitor file:
  51. send_cmd() {
  52. QMC=$@
  53. echo "Executing: ${QMC}"
  54. echo "${QMC}" | socat - UNIX-CONNECT:${VM_MONITOR_FILE}
  55. }
  56.  
  57. # Start the VM:
  58. start_vm() {
  59.  
  60. # Check if the process started successfully:
  61. if [ -d /proc/${VM_PID_ID} ]; then
  62. echo "ERROR: VM already running. ( PID is ${VM_PID_ID} )."
  63. return 1
  64. fi
  65.  
  66. echo "Starting VM..."
  67.  
  68. # Create run dir, if it does not exists
  69. if [ ! -d ${VM_DIR}/run ]; then
  70. mkdir ${VM_DIR}/run
  71. fi
  72.  
  73. # Create `macvtap` device, if it does not exists:
  74. if [ ! -d "/sys/class/net/${NET_DEV}" ]; then
  75. ip link add link enp8s0f0 name ${NET_DEV} address ${NET_MAC} type macvtap mode bridge
  76. ip link set ${NET_DEV} up
  77. fi
  78. #-device usb-host,hostbus=7,hostport=1 \
  79. #-device usb-host,hostbus=7,hostport=1 \
  80. #-device usb-host,hostbus=7,hostaddr=2 \
  81. #-device usb-host,vendorid=0x05e3,productid=0x0610 \
  82. #-device usb-host,vendorid=0x8086,productid=0x0808 \
  83. #-device usb-host,hostbus=6,hostaddr=3 \
  84. #-machine q35,accel=kvm \
  85. #-device usb-ehci,id=ehci \
  86. #-device usb-host,bus=ehci.0,hostbus=2,hostaddr=4 \
  87. #-device usb-host,hostbus=2,hostport=2.1 \
  88. #-device usb-host,hostbus=7,hostaddr=2 \
  89. #-device usb-host,hostbus=7,hostaddr=3 \
  90. #-device usb-host,hostbus=2,hostport=2.3 \
  91.  
  92. # Start the VM:
  93. qemu-system-x86_64 \
  94. \
  95. -enable-kvm \
  96. -cpu host,kvm=off,hv_relaxed,hv_spinlocks=0x1fff,hv_vapic,hv_vendor_id=randomString \
  97. -machine pc-i440fx-2.10,accel=kvm \
  98. -smp 8,sockets=2,cores=2,threads=2 \
  99. -m 8192 \
  100. \
  101. -boot order=dc \
  102. -drive if=pflash,format=raw,readonly,file=/usr/share/ovmf/x64/OVMF_CODE.fd \
  103. -drive if=pflash,format=raw,file=/usr/share/ovmf/x64/OVMF_VARS.fd \
  104. -drive file=/dev/sdb,format=raw,cache=none,if=virtio \
  105. \
  106. -usb \
  107. \
  108. -device vfio-pci,host=06:00.0,multifunction=on \
  109. -device vfio-pci,host=06:00.1 \
  110. -vga none \
  111. \
  112. -net nic,model=virtio,macaddr=$(cat "/sys/class/net/${NET_DEV}/address") \
  113. -net tap,fd=3 3<>/dev/tap$(cat "/sys/class/net/${NET_DEV}/ifindex") \
  114. \
  115. -k en-us \
  116. -vnc :${VM_ID} \
  117. -monitor unix:${VM_MONITOR_FILE},server,nowait \
  118. -pidfile ${VM_PID_FILE} \
  119. -daemonize
  120.  
  121. # Wait for the VM to start, print message and exit:
  122. SECONDS=0
  123. SUCCESSFUL_BOOT=false
  124. while [ $SECONDS -lt 120 ]; do
  125. if [ -f ${VM_PID_FILE} ]; then
  126. set_vm_pid # Set the pid from the pid file:
  127. if [ -d /proc/${VM_PID_ID} ]; then
  128. echo "VM START successfully. PID: ${VM_PID_ID}. Time: ${SECONDS} seconds."
  129. exit 0
  130. fi
  131. fi
  132. sleep 1
  133. let "SECONDS++"
  134. done
  135.  
  136. # If the VM does not start in 120 sec, print error and exit:
  137. echo "VM KILLED successfully. PID: ${VM_PID_ID}. Time: ${SECONDS} seconds."
  138. return 1
  139.  
  140. }
  141.  
  142. # Reset VM using QEMU console:
  143. reset_vm() {
  144.  
  145. # Check and assigne the PID and MONITOR files:
  146. check_vm_pid
  147. check_vm_monitor
  148.  
  149. # Check if the VM is runnig at all. If not, exit:
  150. if [ ! -d /proc/${VM_PID_ID} ]; then
  151. exit 1
  152. fi
  153.  
  154. send_cmd system_reset
  155.  
  156. # Print result message and exit:
  157. if [ $? = 0 ]; then
  158. echo "VM RESTART successfully. Time: ${SECONDS} seconds."
  159. else
  160. echo "VM RESTART faild. Time: ${SECONDS} seconds."
  161. exit 1
  162. fi
  163.  
  164. }
  165.  
  166. # Stop VM using QEMU console:
  167. stop_vm() {
  168.  
  169. # Check and assigne the PID and MONITOR files:
  170. check_vm_pid
  171. check_vm_monitor
  172.  
  173. # Check if the VM is runnig at all. If not, exit:
  174. if [ ! -d /proc/${VM_PID_ID} ]; then
  175. exit 1
  176. fi
  177.  
  178. # If VM is running, check the task ( [s]top, [t]erminate, [k]ill )
  179. case "$1" in
  180. s) send_cmd "system_powerdown" ;;
  181. t) send_cmd "quit" ;;
  182. k) kill -9 ${VM_PID_ID} ;;
  183. esac
  184.  
  185. # Wait for the VM to exit, and clean temporary files:
  186. SECONDS=0
  187. SUCCESSFUL_SHUTDOWN=false
  188. while [ $SECONDS -lt 120 ]; do
  189. if [ ! -d /proc/${VM_PID_ID} ]; then
  190. if [ -e ${VM_MONITOR_FILE} ]; then rm ${VM_MONITOR_FILE}; fi
  191. if [ -f ${VM_PID_FILE} ]; then rm ${VM_PID_FILE}; fi
  192. SUCCESSFUL_SHUTDOWN=true
  193. break
  194. fi
  195. sleep 1
  196. let "SECONDS++"
  197. done
  198.  
  199. # Print result message and exit:
  200. if [ ${SUCCESSFUL_SHUTDOWN} = true ]; then
  201. case "$1" in
  202. s) echo "VM SHUTDOWN successfully. Time: ${SECONDS} seconds." ;;
  203. t) echo "VM TERMINATE successfully. Time: ${SECONDS} seconds." ;;
  204. k) echo "VM KILLED successfully. PID: ${VM_PID_ID}. Time: ${SECONDS} seconds." ;;
  205. esac
  206. else
  207. case "$1" in
  208. s) echo "VM SHUTDOWN faild. Time: ${SECONDS} seconds." ;;
  209. t) echo "VM TERMINATE failed. Time: ${SECONDS} seconds." ;;
  210. k) echo "VM KILLED failed. PID: ${VM_PID_ID}. Time: ${SECONDS} seconds." ;;
  211. esac
  212. exit 1
  213. fi
  214. }
  215.  
  216. # Check the VM status:
  217. get_status() {
  218. if [ -d /proc/${VM_PID_ID} ]; then
  219. echo "ON-LINE ( PID: ${VM_PID_ID} )"
  220. else
  221. echo "OFF-LINE"
  222. fi
  223. }
  224.  
  225. # Get the VM name:
  226. get_name() {
  227. echo ${VM_NAME}
  228. }
  229.  
  230. # Get VM ID:
  231. get_id() {
  232. echo ${VM_ID}
  233. }
  234.  
  235. # Print VM info:
  236. check_info() {
  237. echo ""
  238. echo " VM info:"
  239. echo " ID : ${VM_ID}"
  240. echo " Name : ${VM_NAME}"
  241. echo " Network info:"
  242. echo " Deveice : ${NET_DEV}@enp8s0f0"
  243. echo " MAC : ${NET_MAC}"
  244. echo " Disk info:"
  245. echo " Directory : ${VM_DIR}"
  246. echo " Disk : `ls ${VM_DIR}/${VM_NAME}.raw`"
  247. echo " Size : `ls -alh ${VM_DIR}/${VM_NAME}.raw | awk '{print $5}'`"
  248. echo " Current status:"
  249. echo " VM Running : $(get_status)"
  250. echo ""
  251. }
  252.  
  253. #Print usage:
  254. pring_usage() {
  255. echo ""
  256. echo " Usage:"
  257. echo ""
  258. echo " Interact:"
  259. echo " [start] - Initialize and start VM."
  260. echo " [reboot] - Reboot VM gracefully. Same as 'stop' and 'start' consequently."
  261. echo " [reset] - Reset VM ungracefully. Same as hardware reset button. ( QEMU system_reset )"
  262. echo " [stop] - Stop VM gracefully. Same as OS shutdown. ( QEMU system_powerdown )."
  263. echo " [terminate] - Terminate VM immediately. Same is power failure. ( QEMU quit )"
  264. echo " [kill] - Kill VM by killing it's process and cleaning data. ( kill -9 [pid] )"
  265. echo " [send] '[cmd]' - Send a commnd to VM via QEMU monitor. Second parmater a valid QEMU command."
  266. echo ""
  267. echo " Get info:"
  268. echo " [status] - Get VM status."
  269. echo " [name] - Get VM name."
  270. echo " [id] - Get VM id."
  271. echo " [info] - Print info about VM."
  272. echo " [help] - Print this info. "
  273. echo ""
  274. }
  275.  
  276. # BEGIN:
  277.  
  278. # Set the PID:
  279. set_vm_pid
  280. set_vm_monitor
  281.  
  282. # Switch between parameters:
  283. case "$1" in
  284. start) start_vm ;; # Start the VM;
  285. reboot) stop_vm "s"; start_vm; ;; # Restart VM by QEMU command;
  286. reset) reset_vm ;; # Run stop and then run start;
  287. stop) stop_vm "s" ;; # Stop VM by QEMU command;
  288. terminate) stop_vm "t" ;; # Termnate VM by QEMU command;
  289. kill) stop_vm "k" ;; # Kill VM by killing the process;
  290. send) send_cmd $2 ;; # Send a custom command to QEMU console;
  291. status) get_status ;; # Get VM status;
  292. name) get_name ;; # Get VM name;
  293. id) get_id ;; # Get VM id;
  294. info) check_info ;; # Get info about the VM;
  295. help) pring_usage ;; # Print usage;
  296. *)
  297. echo "Invalid parameter: $1"
  298. pring_usage
  299. exit 1
  300. ;;
  301. esac
  302.  
  303. exit
  304.  
  305. # END.
  306.  
  307. # Block comment bellow :)
  308. : << 'END'
  309. -boot
  310. -boot menu=on - show boot menu;
  311. -boot c - boot from the first virtual hdd;
  312. -boot d - boot from the first cd-rom;
  313. -boot n - boot from network;
  314. END
  315. [kvasilev@darkstar win10]$
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement