docker-qemu/start-qemu

74 lines
1.7 KiB
Plaintext
Raw Normal View History

2014-09-20 07:00:11 +00:00
#!/bin/bash
set -e
# main available options:
# QEMU_CPU=n (cores)
# QEMU_RAM=nnn (megabytes)
# QEMU_HDA (filename)
# QEMU_HDA_SIZE (bytes, suffixes like "G" allowed)
# QEMU_CDROM (filename)
2015-07-15 17:19:46 +00:00
# QEMU_BOOT (-boot)
# QEMU_PORTS="xxx[ xxx ...]" (space separated port numbers)
# QEMU_NET_USER_EXTRA="net=192.168.76.0/24,dhcpstart=192.168.76.9" (extra raw args for "-net user,...")
2014-09-20 07:00:11 +00:00
hostArch="$(uname -m)"
qemuArch="${QEMU_ARCH:-$hostArch}"
qemu="${QEMU_BIN:-qemu-system-$qemuArch}"
qemuArgs=()
qemuPorts=( 22 $QEMU_PORTS )
2014-09-20 07:00:11 +00:00
if [ -e /dev/kvm ]; then
qemuArgs+=( -enable-kvm )
elif [ "$hostArch" = "$qemuArch" ]; then
echo >&2
echo >&2 'warning: /dev/kvm not found'
echo >&2 ' PERFORMANCE WILL SUFFER'
echo >&2 ' (hint: docker run --device /dev/kvm ...)'
echo >&2
sleep 3
fi
qemuArgs+=( -smp "${QEMU_CPU:-1}" )
qemuArgs+=( -m "${QEMU_RAM:-512}" )
if [ "$QEMU_HDA" ]; then
if [ ! -f "$QEMU_HDA" -o ! -s "$QEMU_HDA" ]; then
2014-09-20 07:24:50 +00:00
(
set -x
qemu-img create -f qcow2 -o preallocation=off "$QEMU_HDA" "${QEMU_HDA_SIZE:-8G}"
)
2014-09-20 07:00:11 +00:00
fi
2015-11-04 05:52:13 +00:00
# http://wiki.qemu.org/download/qemu-doc.html#Invocation
#qemuArgs+=( -hda "$QEMU_HDA" )
2017-03-09 19:18:36 +00:00
qemuArgs+=( -drive file="$QEMU_HDA",index=0,media=disk,discard=unmap )
2014-09-20 07:00:11 +00:00
fi
if [ "$QEMU_CDROM" ]; then
qemuArgs+=( -cdrom "$QEMU_CDROM" )
fi
2015-07-15 17:19:46 +00:00
if [ "$QEMU_BOOT" ]; then
qemuArgs+=( -boot "$QEMU_BOOT" )
fi
netArg='user'
netArg+=",hostname=$(hostname)"
if [ "$QEMU_NET_USER_EXTRA" ]; then
netArg+=",$QEMU_NET_USER_EXTRA"
fi
for port in "${qemuPorts[@]}"; do
netArg+=",hostfwd=tcp::$port-:$port"
netArg+=",hostfwd=udp::$port-:$port"
done
2014-09-20 07:00:11 +00:00
qemuArgs+=(
-net nic
-net "$netArg"
2014-09-20 07:00:11 +00:00
-vnc ':0'
-serial stdio
"$@"
)
set -x
exec "$qemu" "${qemuArgs[@]}"