When the deploy-qemu script is run with less than 2 arguments, it ended with error, instead of printing usage. This was due to using 'set -u' and trying to expand unset variables "$1" and "$2" as part of checking if they were provided. The issue has been fixed by checking number of provided arguments, instead of their content. The same approach is used in 'deploy-openstack' script. Signed-off-by: Tomas Hozza <thozza@redhat.com>
73 lines
1.8 KiB
Bash
Executable file
73 lines
1.8 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
#
|
|
# deploy-qemu IMAGE USERDATA
|
|
#
|
|
# Starts an ephemeral virtual machine in qemu, injecting configuration via
|
|
# cloud-init. Stopping this script stops the VM and discards all data.
|
|
#
|
|
# IMAGE -- An os image that can be booted by qemu and has cloud-init
|
|
# installed and enabled. No changes are made to this file.
|
|
#
|
|
# USERDATA -- A cloud-init user-data config file, or a directory of
|
|
# configuration as accepted by the `gen-user-data` tool.
|
|
#
|
|
# In addition, if the QEMU_EXTRA_ARGS environment variable is defined, it adds
|
|
# its content as additional arguments to qemu.
|
|
|
|
set -euo pipefail
|
|
|
|
if [[ "$#" != 2 ]]; then
|
|
echo "usage: $0 IMAGE USERDATA"
|
|
exit 1
|
|
fi
|
|
|
|
scriptdir=$(dirname "$0")
|
|
image=$1
|
|
userdata=$2
|
|
read -ra qemu_extra_args <<< "${QEMU_EXTRA_ARGS:-}"
|
|
workdir=$(mktemp -d "$scriptdir/qemu-tmp-XXXXXX")
|
|
function cleanup() {
|
|
rm -rf "$workdir"
|
|
}
|
|
trap cleanup EXIT
|
|
|
|
mkdir "$workdir/cidata"
|
|
|
|
if [ -d "$userdata" ]; then
|
|
"$scriptdir/gen-user-data" "$userdata" > "$workdir/cidata/user-data"
|
|
else
|
|
cp "$userdata" "$workdir/cidata/user-data"
|
|
fi
|
|
|
|
echo -e "instance-id: nocloud\nlocal-hostname: vm\n" > "$workdir/cidata/meta-data"
|
|
|
|
case $(uname -s) in
|
|
"Linux")
|
|
genisoimage \
|
|
-input-charset utf-8 \
|
|
-output "$workdir/cloudinit.iso" \
|
|
-volid cidata \
|
|
-joliet \
|
|
-rock \
|
|
-quiet \
|
|
-graft-points \
|
|
"$workdir/cidata/user-data" \
|
|
"$workdir/cidata/meta-data"
|
|
;;
|
|
|
|
"Darwin")
|
|
# conviently uses the last component of source as volumeid, which has to be cidata
|
|
hdiutil makehybrid -iso -joliet -o "$workdir/cloudinit.iso" "$workdir/cidata"
|
|
;;
|
|
esac
|
|
|
|
qemu-system-x86_64 \
|
|
-M accel=kvm:hvf \
|
|
-m 1024 \
|
|
-snapshot \
|
|
-cpu host \
|
|
-net nic,model=virtio \
|
|
-net user,hostfwd=tcp::2222-:22,hostfwd=tcp::4430-:443 \
|
|
-cdrom "$workdir/cloudinit.iso" \
|
|
"${qemu_extra_args[@]}" "$image"
|