From aa17037e1a59db38690db44d35b0176cdc5d533b Mon Sep 17 00:00:00 2001 From: Jonathan Maw Date: Tue, 4 Dec 2012 15:54:51 +0000 Subject: Add script to take a baserock image and run it in virtualbox --- run-image | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100755 run-image diff --git a/run-image b/run-image new file mode 100755 index 0000000..bf3dfa9 --- /dev/null +++ b/run-image @@ -0,0 +1,54 @@ +#!/bin/bash +set -e + +# Usage: run-image IMAGE_FILE +IMAGE_FILE="$1" + +if [ -z "$IMAGE_FILE" ]; then + echo "The image to run must be specified" + exit 1 +fi + +# Extract an .img.gz to a .img +if echo "$IMAGE_FILE" | grep -q "\.gz$"; then + EXTRACTED_FILE=$(echo "$IMAGE_FILE" | sed 's|\(.*\)\.gz$|\1|') + if [ ! -e "$EXTRACTED_FILE" ]; then + echo "A .gz file was specified. Decompressing." + gzip -dc "$IMAGE_FILE" >"$EXTRACTED_FILE" + fi + IMAGE_FILE="$EXTRACTED_FILE" +fi + +# Verify the file is of correct type +if ! echo "$IMAGE_FILE" | grep -q "\.img$"; then + echo "Specified file is not a disk image" + exit 1 +fi + +IMAGE_BASEPATH=$(echo "$IMAGE_FILE" | sed 's|\(.*\)\.img|\1|') +IMAGE_BASENAME=$(basename "$IMAGE_FILE" .img) +IMAGE_VBOXNAME=$(basename "$IMAGE_FILE" .img | sed 's|\s|\.|g') + +echo "ensuring vm is created" + +# Convert the .img file into a VirtualBox .vdi if no .vdi exists +if [ ! -e "$IMAGE_BASEPATH.vdi" ]; then + VBoxManage convertdd "$IMAGE_BASEPATH.img" "$IMAGE_BASEPATH.vdi" +fi + +# Create a virtual machine if none exists. +if ! VBoxManage showvminfo "$IMAGE_VBOXNAME" &>/dev/null; then + VBoxManage createvm --name "$IMAGE_VBOXNAME" --ostype Linux26_64 --register +fi + + +# Configure the virtual machine +VBoxManage modifyvm "$IMAGE_VBOXNAME" --ioapic on --memory 1024 --nic1 nat +# Ensure the SATA controller is set up +if ! VBoxManage showvminfo "$IMAGE_VBOXNAME" | grep "SATA Controller" &>/dev/null; then + VBoxManage storagectl "$IMAGE_VBOXNAME" --name "SATA Controller" --add sata --bootable on --sataportcount 1 +fi + +VBoxManage storageattach "$IMAGE_VBOXNAME" --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium "$IMAGE_BASEPATH.vdi" + +VBoxManage startvm "$IMAGE_VBOXNAME" -- cgit v1.2.1