summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Maw <jonathan.maw@codethink.co.uk>2012-12-04 15:54:51 +0000
committerJonathan Maw <jonathan.maw@codethink.co.uk>2012-12-04 15:56:18 +0000
commitaa17037e1a59db38690db44d35b0176cdc5d533b (patch)
tree14e236ceb5ccba79d3306b5b34ed3a227361212c
parent62cdb1477c5a8d3fca15e02418227bacb73b1a5a (diff)
downloadgenivi-initial-setup-aa17037e1a59db38690db44d35b0176cdc5d533b.tar.gz
Add script to take a baserock image and run it in virtualbox
-rwxr-xr-xrun-image54
1 files changed, 54 insertions, 0 deletions
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"