Skip to content

Commit 030752d

Browse files
committed
Add dev container configuration
The following dev container configuration can be found in the .devcontainer directory. It contains: - devcontainer.json a configuration file for the Dev container that puts everything toghether - create_env.sh script that is run on the host machine and creates a .devcontainer/.env file that contains the necessary group info for the container image build phase - Dockerfile.base config for a base container image that is pre-built by using the .github/workflows/CreateDevcontainerImage.yml action that is automatically triggered when a change is detected for Dockerfile.base or the workflow configuration. - Dockerfile config that uses the pre-build image as a base image, creates a group with the same GID of the device being passed to the docker container and adds the user to that group Note: The reason why the environment file generation is needed is because in order to access the device (/dev/kvm) inside the container, the user needs to belong to the correct group, so the group is created at image build time. Signed-off-by: Doru Blânzeanu <[email protected]>
1 parent 94512d8 commit 030752d

File tree

6 files changed

+169
-1
lines changed

6 files changed

+169
-1
lines changed

.devcontainer/Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM ghcr.io/dblnz/hyperlight-devcontainer:latest
2+
3+
COPY .env /tmp/.env
4+
5+
RUN . /tmp/.env && \
6+
sudo groupadd -r -g ${DEVICE_GID} -U ${USER} ${DEVICE_GROUP}
7+

.devcontainer/Dockerfile.base

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## Dockerfile for devcontainer
2+
3+
FROM mcr.microsoft.com/devcontainers/base:debian AS base
4+
5+
ARG USER
6+
ARG GROUP
7+
ARG RUST_TOOLCHAIN
8+
9+
ENV HOME="/home/${USER}"
10+
ENV PATH="$HOME/.cargo/bin:$PATH"
11+
12+
# Install dependencies
13+
RUN apt-get update \
14+
&& apt-get -y install \
15+
build-essential \
16+
cmake \
17+
curl \
18+
git \
19+
gnupg \
20+
lsb-release \
21+
make \
22+
software-properties-common \
23+
sudo \
24+
wget
25+
26+
# Install llvm
27+
RUN wget https://apt.llvm.org/llvm.sh \
28+
&& chmod +x ./llvm.sh \
29+
&& sudo ./llvm.sh 17 all \
30+
&& sudo ln -s /usr/lib/llvm-17/bin/clang-cl /usr/bin/clang-cl \
31+
&& sudo ln -s /usr/lib/llvm-17/bin/llvm-lib /usr/bin/llvm-lib \
32+
&& sudo ln -s /usr/lib/llvm-17/bin/lld-link /usr/bin/lld-link \
33+
&& sudo ln -s /usr/lib/llvm-17/bin/llvm-ml /usr/bin/llvm-ml \
34+
&& sudo ln -s /usr/lib/llvm-17/bin/ld.lld /usr/bin/ld.lld \
35+
&& sudo ln -s /usr/lib/llvm-17/bin/clang /usr/bin/clang
36+
37+
FROM base AS dev
38+
39+
# Make sure the devcontainer user has sudo access
40+
RUN chown -R "${USER}:$GROUP" /home/${USER} \
41+
&& echo "${USER} ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
42+
43+
# Persist bash hystory
44+
RUN SNIPPET="export PROMPT_COMMAND='history -a' && export HISTFILE=/commandhistory/.bash_history" \
45+
&& mkdir /commandhistory \
46+
&& touch /commandhistory/.bash_history \
47+
&& chown -R $USER /commandhistory \
48+
&& echo "$SNIPPET" >> "/home/$USER/.bashrc"
49+
50+
USER $USER
51+
52+
# Install rust
53+
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y \
54+
&& rustup default $RUST_TOOLCHAIN \
55+
&& rustup target add x86_64-unknown-linux-gnu \
56+
&& rustup target add x86_64-unknown-none \
57+
&& rustup target add x86_64-pc-windows-msvc \
58+
&& cargo install just
59+

