Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [kvasilev@darkstar win10]$ cat win10.sh
- #!/bin/bash
- DIR=/srv/vm/
- ISO_DIR=/srv/iso
- VM_ID=01 # Change if new VM is created;
- VM_NAME=win10 # Change if new VM is created;
- VM_DIR=${DIR}${VM_NAME}
- VM_PID_FILE=${VM_DIR}/run/${VM_NAME}.pid
- VM_PID_ID="0"
- VM_MONITOR_FILE=${VM_DIR}/run/${VM_NAME}.mon
- VM_MON_ID="0"
- NET_DEV=${VM_NAME}
- NET_MAC=00:00:00:00:00:${VM_ID}
- # Set the VM pid:
- set_vm_pid() {
- if [ -f ${VM_PID_FILE} ]; then
- VM_PID_ID=`cat ${VM_PID_FILE}`
- fi
- }
- # Set if monitor:
- set_vm_monitor() {
- if [ -e ${VM_MONITOR_FILE} ]; then
- VM_MON_ID="1"
- fi
- }
- # Check if VM pid is set:
- check_vm_pid() {
- if [ ${VM_PID_ID} == 0 ]; then
- echo "ERROR: VM not running. ( PID is ${VM_PID_ID} )."
- exit 1
- fi
- }
- # Check if monitor is set:
- check_vm_monitor() {
- if [ ${VM_MON_ID} == 0 ]; then
- echo "ERROR: QEMU monitor file \"${VM_MONITOR_FILE}\" not found."
- exit 1
- fi
- }
- # Send a command to qemu console viq the monitor file:
- send_cmd() {
- QMC=$@
- echo "Executing: ${QMC}"
- echo "${QMC}" | socat - UNIX-CONNECT:${VM_MONITOR_FILE}
- }
- # Start the VM:
- start_vm() {
- # Check if the process started successfully:
- if [ -d /proc/${VM_PID_ID} ]; then
- echo "ERROR: VM already running. ( PID is ${VM_PID_ID} )."
- return 1
- fi
- echo "Starting VM..."
- # Create run dir, if it does not exists
- if [ ! -d ${VM_DIR}/run ]; then
- mkdir ${VM_DIR}/run
- fi
- # Create `macvtap` device, if it does not exists:
- if [ ! -d "/sys/class/net/${NET_DEV}" ]; then
- ip link add link enp8s0f0 name ${NET_DEV} address ${NET_MAC} type macvtap mode bridge
- ip link set ${NET_DEV} up
- fi
- #-device usb-host,hostbus=7,hostport=1 \
- #-device usb-host,hostbus=7,hostport=1 \
- #-device usb-host,hostbus=7,hostaddr=2 \
- #-device usb-host,vendorid=0x05e3,productid=0x0610 \
- #-device usb-host,vendorid=0x8086,productid=0x0808 \
- #-device usb-host,hostbus=6,hostaddr=3 \
- #-machine q35,accel=kvm \
- #-device usb-ehci,id=ehci \
- #-device usb-host,bus=ehci.0,hostbus=2,hostaddr=4 \
- #-device usb-host,hostbus=2,hostport=2.1 \
- #-device usb-host,hostbus=7,hostaddr=2 \
- #-device usb-host,hostbus=7,hostaddr=3 \
- #-device usb-host,hostbus=2,hostport=2.3 \
- # Start the VM:
- qemu-system-x86_64 \
- \
- -enable-kvm \
- -cpu host,kvm=off,hv_relaxed,hv_spinlocks=0x1fff,hv_vapic,hv_vendor_id=randomString \
- -machine pc-i440fx-2.10,accel=kvm \
- -smp 8,sockets=2,cores=2,threads=2 \
- -m 8192 \
- \
- -boot order=dc \
- -drive if=pflash,format=raw,readonly,file=/usr/share/ovmf/x64/OVMF_CODE.fd \
- -drive if=pflash,format=raw,file=/usr/share/ovmf/x64/OVMF_VARS.fd \
- -drive file=/dev/sdb,format=raw,cache=none,if=virtio \
- \
- -usb \
- \
- -device vfio-pci,host=06:00.0,multifunction=on \
- -device vfio-pci,host=06:00.1 \
- -vga none \
- \
- -net nic,model=virtio,macaddr=$(cat "/sys/class/net/${NET_DEV}/address") \
- -net tap,fd=3 3<>/dev/tap$(cat "/sys/class/net/${NET_DEV}/ifindex") \
- \
- -k en-us \
- -vnc :${VM_ID} \
- -monitor unix:${VM_MONITOR_FILE},server,nowait \
- -pidfile ${VM_PID_FILE} \
- -daemonize
- # Wait for the VM to start, print message and exit:
- SECONDS=0
- SUCCESSFUL_BOOT=false
- while [ $SECONDS -lt 120 ]; do
- if [ -f ${VM_PID_FILE} ]; then
- set_vm_pid # Set the pid from the pid file:
- if [ -d /proc/${VM_PID_ID} ]; then
- echo "VM START successfully. PID: ${VM_PID_ID}. Time: ${SECONDS} seconds."
- exit 0
- fi
- fi
- sleep 1
- let "SECONDS++"
- done
- # If the VM does not start in 120 sec, print error and exit:
- echo "VM KILLED successfully. PID: ${VM_PID_ID}. Time: ${SECONDS} seconds."
- return 1
- }
- # Reset VM using QEMU console:
- reset_vm() {
- # Check and assigne the PID and MONITOR files:
- check_vm_pid
- check_vm_monitor
- # Check if the VM is runnig at all. If not, exit:
- if [ ! -d /proc/${VM_PID_ID} ]; then
- exit 1
- fi
- send_cmd system_reset
- # Print result message and exit:
- if [ $? = 0 ]; then
- echo "VM RESTART successfully. Time: ${SECONDS} seconds."
- else
- echo "VM RESTART faild. Time: ${SECONDS} seconds."
- exit 1
- fi
- }
- # Stop VM using QEMU console:
- stop_vm() {
- # Check and assigne the PID and MONITOR files:
- check_vm_pid
- check_vm_monitor
- # Check if the VM is runnig at all. If not, exit:
- if [ ! -d /proc/${VM_PID_ID} ]; then
- exit 1
- fi
- # If VM is running, check the task ( [s]top, [t]erminate, [k]ill )
- case "$1" in
- s) send_cmd "system_powerdown" ;;
- t) send_cmd "quit" ;;
- k) kill -9 ${VM_PID_ID} ;;
- esac
- # Wait for the VM to exit, and clean temporary files:
- SECONDS=0
- SUCCESSFUL_SHUTDOWN=false
- while [ $SECONDS -lt 120 ]; do
- if [ ! -d /proc/${VM_PID_ID} ]; then
- if [ -e ${VM_MONITOR_FILE} ]; then rm ${VM_MONITOR_FILE}; fi
- if [ -f ${VM_PID_FILE} ]; then rm ${VM_PID_FILE}; fi
- SUCCESSFUL_SHUTDOWN=true
- break
- fi
- sleep 1
- let "SECONDS++"
- done
- # Print result message and exit:
- if [ ${SUCCESSFUL_SHUTDOWN} = true ]; then
- case "$1" in
- s) echo "VM SHUTDOWN successfully. Time: ${SECONDS} seconds." ;;
- t) echo "VM TERMINATE successfully. Time: ${SECONDS} seconds." ;;
- k) echo "VM KILLED successfully. PID: ${VM_PID_ID}. Time: ${SECONDS} seconds." ;;
- esac
- else
- case "$1" in
- s) echo "VM SHUTDOWN faild. Time: ${SECONDS} seconds." ;;
- t) echo "VM TERMINATE failed. Time: ${SECONDS} seconds." ;;
- k) echo "VM KILLED failed. PID: ${VM_PID_ID}. Time: ${SECONDS} seconds." ;;
- esac
- exit 1
- fi
- }
- # Check the VM status:
- get_status() {
- if [ -d /proc/${VM_PID_ID} ]; then
- echo "ON-LINE ( PID: ${VM_PID_ID} )"
- else
- echo "OFF-LINE"
- fi
- }
- # Get the VM name:
- get_name() {
- echo ${VM_NAME}
- }
- # Get VM ID:
- get_id() {
- echo ${VM_ID}
- }
- # Print VM info:
- check_info() {
- echo ""
- echo " VM info:"
- echo " ID : ${VM_ID}"
- echo " Name : ${VM_NAME}"
- echo " Network info:"
- echo " Deveice : ${NET_DEV}@enp8s0f0"
- echo " MAC : ${NET_MAC}"
- echo " Disk info:"
- echo " Directory : ${VM_DIR}"
- echo " Disk : `ls ${VM_DIR}/${VM_NAME}.raw`"
- echo " Size : `ls -alh ${VM_DIR}/${VM_NAME}.raw | awk '{print $5}'`"
- echo " Current status:"
- echo " VM Running : $(get_status)"
- echo ""
- }
- #Print usage:
- pring_usage() {
- echo ""
- echo " Usage:"
- echo ""
- echo " Interact:"
- echo " [start] - Initialize and start VM."
- echo " [reboot] - Reboot VM gracefully. Same as 'stop' and 'start' consequently."
- echo " [reset] - Reset VM ungracefully. Same as hardware reset button. ( QEMU system_reset )"
- echo " [stop] - Stop VM gracefully. Same as OS shutdown. ( QEMU system_powerdown )."
- echo " [terminate] - Terminate VM immediately. Same is power failure. ( QEMU quit )"
- echo " [kill] - Kill VM by killing it's process and cleaning data. ( kill -9 [pid] )"
- echo " [send] '[cmd]' - Send a commnd to VM via QEMU monitor. Second parmater a valid QEMU command."
- echo ""
- echo " Get info:"
- echo " [status] - Get VM status."
- echo " [name] - Get VM name."
- echo " [id] - Get VM id."
- echo " [info] - Print info about VM."
- echo " [help] - Print this info. "
- echo ""
- }
- # BEGIN:
- # Set the PID:
- set_vm_pid
- set_vm_monitor
- # Switch between parameters:
- case "$1" in
- start) start_vm ;; # Start the VM;
- reboot) stop_vm "s"; start_vm; ;; # Restart VM by QEMU command;
- reset) reset_vm ;; # Run stop and then run start;
- stop) stop_vm "s" ;; # Stop VM by QEMU command;
- terminate) stop_vm "t" ;; # Termnate VM by QEMU command;
- kill) stop_vm "k" ;; # Kill VM by killing the process;
- send) send_cmd $2 ;; # Send a custom command to QEMU console;
- status) get_status ;; # Get VM status;
- name) get_name ;; # Get VM name;
- id) get_id ;; # Get VM id;
- info) check_info ;; # Get info about the VM;
- help) pring_usage ;; # Print usage;
- *)
- echo "Invalid parameter: $1"
- pring_usage
- exit 1
- ;;
- esac
- exit
- # END.
- # Block comment bellow :)
- : << 'END'
- -boot
- -boot menu=on - show boot menu;
- -boot c - boot from the first virtual hdd;
- -boot d - boot from the first cd-rom;
- -boot n - boot from network;
- END
- [kvasilev@darkstar win10]$
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement