From ad45725642f2b1b2fb089614807299edf2aaadee Mon Sep 17 00:00:00 2001 From: Hippolyte Chauvin Date: Mon, 4 Dec 2023 12:00:53 +0100 Subject: [PATCH] Ajout du script --- bin/qemush | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100755 bin/qemush diff --git a/bin/qemush b/bin/qemush new file mode 100755 index 0000000..b1ce775 --- /dev/null +++ b/bin/qemush @@ -0,0 +1,124 @@ +#!/bin/bash + +exec_as() { + local user + user="$1" + shift + + if [ "$(whoami)" != "$user" ]; then + exec sudo -u "$user" "$0" "$@" + else + cd || exit + fi +} + +exec_as qemu "$@" + +bin=bin +images=images +if [ -t 1 ]; then + cat=src-hilite-lesspipe.sh +else + cat='cat' +fi +PATH="./${bin}:${PATH}" +EDITOR="${EDITOR:-nvim}" +alias ls='ls --color=auto' +alias exec='exec ' +shopt -s expand_aliases +umask 027 + +perror() { + >&2 printf '\033[1;31mERROR:\033[0m \033[1m%s\033[0m\n' "$*" +} + +public_help() { + local name + name="$(basename "$0")" + exec cat << EOF +${name}: usage: + ${name} active - (default behaviour) list active VMs + ${name} start - start a VM + ${name} watch - attach VM screen session + ${name} ls - list available VMs + ${name} edit - edit VM launching script + ${name} rm - delete launch script + ${name} diskls - list available disk images + ${name} diskadd - create a disk image + ${name} diskrm - delete disk image + ${name} shell - start a shell as user qemu + ${name} help - show this help +EOF +} + +error_usage() { + perror "Invalid usage" + >&2 public_help + return 1 +} + +public_start() { + local vm_name="$1" + shift + + exec screen -S "$vm_name" "$vm_name" "$@" +} + +public_watch() { + exec screen -dr "$1" +} + +public_active() { + exec screen -ls +} + +public_ls() { + echo "Available machines:" + exec ls "$bin" +} + +diskpath() { + printf "%s.qcow2" "${images}/${1}" +} + +public_diskadd() { + exec qemu-img create -f qcow2 "$(diskpath "$1")" "$2" +} + +public_diskrm() { + exec rm -vi -- "$(diskpath "$1")" +} + +public_diskls() { + echo "Available disks:" + exec ls "$images" +} + +public_edit() { + local file="${bin}/${1}" + "$EDITOR" "$file" + exec chmod u+x "$file" +} + +public_rm() { + exec rm -vi -- "${bin}/${1}" +} + +public_shell() { + exec bash -i +} + +public_cat() { + exec "$cat" "${bin}/${1}" +} + +function="$1" +shift + +if [ -z "$function" ]; then + public_active +elif declare -F | cut -d \ -f 3- | grep '^public_' | sed 's/^public_//' | grep -q "^${function}$"; then + "public_${function}" "$@" +else + error_usage +fi