Skip to content

Instantly share code, notes, and snippets.

@mcgrew
Created March 21, 2009 10:35
Show Gist options
  • Save mcgrew/82807 to your computer and use it in GitHub Desktop.
Save mcgrew/82807 to your computer and use it in GitHub Desktop.
A script for running (and listing) virtual machine images with kvm/qemu
#!/bin/bash
DEFAULT_RAM=768;
SNAPSHOT="";
VGA="std";
VMDIR="${HOME}/vm";
VM="qemu-kvm";
#AUDIO_DRV="sdl"
AUDIO_DRV="pa"
RAM=$DEFAULT_RAM;
TABLET=" -usbdevice tablet ";
NOQUIT=" -no-quit ";
while getopts "qsv:nm:rl" optname; do
case "$optname" in
"s")
NOQUIT="";
SNAPSHOT=",snapshot=on";;
"m")
RAM=$OPTARG;;
"n")
NET="-net nic,model=ne2k_pci -net tap,script=/etc/qemu-ifup";;
"v")
VGA=$OPTARG;;
"q")
VM="qemu";;
"r")
# TABLET="";
REMOTE=1;
# VGA="none";
MOREOPTS="$MOREOPTS -redir tcp:3389::3389 ";;
"l")
MOREOPTS="$MOREOPTS -loadvm default";;
*)
echo "Unknown error while processing options" # Should not occur
exit;;
esac
done
shift `expr $OPTIND - 1`
if [ $# -eq 0 ]; then
echo "Usage:";
echo " `echo $0 | sed 's/.*\///'` [-s] [-v] [-m <num>] image [<extra_opts>]";
echo " Options:";
echo " -s Run in snapshot mode. This will prevent any changes to the first virtual drive from being written to disk.";
echo " -v [std|cirrus|vmware|none] Emulate a particular VGA card. Defaults to std."
echo " -m <num> The number of megabytes of ram to allocate to the virtual machine. Defaults to $DEFAULT_RAM."
echo " -n Connects the vm to a tap device for a real network address."
echo " -r Set up port forwarding to allow remote desktop connections to a windows guest (10.0.2.15)";
echo " -q Use the qemu executable instead of $VM. This is necessary for some guest operating systems.";
echo " <extra_opts> Extra options to be passed to $VM; view the qemu man page for more details";
echo;
echo "The following virtual machine images are available:";
ls $VMDIR | grep -E "\.img$|\.vmdk$|\.qcow2$" | sed -e "s/\.img$//" -e "s/\.vmdk$//" -e "s/\.qcow2$//" -e "s/^/ /";
exit;
fi
img=''
if [ -r $VMDIR/$1.img ]; then
img=$1.img;
fi;
if [ -r $VMDIR/$1.vmdk ]; then
img=$1.vmdk;
fi;
if [ -r $VMDIR/$1.qcow2 ]; then
img=$1.qcow2;
fi;
if [ -d $VMDIR/$img ]; then
echo "Virtual Machine image '$1' not found"
exit;
fi;
if [ -r $VMDIR/storage.img -a ! -r $VMDIR/storage.lock ]; then
STORAGE="-drive file=$VMDIR/storage.img,media=disk,index=1"
touch $VMDIR/storage.lock
fi;
echo "Launching virtual machine '$1' ...";
shift
QEMU_AUDIO_DRV=$AUDIO_DRV $VM $NOQUIT -vga $VGA -usb $TABLET -m $RAM -soundhw es1370 $NET -localtime $MOREOPTS -drive file=$VMDIR/$img,media=disk,index=0$SNAPSHOT $STORAGE $* 2>>/tmp/vm.log
if [ "$STORAGE" ]; then
rm $VMDIR/storage.lock;
fi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment