#!/bin/sh # Script by Ypnose - http://ywstd.fr p_err() { printf "%s\n" "$1" >&2 exit 1 } usage() { p_err "usage: ${0##*/} [-b ISO] [-c] [-f FMT] [-h] [-m RAM] [-n] [-p SSH PORT] [-s] [-u OVMF DIR] [IMG]" } qemu_launch() { set -x qemu-system-x86_64 \ -m "${VM_RAM:-2G}" \ -enable-kvm \ -drive file="$VM_IMG",format="${VM_FMT:-qcow2}" \ -nic user,model=e1000,hostfwd=tcp::"${VM_SSH:-9922}"-:22,hostfwd=tcp::8080-:80,hostfwd=tcp::8443-:443,hostfwd=tcp::8888-:8888 \ "$@" } while getopts ":b:cf:hm:np:su:" opt; do case $opt in b) VM_ISO="$OPTARG" ;; c) VM_UI="curses" ;; f) VM_FMT="$OPTARG" ;; h) usage ;; m) VM_RAM="$OPTARG" ;; n) VM_UI="serial" ;; p) VM_SSH="$OPTARG" ;; s) VM_TMP=1 ;; u) VM_OVMF="$OPTARG" ;; *) usage ;; esac done shift $(( OPTIND - 1 )) if [ "$#" -ne 1 ]; then usage fi VM_IMG="$1" if [ ! -f "$VM_IMG" ]; then p_err "$VM_IMG not found. You need to create it with qemu-img" fi if ! grep -q '^kvm' /proc/modules; then p_err "kvm module is not loaded" fi if [ -n "${VM_ISO:+s}" ]; then if [ ! -f "$VM_ISO" ]; then p_err "$VM_ISO not found" else VM_OPTS="-cdrom $VM_ISO -boot order=d" fi else # CD-ROM image disables temporary file [ "$VM_TMP" = 1 ] && VM_OPTS="-snapshot" fi # UEFI boot mode if [ -n "${VM_OVMF:+s}" ]; then if [ ! -r "${VM_OVMF}/OVMF_CODE.fd" ] || [ ! -r "${VM_OVMF}/OVMF_VARS.fd" ]; then p_err "${VM_OVMF}/OVMF_CODE.fd or ${VM_OVMF}/OVMF_VARS.fd missing or unreadable" fi VM_OPTS="$VM_OPTS -drive if=pflash,file=${VM_OVMF}/OVMF_CODE.fd,format=raw,readonly=on -drive if=pflash,file=${VM_OVMF}/OVMF_VARS.fd,format=raw" fi case "$VM_UI" in curses) VM_OPTS="$VM_OPTS -display curses" ;; serial) VM_OPTS="$VM_OPTS -nographic" ;; *) VM_OPTS="$VM_OPTS -display gtk -vga std" ;; esac qemu_launch $VM_OPTS exit