Unverified Commit d09588b9 authored by Kroese's avatar Kroese Committed by GitHub
Browse files

fix: Refactor

* fix: Refactor
parent 19aa3137
Loading
Loading
Loading
Loading
+5 −27
Original line number Diff line number Diff line
@@ -19,35 +19,13 @@ cd /run
trap - ERR

if [[ "$CONSOLE" == [Yy]* ]]; then
  exec qemu-system-x86_64 -pidfile "$QEMU_PID" ${ARGS:+ $ARGS}
  exit $?
  exec qemu-system-x86_64 ${ARGS:+ $ARGS} && exit $?
fi

[[ "$DEBUG" == [Yy1]* ]] && info "$VERS" && set -x
msg=$(qemu-system-x86_64 -daemonize -pidfile "$QEMU_PID" ${ARGS:+ $ARGS})
{ set +x; } 2>/dev/null
msg=$(qemu-system-x86_64 -daemonize ${ARGS:+ $ARGS})

if [[ "$msg" != "char"* ||  "$msg" != *"serial0)" ]]; then
  echo "$msg"
fi

dev="${msg#*/dev/p}"
dev="/dev/p${dev%% *}"

if [ ! -c "$dev" ]; then
  dev=$(echo 'info chardev' | nc -q 1 -w 1 localhost "$QEMU_PORT" | tr -d '\000')
  dev="${dev#*charserial0}"
  dev="${dev#*pty:}"
  dev="${dev%%$'\n'*}"
  dev="${dev%%$'\r'*}"
fi

if [ ! -c "$dev" ]; then
  error "Device '$dev' not found!"
  finish 34
fi

cat "$dev" 2>/dev/null & wait $! || true
{ set +x; } 2>/dev/null && terminal "$msg"
cat "$QEMU_TERM" 2>/dev/null & wait $! || true

sleep 1
finish 0
sleep 1 && finish 0
+32 −1
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ API_CMD=6
API_TIMEOUT=50
API_HOST="127.0.0.1:2210"

QEMU_TERM=""
QEMU_PORT=7100
QEMU_TIMEOUT=50
QEMU_PID="/run/qemu.pid"
@@ -56,6 +57,34 @@ finish() {
  exit "$reason"
}

terminal() {

  local msg=$1

  if [[ "${msg,,}" != "char"* ||  "$msg" != *"serial0)" ]]; then
    echo "$msg"
  fi

  local dev="${msg#*/dev/p}"
  dev="/dev/p${dev%% *}"

  if [ ! -c "$dev" ]; then
    dev=$(echo 'info chardev' | nc -q 1 -w 1 localhost "$QEMU_PORT" | tr -d '\000')
    dev="${dev#*charserial0}"
    dev="${dev#*pty:}"
    dev="${dev%%$'\n'*}"
    dev="${dev%%$'\r'*}"
  fi

  if [ ! -c "$dev" ]; then
    error "Device '$dev' not found!"
    finish 34 && return 34
  fi

  QEMU_TERM="$dev"
  return 0
}

_graceful_shutdown() {

  local code=$?
@@ -129,4 +158,6 @@ _graceful_shutdown() {

_trap _graceful_shutdown SIGTERM SIGHUP SIGINT SIGABRT SIGQUIT

MON_OPTS="-monitor telnet:localhost:$QEMU_PORT,server,nowait,nodelay"
MON_OPTS="\
        -pidfile $QEMU_PID \
        -monitor telnet:localhost:$QEMU_PORT,server,nowait,nodelay"