summaryrefslogtreecommitdiff
path: root/debvm-run
diff options
context:
space:
mode:
authorHelmut Grohne <helmut@subdivi.de>2022-12-31 07:44:49 +0100
committerHelmut Grohne <helmut@subdivi.de>2022-12-31 07:44:49 +0100
commita7e264672d431fc3a36b47c48ab016b20b03071b (patch)
tree8e6ad944ea49d89948902fdb2557eeb9890b3af6 /debvm-run
parentc856e06999fdc2b992e07c1b82b4af08c834f78b (diff)
parent947506522176e2b966fa1dc6389fa366322dec12 (diff)
downloaddebvm-a7e264672d431fc3a36b47c48ab016b20b03071b.tar.gz
Merge branch main into debian
Diffstat (limited to 'debvm-run')
-rwxr-xr-xdebvm-run130
1 files changed, 108 insertions, 22 deletions
diff --git a/debvm-run b/debvm-run
index edb01ad..d6a5422 100755
--- a/debvm-run
+++ b/debvm-run
@@ -2,46 +2,132 @@
# Copyright 2022 Helmut Grohne <helmut@subdivi.de>
# SPDX-License-Identifier: MIT
+: <<'POD2MAN'
+=head1 NAME
+
+debvm-run - Run a VM image created by debvm-create
+
+=head1 SYNOPSIS
+
+B<debvm-run> [B<-g>] [B<-i> I<image>] [B<-s> I<sshport>] [B<--> I<qemu options>]
+
+=head1 DESCRIPTION
+
+B<debvm-run> is essentially a thin wrapper around B<qemu> for running a virtual machine image created by B<debvm-create> or something compatible.
+The virtual machine image is expected to be a raw ext4 image with file system label B<debvm>.
+The architecture of the machine is detected from the contained B</bin/true>.
+It must contain a symbolic link pointing to a kernel image at B</vmlinuz> or B</vmlinux> depending on the architecture and a symbolic link pointing to an initrd image at B</initrd.img>.
+Both are extracted and passed to B<qemu>.
+A net interface configured for user mode is added automatically.
+
+=head1 OPTIONS
+
+=over 8
+
+=item B<-g>, B<--graphical>
+
+By default, the option B<-nographic> is passed to B<qemu> and one interacts with the serial console of the machine.
+This configuration is skipped in the presence of this option.
+
+=item B<-i> I<image>, B<--image>=I<image>
+
+This option specifies the location of the virtual machine image file.
+By default B<rootfs.ext4> in the working directory is used.
+
+=item B<-s> I<sshport>, B<--sshport>=I<sshport>
+
+If given, B<qemu> is configured to pass connections to I<127.0.0.1:sshport> to port 22 of the virtual machine.
+
+=item B<--> I<qemu options>
+
+All options beyond a double dash are passed to B<qemu>.
+This can be used to configure additional hardware components.
+One possible use of this method is passing B<-snapshot> to avoid modifying the virtual machine image.
+
+=back
+
+=head1 LIMITATIONS
+
+Due to the way kernel and bootloader are being extracted before running B<qemu>, one cannot upgrade a kernel and then just reboot.
+Attempting to do so, will still use the old kernel.
+Instead, B<qemu> must be terminated and B<debvm-run> should be launched again to pick up the new kernel.
+In order to avoid accidental reboots, one may pass B<-no-reboot> to B<qemu>.
+
+=head1 SEE ALSO
+
+ debvm-create(1) qemu(1)
+
+=cut
+POD2MAN
+
set -u
-IMAGE=rootfs.ext2
+IMAGE=rootfs.ext4
SSHPORT=
GRAPHICAL=
+nth_arg() {
+ shift "$1"
+ printf "%s" "$1"
+}
+
die() {
echo "$*" 1>&2
exit 1
}
-
usage() {
die "usage: $0 [-g] [-i image] [-s sshport] [-- qemu options]"
}
+usage_error() {
+ echo "error: $*" 1>&2
+ usage
+}
-while test "$#" -gt 0; do
- case "$1" in
- -g)
- GRAPHICAL=1
- shift
- ;;
- -i)
- test "$#" -eq 1 && usage
- IMAGE=$2
- shift 2
+opt_graphical() {
+ GRAPHICAL=1
+}
+opt_image() {
+ IMAGE=$1
+}
+opt_sshport() {
+ SSHPORT=$1
+}
+
+while getopts :gi:s:-: OPTCHAR; do
+ case "$OPTCHAR" in
+ g) opt_graphical ;;
+ i) opt_image "$OPTARG" ;;
+ s) opt_sshport "$OPTARG" ;;
+ -)
+ case "$OPTARG" in
+ help)
+ usage
+ ;;
+ graphical|image|sshport)
+ test "$OPTIND" -gt "$#" && usage_error "missing argument for --$OPTARG"
+ "opt_$OPTARG" "$(nth_arg "$OPTIND" "$@")"
+ OPTIND=$((OPTIND+1))
+ ;;
+ image=*|sshport=*)
+ "opt_${OPTARG%%=*}" "${OPTARG#*=}"
+ ;;
+ *)
+ usage_error "unrecognized option --$OPTARG"
+ ;;
+ esac
;;
- -s)
- test "$#" -eq 1 && usage
- SSHPORT=$2
- shift 2
+ :)
+ usage_error "missing argument for -$OPTARG"
;;
- --)
- shift
- break
+ '?')
+ usage_erro "unrecognized option -$OPTARG"
;;
*)
- usage
+ die "internal error while parsing command options, please report a bug"
;;
esac
done
+shift "$((OPTIND - 1))"
test -f "$IMAGE" || die "image '$IMAGE' not found"
test -s "$IMAGE" || die "image '$IMAGE' is empty"
@@ -140,9 +226,9 @@ else
;;
esac
fi
-if test "$MAX_SMP" -gt 1; then
+if test -z "$MAX_SMP" || test "$MAX_SMP" -gt 1; then
NPROC=$(nproc)
- test "$NPROC" -gt "$MAX_SMP" && NPROC=$MAX_SMP
+ test -n "$MAX_SMP" && test "$NPROC" -gt "$MAX_SMP" && NPROC=$MAX_SMP
set -- -smp "$NPROC" "$@"
fi