summaryrefslogtreecommitdiff
path: root/x86_64-generic/run-image
blob: bf3dfa9bef90f59bf8353efff6388ab216dfb8af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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"