Skip to content

Commit

Permalink
add dev_random example, remove device field (#81)
Browse files Browse the repository at this point in the history
* add dev_random example

This example illustrates how to use the mount option on partitions.

* remove device field from the partition config

This field was not used at all in the codebase, having it in the config
is misleading. Further on, it seems that there is no point in having it:
device files can be mounted through the mount option already.

Maybe we can eventually build something where for each partition
with at least one device mount we run `udevadm wait --settle <device>
[<device>]...` in a satellite thread, and we re-mount a device when it
pops back. That way we can re-attach a device without restarting the
hypervisor.

Further discussion in #80

* refine CI, add prettier

---------

Co-authored-by: Wanja Zaeske <[email protected]>
  • Loading branch information
wucke13 and Wanja Zaeske authored Jul 19, 2023
1 parent 8ce18f9 commit 53bb05e
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 57 deletions.
39 changes: 35 additions & 4 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,53 @@
name: Run checks
name: Checks

on:
pull_request:
push:
branches: [main]

jobs:
nix-checks:
nix-flake-check:
name: Run nix flake check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v22
with:
github_access_token: ${{ secrets.GITHUB_TOKEN }}
- uses: cachix/cachix-action@v12
with:
name: dlr-ft
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}"
- name: Nix Flake Check
run: nix flake check

impure-checks:
name: Run check ${{ matrix.check }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
check:
- udeps
- treefmt --fail-on-change
- audit --deny warnings
steps:
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v22
with:
github_access_token: ${{ secrets.GITHUB_TOKEN }}
- uses: cachix/cachix-action@v12
with:
name: dlr-ft
authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}"
- uses: actions/cache@v3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-${{ github.job }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Run check ${{ matrix.check }}
run: nix develop --command ${{ matrix.check }}
51 changes: 13 additions & 38 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,54 +16,29 @@ jobs:
- hello_part
- fuel_tank
- ping
- dev_random
env:
DURATION: 10s
RUST_LOG: trace
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v22
with:
github_access_token: ${{ secrets.GITHUB_TOKEN }}
- uses: cachix/cachix-action@v12
with:
name: dlr-ft
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}"
- uses: actions/cache@v3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-${{ github.job }}-${{ matrix.example }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Check CGroup
run: systemd-run --user --scope cat /proc/self/cgroup
- name: Run example ${{ matrix.example }}
run: nix develop --command systemd-run-example-${{ matrix.example }} --duration $DURATION

checks:
name: Run check ${{ matrix.check }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
check:
- udeps
- treefmt --fail-on-change
- audit --deny warnings
steps:
- uses: actions/checkout@v2
- uses: cachix/install-nix-action@v13
with:
nix_path: nixpkgs=channel:nixos-stable
extra_nix_config: |
experimental-features = nix-command flakes
access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
- name: Cache Nix store
uses: actions/cache@v3
id: nix-cache
with:
path: /tmp/nixcache
key: nix-${{ runner.os }}
- name: Import Nix store cache
if: "steps.nix-cache.outputs.cache-hit == 'true'"
run: nix copy --no-check-sigs --from /tmp/nixcache --all
- name: Run check ${{ matrix.check }}
run: nix develop --command ${{ matrix.check }}
- name: Export Nix store cache
if: "steps.nix-cache.outputs.cache-hit != 'true'"
run: |
nix copy --no-check-sigs --to /tmp/nixcache \
.#devShells.x86_64-linux.default
run: nix develop --command systemd-run-example-${{ matrix.example }} --duration $DURATION
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[workspace]
resolver = "2"

members = [
"hypervisor",
Expand All @@ -12,6 +13,8 @@ members = [

"examples/ping_client",
"examples/ping_server",

"examples/dev_random",
]

[workspace.dependencies]
Expand Down
10 changes: 10 additions & 0 deletions examples/dev_random.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
major_frame: 1s
partitions:
- id: 0
name: partition_0
duration: 1s
offset: 0ms
period: 1s
image: target/x86_64-unknown-linux-musl/release/dev_random
mounts:
- [/dev/random, /dev/random]
11 changes: 11 additions & 0 deletions examples/dev_random/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "dev_random"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
a653rs = { workspace = true, features = ["macros"] }
a653rs-linux = { path = "../../partition" }
log.workspace = true
55 changes: 55 additions & 0 deletions examples/dev_random/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use a653rs::partition;
use a653rs::prelude::PartitionExt;
use a653rs_linux::partition::ApexLogger;
use log::LevelFilter;

fn main() {
ApexLogger::install_panic_hook();
ApexLogger::install_logger(LevelFilter::Trace).unwrap();

dev_random::Partition.run()
}

#[partition(a653rs_linux::partition::ApexLinuxPartition)]
mod dev_random {
use log::info;
use std::{fs::*, io::Read};

#[start(cold)]
fn cold_start(mut ctx: start::Context) {
// create and start an aperiodic process
ctx.create_process_0().unwrap().start().unwrap();
}

// do the same as a cold_start
#[start(warm)]
fn warm_start(ctx: start::Context) {
cold_start(ctx);
}

// this aperiodic process opens /dev/random and reads some random bytes from it
#[aperiodic(
time_capacity = "Infinite",
stack_size = "8KB",
base_priority = 1,
deadline = "Soft"
)]
fn process_0(_: process_0::Context) {
info!("started process_0");

// open the device file and read its metadata
let filename = "/dev/random";
let mut f = File::open(&filename).expect("no file found");
let metadata = metadata(&filename).expect("unable to read metadata");
info!("metadata: {metadata:#?}");

// read 16 bytes from the device
let mut buffer = [0u8; 16];
f.read(&mut buffer).expect("buffer overflow");
info!("got some randomness: {buffer:?}");

info!("terminating this partitiong by setting the operating mode to idle");
// TODO wait for https://github.com/DLR-FT/a653rs/issues/22 to be fixed
// Hypervisor::set_partition_mode(OperatingMode::Idle);
}
}
5 changes: 5 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
name = "ping";
partitions = [ "ping_server" "ping_client" ];
}
{
name = "dev_random";
partitions = [ "dev_random" ];
}
];

cargoPackageList = ps: builtins.map (p: "--package=${p}") ps;
Expand Down Expand Up @@ -116,6 +120,7 @@
cargo-audit
cargo-expand
nixpkgs-fmt
nodePackages.prettier
];
git.hooks = {
enable = true;
Expand Down
17 changes: 2 additions & 15 deletions hypervisor/src/hypervisor/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,31 +122,18 @@ pub struct Partition {
/// Path to the executable of the partition
pub image: PathBuf,

/// Devices to be mounted into the partitions namespace
///
/// Use this to pass devices into the partition, i. e. to get access to a
/// physical serial port.
#[serde(default)]
pub devices: Vec<Device>,

// TODO
#[serde(default)]
pub hm_table: PartitionHMTable,

/// Bindmounts from host to partition
///
/// Use this to expose a path from the host environment inside of a
/// partitions mnt namespace
/// Use this to expose a path / file / device file from the host environment
/// to the inside of a partitions.
#[serde(default)]
pub mounts: Vec<(PathBuf, PathBuf)>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Device {
pub path: PathBuf,
pub read_only: bool,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum Channel {
Queuing(QueuingChannelConfig),
Expand Down

0 comments on commit 53bb05e

Please sign in to comment.