summaryrefslogtreecommitdiff
path: root/bin/vm
diff options
context:
space:
mode:
Diffstat (limited to 'bin/vm')
-rwxr-xr-xbin/vm45
1 files changed, 23 insertions, 22 deletions
diff --git a/bin/vm b/bin/vm
index 19255d2..b0e1303 100755
--- a/bin/vm
+++ b/bin/vm
@@ -28,7 +28,7 @@ add() {
while getopts :c:i:k:p:s: opt; do
case $opt in
[cikps]) eval "$opt=$OPTARG";;
- *) Opth=1 add; exit ;;
+ *) Opth=1 add "$1"; exit ;;
esac
done
shift $((OPTIND - 1))
@@ -38,25 +38,27 @@ add() {
[ "$1" = 'alpine-iso' ] && return
mkdir -p "$dir/$1" && cd "$dir/$1" || die "add $1 failed"
hdd=${p-$1.raw}
- [ -f "$hdd" ] || dd if=/dev/zero of=$hdd bs=1 count=0 seek=${s-32g} 2>/dev/null
+ [ -f "$hdd" ] || dd if=/dev/zero of="$hdd" bs=1 count=0 seek="${s-32g}" 2>/dev/null
echo "hdd=\"$hdd\"" > config
[ "$c" ] && {
[ -d "../$c" ] || die "invalid directory: $dir/$c"
iso=$(getconf "$c" iso)
- [ "$iso" ] && echo "iso=\"../$c/$iso\"" >> config
cp "../$c/vmlinux" "../$c/initrd" .
- echo "kernel=${k-vmlinux}" >> config
- echo "initrd=initrd" >> config
- echo "cpu=1" >> config
- echo "ram=512" >> config
- echo "arg=\"console=hvc0\"" >> config
+ [ "$iso" ] && echo "iso=\"../$c/$iso\"" >> config
+ cat <<- EOT >> config
+ kernel=${k-vmlinux}
+ initrd=initrd
+ cpu=1
+ ram=512
+ arg="console=hvc0"
+ EOT
}
}
console() {
usage 'console name' 'Attach a console to a VM' && return
[ "$1" ] || die 'console: name is missing'
- cd "$dir/$1" || die 'console $1 failed'
+ cd "$dir/$1" || die "console $1 failed"
[ -f vftool.pid ] || die "vm $1 is not active"
screen -r "$1"
}
@@ -66,10 +68,10 @@ del() {
case $1 in ''|*/*|.*) die "invalid VM name: $1" ;; esac
[ -d "$dir/$1" ] || die "$dir/$1 not found"
[ -f "$dir/$1/vftool.pid" ] && die "vm $1 is still active, stop it first"
- rm -rf "$dir/$1"
+ rm -rf "${dir:?}/$1"
}
-die() { [ "$1" ] && echo "$0: $@" >&2; exit 1; }
+die() { [ "$1" ] && echo "$0: $*" >&2; exit 1; }
edit() {
usage 'edit name' 'Edit a VM configuration' && return
@@ -128,14 +130,14 @@ exp() {
finalize() {
tty=$(vftool_tty)
- echo "root\nuname -a" >> "$tty"
+ printf 'root\nuname -a\n' >> "$tty"
}
getconf() { awk -F '=' -v k="$2" '$1 == k {print $2}' "$dir/$1/config"; }
help() {
usage 'help' 'Print this help text' && return
- echo "$vm_version\nManage virtual machines\nUsage: vm command [options] [args]"
+ printf '%s\n' "$vm_version\nManage virtual machines\nUsage: vm command [options] [args]"
Opth=1; for c in $Cmdlist; do $c; done
}
@@ -210,13 +212,12 @@ ls() {
}
start() {
- usage 'start [-af][-c vm] name' 'Start a VM' && return
- while getopts :ac:f opt; do
+ usage 'start [-af] name' 'Start a VM' && return
+ while getopts :af opt; do
case $opt in
(a) a=1 ;;
- (c) from=$OPTARG ;;
(f) f=1 ;;
- (*) Opth=1 start; exit;;
+ (*) Opth=1 start "$1"; exit;;
esac
done
shift $((OPTIND - 1))
@@ -227,7 +228,7 @@ start() {
[ -f vftool.pid ] && die "Error: process $(cat vftool.pid) is active or $PWD/vftool.pid should be removed"
start_vm & sleep 2
[ "$f" ] && finalize
- [ "$a" ] && vm console $1
+ [ "$a" ] && vm console "$1"
}
start_vm() (
@@ -254,9 +255,9 @@ start_vm() (
stop() {
usage 'stop name' 'Stop a VM' && return
[ "$1" ] || die 'stop: name missing'
- cd "$dir/$1" || die 'stop $1 failed'
+ cd "$dir/$1" || die "stop $1 failed"
[ -f vftool.pid ] || die "stop: vm $1 is not active"
- kill $(cat vftool.pid) || rm -f vftool.pid
+ kill "$(cat vftool.pid)" || rm -f vftool.pid
}
usage() { [ "$Opth" ] && printf " %-34s %s\n" "$1" "$2"; }
@@ -274,8 +275,8 @@ Cmdlist='add console del edit exp info help ls log start stop version'
[ "$1" ] && C=$1 && shift 1 || { help; exit 1; }
for c in $Cmdlist; do
case $c in
- ($C) cmd=$c; break ;;
- ($C*) [ "$cmd" ] && die "ambiguous command $C" || cmd=$c ;;
+ ("$C") cmd=$c; break ;;
+ ("$C"*) [ "$cmd" ] && die "ambiguous command $C" || cmd=$c ;;
esac
done
[ "$cmd" ] || { help; exit 1; } && $cmd "$@"