Skip to content

Commit

Permalink
feat: add possibility to build hypervisor host & guest filesystems
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmikhalevich committed Feb 13, 2024
1 parent e063228 commit 9fafc37
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ control
machine-emulator-tools-*.tar.gz
machine-emulator-tools-*.deb
rootfs.*
!rootfs.Dockerfile

# Prerequisites
*.d
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
- Add possibility to build hypervisor host and guest filesystems

## [0.14.1] - 2023-12-18
### Fixed
Expand Down
29 changes: 22 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ VERSION := $(MAJOR).$(MINOR).$(PATCH)$(LABEL)
TOOLS_DEB := machine-emulator-tools-v$(VERSION).deb
TOOLS_IMAGE := cartesi/machine-emulator-tools:$(VERSION)
TOOLS_ROOTFS := rootfs-tools-v$(VERSION).ext2
TOOLS_HV_HOST_ROOTFS := rootfs-hv-host-tools-v$(VERSION).ext2
TOOLS_HV_GUEST_ROOTFS := rootfs-hv-guest-tools-v$(VERSION).ext2

IMAGE_KERNEL_VERSION ?= v0.19.1
LINUX_VERSION ?= 6.5.9-ctsi-1
Expand Down Expand Up @@ -51,16 +53,29 @@ $(TOOLS_DEB) deb: build
control: Makefile control.template
@sed 's|ARG_VERSION|$(VERSION)|g' control.template > control

define get_dockerfile_name
$(firstword $(subst -tools-v$(VERSION), ,$(1))).Dockerfile
endef

$(TOOLS_ROOTFS) fs: $(TOOLS_DEB)
%.tar: $(TOOLS_DEB)
@docker buildx build --platform=linux/riscv64 \
--progress=plain \
--no-cache \
--build-arg TOOLS_DEB=$(TOOLS_DEB) \
--output type=tar,dest=rootfs.tar \
--file fs/Dockerfile \
. && \
bsdtar -cf rootfs.gnutar --format=gnutar @rootfs.tar && \
xgenext2fs -fzB 4096 -b 25600 -i 4096 -a rootfs.gnutar -L rootfs $(TOOLS_ROOTFS) && \
rm -f rootfs.gnutar rootfs.tar
--output type=tar,dest=$@ \
--file fs/$(call get_dockerfile_name,$@) .

%.gnutar: %.tar
bsdtar -cf $@ --format=gnutar @$<

%.ext2: %.gnutar
xgenext2fs -fzB 4096 -b 25600 -i 4096 -a $< -L $* $@

fs: $(TOOLS_ROOTFS)

hv-guest-fs: $(TOOLS_HV_GUEST_ROOTFS)
hv-host-fs: $(TOOLS_HV_HOST_ROOTFS)
hv-fs: hv-guest-fs hv-host-fs

env:
@echo TOOLS_DEB=$(TOOLS_DEB)
Expand Down
35 changes: 35 additions & 0 deletions fs/rootfs-hv-guest.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
FROM --platform=linux/riscv64 riscv64/ubuntu:22.04
ARG TOOLS_DEB=machine-emulator-tools-v0.14.0.deb

RUN <<EOF
# Update system
apt-get update && apt-get upgrade -y

# Install busybox
apt-get install -y busybox-static

# Install debug tools
apt-get install -y gdb strace device-tree-compiler

# Install testing tools
apt-get install -y stress-ng

# Install socat (for testing VSOCKETS)
apt-get install -y socat

# Install python3
apt-get install -y python3

