2018-08-06 01:49:45 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
# 2018 Luke Shumaker
|
2018-08-12 20:37:04 +00:00
|
|
|
declare -r NAME=osi-run
|
2018-08-15 01:15:21 +00:00
|
|
|
declare -r VERSION=20180814
|
2018-08-12 20:37:04 +00:00
|
|
|
|
|
|
|
set -euE
|
2018-08-16 05:32:59 +00:00
|
|
|
source ./lib/osi.sh
|
2018-08-11 22:46:04 +00:00
|
|
|
|
2018-08-12 20:37:04 +00:00
|
|
|
main() {
|
2018-08-14 15:37:29 +00:00
|
|
|
local arg_mode=main
|
2018-08-12 20:37:04 +00:00
|
|
|
local args
|
2018-08-14 15:37:29 +00:00
|
|
|
if ! args="$(getopt -n "${0##*/}" -o hV -l help,version -- "$@")"; then
|
2018-08-12 20:37:04 +00:00
|
|
|
arg_mode=error
|
|
|
|
else
|
2018-08-14 20:19:07 +00:00
|
|
|
eval "set -- $args"
|
2018-08-12 20:37:04 +00:00
|
|
|
while true; do
|
|
|
|
case "$1" in
|
|
|
|
-V|--version) shift; arg_mode=version;;
|
|
|
|
-h|--help) shift; arg_mode=usage;;
|
|
|
|
--) shift; break;;
|
|
|
|
*) error 1 'Internal error. The programmer writing this tool screwed up.';;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
case "$arg_mode" in
|
2018-08-14 15:37:29 +00:00
|
|
|
main)
|
2018-08-12 20:37:04 +00:00
|
|
|
if (( $# != 1 )); then
|
|
|
|
if (( $# == 0 )); then
|
|
|
|
error 0 "Expected 1 positional argument, got none"
|
|
|
|
else
|
2018-08-14 20:17:31 +00:00
|
|
|
error 0 "Expected 1 positional argument, got %d: %s" "$#" "${*@Q}"
|
2018-08-12 20:37:04 +00:00
|
|
|
fi
|
|
|
|
arg_mode=error
|
|
|
|
else
|
|
|
|
arg_image=$1
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
case "$arg_mode" in
|
|
|
|
error) print "Try '%q --help' for more information" "${0##*/}" >&2; return 2;;
|
|
|
|
version)
|
|
|
|
print "%s (notsystemd) %s" "$NAME" "$VERSION"
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
usage)
|
2018-08-15 01:15:21 +00:00
|
|
|
print 'Usage: %s [OPTIONS] FILENAME.img [< SCRIPT.sh]' "${0##*/}"
|
|
|
|
print 'Operating System Image: Interactive Shell'
|
2018-08-12 20:37:04 +00:00
|
|
|
echo
|
|
|
|
print 'OPTIONS:'
|
|
|
|
print ' -h, --help display this help'
|
|
|
|
print ' -V, --version output version information'
|
|
|
|
return 0
|
|
|
|
;;
|
2018-08-14 15:37:29 +00:00
|
|
|
main)
|
2018-08-15 01:15:21 +00:00
|
|
|
tmp=$(mktemp -td -- "${0##*/}.XXXXXXXXXX")
|
|
|
|
trap "rm -rf -- ${tmp@Q}" EXIT
|
2018-08-06 01:49:45 +00:00
|
|
|
|
2018-08-15 01:15:21 +00:00
|
|
|
mkfifo -- "$tmp/sync"
|
|
|
|
|
|
|
|
qemu-system-x86_64 \
|
|
|
|
-machine accel=kvm \
|
|
|
|
-m 512 \
|
2018-08-16 18:47:39 +00:00
|
|
|
-vnc unix:"$tmp/vnc.sock" \
|
2018-08-15 01:15:21 +00:00
|
|
|
-name "osi-shell $arg_image" \
|
|
|
|
-drive media=disk,format=raw,if=virtio,file="$arg_image" \
|
|
|
|
-serial stdio \
|
|
|
|
-serial file:"$tmp/exit" \
|
|
|
|
< <(cat <"$tmp/sync" >/dev/null; cat; while sleep 0.1; do printf '\x04'; done) \
|
2018-08-16 05:32:59 +00:00
|
|
|
> >(read -r -N1 c; printf '%s' "$c"; :>"$tmp/sync"; exec cat)
|
2018-08-15 01:15:21 +00:00
|
|
|
|
|
|
|
return "$(sed 's/\r$//' <"$tmp/exit")"
|
|
|
|
;;
|
2018-08-12 20:37:04 +00:00
|
|
|
esac
|
2018-08-06 01:49:45 +00:00
|
|
|
}
|
2018-08-12 20:37:04 +00:00
|
|
|
|
|
|
|
main "$@"
|