From 749b5d1b20372c06fb99c80e956aef7f4154c998 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauri=20V=C3=B5sandi?= Date: Sun, 30 May 2021 08:01:35 +0000 Subject: [PATCH] Initial commit --- .gitignore | 1 + Dockerfile | 5 +++++ README.md | 21 +++++++++++++++++++++ docker-compose.yml | 18 ++++++++++++++++++ entrypoint.sh | 38 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 docker-compose.yml create mode 100755 entrypoint.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1377554 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.swp diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6b89b45 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM alpine +RUN apk add qemu-system-x86_64 qemu-modules bash tcpdump +COPY entrypoint.sh /entrypoint.sh +COPY ifup.sh /ifup.sh +ENTRYPOINT /entrypoint.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..0694229 --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# Dockerized QEMU + +Dockerized QEMU image for reproducibly testing stuff on Windows. +Prepare OS image as described below. +If OS image is mounted to the Docker container as read-only +it is first copied during Docker container launch. +Specify MAC address via environment variable `MAC`. +For more example see `docker-compose.yml` + + +# Windows template image preparation + +Prepare the image using libvirt or similar where you can specify +virtio SCSI controller for storage. +For Windows 10 install also mount +[virtio SCSI and networking drivers ISO](https://github.com/virtio-win/virtio-win-pkg-scripts/blob/master/README.md). +In Windows 10 partitioning wizard provide drivers from that ISO. +Install [OpenSSH server](https://docs.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse). +Make sure OpenSSH starts during boot. +Insert Jenkins or your favourite CI/CD software SSH public key into the image. +Use the resulting virtual machine disk image with this Docker image. diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..5fbbb35 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,18 @@ +version: '3' +networks: + pub: + external: true +services: + app: + networks: + pub: + build: + context: . + privileged: true + restart: unless-stopped + volumes: + - /home/lauri/test.bin:/data/image.bin:ro + tmpfs: + - /tmp + environment: + - MAC=52:54:00:5e:00:05 diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 0000000..5770a25 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,38 @@ +#!/bin/bash +set -x +set -e + +# Source image path +IMAGE=/data/image.bin + +# TODO: With QEMU snapshots instead +if [ ! -w $IMAGE ]; then + echo "Data volume is not writable, assuming discardable VM and copying to tmpfs" + cp $IMAGE /tmp/image.bin + IMAGE=/tmp/image.bin +fi + +# Create macvlan interface on Docker's eth0 +ip link add link eth0 name macvtap0 type macvtap mode bridge +ip link set dev macvtap0 address ${MAC:?} +ip link set macvtap0 up + +# No udev in Docker +IFS=: read major minor < <(cat /sys/devices/virtual/net/macvtap0/tap*/dev) +mknod "/dev/tap$(cat /sys/class/net/macvtap0/ifindex)" c $major $minor + +# Launch QEMU instance +/usr/bin/qemu-system-x86_64 \ + -machine pc-i440fx-4.2,accel=kvm \ + -m 8192 \ + -device virtio-scsi-pci,id=scsi0,bus=pci.0,addr=0x6 \ + -blockdev "{\"driver\":\"file\",\"filename\":\"$IMAGE\",\"node-name\":\"libvirt-1-storage\",\"auto-read-only\":true,\"discard\":\"unmap\"}" \ + -blockdev "{\"node-name\":\"libvirt-1-format\",\"read-only\":false,\"driver\":\"raw\",\"file\":\"libvirt-1-storage\"}" \ + -device scsi-hd,bus=scsi0.0,channel=0,scsi-id=0,lun=0,device_id=drive-scsi0-0-0-0,drive=libvirt-1-format,id=scsi0-0-0-0,bootindex=2 \ + -device virtio-net-pci,netdev=hostnet0,id=net0,mac=$(cat /sys/class/net/macvtap0/address) \ + -device virtio-balloon-pci \ + -usb \ + -device usb-ehci,id=ehci \ + -device usb-tablet,bus=usb-bus.0 \ + -vnc :0 \ + -netdev tap,id=hostnet0,vhost=on,fd=9 9<>/dev/tap$(cat /sys/class/net/macvtap0/ifindex)