# Make build more or less reproducible
rm -rf /var/lib/apt/lists/* /var/log/*
EOF

# Install emulator tools
ADD ${TOOLS_DEB} /tmp/
RUN <<EOF
dpkg -i /tmp/${TOOLS_DEB}
rm -f /tmp/${TOOLS_DEB}
EOF

# Replace machine name
RUN echo guest-machine > /etc/hostname
86 changes: 86 additions & 0 deletions fs/rootfs-hv-host.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
FROM --platform=$BUILDPLATFORM ubuntu:22.04 as cross-builder
ENV BUILD_BASE=/tmp/build-extra
ENV LIBFDT_DIR=${BUILD_BASE}/dtc
ENV LKVM_DIR=${BUILD_BASE}/kvmtool
ENV CROSS_COMPILE=/usr/bin/riscv64-linux-gnu-

# Install dependencies
RUN DEBIAN_FRONTEND=noninteractive apt-get update && \
apt-get install -y --no-install-recommends \
build-essential \
ca-certificates \
wget \
crossbuild-essential-riscv64 \
gcc-12-riscv64-linux-gnu \
git \
pkg-config \
flex \
bison \
&& \
adduser developer -u 499 --gecos ",,," --disabled-password && \
mkdir -p ${BUILD_BASE} && chown -R developer:developer ${BUILD_BASE} && \
rm -rf /var/lib/apt/lists/*

USER developer
WORKDIR ${BUILD_BASE}

# Build benchmark binaries
COPY fs/dhrystone.patch ${BUILD_BASE}/
COPY fs/shasumfile ${BUILD_BASE}/
RUN mkdir benchmarks && cd benchmarks && \
wget https://www.netlib.org/benchmark/whetstone.c https://www.netlib.org/benchmark/dhry-c && \
shasum -ca 256 ../shasumfile &&\
bash dhry-c && \
patch -p1 < ../dhrystone.patch && \
riscv64-linux-gnu-gcc-12 -O2 -o whetstone whetstone.c -lm && \
riscv64-linux-gnu-gcc-12 -O2 -o dhrystone dhry_1.c dhry_2.c -lm

# Build libfdt
RUN <<EOF
cd ${BUILD_BASE}
git clone git://git.kernel.org/pub/scm/utils/dtc/dtc.git
cd dtc
CC="riscv64-linux-gnu-gcc-12 -mabi=lp64d -march=rv64gc" make libfdt
make NO_PYTHON=1 NO_YAML=1 INCLUDEDIR=${LIBFDT_DIR} LIBDIR=${LIBFDT_DIR} install-lib install-includes
cd ..
EOF

# Build kvmtool
RUN <<EOF
git clone https://github.com/edubart/kvmtool.git
cd kvmtool
ls ${LIBFDT_LIBDIR}
WERROR=0 ARCH=riscv LIBFDT_DIR=${LIBFDT_DIR} CROSS_COMPILE=${CROSS_COMPILE} make lkvm-static -j4
install lkvm-static ${LKVM_DIR}/lkvm
EOF

# Final image
FROM --platform=linux/riscv64 riscv64/ubuntu:22.04
ARG TOOLS_DEB=machine-emulator-tools-v0.14.0.deb
ARG KERNEL_VERSION=6.5.9-ctsi-1
ARG KERNEL_RELEASE=v0.19.1
# Download guest kernel
ADD https://github.com/cartesi/image-kernel/releases/download/${KERNEL_RELEASE}/linux-${KERNEL_VERSION}-${KERNEL_RELEASE}-no-opensbi.bin /tmp/linux.bin
ADD ${TOOLS_DEB} /tmp/
RUN apt-get update && \
apt-get install -y --no-install-recommends \
busybox-static=1:1.30.1-7ubuntu3 \
coreutils=8.32-4.1ubuntu1 \
bash=5.1-6ubuntu1 \
psmisc=23.4-2build3 \
bc=1.07.1-3build1 \
curl=7.81.0-1ubuntu1.15 \
device-tree-compiler=1.6.1-1 \
jq=1.6-2.1ubuntu3 \
lua5.4=5.4.4-1 \
lua-socket=3.0~rc1+git+ac3201d-6 \
xxd=2:8.2.3995-1ubuntu2.15 \
file=1:5.41-3ubuntu0.1 \
/tmp/${TOOLS_DEB} \
&& \
useradd --create-home --user-group dapp && \
rm -rf /var/lib/apt/lists/* /tmp/${TOOLS_DEB}
RUN mkdir hv && mv /tmp/linux.bin /hv/
COPY --chown=root:root --from=cross-builder /tmp/build-extra/benchmarks/whetstone /usr/bin/
COPY --chown=root:root --from=cross-builder /tmp/build-extra/benchmarks/dhrystone /usr/bin/
COPY --chown=root:root --from=cross-builder /tmp/build-extra/kvmtool/lkvm /usr/bin/
File renamed without changes.

0 comments on commit 9fafc37

Please sign in to comment.