Last active
January 26, 2016 04:57
-
-
Save Raven24/9596410 to your computer and use it in GitHub Desktop.
wrapper script for starting Qemu VMs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# author: Florian Staudacher | |
### | |
# Usage: | |
# ./start.sh | |
# uses the configured base image, creates an overlay (if set) and starts | |
# the VM. The overlay will be discarded at the end (you will have the | |
# possibility to make a copy before it is deleted). | |
# | |
# ./start.sh os_image.qcow2 | |
# uses the given image as base image, creates an overlay (if set) and starts | |
# the VM. The overlay will be discarded (you may also keep it by copying it | |
# somewhere when prompted). | |
### | |
################################################################################ | |
## CONFIG ------------------------------------------------------------------## | |
################################################################################ | |
# point this to a cdrom .img if you want to use one | |
# | |
# EXAMPLE: | |
# VM_CDROM="my_cdrom.iso" | |
# | |
VM_CDROM="" | |
# point this to a local path in your filesystem to get it as SMB share in the | |
# guest OS. It should then be reachable under "\\10.0.2.4\qemu". | |
# for more info on qemu SLIRP networking, see: | |
# http://wiki.qemu.org/Documentation/Networking#User_Networking_.28SLIRP.29 | |
# | |
# NOTE: this requires an installed samba server on the host OS, also qemu | |
# will have to be run as root, otherwise the samba server won't start. | |
# There was a change in recent samba versions, so this will only work with | |
# samba < 4.0 - in that case, you have to configure and start samba yourself. | |
# see: http://bugs.debian.org/727756 | |
# | |
# EXAMPLE: | |
# VM_SMB_SHARE="/home/user/share" | |
# | |
VM_SMB_SHARE="" | |
# set the amount of memory used for the VM (in MB) | |
VM_MEMORY="768" | |
# set the name of the used VM (base) image | |
# this can also be a previously created overlay image | |
VM_IMAGE="root_image.qcow2" | |
# set a port forwarding from the host OS to the VM | |
# this only applies if "user networking" is used (which this script does) | |
# | |
# EXAMPLE: | |
# VM_FWD_PORT="1234::80" | |
# | |
VM_FWD_PORT="10022::22" | |
# specify if you want to use a temporary overlay image to leave the original | |
# VM image untouched. The overlay image will be deleted after shutdown. | |
# To make permanent changes to the base image, don't use an overlay... | |
# | |
# EXAMPLE: | |
# USE_OVERLAY=0 | |
USE_OVERLAY=1 | |
################################################################################ | |
VM_USED_IMAGE="$VM_IMAGE" | |
REL_SCRIPT_PATH="`dirname \"$0\"`" | |
SCRIPT_PATH="`(cd \"$REL_SCRIPT_PATH\" && pwd )`" | |
TEMPNAME=`mktemp -u --suffix ".qcow2" "$SCRIPT_PATH/tmp.XXXXX"` | |
if [ -n "$1" ]; then | |
VM_USED_IMAGE="$1" | |
fi | |
if [ "$USE_OVERLAY" -eq 1 ]; then | |
qemu-img create -b "$VM_USED_IMAGE" -f qcow2 "$TEMPNAME" | |
VM_USED_IMAGE="$TEMPNAME" | |
printf "using temporary overlay image: %s\n" "$TEMPNAME" | |
fi | |
VM_CDROM_CMD="" | |
if [ -n "$VM_CDROM" ]; then | |
VM_CDROM_CMD="-cdrom $VM_CDROM" | |
fi | |
VM_SMB_CMD="" | |
if [ -n "$VM_SMB_SHARE" ]; then | |
VM_SMB_CMD=',smb=' | |
VM_SMB_CMD+="$VM_SMB_SHARE" | |
fi | |
VM_PORTFWD_CMD="" | |
if [ -n "$VM_FWD_PORT" ]; then | |
VM_PORTFWD_CMD="-redir tcp:$VM_FWD_PORT" | |
fi | |
# check kernel word length | |
QEMU_CMD="qemu-system" | |
case `uname -m` in | |
x86_64) | |
QEMU_CMD="$QEMU_CMD-x86_64" | |
;; | |
i686) | |
QEMU_CMD="$QEMU_CMD-i386" | |
;; | |
*) | |
printf "unknown architecture\n" | |
exit 1 | |
;; | |
esac | |
# try to enable kvm, if available | |
VM_KVM_CMD="" | |
`lsmod | grep "kvm" >/dev/null 2>&1` | |
if [ $? -eq 0 ]; then | |
VM_KVM_CMD="-enable-kvm" | |
fi | |
# start qemu | |
$QEMU_CMD -hda "$VM_USED_IMAGE" $VM_KVM_CMD -usb -usbdevice tablet -m "$VM_MEMORY" -rtc base=localtime -vga std -net nic,model=rtl8139 -net user$VM_SMB_CMD $VM_PORTFWD_CMD $VM_CDROM_CMD | |
# done ... | |
if [ "$USE_OVERLAY" -eq 1 ]; then | |
printf "removing temporary overlay file\n" | |
read -p "Press [Enter] to continue (copy the overlay somewhere to keep it) ..." | |
rm "$TEMPNAME" | |
fi | |
printf "finished.\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment