vbox: use raw disk image volumes

Users can now just re-run `fbox build-disk` after each build to refresh their
disk, and that process should be much faster than before.

Arguments to build-disk are passed through to make-fuchsia-vol, so smaller
disks and different partition geometries can be used, as well as passing
command line files to include.

Change-Id: I8102390f7ef610c6e4c40165a7e36a07eeb60a80
diff --git a/vbox/cmds/build-disk.sh b/vbox/cmds/build-disk.sh
index 8112e59..be62223 100755
--- a/vbox/cmds/build-disk.sh
+++ b/vbox/cmds/build-disk.sh
@@ -3,19 +3,41 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-mkdir -p $(dirname $FUCHSIA_VBOX_VDI)
+mkdir -p $(dirname $FUCHSIA_VBOX_RAW)
 
-if [[ ! -e ${FUCHSIA_VBOX_SOURCE_DISK} ]]; then
-  echo "$FUCHSIA_VBOX_SOURCE_DISK not found, building..."
-  "${FUCHSIA_SCRIPTS_DIR}/installer/build-installable-userfs.sh"
+mfv=$FUCHSIA_BUILD_DIR/host_x64/make-fuchsia-vol
+
+if [[ ! -x $mfv ]]; then
+	echo "You need to build the 'make-fuchsia-vol' package" >&2
+	exit 1
 fi
 
-if [[ -e ${FUCHSIA_VBOX_VDI} ]]; then
-	uuid=$(VBoxManage showmediuminfo out/vbox/efi_fs.vdi  | grep UUID: | head -n 1 | awk '{print $2}')
-	if [[ -n "${uuid}" ]]; then
-		uuid="--uuid=${uuid}"
-		rm "${FUCHSIA_VBOX_VDI}"
-	fi
+if [[ ! -e $FUCHSIA_VBOX_RAW ]]; then
+	echo "Allocating raw image space"
+	case $(uname) in
+		Linux)
+			fallocate -l $FUCHSIA_VBOX_DISK_SIZE $FUCHSIA_VBOX_RAW
+			;;
+		Darwin)
+			mkfile -n $FUCHSIA_VBOX_DISK_SIZE $FUCHSIA_VBOX_RAW
+			;;
+		*)
+			echo "Unsupported platform" >&2
+			exit 1
+			;;
+	esac
 fi
 
-VBoxManage convertfromraw $uuid "${FUCHSIA_VBOX_SOURCE_DISK}" "${FUCHSIA_VBOX_VDI}"
+if [[ ! -e $FUCHSIA_VBOX_VMDK ]]; then
+	VBoxManage internalcommands createrawvmdk -filename ${FUCHSIA_VBOX_VMDK} -rawdisk ${FUCHSIA_VBOX_RAW}
+fi
+
+if [[ ! -e $FUCHSIA_BUILD_DIR/cmdline ]]; then
+	echo "$FUCHSIA_BUILD_DIR/cmdline is not present. Populate it to set a kernel command line"
+fi
+
+# builds/updates the disk image:
+if ! "$mfv" "$@" "$FUCHSIA_VBOX_RAW" ; then
+	echo "Raw disk image build failed" >&2
+	exit 1
+fi
diff --git a/vbox/cmds/console.sh b/vbox/cmds/console.sh
index c3d387f..c385bd5 100755
--- a/vbox/cmds/console.sh
+++ b/vbox/cmds/console.sh
@@ -9,4 +9,8 @@
 trap "stty $oldtty" EXIT
 echo "Connecting to ${FUCHSIA_OUT_DIR}/vbox/${FUCHSIA_VBOX_NAME}.sock"
 echo "Use CTRL+Q to exit the serial console"
-socat stdio,rawer,escape=0x11 unix-connect:${FUCHSIA_OUT_DIR}/vbox/${FUCHSIA_VBOX_NAME}.sock
\ No newline at end of file
+raw=rawer
+if [[ $(uname) = 'Linux' ]]; then
+  raw=raw,echo=0
+fi
+socat stdio,$raw,escape=0x11 unix-connect:${FUCHSIA_OUT_DIR}/vbox/${FUCHSIA_VBOX_NAME}.sock
\ No newline at end of file
diff --git a/vbox/cmds/create.sh b/vbox/cmds/create.sh
index 197feef..1dc3a95 100755
--- a/vbox/cmds/create.sh
+++ b/vbox/cmds/create.sh
@@ -3,6 +3,10 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
+if [[ ! -e $FUCHSIA_VBOX_VMDK ]]; then
+  "${FUCHSIA_VBOX_SCRIPT_DIR}/fbox.sh" build-disk || exit $?
+fi
+
 if ! VBoxManage list vms | grep "\"$FUCHSIA_VBOX_NAME\""; then
   VBoxManage createvm --name "${FUCHSIA_VBOX_NAME}" --register
 else
