Skip to content

Instantly share code, notes, and snippets.

@tonejito
Created September 25, 2013 05:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tonejito/6695404 to your computer and use it in GitHub Desktop.
Save tonejito/6695404 to your computer and use it in GitHub Desktop.
thesis-vm - Launches a serial console enabled VM and connects to ttyS0 with gnu screen Andres Hernandez - tonejito September 2013
#!/bin/bash
# thesis-vm - Launches a serial console enabled VM and connects to ttyS0 with gnu screen
# Andres Hernandez - tonejito
# September 2013
RM=rm
TEE=tee
SED=sed
GREP=grep
ECHO=echo
SLEEP=sleep
CLEAR=clear
RESET=reset
SOCAT=socat
SCREEN=screen
MKTEMP=mktemp
KILLALL=killall
VBOXHEADLESS=VBoxHeadless
# Get a random tmp filename
PASSTHROUGH=`$MKTEMP`
# Sleep for a couple seconds
SECS=2
# Virtual Machine name and location
VM_NAME=Thesis
VIRTUALBOX_VMs="$HOME/VirtualBox VMs"
VM_NAME_VBOX="$VIRTUALBOX_VMs/$VM_NAME/$VM_NAME.vbox"
# Filter used with socat output
FILTER='.*N\ PTY\ is\ '
# Pattern to find at $VM_NAME_VBOX
PATTERN='^\ \+<Port\ slot=\"[[:digit:]]\"\ enabled=\"true\"\ IOBase=\"0x[32]f8\"\ IRQ=\"[43]\" server=\"true\"\ path=\"\(.*\)\"\ hostMode=\"HostPipe\"\/>\ *$'
# Get the UNIX domain socket names
UNIX_NAME=`$GREP "$PATTERN" "$VM_NAME_VBOX" | $SED -e "s/$PATTERN/\1/g" | $SED -n "1p"`
# Start the VM if the socket is not found
if [ ! -x $UNIX_NAME ]
then
$VBOXHEADLESS --startvm $VM_NAME &
$SLEEP $SECS
fi
# Detach any existing PTY
$KILLALL $SOCAT
# Map the UNIX domain socket to a PTY and get its name
$SOCAT -d -d $UNIX_NAME PTY,wait-slave 2>&1 | $TEE $PASSTHROUGH &>/dev/null &
$SLEEP $SECS
PTY_NAME=`$GREP "$FILTER" $PASSTHROUGH | $SED -e "s/$FILTER//g"`
# Attach to serial console or die
if [ -n "$PTY_NAME" ]
then
$SCREEN $PTY_NAME
$RM $UNIX_NAME
else
$ECHO "= T - T ="
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment