Skip to content

Commit dcbddc3

Browse files
authored
Add devcontainer config (#54)
* 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]> * Change the link to GHCR image Signed-off-by: Doru Blânzeanu <[email protected]> * Change device ownership inside the container - This avoids creating a new group in a new container image - Also there is no need for the environment file to be dynamically generated Signed-off-by: Doru Blânzeanu <[email protected]> * Update Dockerfile in github action Signed-off-by: Doru Blânzeanu <[email protected]> * Update GHCR link to use the hyperlight-dev container image Signed-off-by: Doru Blânzeanu <[email protected]> * Add defaults to arguments of container image Dockerfile Signed-off-by: Doru Blânzeanu <[email protected]> * Fix Dockerfile and update workflow to read the rust version Signed-off-by: Doru Blânzeanu <[email protected]> * Add codespace reference to README.md Signed-off-by: Doru Blânzeanu <[email protected]> * Remove unused persmission for gh workflow Signed-off-by: Doru Blânzeanu <[email protected]> * Small changes to comments and bash script for consistency Signed-off-by: Doru Blânzeanu <[email protected]> * Add gnuplot to devcontainer image Signed-off-by: Doru Blânzeanu <[email protected]> --------- Signed-off-by: Doru Blânzeanu <[email protected]>
1 parent 1f66dca commit dcbddc3

File tree

6 files changed

+177
-1
lines changed

6 files changed

+177
-1
lines changed

Diff for: .devcontainer/Dockerfile

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

Diff for: .devcontainer/devcontainer.json

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
"image": "ghcr.io/hyperlight-dev/hyperlight-devcontainer:latest",
7+
8+
"containerUser": "vscode",
9+
// Environment for the container also used by the `postCreateCommand`
10+
"containerEnv": {
11+
"DEVICE": "/dev/kvm",
12+
"KVM_SHOULD_BE_PRESENT": "true",
13+
"REMOTE_USER": "vscode",
14+
"REMOTE_GROUP": "vscode"
15+
},
16+
17+
"runArgs": [
18+
"--device=/dev/kvm"
19+
],
20+
21+
// Use 'postCreateCommand' to run commands after the container is created
22+
"postCreateCommand": "bash .devcontainer/setup.sh",
23+
24+
"customizations": {
25+
"vscode": {
26+
"extensions": [
27+
"ms-vscode.cmake-tools",
28+
"rust-lang.rust-analyzer",
29+
"vadimcn.vscode-lldb"
30+
]
31+
}
32+
}
33+
}

Diff for: .devcontainer/setup.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
# Change device ownership
4+
sudo chown -R $REMOTE_USER:$REMOTE_GROUP $DEVICE
5+

Diff for: .github/workflows/CreateDevcontainerImage.yml

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: Create and publish devcontainer Docker image
2+
3+
on:
4+
push:
5+
branches:
6+
- "main"
7+
paths:
8+
- ".devcontainer/Dockerfile"
9+
- ".github/workflows/CreateDevcontainerImage.yml"
10+
- "rust-toolchain.toml"
11+
12+
# 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.
13+
env:
14+
REGISTRY: ghcr.io
15+
IMAGE_NAME: ${{ github.repository }}-devcontainer
16+
USER: vscode
17+
GROUP: vscode
18+
LLVM_VERSION: 17
19+
RUST_TOOLCHAIN_DEFAULT: 1.81.0
20+
RUST_TOOLCHAIN_FILE: rust-toolchain.toml
21+
22+
# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu.
23+
jobs:
24+
build-and-push-image:
25+
runs-on: ubuntu-latest
26+
# Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job.
27+
permissions:
28+
contents: read
29+
packages: write
30+
31+
steps:
32+
- name: Checkout repository
33+
uses: actions/checkout@v3
34+
35+
- name: Read Rust toolchain version from ${{ env.RUST_TOOLCHAIN_FILE }}
36+
id: toolchain
37+
run: |
38+
version=$(cat ${{ env.RUST_TOOLCHAIN_FILE }} | sed -n '/\[toolchain\]/,/^\[/{/^\s*channel = /s/[^"]*"\([^"]*\)".*/\1/p}')
39+
cat ${{ env.RUST_TOOLCHAIN_FILE }} | grep $version &> /dev/null \
40+
&& echo "RUST_TOOLCHAIN=${version}" >> "$GITHUB_OUTPUT" \
41+
|| echo "RUST_TOOLCHAIN=${{ env.RUST_TOOLCHAIN_FILE }}" >> "$GITHUB_OUTPUT"
42+
43+
# 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.
44+
- name: Log in to the Container registry
45+
uses: docker/login-action@v1
46+
with:
47+
registry: ${{ env.REGISTRY }}
48+
username: ${{ github.actor }}
49+
password: ${{ secrets.GITHUB_TOKEN }}
50+
51+
- name: Extract metadata (tags, labels) for Docker
52+
id: meta
53+
uses: docker/metadata-action@v5
54+
with:
55+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
56+
57+
- name: Build and push Docker image
58+
id: push
59+
uses: docker/build-push-action@v6
60+
with:
61+
context: ./.devcontainer
62+
push: true
63+
tags: |
64+
${{ steps.meta.outputs.tags }}
65+
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
66+
labels: ${{ steps.meta.outputs.labels }}
67+
build-args: |
68+
USER=${{ env.USER }}
69+
GROUP=${{ env.GROUP }}
70+
LLVM_VERSION=${{ env.LLVM_VERSION }}
71+
RUST_TOOLCHAIN=${{ steps.toolchain.outputs.RUST_TOOLCHAIN }}

Diff for: .gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -474,4 +474,4 @@ hyperlight_guest.h
474474
# created by vs code c# extension
475475
.mono
476476

477-
!.gitkeep
477+
!.gitkeep

Diff for: README.md

+4
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ If you get the error `Error: NoHypervisorFound` and KVM or mshv is set up then t
224224

225225
For more details on how to verify that KVM is correctly installed and permissions are correct, follow the guide [here](https://help.ubuntu.com/community/KVM/Installation).
226226

227+
### Or you can use a codespace
228+
229+
[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/hyperlight-dev/hyperlight)
230+
227231
## Contributing to Hyperlight
228232

229233
If you are interested in contributing to Hyperlight, running the entire test-suite is a good way to get started. To do so, on your console, run the following commands:

0 commit comments

Comments
 (0)