Compare commits
No commits in common. "2defbc140b63b3a3617eb9fae68d115a91a15587" and "8fccf7343f349a73552320975bf1bb44b2a0d178" have entirely different histories.
2defbc140b
...
8fccf7343f
7 changed files with 41 additions and 107 deletions
11
Makefile
11
Makefile
|
@ -13,12 +13,10 @@ SRCD := src
|
||||||
SRC_QEMU_HOME := qemu
|
SRC_QEMU_HOME := qemu
|
||||||
SRC_QEMU_BIN := $(SRC_QEMU_HOME)/bin
|
SRC_QEMU_BIN := $(SRC_QEMU_HOME)/bin
|
||||||
SRC_LAUNCHERSD := $(SRC_QEMU_HOME)/launchers
|
SRC_LAUNCHERSD := $(SRC_QEMU_HOME)/launchers
|
||||||
SRC_TMUX_CONF := $(SRC_QEMU_HOME)/tmux.conf
|
|
||||||
# Actual qemu Unix user home
|
# Actual qemu Unix user home
|
||||||
QEMU_HOME := $(shell echo ~$(QEMU_USER))
|
QEMU_HOME := $(shell echo ~$(QEMU_USER))
|
||||||
QEMU_BIN := $(QEMU_HOME)/bin
|
QEMU_BIN := $(QEMU_HOME)/bin
|
||||||
LAUNCHERSD := $(QEMU_HOME)/launchers
|
LAUNCHERSD := $(QEMU_HOME)/launchers
|
||||||
TMUX_CONF := $(QEMU_HOME)/.tmux.conf
|
|
||||||
|
|
||||||
# Names of all qemush modules
|
# Names of all qemush modules
|
||||||
SRC_MODULES_NAMES := $(notdir $(wildcard $(SRC_QEMU_BIN)/*))
|
SRC_MODULES_NAMES := $(notdir $(wildcard $(SRC_QEMU_BIN)/*))
|
||||||
|
@ -33,7 +31,7 @@ BINS_MODE := 740
|
||||||
|
|
||||||
# Compiler options
|
# Compiler options
|
||||||
CC := cc
|
CC := cc
|
||||||
CC_OPTIONS = -O2
|
CC_OPTIONS = -O3
|
||||||
# C sources file format
|
# C sources file format
|
||||||
SRC_FMT := .c
|
SRC_FMT := .c
|
||||||
# Names of C programs to compile
|
# Names of C programs to compile
|
||||||
|
@ -42,10 +40,10 @@ SRC_C_SOURCES_NAMES := $(notdir $(basename $(wildcard $(SRCD)/*$(SRC_FMT))))
|
||||||
C_BINARIES := $(addprefix $(QEMU_BIN)/,$(SRC_C_SOURCES_NAMES))
|
C_BINARIES := $(addprefix $(QEMU_BIN)/,$(SRC_C_SOURCES_NAMES))
|
||||||
|
|
||||||
# Directories in ~qemu necessary for qemush to work
|
# Directories in ~qemu necessary for qemush to work
|
||||||
QEMUSH_DIRS_NAMES := bin launchers disks
|
QEMUSH_DIRS_NAMES := bin launchers images
|
||||||
QEMUSH_DIRS := $(addprefix $(QEMU_HOME)/,$(QEMUSH_DIRS_NAMES))
|
QEMUSH_DIRS := $(addprefix $(QEMU_HOME)/,$(QEMUSH_DIRS_NAMES))
|
||||||
|
|
||||||
install: $(QEMUSH_DIRS) $(MODULES) $(C_BINARIES) $(QEMUSH) $(LAUNCHERS) $(TMUX_CONF)
|
install: $(QEMUSH_DIRS) $(MODULES) $(C_BINARIES) $(QEMUSH) $(LAUNCHERS)
|
||||||
|
|
||||||
$(QEMUSH_DIRS):
|
$(QEMUSH_DIRS):
|
||||||
$(SUDO_QEMU) mkdir -p $@
|
$(SUDO_QEMU) mkdir -p $@
|
||||||
|
@ -63,6 +61,3 @@ $(C_BINARIES): $(QEMU_BIN)%: $(SRCD)%$(SRC_FMT)
|
||||||
|
|
||||||
$(LAUNCHERS): $(LAUNCHERSD)%: $(SRC_LAUNCHERSD)%
|
$(LAUNCHERS): $(LAUNCHERSD)%: $(SRC_LAUNCHERSD)%
|
||||||
$(SUDO_QEMU) install -m $(BINS_MODE) $^ $@
|
$(SUDO_QEMU) install -m $(BINS_MODE) $^ $@
|
||||||
|
|
||||||
$(TMUX_CONF): $(SRC_TMUX_CONF)
|
|
||||||
$(SUDO_QEMU) install -m 640 $^ $@
|
|
||||||
|
|
15
README.md
15
README.md
|
@ -33,27 +33,30 @@ the same virtual machines
|
||||||
- **Modularization**: `qemush` launching scripts are intended to be
|
- **Modularization**: `qemush` launching scripts are intended to be
|
||||||
stackable to reuse common `qemu` parameters in all virtual machines
|
stackable to reuse common `qemu` parameters in all virtual machines
|
||||||
needing them
|
needing them
|
||||||
- **Process supervision**: `qemush` uses `tmux` to supervise processes and
|
- **Process supervision**: `qemush` uses `screen` to supervise processes
|
||||||
keep track of them
|
and keep track of them
|
||||||
- **Copy-on-write**: images are formatted using `qcow2` to use less space
|
- **Copy-on-write**: images are formatted using `qcow2` to use less space
|
||||||
on disk
|
on disk
|
||||||
|
|
||||||
## Dependencies
|
## Dependencies
|
||||||
|
|
||||||
All dependencies are common packages for a distribution, you'll be able to
|
All dependencies are common packages for a distribution, you'll be able to
|
||||||
grab them from your favorite packages sources.
|
grab them from your packages sources.
|
||||||
|
|
||||||
- `qemu` - this is literally a QEMU wrapper so there's a chance you'll
|
- `qemu` - this is literally a QEMU wrapper so there's a chance you'll
|
||||||
need it
|
need it
|
||||||
- `bash` - the `qemush` interpreter
|
- `bash` - the `qemush` interpreter
|
||||||
- `coreutils` - used for basic OS operations
|
- `coreutils` - used for basic OS operations
|
||||||
- `sudo` - execute commands as `qemu`
|
- `sudo` - execute commands as `qemu`
|
||||||
- `tmux` - for process supervision
|
- `screen` - for process supervision
|
||||||
- `source-highlight` - for syntax highlighting when displaying launching
|
- `source-highlight` - for syntax highlighting when displaying launching
|
||||||
scripts
|
scripts
|
||||||
- `pathof` - see [Installation instructions](#installation-instructions)
|
- `diskpath` - see [Add `qemush` modules in
|
||||||
|
`~qemu/bin`](#add-qemush-modules-in-qemubin)
|
||||||
- any text editor - used for builtin function to edit launching scripts
|
- any text editor - used for builtin function to edit launching scripts
|
||||||
|
|
||||||
|
You can run `qemush depcheck` to check if all dependencies are met.
|
||||||
|
|
||||||
## Installation instructions
|
## Installation instructions
|
||||||
|
|
||||||
### QEMU user and group
|
### QEMU user and group
|
||||||
|
@ -88,7 +91,7 @@ by the `Makefile` for the installation process.
|
||||||
|
|
||||||
### Manual installation (what does the `Makefile` do)
|
### Manual installation (what does the `Makefile` do)
|
||||||
|
|
||||||
- Create `disks`, `launchers` and `bin` directories in `~qemu`
|
- Create `images`, `launchers` and `bin` directories in `~qemu`
|
||||||
- Copy `qemush` scripts parts from `qemu/bin` in `~qemu/bin` with mode
|
- Copy `qemush` scripts parts from `qemu/bin` in `~qemu/bin` with mode
|
||||||
`740`
|
`740`
|
||||||
- Compile C programs from `src` in `~qemu/bin` with mode `740`
|
- Compile C programs from `src` in `~qemu/bin` with mode `740`
|
||||||
|
|
78
bin/qemush
78
bin/qemush
|
@ -1,7 +1,5 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
# version=0.2.0
|
|
||||||
|
|
||||||
# Function to re-exec the script as another user via sudo (only if needed)
|
|
||||||
exec_as() {
|
exec_as() {
|
||||||
local user
|
local user
|
||||||
user="$1"
|
user="$1"
|
||||||
|
@ -12,19 +10,12 @@ exec_as() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Exec the script as qemu
|
|
||||||
exec_as qemu "$@"
|
exec_as qemu "$@"
|
||||||
|
|
||||||
# Directories used in the program
|
|
||||||
bin="${HOME}/launchers"
|
bin="${HOME}/launchers"
|
||||||
images="${HOME}/disks"
|
images="${HOME}/images"
|
||||||
|
|
||||||
# Environment
|
|
||||||
PATH="${bin}:${HOME}/bin:${PATH}"
|
PATH="${bin}:${HOME}/bin:${PATH}"
|
||||||
EDITOR="${EDITOR:-nvim}"
|
EDITOR="${EDITOR:-nvim}"
|
||||||
export QEMUSH_NAME
|
|
||||||
|
|
||||||
# Aliases
|
|
||||||
alias ls='ls --color=auto'
|
alias ls='ls --color=auto'
|
||||||
alias exec='exec '
|
alias exec='exec '
|
||||||
if [ -t 1 ]; then
|
if [ -t 1 ]; then
|
||||||
|
@ -33,20 +24,15 @@ else
|
||||||
alias pretty_cat=cat
|
alias pretty_cat=cat
|
||||||
fi
|
fi
|
||||||
shopt -s expand_aliases
|
shopt -s expand_aliases
|
||||||
|
|
||||||
# Set a restrictive umask to make sure qemu user files are private
|
|
||||||
umask 027
|
umask 027
|
||||||
|
|
||||||
# Function to print a colored error
|
|
||||||
perror() {
|
perror() {
|
||||||
>&2 printf '\033[1;31mERROR:\033[0m \033[1m%s\033[0m\n' "$*"
|
>&2 printf '\033[1;31mERROR:\033[0m \033[1m%s\033[0m\n' "$*"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Function to show the usage
|
|
||||||
public_help() {
|
public_help() {
|
||||||
local name
|
local name
|
||||||
name="$(basename "$0")"
|
name="$(basename "$0")"
|
||||||
|
|
||||||
exec cat << EOF
|
exec cat << EOF
|
||||||
${name}: usage:
|
${name}: usage:
|
||||||
${name} active - (default behaviour) list active VMs
|
${name} active - (default behaviour) list active VMs
|
||||||
|
@ -66,115 +52,95 @@ ${name}: usage:
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
# Function to throw an invalid usage error (skill issue)
|
|
||||||
error_usage() {
|
error_usage() {
|
||||||
perror "Invalid usage"
|
perror "Invalid usage"
|
||||||
>&2 public_help
|
>&2 public_help
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# Function to start a virtual machine ; fails if the virtual machine is
|
|
||||||
# already started thanks to tmux
|
|
||||||
public_start() {
|
public_start() {
|
||||||
QEMUSH_NAME="$1"
|
local vm_name="$1"
|
||||||
shift
|
shift
|
||||||
|
|
||||||
exec tmux new-session \
|
exec screen -S "$vm_name" "$vm_name" "$@"
|
||||||
-s "$QEMUSH_NAME" \
|
|
||||||
"$QEMUSH_NAME" \
|
|
||||||
"$@"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Attach to a running virtual machine output, the latest opened if no
|
|
||||||
# argument is provided
|
|
||||||
public_watch() {
|
public_watch() {
|
||||||
[ -n "$1" ] && set -- -t "$1"
|
exec screen -dr "$1"
|
||||||
|
|
||||||
exec tmux attach "$@"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# List running virtual machines
|
|
||||||
public_active() {
|
public_active() {
|
||||||
exec tmux list-sessions
|
exec screen -ls
|
||||||
}
|
}
|
||||||
|
|
||||||
# List available virtual machines entrypoints
|
|
||||||
public_ls() {
|
public_ls() {
|
||||||
echo "Available machines:"
|
echo "Available machines:"
|
||||||
exec ls "$bin"
|
exec ls "$bin"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Create a copy-on-write disk for a virtual machine
|
|
||||||
public_diskadd() {
|
public_diskadd() {
|
||||||
QEMUSH_NAME="$1"
|
exec qemu-img create -f qcow2 "$(diskpath "$1")" "$2"
|
||||||
shift
|
|
||||||
|
|
||||||
exec qemu-img create -f qcow2 "$(pathof disk)" "$1"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Delete a disk
|
|
||||||
public_diskrm() {
|
public_diskrm() {
|
||||||
QEMUSH_NAME="$1"
|
exec rm -vi -- "$(diskpath "$1")"
|
||||||
shift
|
|
||||||
|
|
||||||
exec rm -vi -- "$(pathof disk)"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# List available disks
|
|
||||||
public_diskls() {
|
public_diskls() {
|
||||||
echo "Available disks:"
|
echo "Available disks:"
|
||||||
exec ls "$images"
|
exec ls "$images"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Edit a virtual machine entrypoint with a text editor
|
|
||||||
public_edit() {
|
public_edit() {
|
||||||
local file="${bin}/${1}"
|
local file="${bin}/${1}"
|
||||||
"$EDITOR" "$file"
|
"$EDITOR" "$file"
|
||||||
[ -f "$file" ] && exec chmod u+x "$file"
|
[ -f "$file" ] && exec chmod u+x "$file"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Delete a virtual machine entrypoint
|
|
||||||
public_rm() {
|
public_rm() {
|
||||||
exec rm -vi -- "${bin}/${1}"
|
exec rm -vi -- "${bin}/${1}"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Invoke bash as qemu user in its home directory
|
|
||||||
public_shell() {
|
public_shell() {
|
||||||
cd || return
|
cd || return
|
||||||
exec bash -i
|
exec bash -i
|
||||||
}
|
}
|
||||||
|
|
||||||
# Output the content of an entrypoint, with coloration if on a virtual
|
|
||||||
# terminal
|
|
||||||
public_cat() {
|
public_cat() {
|
||||||
pretty_cat "${bin}/${1}"
|
pretty_cat "${bin}/${1}"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Copy a file in entrypoints folder
|
|
||||||
public_add() {
|
public_add() {
|
||||||
trap return EXIT
|
trap return EXIT
|
||||||
set -e
|
set -e
|
||||||
|
local name="${2:-$(basename "$1")}"
|
||||||
local name
|
|
||||||
name="${2:-$(basename "$1")}"
|
|
||||||
cp -v -i -- "$1" "${bin}/${2}"
|
cp -v -i -- "$1" "${bin}/${2}"
|
||||||
chmod 740 "${bin}/${name}"
|
chmod 740 "${bin}/${name}"
|
||||||
|
|
||||||
set +e
|
set +e
|
||||||
trap - EXIT
|
trap - EXIT
|
||||||
}
|
}
|
||||||
|
|
||||||
# Run shell commands as qemu
|
|
||||||
public_do() {
|
public_do() {
|
||||||
exec sh -c "$*"
|
exec sh -c "$*"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Retrieve user requested function
|
bold_print() {
|
||||||
|
printf '\033['"${1}m${2}"'\033[0m: %s\n' "$3"
|
||||||
|
}
|
||||||
|
|
||||||
|
public_depcheck() {
|
||||||
|
for i in diskpath screen source-highlight ls rm cp chmod cat; do
|
||||||
|
if command -v "$i" > /dev/null; then
|
||||||
|
bold_print '1;32' OK "$i"
|
||||||
|
else
|
||||||
|
bold_print '1;31' KO "$i"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
function="$1"
|
function="$1"
|
||||||
shift
|
shift
|
||||||
|
|
||||||
# Defauts to `active` if no function is supplied; else checks for a public
|
|
||||||
# function named after the argument; else fails
|
|
||||||
if [ -z "$function" ]; then
|
if [ -z "$function" ]; then
|
||||||
public_active
|
public_active
|
||||||
elif declare -F | cut -d \ -f 3- | grep '^public_' | sed 's/^public_//' | grep -q "^${function}$"; then
|
elif declare -F | cut -d \ -f 3- | grep '^public_' | sed 's/^public_//' | grep -q "^${function}$"; then
|
||||||
|
|
2
qemu/bin/diskpath
Executable file
2
qemu/bin/diskpath
Executable file
|
@ -0,0 +1,2 @@
|
||||||
|
#!/bin/sh
|
||||||
|
printf '%s/%s.qcow2\n' ~/images "$1"
|
|
@ -1,22 +0,0 @@
|
||||||
#!/bin/sh -e
|
|
||||||
|
|
||||||
# Check if QEMUSH_NAME variable is initialized, abort if not
|
|
||||||
[ -n "$QEMUSH_NAME" ]
|
|
||||||
|
|
||||||
# Choose the base dir to print according to argv[1]
|
|
||||||
case "$1" in
|
|
||||||
socket)
|
|
||||||
path="${HOME}/sockets"
|
|
||||||
;;
|
|
||||||
disk)
|
|
||||||
path="${HOME}/disks"
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
false
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
# Create the base dir if it doesn't exist and print the path of the
|
|
||||||
# ressource
|
|
||||||
mkdir -p -- "$path"
|
|
||||||
printf %s/%s\\n "$path" "$QEMUSH_NAME"
|
|
|
@ -1,14 +1,9 @@
|
||||||
#!/bin/sh -x
|
#!/bin/sh -x
|
||||||
|
name="$(basename "$0")"
|
||||||
# Store VM name in environment variable
|
|
||||||
export QEMUSH_NAME
|
|
||||||
[ -z "$QEMUSH_NAME" ] && QEMUSH_NAME=$(basename "$0")
|
|
||||||
|
|
||||||
# Launch the virtual machine
|
|
||||||
exec kvm-spice \
|
exec kvm-spice \
|
||||||
-monitor "unix:$(pathof socket)" \
|
-monitor stdio \
|
||||||
-drive file="$(pathof disk)",if=virtio \
|
-drive file="$(diskpath "$name")",if=virtio \
|
||||||
-net user,hostname="${QEMUSH_NAME}" \
|
-net user,hostname="${name}" \
|
||||||
-usbdevice tablet \
|
-usbdevice tablet \
|
||||||
-name "$QEMUSH_NAME" \
|
-name "$name" \
|
||||||
"$@"
|
"$@"
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
# Keep tmux sessions visible after exit
|
|
||||||
set -g remain-on-exit on
|
|
||||||
|
|
||||||
# Override the nologin directive
|
|
||||||
set -g default-shell /bin/sh
|
|
Loading…
Reference in a new issue