.devcontainer/create_env.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
ENV_FILE=.devcontainer/.env
4+
5+
DEVICE="/dev/kvm"
6+
DEVICE_GROUP=$(ls -l $DEVICE | awk '{print $4}')
7+
DEVICE_GID=$(getent group $DEVICE_GROUP | cut -d: -f3)
8+
9+
echo "USER=vscode" > $ENV_FILE
10+
echo "GROUP=vscode" >> $ENV_FILE
11+
echo "DEVICE_GID=$DEVICE_GID" >> $ENV_FILE
12+
echo "DEVICE_GROUP=$DEVICE_GROUP" >> $ENV_FILE

.devcontainer/devcontainer.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// For more info on the configuration below, check out the link:
2+
// https://code.visualstudio.com/docs/devcontainers/create-dev-container
3+
{
4+
"name": "Hyperlight",
5+
6+
"build": {
7+
"dockerfile": "Dockerfile"
8+
},
9+
10+
"remoteUser": "vscode",
11+
12+
"runArgs": [
13+
"--env-file", ".devcontainer/.env",
14+
"--device=/dev/kvm"
15+
],
16+
17+
// Use 'initializeCommand' to run commands before container image build
18+
"initializeCommand": "bash .devcontainer/create_env.sh",
19+
20+
"customizations": {
21+
"vscode": {
22+
"extensions": [
23+
"ms-vscode.cmake-tools",
24+
"rust-lang.rust-analyzer"
25+
]
26+
}
27+
}
28+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: Create and publish devcontainer Docker image
2+
3+
on:
4+
push:
5+
paths:
6+
- ".devcontainer/Dockerfile.base"
7+
- ".github/workflows/CreateDevcontainerImage.yml"
8+
9+
# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds.
10+
env:
11+
REGISTRY: ghcr.io
12+
IMAGE_NAME: ${{ github.repository }}-devcontainer
13+
USER: vscode
14+
GROUP: vscode
15+
RUST_TOOLCHAIN: 1.81.0
16+
17+
# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu.
18+
jobs:
19+
build-and-push-image:
20+
runs-on: ubuntu-latest
21+
# Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job.
22+
permissions:
23+
contents: read
24+
packages: write
25+
attestations: write
26+
id-token: write
27+
28+
steps:
29+
- name: Checkout repository
30+
uses: actions/checkout@v3
31+
# Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here.
32+
- name: Log in to the Container registry
33+
uses: docker/login-action@v1
34+
with:
35+
registry: ${{ env.REGISTRY }}
36+
username: ${{ github.actor }}
37+
password: ${{ secrets.GITHUB_TOKEN }}
38+
39+
- name: Extract metadata (tags, labels) for Docker
40+
id: meta
41+
uses: docker/metadata-action@v5
42+
with:
43+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
44+
45+
- name: Build and push Docker image
46+
id: push
47+
uses: docker/build-push-action@v6
48+
with:
49+
context: ./.devcontainer
50+
file: ./.devcontainer/Dockerfile.base
51+
push: true
52+
tags: |
53+
${{ steps.meta.outputs.tags }}
54+
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
55+
labels: ${{ steps.meta.outputs.labels }}
56+
build-args: |
57+
USER=${{ env.USER }}
58+
GROUP=${{ env.GROUP }}
59+
RUST_TOOLCHAIN=${{ env.RUST_TOOLCHAIN }}

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
# Mono auto generated files
1717
mono_crash.*
1818

19+
# Devcontainer generated files
20+
.devcontainer/.env
21+
1922
# Build results
2023
**/[Dd]ebug/*
2124
/[Dd]ebugPublic/
@@ -474,4 +477,4 @@ hyperlight_guest.h
474477
# created by vs code c# extension
475478
.mono
476479

477-
!.gitkeep
480+
!.gitkeep

0 commit comments

Comments
 (0)