osi-tools/bin/osi-shell

73 lines
1.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# Copyright (C) 2018 Luke Shumaker
# Copyright (C) 2023-2024 Umorpha Systems
# SPDX-License-Identifier: AGPL-3.0-or-later
declare -r NAME=osi-shell
declare -r VERSION=20231023
set -euE
install_prefix="$(realpath --logical --canonicalize-missing -- "${BASH_SOURCE[0]}/../..")"
readonly install_prefix
source "${install_prefix}/lib/osi.sh"
source "${install_prefix}/lib/argparse.sh"
main() {
local arg_image=
local arg_qemu=()
opt_positional() {
if (( $# < 1 )); then
argparse:error "Expected at least 1 positional argument, got none"
return
fi
arg_image=$1
arg_qemu=("${@:2}")
}
argparse main "$@"
case "$opt_mode" in
error)
osi.sh:print "Try '%q --help' for more information" "${0##*/}" >&2
return $EXIT_INVALIDARGUMENT
;;
version)
osi.sh:print "%s (osi-tools) %s" "$NAME" "$VERSION"
return $EXIT_SUCCESS
;;
help)
osi.sh:print 'Usage: %s [OPTIONS] FILENAME.img [-- QEMU_ARGS...] [< SCRIPT.sh]' "${0##*/}"
osi.sh:print ' or: %s [OPTIONS] -- FILENAME.img [QEMU_ARGS...] [< SCRIPT.sh]' "${0##*/}"
osi.sh:print 'Operating System Image: Interactive Shell'
echo
osi.sh:print 'OPTIONS:'
echo "${opt_flaghelp}"
return $EXIT_SUCCESS
;;
main)
tmp=$(mktemp -td -- "${0##*/}.XXXXXXXXXX")
trap "rm -rf -- ${tmp@Q}" EXIT
mkfifo -- "$tmp/sync"
qemu-system-x86_64 \
-machine accel=kvm \
-m 512 \
-vnc unix:"$tmp/vnc.sock" \
-name "osi-shell $arg_image" \
-drive media=disk,format=raw,if=virtio,file="$arg_image" \
-serial stdio \
-serial file:"$tmp/exit" \
"${arg_qemu[@]}" \
< <(cat <"$tmp/sync" >/dev/null; cat; while sleep 0.1; do printf '\x04'; done) \
> >(read -r -N1 c; printf '%s' "$c"; :>"$tmp/sync"; exec cat)
return "$(sed 's/\r$//' <"$tmp/exit")"
;;
*) osi.sh:error $EXIT_FAILURE 'Internal error. The programmer writing this tool screwed up.';;
esac
}
main "$@"