Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[base/kvm] Add KVM enabled base container #949

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Next Next commit
[base/kvm] Add KVM enabled base container
nrybowski committed May 23, 2023
commit 88d9b4995276b00eaa12a0d1f6b56d688240cef2
82 changes: 82 additions & 0 deletions base-containers/kvm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Rockylinux does not enable 9P virtfs in the shipped QEMU builds, hence we have to rebuild our own version
# Inspired from https://github.com/acudovs/qemu-kvm-virtfs/blob/master/rpmbuild/build
FROM rockylinux:8 as builder

# Enable additional repos
RUN dnf -y install 'dnf-command(config-manager)' &&\
dnf config-manager --set-enabled powertools

# Install dependencies to build QEMU
RUN yum -y update && yum -y install \
glusterfs-api-devel \
glusterfs-devel \
iasl \
libcacard-devel \
libpmem-devel \
nss-devel \
pkgconfig \
spice-protocol \
spice-server-devel \
usbredir-devel \
yum-utils \
'pkgconfig(epoxy)' \
'pkgconfig(gbm)' \
'pkgconfig(libdrm)'

# Apply patches and build QEMU
WORKDIR /opt
RUN yum-builddep -y qemu-kvm
RUN yumdownloader --source qemu-kvm
nrybowski marked this conversation as resolved.
Show resolved Hide resolved
RUN rpm -Uvh qemu-kvm-*.src.rpm
RUN sed -i -e 's/--disable-virtfs/--enable-virtfs/' \
-e 's/--disable-virtiofsd/--enable-virtiofsd/g' \
/root/rpmbuild/SPECS/qemu-kvm.spec
RUN sed -i -e '/^%files -n qemu-kvm-common/,/^$/s/^$/%{_bindir}\/virtfs-proxy-helper\n%{_mandir}\/man1\/virtfs-proxy-helper.1.gz\n/' \
-e '/^%if %{rhev}$/,/^%else$/s/pkgsuffix -ev/pkgsuffix -virtfs/' \
-e '/%define rhel_rhev_conflicts()/ a Provides: %1-ev = %{epoch}:%{version}-%{release} \\\nObsoletes: %1-ev < %{obsoletes_version} \\' \
-e 's/rm -rf ${RPM_BUILD_ROOT}%{_mandir}\/man1\/virtfs-proxy-helper\*//g' \
-e 's/rm -rf ${RPM_BUILD_ROOT}%{_libexecdir}\/virtfs-proxy-helper/mv ${RPM_BUILD_ROOT}%{_libexecdir}\/virtfs-proxy-helper ${RPM_BUILD_ROOT}%{_bindir}\/virtfs-proxy-helper/g' \
/root/rpmbuild/SPECS/qemu-kvm.spec
COPY virtio-9p-pci.patch /root/rpmbuild/SOURCES/
COPY qemu-kvm.spec.patch /tmp
RUN patch /root/rpmbuild/SPECS/qemu-kvm.spec /tmp/qemu-kvm.spec.patch
RUN rpmbuild -ba --clean /root/rpmbuild/SPECS/qemu-kvm.spec

# Build GNU telnetd server since classical builds do not allow running bash as login util
RUN wget https://ftp.gnu.org/gnu/inetutils/inetutils-2.4.tar.xz &&\
tar xf inetutils-2.4.tar.xz &&\
cd inetutils-2.4 &&\
./configure --disable-servers --disable-clients --enable-telnetd --enable-telnet &&\
make -j$(nproc)

# =====================
# KVM base container
# =====================
ARG VERSION=latest
#FROM ghcr.io/inginious/env-base:${VERSION}
FROM ghcr.io/inginious/env-base

# Install QEMU with 9P virtifs enabled
COPY --from=builder /root/rpmbuild/RPMS/x86_64/qemu-*.rpm /tmp/
RUN yum localinstall -y /tmp/*rpm
RUN rm -rf /tmp/*rpm
RUN ln -s /usr/libexec/qemu-kvm /bin/qemu-kvm

# Install GNU telnet utils
COPY --from=builder /opt/inetutils-2.4/telnet/telnet /usr/sbin
COPY --from=builder /opt/inetutils-2.4/telnet/telnetd /usr/sbin

# Install dependecies
RUN dnf install -y expect xinetd git

# Install virtme
WORKDIR /opt
COPY virtme.patch .
RUN git clone https://github.com/amluto/virtme &&\
git -C virtme apply < virtme.patch &&\
ln -s virtme/virtme-run /usr/sbin/virtme-run

# Get expect script launching the VM
COPY run.expect /

WORKDIR /
Empty file added base-containers/kvm/README.md
Empty file.
11 changes: 11 additions & 0 deletions base-containers/kvm/qemu-kvm.spec.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--- qemu-kvm.spec 2023-05-22 13:26:12.087046202 +0000
+++ /root/rpmbuild/SPECS/qemu-kvm.spec 2023-05-22 12:40:09.838715272 +0000
@@ -652,6 +652,8 @@
Patch256: kvm-dma-helpers-prevent-dma_blk_cb-vs-dma_aio_cancel-rac.patch
# For bz#2090990 - qemu crash with error scsi_req_unref(SCSIRequest *): Assertion `req->refcount > 0' failed or scsi_dma_complete(void *, int): Assertion `r->req.aiocb != NULL' failed [8.7.0]
Patch257: kvm-virtio-scsi-reset-SCSI-devices-from-main-loop-thread.patch
+# Enable 9P virtfs
+Patch258: virtio-9p-pci.patch

BuildRequires: wget
BuildRequires: rpm-build
9 changes: 9 additions & 0 deletions base-containers/kvm/run.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/expect -f

set timeout 600
spawn /virtme/virtme-run --memory 256 --kimg /task/student/scripts/bzImage
expect "virtme-init: console is ttyS0\r"
send -- "ip a add 10.0.2.15/24 dev enp0s2\r"
send -- "ip l set dev enp0s2 up\r"
send -- "stdbuf -oL xinetd -d\r"
wait 600
10 changes: 10 additions & 0 deletions base-containers/kvm/virtio-9p-pci.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
+++ a/configs/devices/x86_64-softmmu/x86_64-rh-devices.mak 2023-05-22 12:17:06.102529121 +0000
--- /dev/null
@@ -90,6 +90,7 @@
CONFIG_VHOST_USER_BLK=y
CONFIG_VIRTIO_PCI=y
CONFIG_VIRTIO_VGA=y
+CONFIG_VIRTIO_9P=y
CONFIG_VMMOUSE=y
CONFIG_VMPORT=y
CONFIG_VTD=y
13 changes: 13 additions & 0 deletions base-containers/kvm/virtme.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git a/virtme/architectures.py b/virtme/architectures.py
index 6ba59e0..604eee7 100644
--- a/virtme/architectures.py
+++ b/virtme/architectures.py
@@ -77,7 +77,7 @@ class Arch_x86(Arch):
ret = Arch.qemuargs(is_native)

# Add a watchdog. This is useful for testing.
- ret.extend(['-watchdog', 'i6300esb'])
+ ret.extend(['-device', 'i6300esb', '-action', 'watchdog=pause'])

if is_native and os.access('/dev/kvm', os.R_OK):
# If we're likely to use KVM, request a full-featured CPU.