Automatically start Virtual Machine and Auto-mount — 15 Dec, 2016

I love Virtual Machines, they are useful and with them you can do whatever you want without cluttering your main system.

I like to keep my main os as simple as possible, if I want some server software I usually create a Virtual Machine for that.

My workflow to start working with web development was something like this:

  1. Open VirtualBox
  2. Start the virtual machine
  3. Mount the /var/www in the Host OS with SSHFS
  4. Open VIM and start working

I had an alias to mount the /var/www directory but that isn't enough, I still had to open VirtualBox, start the virtual machine and I had to keep the Virtual Machine window open.

I made a simple script to automate that, plus the VM runs in headless mode, all you need is VirtualBox, a virtual machine and SSHFS.

The script:

#! /bin/sh


if [ "$ACTION" == "start" ]; then

    VBoxManage startvm "$VM_NAME" --type headless
    sleep $BOOT_WAIT
    echo "$PASSWORD" | sshfs $VM_USER@$VM_IP:$VM_PATH $LOCAL_PATH -o password_stdin
    echo "DONE"

if [ "$ACTION" == "stop" ]; then
    fusermount -u $LOCAL_PATH
    VBoxManage controlvm "$VM_NAME" acpipowerbutton
    echo "DONE"

# no action provided or invalid, show usage
echo "Start Virtual Machine and automount"
echo "Usage: <action> <vm_name> <vm_user> <vm_ip> <vm_addr> <local_mountpoint> <vm_password> <wait_for_boot_time_in_seconds>"
echo "Examples:"
echo ' start "TURNKEY LAMP" root vmlamp.local /var/www ~/vmwww password 10'
echo ' stop "TURNKEY LAMP" ~/vmwww'

You can download with curl using:

curl > && chmod +x

The usage of this script is very simple, just call ./ --help and it will print some examples.

Tip: Create an alias for each of your virtual machines, this way you don't need to type all those arguments. You can do that in .bashrc or .zshrc, (...) depending of your shell.

By the way, is a great tool, you should take a look at it.

As always you can contact me if you have some suggestions. Thank you for reading.