@@ -14,7 +18,6 @@
 fi
 
 VBoxManage modifyvm "${FUCHSIA_VBOX_NAME}" \
-  --paravirtprovider=kvm \
   --memory $FUCHSIA_VBOX_RAM \
   --audio null \
   --audiocontroller hda \
@@ -23,11 +26,9 @@
   --hpet on \
   --pae on \
   --longmode on \
-  --cpuid-portability-level 0 \
   --cpus $FUCHSIA_VBOX_CPUS \
   --hwvirtex on \
   --vram 128 \
-  --accelerate3d on \
   --firmware efi \
   --nestedpaging on \
   --nic1 "nat" \
@@ -39,12 +40,11 @@
   --vtxvpid on \
   --largepages on \
   --usbehci off \
-  --usbxhci off \
   --keyboard usb \
   --mouse usbtablet
 
 if $existing; then
-	VBoxManage storagectl "${FUCHSIA_VBOX_NAME}" --name STORAGE --remove
+	VBoxManage storagectl "${FUCHSIA_VBOX_NAME}" --name STORAGE --remove > /dev/null 2>&1
 fi
 
 VBoxManage storagectl "${FUCHSIA_VBOX_NAME}" --name STORAGE \
@@ -58,4 +58,4 @@
   --device 0 \
   --type hdd \
   --nonrotational on \
-  --medium "${FUCHSIA_VBOX_VDI}"
+  --medium "${FUCHSIA_VBOX_VMDK}"
diff --git a/vbox/env.sh b/vbox/env.sh
index 354707a..8f65ade 100755
--- a/vbox/env.sh
+++ b/vbox/env.sh
@@ -25,8 +25,9 @@
 export FUCHSIA_VBOX_CPUS
 export FUCHSIA_VBOX_RAM=${FUCHSIA_VBOX_RAM:-4096}
 export FUCHSIA_VBOX_VRAM=${FUCHSIA_VBOX_VRAM:-128}
-export FUCHSIA_VBOX_SOURCE_DISK=${FUCHSIA_VBOX_SOURCE_DISK:-"$FUCHSIA_OUT_DIR/build-installer/efi_fs"}
-export FUCHSIA_VBOX_VDI=${FUCHSIA_VBOX_VDI:-$FUCHSIA_OUT_DIR/vbox/efi_fs.vdi}
+export FUCHSIA_VBOX_DISK_SIZE=${FUCHSIA_VBOX_DISK_SIZE:-4g)}
+export FUCHSIA_VBOX_RAW=${FUCHSIA_VBOX_RAW:-$FUCHSIA_OUT_DIR/vbox/disk.raw}
+export FUCHSIA_VBOX_VMDK=${FUCHSIA_VBOX_VMDK:-$FUCHSIA_OUT_DIR/vbox/disk.vmdk}
 export FUCHSIA_VBOX_CONSOLE_SOCK=${FUCHSIA_VBOX_CONSOLE_SOCK:-"$FUCHSIA_OUT_DIR/vbox/${FUCHSIA_VBOX_NAME}.sock"}
 
 if [[ -z ${FUCHSIA_VBOX_CPUS} ]]; then
diff --git a/vbox/fbox.sh b/vbox/fbox.sh
index a0126e8..74e3222 100755
--- a/vbox/fbox.sh
+++ b/vbox/fbox.sh
@@ -3,7 +3,7 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-FUCHSIA_VBOX_SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
+export FUCHSIA_VBOX_SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
 source "$FUCHSIA_VBOX_SCRIPT_DIR/env.sh"
 
 usage() {
@@ -24,7 +24,11 @@
 
   start)
     shift
-    VBoxManage startvm "${FUCHSIA_VBOX_NAME}" "$@"
+    if [[ $(uname) == "Linux" && -z $DISPLAY ]]; then
+      VBoxHeadless -s "${FUCHSIA_VBOX_NAME}" "$@"
+    else
+      VBoxManage startvm "${FUCHSIA_VBOX_NAME}" "$@"
+    fi
     ;;
   off|stop)
     shift