summaryrefslogtreecommitdiff
path: root/x86_64-generic/run-image
diff options
context:
space:
mode:
Diffstat (limited to 'x86_64-generic/run-image')
-rwxr-xr-xx86_64-generic/run-image54
1 files changed, 54 insertions, 0 deletions
diff --git a/x86_64-generic/run-image b/x86_64-generic/run-image
new file mode 100755
index 0000000..bf3dfa9
--- /dev/null
+++ b/x86_64-generic/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"