Skip to content

Instantly share code, notes, and snippets.

@japaric
Created January 6, 2017 20:41
Show Gist options
  • Save japaric/b71af47968afa4cd86157552aaee51d1 to your computer and use it in GitHub Desktop.
Save japaric/b71af47968afa4cd86157552aaee51d1 to your computer and use it in GitHub Desktop.
set -ex
# QEMU image builder
#
# Meant to be run inside a Docker container (`docker run --privileged (..)`)
# Output artifacts: `~/$arch.qcow2` (rootfs image) and `~/vmlinuz` (kernel)
#
# The idea is that the `testd` project will provide tarballs with the rootfs
# image (containing a `testd` binary) + the kernel so the end user can quickly
# spin up a QEMU VM running `testd`.
main() {
# NOTE only tested with arch=armhf
local arch=armhf
local dependencies=(
binutils
bzip2
curl
qemu-utils
)
apt-get update
apt-get install --no-install-recommends -y ${dependencies[@]}
qemu-img create -f raw ~/$arch.raw 1G
mkfs.ext4 ~/$arch.raw
mkdir /$arch
mount -o loop ~/$arch.raw /$arch
# rootfs
curl http://cdimage.ubuntu.com/ubuntu-base/releases/16.04/release/ubuntu-base-16.04-core-$arch.tar.gz | \
tar -C /$arch -xz
# kernel
local td=$(mktemp -d)
pushd $td
curl -O http://kernel.ubuntu.com/~kernel-ppa/mainline/v4.4.39/linux-image-4.4.39-040439-generic_4.4.39-040439.201612151346_$arch.deb
ar x linux-image-4.4.39-040439-generic_4.4.39-040439.201612151346_$arch.deb data.tar.bz2
tar -C /$arch -xjf data.tar.bz2
cp /$arch/boot/vmlinuz-* ~/vmlinuz
# TODO probably remove this
# allow logins via the console
cd /$arch/etc/systemd/system/getty.target.wants
ln -s /lib/systemd/system/[email protected] [email protected]
# TODO remove this (this is only for testing)
# set root password to "root" (without the quotes)
sed -i -e "s|^root:[^:]\+:|root:"'$1$xyz$YCCZsvYCmSe.qXSOYUWkJ/'":|" /$arch/etc/shadow
# TODO install `testd`
# TODO configure systemd to run `testd` at boot
# TODO install other packages (git for Cargo test suite?)
sync
umount /$arch
qemu-img convert -f raw -O qcow2 ~/$arch.raw ~/$arch.qcow2
# clean up
popd
rm -rf $td
rm ~/$arch.raw
apt-get purge --auto-remove -y ${dependencies[@]}
# rm $0
return
# This is how to use the image
qemu-system-arm \
-M virt \
-append "root=/dev/vda" \
-hda $arch.qcow2 \
-kernel vmlinuz \
-m 1024 \
-monitor telnet:127.0.0.1:1234,server,nowait \
-no-reboot \
-nographic \
-serial stdio
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment