akaros/scripts/ak-scripts/ak-launch-qemu-monitor.sh
<<
>>
Prefs
   1#!/usr/bin/env bash
   2#
   3# Copyright (c) 2015 Google Inc.
   4# Kevin Klues <klueska@cs.berkeley.edu>
   5# See LICENSE for details.
   6
   7sleep_cmd="sleep 0xda39a3ee5e6b4b0d3255bfef95601890afd80709"
   8welcome_cmd="echo -ne \"Welcome to the qemu monitor!\nPress 'Ctrl-a d' to detach and return to your shell.\n\""
   9
  10function short_description() {
  11        echo "Launch a qemu monitor to attach to a qemu instance"
  12}
  13
  14function usage() {
  15        echo "Usage:"
  16        echo "    ${cmd} -h | --help"
  17        echo "    ${cmd} [ --print-tty-only ]"
  18        echo ""
  19        echo "Options:"
  20        echo "    -h --help         Display this screen and exit"
  21        echo "    --print-tty-only  Launch the monitor, print its tty and exit."
  22        echo "                      Dont actually enter the monitor."
  23}
  24
  25function get_qemu_monitor_tty() {
  26        while [ "${tty_dev}" = "" ]; do
  27                local ps_cmd="ps -a -o tty=TTY -o args | grep \"${sleep_cmd}\" | grep -v grep"
  28                local ps_info="$(eval "${ps_cmd}")"
  29                local tty_dev="$(echo ${ps_info} | cut -d" " -f 1)"
  30        done
  31        echo "/dev/${tty_dev}"
  32}
  33
  34function main() {
  35        # Create the monitor if there isn't one yet
  36        local name="ak-qemu-monitor"
  37        local list="$(screen -list | grep ${name})"
  38        if [ "${list}" == "" ]; then
  39                screen -d -m -S ${name} /bin/bash -c "${welcome_cmd};${sleep_cmd}"
  40        fi
  41
  42        # If ${print_tty_only} is set, print the tty and exit
  43        if [ "${print_tty_only}" = "true" ]; then
  44                get_qemu_monitor_tty
  45                exit 0
  46        fi
  47
  48        # Otherwise...
  49        # Print some info about using the monitor
  50        echo ""
  51        echo "You are about to enter the qemu monitor for Akaros!"
  52        echo "We use 'screen' to create a tty device to host the monitor."
  53        echo "Once attached, you can detach from the monitor at anytime"
  54        echo "using the normal screen command:"
  55        echo ""
  56        echo "    Ctrl-a d"
  57        echo ""
  58        echo "To reattach from a shell, simply rerun this script:"
  59        echo ""
  60        echo "    ak launch-qemu-monitor"
  61        echo ""
  62        echo "While in the monitor, you should be able to run all of"
  63        echo "the normal qemu monitor commands. See the following link"
  64        echo "for more information:"
  65        echo ""
  66        echo "    https://en.wikibooks.org/wiki/QEMU/Monitor"
  67        echo ""
  68
  69        # Wait for any key to be pressed
  70        echo "Press any key to continue..."
  71        (tty_state="$(stty -g)"
  72        stty -icanon
  73        LC_ALL=C dd bs=1 count=1 > /dev/null 2>&1
  74        stty "$tty_state"
  75        ) < /dev/tty
  76
  77        # Attach the monitor
  78        screen -d -r ${name}
  79}
  80
  81