Skip to content

Instantly share code, notes, and snippets.

@codeon-nat
Last active February 24, 2019 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeon-nat/43f43fa3b084f2553fe593a4e9bd8001 to your computer and use it in GitHub Desktop.
Save codeon-nat/43f43fa3b084f2553fe593a4e9bd8001 to your computer and use it in GitHub Desktop.
QEMU ARM Virtual Machine with xenial Ubuntu
#! /bin/sh
#
# Copyright (c) 2019 Nat! - Mulle kybernetiK
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# Neither the name of Mulle kybernetiK nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
#
# replace this with a random MAC address of your choice (or leave as is)
MAC="00:50:56:02:3f:bf"
#
# If you use internal bridging uncomment this and make this your bridge
# device
# BRIDGE=virbr0
#
# Xenial is the last ubuntu I found with a arm64-uefi1.img provided
#
RELEASE=xenial
URL="https://cloud-images.ubuntu.com/${RELEASE}/current/${RELEASE}-server-cloudimg-arm64-uefi1.img"
sudo apt-get install qemu-system-arm qemu-efi cloud-image-utils
if [ ! -f flash0.img ]
then
dd if=/dev/zero of=flash0.img bs=1M count=64 || exit 1
fi
if [ ! -f QEMU_EFI.fd ]
then
dd if=/usr/share/qemu-efi/QEMU_EFI.fd of=flash0.img conv=notrunc || exit 1
fi
if [ ! -f flash1.img ]
then
dd if=/dev/zero of=flash1.img bs=1M count=64
fi
file="`basename -- "${URL}"`"
if [ ! -f "${file}" ]
then
curl -O "${URL}" || exit 1
qemu-img resize "${file}" +20G
fi
if [ ! -f user-data.img ]
then
cat > user-data <<EOF
#cloud-config
password: ${PASSWORD:-nextnext}
chpasswd: { expire: False }
ssh_pwauth: True
EOF
cloud-localds user-data.img user-data || exit 1
rm user-data
fi
if [ ! -z "${BRIDGE}" ]
then
#
# qemu needs this funny bridge allowance to connect ?
#
if [ ! -f /etc/qemu/bridge.conf ]
then
sudo mkdir -p /etc/qemu
echo "allow ${BRIDGE}" | sudo tee "/etc/qemu/bridge.conf" > /dev/null
fi
NETDEV="bridge,br=${BRIDGE}"
else
NETDEV="type=tap"
fi
# https://wiki.ubuntu.com/ARM64/QEMU
# https://askubuntu.com/questions/281763/is-there-any-prebuilt-qemu-ubuntu-image32bit-online/1081171#1081171
sudo qemu-system-aarch64 \
-smp "${NCPU:-8}" \
-m "${RAM:-8192}" \
-cpu cortex-a57 \
-M virt \
-nographic \
-drive file="flash0.img",format=raw,if=pflash \
-drive file="flash1.img",format=raw,if=pflash \
-drive file="${file}",id=hd0,if=none \
-drive file="user-data.img",format=raw \
-device virtio-blk-device,drive=hd0 \
-device virtio-net-device,netdev=net0,mac="${MAC}" \
-netdev "${NETDEV}",id=net0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment