osi-tools/osi-run

79 lines
2.0 KiB
Plaintext
Raw Normal View History

2018-08-06 01:49:45 +00:00
#!/usr/bin/env bash
# 2018 Luke Shumaker
declare -r NAME=osi-run
declare -r VERSION=20180812
set -euE
source "${BASH_SOURCE[0]%/*}/lib/osi.sh"
2018-08-11 22:46:04 +00:00
main() {
2018-08-11 21:15:13 +00:00
2018-08-14 15:37:29 +00:00
local arg_mode=main
local arg_mountpoint=
local arg_image=
local arg_qemu=(
2018-08-06 01:49:45 +00:00
qemu-system-x86_64
-machine accel=kvm
-m 512
)
local args
2018-08-14 15:37:29 +00:00
if ! args="$(getopt -n "${0##*/}" -o hV -l help,version -- "$@")"; then
arg_mode=error
else
eval "args=($args)"
set -- "${args[@]}"
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)
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}"
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)
print 'Usage: %s [OPTIONS] FILENAME.img < SCRIPT.sh' "${0##*/}"
print 'Operating System Image: Run Script'
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)
arg_mountpoint=$(mktemp -dt -- "${0##*/}.XXXXXXXXXX")
trap "rmdir -- ${arg_mountpoint@Q}" EXIT
2018-08-14 15:37:29 +00:00
sudo -- ./osi-mount --root -- "$arg_image" "$arg_mountpoint" "install -Dm755 /dev/stdin ${arg_mountpoint@Q}/etc/osi-run"
"${arg_qemu[@]}" \
2018-08-13 03:37:06 +00:00
-name "osi-run $arg_image" \
-drive media=disk,format=raw,if=virtio,file="$arg_image"
2018-08-14 15:37:29 +00:00
sudo -- ./osi-mount --user -- "cat ${arg_mountpoint@Q}/var/log/osi-run.out; exit \$(cat ${arg_mountpoint@Q}/var/log/osi-run.status)"
;;
2018-08-06 01:49:45 +00:00
*) error 1 'Internal error. The programmer writing this tool screwed up.';;
esac
2018-08-06 01:49:45 +00:00
}
main "$@"