scripts: Setup storage for moblab vm

This CL adds a '--moblab' option to cros_start_vm for
starting an image with a moblab environment.

The moblab environment provides:
  - A 35GB disk for use by the VM
  - 4GB of allotted RAM
  - SSH from host to guest still works, you will need to
    follow the SSH instructions for moblab
    (ie. testing keys and ssh config)
  - Hostforwarding access of moblab webpages:
    - 127.0.0.1:9991 leads to the Mob* Monitor on the vm
    - 127.0.0.1:8888 leads to the AFE on the vm
  - Ethernet over USB connections from the vm to a DUT
    work. You need to specify the correct USB device for
    passthrough.

BUG=chromium:532548
TEST=Launched the VM and checked that storage was present,
checked that ssh worked and that I was able to access the
AFE and Mob* Monitor pages from the host.

Change-Id: I4a5d04475fb756f87f22ca8b1c541d501d6d934a
Reviewed-on: https://chromium-review.googlesource.com/303499
Commit-Ready: Matthew Sartori <msartori@chromium.org>
Tested-by: Matthew Sartori <msartori@chromium.org>
Reviewed-by: Simran Basi <sbasi@chromium.org>
diff --git a/bin/cros_start_vm b/bin/cros_start_vm
index 05e4b80..11f178c 100755
--- a/bin/cros_start_vm
+++ b/bin/cros_start_vm
@@ -20,6 +20,7 @@
     "Usb devices for passthrough. Specified in a comma-separated list
      where each item is of the form <host_usb_bus>:<usb_device_id>
      (eg. --usb_devices=1:3,2:7)"
+DEFINE_boolean moblab ${FLAGS_FALSE} "Setup environment for moblab"
 
 set -e
 
diff --git a/lib/cros_vm_lib.sh b/lib/cros_vm_lib.sh
index 56bf662..3b41028 100644
--- a/lib/cros_vm_lib.sh
+++ b/lib/cros_vm_lib.sh
@@ -181,6 +181,7 @@
     fi
 
     local net_option="-net nic,model=virtio"
+    local net_user="-net user,hostfwd=tcp:127.0.0.1:${FLAGS_ssh_port}-:22"
     if [ -f "$(dirname "${vm_image}")/.use_e1000" ]; then
       info "Detected older image, using e1000 instead of virtio."
       net_option="-net nic,model=e1000"
@@ -216,6 +217,33 @@
       fi
     fi
 
+    local moblab_env=""
+    if [ ${FLAGS_moblab} -eq ${FLAGS_TRUE} ]; then
+      # Increase moblab memory size.
+      moblab_env="-m 4G"
+
+      # Set up an external USB storage device.
+      qemu-img create -f raw usb_disk 35g
+      mkfs.ext4 -F usb_disk
+      e2label usb_disk MOBLAB-STORAGE
+
+      moblab_env+=" -usb -drive id=usb_disk,if=none,file=usb_disk"
+      moblab_env+=" -device usb-storage,drive=usb_disk"
+
+      # Add hostforwarding for important moblab pages to the SLIRP connection.
+      MOB_MONITOR_PORT=9991
+      AFE_PORT=8888
+      DEVSERVER_PORT=7777
+
+      net_user+=",hostfwd=tcp:127.0.0.1:${MOB_MONITOR_PORT}-:9991"
+      net_user+=",hostfwd=tcp:127.0.0.1:${AFE_PORT}-:80"
+      net_user+=",hostfwd=tcp:127.0.0.1:${DEVSERVER_PORT}-:8080"
+
+      info "Mob* Monitor: 127.0.0.1:${MOB_MONITOR_PORT}"
+      info "Autotest: 127.0.0.1:${AFE_PORT}"
+      info "Devserver: 127.0.0.1:${DEVSERVER_PORT}"
+    fi
+
     set_kvm_pipes
     for pipe in "${KVM_PIPE_IN}" "${KVM_PIPE_OUT}"; do
       sudo rm -f "${pipe}"  # assumed safe because, the PID is not running
@@ -249,9 +277,10 @@
       ${net_option} \
       ${nographics} \
       ${snapshot} \
-      -net user,hostfwd=tcp:127.0.0.1:${FLAGS_ssh_port}-:22 \
+      ${net_user} \
       ${incoming} ${incoming_option:+"$incoming_option"} \
       ${usb_passthrough} \
+      ${moblab_env} \
       ${drive}
 
     info "KVM started with pid stored in ${KVM_PID_FILE}"