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)
|
2015-10-27 23:02:00 +00:00
|
|
|
# QEMU_PORTS="xxx[ xxx ...]" (space separated port numbers)
|
2017-03-21 21:01:24 +00:00
|
|
|
# QEMU_NET_USER_EXTRA="net=192.168.76.0/24,dhcpstart=192.168.76.9" (extra raw args for "-net user,...")
|
2017-03-22 21:01:38 +00:00
|
|
|
# QEMU_NO_SSH=1 (suppress automatic port 22 forwarding)
|
|
|
|
# QEMU_NO_SERIAL=1 (suppress automatic "-serial stdio")
|
2020-05-26 20:29:04 +00:00
|
|
|
# QEMU_NO_VNC=1 (suppress automatic "-vnc ':0'")
|
2014-09-20 07:00:11 +00:00
|
|
|
|
|
|
|
hostArch="$(uname -m)"
|
|
|
|
qemuArch="${QEMU_ARCH:-$hostArch}"
|
|
|
|
qemu="${QEMU_BIN:-qemu-system-$qemuArch}"
|
|
|
|
qemuArgs=()
|
2017-03-22 21:01:38 +00:00
|
|
|
|
|
|
|
qemuPorts=()
|
|
|
|
if [ -z "${QEMU_NO_SSH:-}" ]; then
|
|
|
|
qemuPorts+=( 22 )
|
|
|
|
fi
|
|
|
|
qemuPorts+=( ${QEMU_PORTS:-} )
|
2014-09-20 07:00:11 +00:00
|
|
|
|
2021-03-19 15:57:33 +00:00
|
|
|
if [ -e /dev/kvm ] && sh -c 'echo -n > /dev/kvm' &> /dev/null; then
|
|
|
|
# https://github.com/tianon/docker-qemu/issues/4
|
2014-09-20 07:00:11 +00:00
|
|
|
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}" )
|
|
|
|
|
2017-03-22 20:59:30 +00:00
|
|
|
if [ -n "${QEMU_HDA:-}" ]; then
|
2014-09-20 07:00:11 +00:00
|
|
|
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
|
2018-01-16 21:17:25 +00:00
|
|
|
|
2015-11-04 05:52:13 +00:00
|
|
|
# http://wiki.qemu.org/download/qemu-doc.html#Invocation
|
2018-01-16 21:17:25 +00:00
|
|
|
qemuScsiDevice='virtio-scsi-pci'
|
2018-01-16 03:16:52 +00:00
|
|
|
case "$qemuArch" in
|
2021-03-02 23:44:06 +00:00
|
|
|
arm | riscv64) qemuScsiDevice='virtio-scsi-device' ;;
|
2018-01-16 03:16:52 +00:00
|
|
|
esac
|
|
|
|
|
2018-01-16 21:17:25 +00:00
|
|
|
#qemuArgs+=( -hda "$QEMU_HDA" )
|
|
|
|
#qemuArgs+=( -drive file="$QEMU_HDA",index=0,media=disk,discard=unmap )
|
2017-12-15 07:36:51 +00:00
|
|
|
qemuArgs+=(
|
2018-01-14 21:17:06 +00:00
|
|
|
-drive file="$QEMU_HDA",index=0,media=disk,discard=unmap,detect-zeroes=unmap,if=none,id=hda
|
2018-01-16 21:17:25 +00:00
|
|
|
-device "$qemuScsiDevice"
|
2017-12-15 07:36:51 +00:00
|
|
|
-device scsi-hd,drive=hda
|
|
|
|
)
|
2014-09-20 07:00:11 +00:00
|
|
|
fi
|
|
|
|
|
2017-03-22 20:59:30 +00:00
|
|
|
if [ -n "${QEMU_CDROM:-}" ]; then
|
2014-09-20 07:00:11 +00:00
|
|
|
qemuArgs+=( -cdrom "$QEMU_CDROM" )
|
|
|
|
fi
|
|
|
|
|
2017-03-22 20:59:30 +00:00
|
|
|
if [ -n "${QEMU_BOOT:-}" ]; then
|
2015-07-15 17:19:46 +00:00
|
|
|
qemuArgs+=( -boot "$QEMU_BOOT" )
|
|
|
|
fi
|
|
|
|
|
2015-10-27 23:02:00 +00:00
|
|
|
netArg='user'
|
|
|
|
netArg+=",hostname=$(hostname)"
|
2017-03-22 20:59:30 +00:00
|
|
|
if [ -n "${QEMU_NET_USER_EXTRA:-}" ]; then
|
2017-03-21 21:01:24 +00:00
|
|
|
netArg+=",$QEMU_NET_USER_EXTRA"
|
|
|
|
fi
|
2015-10-27 23:02:00 +00:00
|
|
|
for port in "${qemuPorts[@]}"; do
|
|
|
|
netArg+=",hostfwd=tcp::$port-:$port"
|
|
|
|
netArg+=",hostfwd=udp::$port-:$port"
|
|
|
|
done
|
|
|
|
|
2017-12-21 19:45:46 +00:00
|
|
|
qemuNetDevice='virtio-net-pci'
|
|
|
|
case "$qemuArch" in
|
2021-03-02 23:46:10 +00:00
|
|
|
arm | riscv64) qemuNetDevice='virtio-net-device' ;;
|
2017-12-21 19:45:46 +00:00
|
|
|
esac
|
|
|
|
|
2014-09-20 07:00:11 +00:00
|
|
|
qemuArgs+=(
|
2017-12-15 07:36:51 +00:00
|
|
|
-netdev "$netArg,id=net"
|
2017-12-21 19:45:46 +00:00
|
|
|
-device "$qemuNetDevice,netdev=net"
|
2014-09-20 07:00:11 +00:00
|
|
|
)
|
2020-05-26 20:29:04 +00:00
|
|
|
|
2017-03-22 21:01:38 +00:00
|
|
|
if [ -z "${QEMU_NO_SERIAL:-}" ]; then
|
|
|
|
qemuArgs+=(
|
|
|
|
-serial stdio
|
|
|
|
)
|
|
|
|
fi
|
2020-05-26 20:29:04 +00:00
|
|
|
|
|
|
|
if [ -z "${QEMU_NO_VNC:-}" ]; then
|
|
|
|
qemuArgs+=(
|
|
|
|
-vnc ':0'
|
|
|
|
)
|
|
|
|
fi
|
|
|
|
|
2017-03-22 21:01:38 +00:00
|
|
|
qemuArgs+=( "$@" )
|
2014-09-20 07:00:11 +00:00
|
|
|
|
|
|
|
set -x
|
|
|
|
exec "$qemu" "${qemuArgs[@]}"
|