krata: rewrite all repo infrastructure

This commit is contained in:
Alex Zenla
2024-03-07 09:04:05 +00:00
parent baefe0dceb
commit 670e140682
39 changed files with 419 additions and 181 deletions

View File

@ -8,7 +8,7 @@ jobs:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable - uses: dtolnay/rust-toolchain@stable
- run: ./scripts/ci/install-deps.sh - run: ./scripts/ci/install-deps.sh
- run: ./scripts/cargo.sh build - run: ./scripts/build/cargo.sh build
test: test:
name: cargo test name: cargo test
runs-on: ubuntu-latest runs-on: ubuntu-latest
@ -16,7 +16,7 @@ jobs:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable - uses: dtolnay/rust-toolchain@stable
- run: ./scripts/ci/install-deps.sh - run: ./scripts/ci/install-deps.sh
- run: ./scripts/cargo.sh test - run: ./scripts/build/cargo.sh test
clippy: clippy:
name: cargo clippy name: cargo clippy
runs-on: ubuntu-latest runs-on: ubuntu-latest
@ -26,7 +26,7 @@ jobs:
with: with:
components: clippy components: clippy
- run: ./scripts/ci/install-deps.sh - run: ./scripts/ci/install-deps.sh
- run: ./scripts/cargo.sh clippy - run: ./scripts/build/cargo.sh clippy
fmt: fmt:
name: cargo fmt name: cargo fmt
runs-on: ubuntu-latest runs-on: ubuntu-latest
@ -36,7 +36,7 @@ jobs:
with: with:
components: rustfmt components: rustfmt
- run: ./scripts/ci/install-deps.sh - run: ./scripts/ci/install-deps.sh
- run: ./scripts/cargo.sh fmt --all -- --check - run: ./scripts/build/cargo.sh fmt --all -- --check
initrd: initrd:
name: initrd name: initrd
runs-on: ubuntu-latest runs-on: ubuntu-latest
@ -46,4 +46,4 @@ jobs:
with: with:
targets: "x86_64-unknown-linux-gnu,x86_64-unknown-linux-musl" targets: "x86_64-unknown-linux-gnu,x86_64-unknown-linux-musl"
- run: ./scripts/ci/install-deps.sh - run: ./scripts/ci/install-deps.sh
- run: ./initrd/build.sh - run: ./scripts/initrd/build.sh

View File

@ -13,4 +13,4 @@ jobs:
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- run: ./scripts/ci/install-deps.sh - run: ./scripts/ci/install-deps.sh
- run: ./kernel/build.sh - run: ./scripts/kernel/build.sh -j5

View File

@ -2,7 +2,7 @@ name: nightly
on: on:
workflow_dispatch: workflow_dispatch:
schedule: schedule:
- cron: "0 6 * * *" - cron: "0 10 * * *"
jobs: jobs:
build: build:
name: build name: build
@ -13,9 +13,20 @@ jobs:
with: with:
targets: "x86_64-unknown-linux-gnu,x86_64-unknown-linux-musl" targets: "x86_64-unknown-linux-gnu,x86_64-unknown-linux-musl"
- run: ./scripts/ci/install-deps.sh - run: ./scripts/ci/install-deps.sh
- run: ./scripts/bundle.sh - run: ./scripts/dist/bundle.sh
env:
KRATA_KERNEL_BUILD_JOBS: "5"
- run: ./scripts/dist/deb.sh
env:
KRATA_BUNDLE_SKIP_KERNEL_BUILD: "1"
- uses: actions/upload-artifact@v4 - uses: actions/upload-artifact@v4
with: with:
name: krata-nightly-x86_64 name: krata-nightly-bundle-x86_64
path: target/bundle/krata.tgz path: "target/dist/bundle.tgz"
compression-level: 0 compression-level: 0
- uses: actions/upload-artifact@v4
with:
name: krata-nightly-debian-x86_64
path: "target/dist/*_amd64.deb"
compression-level: 0

View File

@ -1,16 +1,16 @@
# krata # krata
An early version of the Edera hypervisor. Not for production use. An early version of the Edera hypervisor.
[Join our community Discord](https://discord.gg/UGZCtX9NG9), or follow the founders [Alex](https://social.treehouse.systems/@alex) and [Ariadne](https://social.treehouse.systems/@ariadne) on Mastodon to follow the future of krata. [Join our community Discord](https://discord.gg/Sy7KrSd2qf), or follow the founders [Alex](https://social.treehouse.systems/@alex) and [Ariadne](https://social.treehouse.systems/@ariadne) on Mastodon to follow the future of krata.
## What is krata? ## What is krata?
The krata prototype makes it possible to launch OCI containers on a Xen hypervisor without utilizing the Xen userspace tooling. krata contains just enough of the userspace of Xen (reimplemented in Rust) to start an x86_64 Xen Linux PV guest, and implements a Linux init process that can boot an OCI container. It does so by converting an OCI image into a squashfs file and packaging basic startup data in a bundle which the init container can read. The krata hypervisor makes it possible to launch OCI containers on a Xen hypervisor without utilizing the Xen userspace tooling. krata contains just enough of the userspace of Xen (reimplemented in Rust) to start an x86_64 Xen Linux PV guest, and implements a Linux init process that can boot an OCI container. It does so by converting an OCI image into a squashfs file and packaging basic startup data in a bundle which the init container can read.
In addition, due to the desire to reduce dependence on the dom0 network, krata contains a networking daemon called kratanet. kratanet listens for krata guests to startup and launches a userspace networking environment. krata guests can access the dom0 networking stack via the proxynat layer that makes it possible to communicate over UDP, TCP, and ICMP (echo only) to the outside world. In addition, each krata guest is provided a "gateway" IP (both in IPv4 and IPv6) which utilizes smoltcp to provide a virtual host. That virtual host in the future could dial connections into the container to access container networking resources. In addition, due to the desire to reduce dependence on the dom0 network, krata contains a networking daemon called kratanet. kratanet listens for krata guests to startup and launches a userspace networking environment. krata guests can access the dom0 networking stack via the proxynat layer that makes it possible to communicate over UDP, TCP, and ICMP (echo only) to the outside world. In addition, each krata guest is provided a "gateway" IP (both in IPv4 and IPv6) which utilizes smoltcp to provide a virtual host. That virtual host in the future could dial connections into the container to access container networking resources.
krata is in it's early days and this project is provided as essentially a demo of what an OCI layer on Xen could look like. krata is in its early days and this project is still a work in progress.
## FAQs ## FAQs
@ -30,7 +30,7 @@ As such, no external contributions are accepted at this time.
### Are external contributions accepted? ### Are external contributions accepted?
Currently no external contributions are accepted. krata is in it's early days and the project is provided under AGPL. Edera may decide to change licensing as we start to build future plans, and so all code here is provided to show what is possible, not to work towards any future product goals. Currently no external contributions are accepted. krata is in its early days and the project is provided under AGPL. Edera may decide to change licensing as we start to build future plans, and so all code here is provided to show what is possible, not to work towards any future product goals.
### What are the future plans? ### What are the future plans?
@ -44,9 +44,9 @@ krata is composed of three major executables:
| Executable | Runs On | User Interaction | Dev Runner | Code Path | | Executable | Runs On | User Interaction | Dev Runner | Code Path |
| ---------- | ------- | ---------------- | --------------------------- | ----------- | | ---------- | ------- | ---------------- | --------------------------- | ----------- |
| kratad | host | backend daemon | ./scripts/kratad-debug.sh | daemon | | kratad | host | backend daemon | ./scripts/debug/kratad.sh | daemon |
| kratanet | host | backend daemon | ./scripts/kratanet-debug.sh | network | | kratanet | host | backend daemon | ./scripts/debug/kratanet.sh | network |
| kratactl | host | CLI tool | ./scripts/kratactl-debug.sh | controller | | kratactl | host | CLI tool | ./scripts/debug/kratactl.sh | controller |
| krataguest | guest | none, guest init | N/A | guest | | krataguest | guest | none, guest init | N/A | guest |
You will find the code to each executable available in the bin/ and src/ directories inside You will find the code to each executable available in the bin/ and src/ directories inside
@ -92,26 +92,26 @@ $ cd krata
6. Build a guest kernel image: 6. Build a guest kernel image:
```sh ```sh
$ ./kernel/build.sh -j4 $ ./scripts/kernel/build.sh -j4
``` ```
7. Copy the guest kernel image at `kernel/target/kernel` to `/var/lib/krata/default/kernel` to have it automatically detected by kratactl. 7. Copy the guest kernel image at `target/kernel/kernel` to `/var/lib/krata/guest/kernel` to have it automatically detected by kratad.
8. Launch `./scripts/kratanet-debug.sh` and keep it running in the foreground. 8. Launch `./scripts/debug/kratanet.sh` and keep it running in the foreground.
9. Launch `./scripts/kratad-debug.sh` and keep it running in the foreground. 9. Launch `./scripts/debug/kratad.sh` and keep it running in the foreground.
10. Run kratactl to launch a guest: 10. Run kratactl to launch a guest:
```sh ```sh
$ ./scripts/kratactl-debug.sh launch --attach alpine:latest $ ./scripts/debug/kratactl.sh launch --attach alpine:latest
``` ```
To detach from the guest console, use `Ctrl + ]` on your keyboard. To detach from the guest console, use `Ctrl + ]` on your keyboard.
To list the running guests, run: To list the running guests, run:
```sh ```sh
$ ./scripts/kratactl-debug.sh list $ ./scripts/debug/kratactl.sh list
``` ```
To destroy a running guest, copy it's UUID from either the launch command or the guest list and run: To destroy a running guest, copy it's UUID from either the launch command or the guest list and run:
```sh ```sh
$ ./scripts/kratactl-debug.sh destroy GUEST_UUID $ ./scripts/debug/kratactl.sh destroy GUEST_UUID
``` ```

View File

@ -1,4 +1,9 @@
use std::{fs, path::PathBuf, str::FromStr, sync::Arc}; use std::{
fs,
path::{Path, PathBuf},
str::FromStr,
sync::Arc,
};
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use ipnetwork::IpNetwork; use ipnetwork::IpNetwork;
@ -59,8 +64,9 @@ impl RuntimeContext {
image_cache_path.push("image"); image_cache_path.push("image");
fs::create_dir_all(&image_cache_path)?; fs::create_dir_all(&image_cache_path)?;
let image_cache = ImageCache::new(&image_cache_path)?; let image_cache = ImageCache::new(&image_cache_path)?;
let kernel = format!("{}/default/kernel", store); let kernel = RuntimeContext::detect_guest_file(&store, "kernel")?;
let initrd = format!("{}/default/initrd", store); let initrd = RuntimeContext::detect_guest_file(&store, "initrd")?;
Ok(RuntimeContext { Ok(RuntimeContext {
image_cache, image_cache,
autoloop: AutoLoop::new(LoopControl::open()?), autoloop: AutoLoop::new(LoopControl::open()?),
@ -70,6 +76,15 @@ impl RuntimeContext {
}) })
} }
fn detect_guest_file(store: &str, name: &str) -> Result<String> {
let path = PathBuf::from(format!("{}/{}", store, name));
if path.is_file() {
return path_as_string(&path);
}
Ok(format!("/usr/share/krata/guest/{}", name))
}
pub async fn list(&mut self) -> Result<Vec<GuestInfo>> { pub async fn list(&mut self) -> Result<Vec<GuestInfo>> {
let mut guests: Vec<GuestInfo> = Vec::new(); let mut guests: Vec<GuestInfo> = Vec::new();
for domid_candidate in self.xen.store.list("/local/domain").await? { for domid_candidate in self.xen.store.list("/local/domain").await? {
@ -270,3 +285,9 @@ impl Runtime {
Runtime::new((*self.store).clone()).await Runtime::new((*self.store).clone()).await
} }
} }
fn path_as_string(path: &Path) -> Result<String> {
path.to_str()
.ok_or_else(|| anyhow!("unable to convert path to string"))
.map(|x| x.to_string())
}

3
kernel/.gitignore vendored
View File

@ -1,2 +1 @@
linux-* /linux-*
target/

View File

@ -1,22 +0,0 @@
#!/bin/sh
set -e
cd "$(dirname "${0}")"
# shellcheck source=config.sh
. "${PWD}/config.sh"
if [ ! -f "${SRC_DIR_NAME}/Makefile" ]
then
rm -rf "${SRC_DIR_NAME}"
curl -L -o "${SRC_DIR_NAME}.txz" "${KERNEL_SRC_URL}"
tar xf "${SRC_DIR_NAME}.txz"
rm "${SRC_DIR_NAME}.txz"
fi
mkdir -p "${OUTPUT_DIR_NAME}"
cp krata.config "${SRC_DIR_NAME}/.config"
make -C "${SRC_DIR_NAME}" "${@}" olddefconfig
make -C "${SRC_DIR_NAME}" "${@}" bzImage
cp "${SRC_DIR_NAME}/arch/x86/boot/bzImage" "${OUTPUT_DIR_NAME}/kernel"

View File

@ -1,5 +1,3 @@
#!/bin/sh #!/bin/sh
KERNEL_VERSION="6.7.8" KERNEL_VERSION="6.7.8"
KERNEL_SRC_URL="https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-${KERNEL_VERSION}.tar.xz" KERNEL_SRC_URL="https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-${KERNEL_VERSION}.tar.xz"
SRC_DIR_NAME="linux-${KERNEL_VERSION}"
OUTPUT_DIR_NAME="target"

View File

@ -2,13 +2,13 @@
parallel = true parallel = true
[pre-commit.commands.build] [pre-commit.commands.build]
run = "./scripts/cargo.sh build" run = "./scripts/build/cargo.sh build"
[pre-commit.commands.test] [pre-commit.commands.test]
run = "./scripts/cargo.sh test" run = "./scripts/build/cargo.sh test"
[pre-commit.commands.fmt] [pre-commit.commands.fmt]
run = "./scripts/cargo.sh fmt --all -- --check" run = "./scripts/build/cargo.sh fmt --all -- --check"
[pre-commit.commands.clippy] [pre-commit.commands.clippy]
run = "./scripts/cargo.sh clippy" run = "./scripts/build/cargo.sh clippy"

39
resources/bundle/install.sh Executable file
View File

@ -0,0 +1,39 @@
#!/bin/sh
set -e
remove_service_if_exists() {
if systemctl show -P FragmentPath "${1}" > /dev/null
then
UNIT_PATH="$(systemctl show -P FragmentPath "${1}")"
if [ -f "${UNIT_PATH}" ]
then
echo "[WARN] disabling removing systemd unit ${UNIT_PATH}" > /dev/stderr
systemctl disable --now "${1}" || true
rm "${UNIT_PATH}"
fi
fi
}
REAL_SCRIPT="$(realpath "${0}")"
cd "$(dirname "${REAL_SCRIPT}")"
remove_service_if_exists kratad.service
remove_service_if_exists kratanet.service
cp kratad.service /usr/lib/systemd/system/kratad.service
cp kratanet.service /usr/lib/systemd/system/kratanet.service
cp kratad kratanet /usr/libexec
cp kratactl /usr/bin
chmod +x /usr/libexec/kratad
chmod +x /usr/libexec/kratanet
chmod +x /usr/bin/kratactl
mkdir -p /var/lib/krata /usr/share/krata/guest
cp kernel /usr/share/krata/guest/kernel
cp initrd /usr/share/krata/guest/initrd
systemctl daemon-reload
systemctl enable kratad.service kratanet.service
systemctl restart kratad.service kratanet.service

12
resources/bundle/uninstall.sh Executable file
View File

@ -0,0 +1,12 @@
#!/bin/sh
set -e
systemctl disable --now kratad.service || true
systemctl disable --now kratanet.service || true
rm -f /usr/lib/systemd/system/kratad.service
rm -f /usr/lib/systemd/system/kratanet.service
rm -f /usr/bin/kratactl
rm -f /usr/libexec/kratad /usr/libexec/kratanet
rm -rf /usr/share/krata

View File

@ -1,32 +0,0 @@
#!/bin/sh
set -e
REAL_SCRIPT="$(realpath "${0}")"
cd "$(dirname "${REAL_SCRIPT}")"
if [ -f "/etc/systemd/system/kratad.service" ]
then
systemctl disable --now kratad.service
fi
if [ -f "/etc/systemd/system/kratanet.service" ]
then
systemctl disable --now kratanet.service
fi
cp kratad.service /etc/systemd/system/kratad.service
cp kratanet.service /etc/systemd/system/kratanet.service
cp kratad kratanet kratactl /usr/local/bin
chmod +x /usr/local/bin/kratad
chmod +x /usr/local/bin/kratanet
chmod +x /usr/local/bin/kratactl
mkdir -p /var/lib/krata/default
cp kernel /var/lib/krata/default/kernel
cp initrd /var/lib/krata/default/initrd
systemctl daemon-reload
systemctl enable kratad.service kratanet.service
systemctl restart kratad.service kratanet.service

View File

@ -1,11 +1,11 @@
[Unit] [Unit]
Description=Krata Controller Daemon Description=Krata Control Daemon
[Service] [Service]
Restart=on-failure Restart=on-failure
Type=simple Type=simple
WorkingDirectory=/var/lib/krata ExecStartPre=/bin/mkdir -p /var/lib/krata
ExecStart=/usr/local/bin/kratad -l unix:///var/lib/krata/daemon.socket ExecStart=/usr/libexec/kratad -l unix:///var/lib/krata/daemon.socket
Environment=RUST_LOG=info Environment=RUST_LOG=info
User=root User=root

View File

@ -4,8 +4,7 @@ Description=Krata Networking Daemon
[Service] [Service]
Restart=on-failure Restart=on-failure
Type=simple Type=simple
WorkingDirectory=/var/lib/krata ExecStart=/usr/libexec/kratanet
ExecStart=/usr/local/bin/kratanet
Environment=RUST_LOG=info Environment=RUST_LOG=info
User=root User=root

22
scripts/build/arch.sh Executable file
View File

@ -0,0 +1,22 @@
#!/bin/sh
set -e
TOOLS_DIR="$(dirname "${0}")"
RUST_TARGET="$("${TOOLS_DIR}/target.sh")"
TARGET_ARCH="$(echo "${RUST_TARGET}" | awk -F '-' '{print $1}')"
if [ "${KRATA_ARCH_ALT_NAME}" = "1" ] || [ "${KRATA_ARCH_KERNEL_NAME}" = "1" ]
then
if [ "${TARGET_ARCH}" = "x86_64" ] && [ "${KRATA_ARCH_KERNEL_NAME}" != "1" ]
then
TARGET_ARCH="amd64"
fi
if [ "${TARGET_ARCH}" = "aarch64" ]
then
TARGET_ARCH="arm64"
fi
fi
echo "${TARGET_ARCH}"

8
scripts/build/cargo.sh Executable file
View File

@ -0,0 +1,8 @@
#!/bin/sh
set -e
TOOLS_DIR="$(dirname "${0}")"
RUST_TARGET="$("${TOOLS_DIR}/target.sh")"
export CARGO_BUILD_TARGET="${RUST_TARGET}"
exec cargo "${@}"

View File

@ -1,41 +0,0 @@
#!/bin/sh
set -e
REAL_SCRIPT="$(realpath "${0}")"
cd "$(dirname "${REAL_SCRIPT}")/.."
KRATA_DIR="${PWD}"
OUTPUT_DIR="${KRATA_DIR}/target/bundle"
mkdir -p "${OUTPUT_DIR}"
BUNDLE_TAR="${OUTPUT_DIR}/krata.tgz"
rm -f "${BUNDLE_TAR}"
BUNDLE_DIR="$(mktemp -d /tmp/krata-bundle.XXXXXXXXXXXXX)"
BUNDLE_DIR="${BUNDLE_DIR}/krata"
mkdir -p "${BUNDLE_DIR}"
for X in kratad kratanet kratactl
do
./scripts/cargo.sh build --release --bin "${X}"
RUST_TARGET="$(./scripts/detect-rust-target.sh)"
cp "${KRATA_DIR}/target/${RUST_TARGET}/release/${X}" "${BUNDLE_DIR}/${X}"
done
./initrd/build.sh
if [ "${KRATA_BUNDLE_SKIP_KERNEL_BUILD}" != "1" ]
then
./kernel/build.sh
fi
cd "${BUNDLE_DIR}"
cp "${KRATA_DIR}/initrd/target/initrd" initrd
cp "${KRATA_DIR}/kernel/target/kernel" kernel
cp "${KRATA_DIR}/resources/systemd/kratad.service" kratad.service
cp "${KRATA_DIR}/resources/systemd/kratanet.service" kratanet.service
cp "${KRATA_DIR}/resources/install/install.sh" install.sh
for X in install.sh kratactl kratad kratanet
do
chmod +x "${X}"
done
cd ..
tar czf "${BUNDLE_TAR}" .
cd "${KRATA_DIR}"
rm -rf "$(dirname "${BUNDLE_DIR}")"

View File

@ -1,8 +0,0 @@
#!/bin/sh
set -e
SCRIPTS_DIR="$(dirname "${0}")"
RUST_TARGET="$("${SCRIPTS_DIR}/detect-rust-target.sh")"
export CARGO_BUILD_TARGET="${RUST_TARGET}"
exec cargo "${@}"

View File

@ -5,3 +5,4 @@ sudo apt-get update
sudo apt-get install -y \ sudo apt-get install -y \
build-essential libssl-dev libelf-dev musl-dev \ build-essential libssl-dev libelf-dev musl-dev \
flex bison bc protobuf-compiler musl-tools flex bison bc protobuf-compiler musl-tools
sudo gem install --no-document fpm

6
scripts/code/fix.sh Executable file
View File

@ -0,0 +1,6 @@
#!/bin/sh
set -e
cd "$(dirname "${0}")/../.."
./scripts/build/cargo.sh clippy --fix --allow-dirty --allow-staged
cargo fmt --all

View File

@ -2,7 +2,7 @@
set -e set -e
REAL_SCRIPT="$(realpath "${0}")" REAL_SCRIPT="$(realpath "${0}")"
cd "$(dirname "${REAL_SCRIPT}")/.." cd "$(dirname "${REAL_SCRIPT}")/../.."
if [ -z "${RUST_LOG}" ] if [ -z "${RUST_LOG}" ]
then then
@ -19,12 +19,13 @@ fi
build_and_run() { build_and_run() {
EXE_TARGET="${1}" EXE_TARGET="${1}"
shift shift
sudo mkdir -p /var/lib/krata/guest
if [ "${KRATA_BUILD_INITRD}" = "1" ] if [ "${KRATA_BUILD_INITRD}" = "1" ]
then then
./initrd/build.sh -q ./scripts/initrd/build.sh -q
sudo cp "initrd/target/initrd" "/var/lib/krata/default/initrd" sudo cp "target/initrd/initrd" "/var/lib/krata/guest/initrd"
fi fi
RUST_TARGET="$(./scripts/detect-rust-target.sh)" RUST_TARGET="$(./scripts/build/target.sh)"
./scripts/cargo.sh build ${CARGO_BUILD_FLAGS} --bin "${EXE_TARGET}" ./scripts/build/cargo.sh build ${CARGO_BUILD_FLAGS} --bin "${EXE_TARGET}"
exec sudo RUST_LOG="${RUST_LOG}" "target/${RUST_TARGET}/debug/${EXE_TARGET}" "${@}" exec sudo RUST_LOG="${RUST_LOG}" "target/${RUST_TARGET}/debug/${EXE_TARGET}" "${@}"
} }

9
scripts/debug/kratactl.sh Executable file
View File

@ -0,0 +1,9 @@
#!/bin/sh
set -e
REAL_SCRIPT="$(realpath "${0}")"
DEBUG_DIR="$(dirname "${REAL_SCRIPT}")"
# shellcheck source=common.sh
. "${DEBUG_DIR}/common.sh"
build_and_run kratactl "${@}"

View File

@ -2,7 +2,8 @@
set -e set -e
REAL_SCRIPT="$(realpath "${0}")" REAL_SCRIPT="$(realpath "${0}")"
# shellcheck source-path=krata-debug-common.sh DEBUG_DIR="$(dirname "${REAL_SCRIPT}")"
. "$(dirname "${REAL_SCRIPT}")/krata-debug-common.sh" # shellcheck source=common.sh
. "${DEBUG_DIR}/common.sh"
KRATA_BUILD_INITRD=1 build_and_run kratad "${@}" KRATA_BUILD_INITRD=1 build_and_run kratad "${@}"

9
scripts/debug/kratanet.sh Executable file
View File

@ -0,0 +1,9 @@
#!/bin/sh
set -e
REAL_SCRIPT="$(realpath "${0}")"
DEBUG_DIR="$(dirname "${REAL_SCRIPT}")"
# shellcheck source=common.sh
. "${DEBUG_DIR}/common.sh"
build_and_run kratanet "${@}"

14
scripts/debug/session.sh Executable file
View File

@ -0,0 +1,14 @@
#!/bin/sh
set -e
stop_service_if_running() {
if sudo systemctl is-active "${1}" > /dev/null 2>&1
then
sudo systemctl stop "${1}"
fi
}
stop_service_if_running "kratad.service"
stop_service_if_running "kratanet.service"
tmuxp load "$(dirname "${0}")/session.yml"

11
scripts/debug/session.yml Normal file
View File

@ -0,0 +1,11 @@
session_name: krata-dev
start_directory: ../..
sleep_after: 3
windows:
- window_name: live
layout: tiled
panes:
- shell_command: ./scripts/debug/kratad.sh
- shell_command: ./scripts/debug/kratanet.sh
- focus: true
shell_command: "alias kratactl=./scripts/debug/kratactl.sh"

46
scripts/dist/bundle.sh vendored Executable file
View File

@ -0,0 +1,46 @@
#!/bin/sh
set -e
# shellcheck source=common.sh
. "$(dirname "${0}")/common.sh"
if [ -z "${KRATA_KERNEL_BUILD_JOBS}" ]
then
KRATA_KERNEL_BUILD_JOBS="2"
fi
BUNDLE_TAR="${OUTPUT_DIR}/bundle.tgz"
rm -f "${BUNDLE_TAR}"
BUNDLE_DIR="$(mktemp -d /tmp/krata-bundle.XXXXXXXXXXXXX)"
BUNDLE_DIR="${BUNDLE_DIR}/krata"
mkdir -p "${BUNDLE_DIR}"
for X in kratad kratanet kratactl
do
./scripts/build/cargo.sh build --release --bin "${X}"
RUST_TARGET="$(./scripts/build/target.sh)"
cp "${KRATA_DIR}/target/${RUST_TARGET}/release/${X}" "${BUNDLE_DIR}/${X}"
done
./scripts/initrd/build.sh
if [ "${KRATA_BUNDLE_SKIP_KERNEL_BUILD}" != "1" ]
then
./scripts/kernel/build.sh "-j${KRATA_KERNEL_BUILD_JOBS}"
fi
cd "${BUNDLE_DIR}"
cp "${KRATA_DIR}/target/initrd/initrd" initrd
cp "${KRATA_DIR}/target/kernel/kernel" kernel
cp "${KRATA_DIR}/resources/systemd/kratad.service" kratad.service
cp "${KRATA_DIR}/resources/systemd/kratanet.service" kratanet.service
cp "${KRATA_DIR}/resources/bundle/install.sh" install.sh
cp "${KRATA_DIR}/resources/bundle/uninstall.sh" uninstall.sh
for X in install.sh uninstall.sh kratactl kratad kratanet
do
chmod +x "${X}"
done
cd ..
tar czf "${BUNDLE_TAR}" .
cd "${KRATA_DIR}"
rm -rf "$(dirname "${BUNDLE_DIR}")"

8
scripts/dist/common.sh vendored Normal file
View File

@ -0,0 +1,8 @@
#!/bin/sh
set -e
REAL_SCRIPT="$(realpath "${0}")"
cd "$(dirname "${REAL_SCRIPT}")/../.."
KRATA_DIR="${PWD}"
OUTPUT_DIR="${KRATA_DIR}/target/dist"
mkdir -p "${OUTPUT_DIR}"

30
scripts/dist/deb.sh vendored Executable file
View File

@ -0,0 +1,30 @@
#!/bin/sh
set -e
# shellcheck source=common.sh
. "$(dirname "${0}")/common.sh"
"${KRATA_DIR}/scripts/dist/systar.sh"
KRATA_VERSION="$("${KRATA_DIR}/scripts/dist/version.sh")"
TARGET_ARCH="$(KRATA_ARCH_ALT_NAME=1 "${KRATA_DIR}/scripts/build/arch.sh")"
cd "${OUTPUT_DIR}"
rm -f "krata_${KRATA_VERSION}_${TARGET_ARCH}.deb"
fpm -s tar -t deb \
--name krata \
--license agpl3 \
--version "${KRATA_VERSION}" \
--architecture "${TARGET_ARCH}" \
--depends "xen-system-${TARGET_ARCH}" \
--description "Krata Hypervisor" \
--url "https://krata.dev" \
--maintainer "Edera Team <contact@edera.dev>" \
-x "usr/lib/**" \
--deb-systemd "${KRATA_DIR}/resources/systemd/kratad.service" \
--deb-systemd "${KRATA_DIR}/resources/systemd/kratanet.service" \
--deb-systemd-enable \
--deb-systemd-auto-start \
"${OUTPUT_DIR}/system.tgz"

31
scripts/dist/systar.sh vendored Executable file
View File

@ -0,0 +1,31 @@
#!/bin/sh
set -e
# shellcheck source=common.sh
. "$(dirname "${0}")/common.sh"
"${KRATA_DIR}/scripts/dist/bundle.sh"
SYSTAR="${OUTPUT_DIR}/system.tgz"
rm -f "${SYSTAR}"
SYSTAR_DIR="$(mktemp -d /tmp/krata-systar.XXXXXXXXXXXXX)"
cd "${SYSTAR_DIR}"
tar xf "${OUTPUT_DIR}/bundle.tgz"
mkdir sys
cd sys
mkdir -p usr/bin usr/libexec
mv ../krata/kratactl usr/bin
mv ../krata/kratanet ../krata/kratad usr/libexec/
mkdir -p usr/lib/systemd/system
mv ../krata/kratad.service ../krata/kratanet.service usr/lib/systemd/system/
mkdir -p usr/share/krata/guest
mv ../krata/kernel ../krata/initrd usr/share/krata/guest
tar czf "${SYSTAR}" --owner 0 --group 0 .
cd "${KRATA_DIR}"
rm -rf "${SYSTAR_DIR}"

15
scripts/dist/version.sh vendored Executable file
View File

@ -0,0 +1,15 @@
#!/bin/sh
set -e
# shellcheck source=common.sh
. "$(dirname "${0}")/common.sh"
cd "${KRATA_DIR}"
KRATA_VERSION="$(grep -A1 -F '[workspace.package]' Cargo.toml | grep 'version' | awk '{print $3}' | sed 's/"//g')"
if [ -z "${KRATA_VERSION}" ]
then
echo "ERROR: failed to determine krata version" > /dev/stderr
exit 1
fi
echo "${KRATA_VERSION}"

View File

@ -1,6 +0,0 @@
#!/bin/sh
set -e
cd "$(dirname "${0}")/.."
./scripts/cargo.sh clippy --fix --allow-dirty --allow-staged
cargo fmt --all

View File

@ -1,17 +1,20 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -e set -e
export RUST_LIBC="musl" REAL_SCRIPT="$(realpath "${0}")"
RUST_TARGET="$(./scripts/detect-rust-target.sh)" cd "$(dirname "${REAL_SCRIPT}")/../.."
export RUSTFLAGS="-Ctarget-feature=+crt-static"
cd "$(dirname "${0}")/.."
KRATA_DIR="${PWD}" KRATA_DIR="${PWD}"
./scripts/cargo.sh build -q --release --bin krataguest cd "${KRATA_DIR}"
export RUST_LIBC="musl"
RUST_TARGET="$(./scripts/build/target.sh)"
export RUSTFLAGS="-Ctarget-feature=+crt-static"
./scripts/build/cargo.sh build "${@}" --release --bin krataguest
INITRD_DIR="$(mktemp -d /tmp/krata-initrd.XXXXXXXXXXXXX)" INITRD_DIR="$(mktemp -d /tmp/krata-initrd.XXXXXXXXXXXXX)"
cp "target/${RUST_TARGET}/release/krataguest" "${INITRD_DIR}/init" cp "target/${RUST_TARGET}/release/krataguest" "${INITRD_DIR}/init"
chmod +x "${INITRD_DIR}/init" chmod +x "${INITRD_DIR}/init"
cd "${INITRD_DIR}" cd "${INITRD_DIR}"
mkdir -p "${KRATA_DIR}/initrd/target" mkdir -p "${KRATA_DIR}/target/initrd"
find . | cpio -R 0:0 --reproducible -o -H newc --quiet > "${KRATA_DIR}/initrd/target/initrd" find . | cpio -R 0:0 --reproducible -o -H newc --quiet > "${KRATA_DIR}/target/initrd/initrd"
rm -rf "${INITRD_DIR}" rm -rf "${INITRD_DIR}"

56
scripts/kernel/build.sh Executable file
View File

@ -0,0 +1,56 @@
#!/bin/sh
set -e
REAL_SCRIPT="$(realpath "${0}")"
cd "$(dirname "${REAL_SCRIPT}")/../.."
KRATA_DIR="${PWD}"
KERNEL_DIR="${KRATA_DIR}/kernel"
cd "${KRATA_DIR}"
# shellcheck source=../../kernel/config.sh
. "${KERNEL_DIR}/config.sh"
KERNEL_SRC="${KERNEL_DIR}/linux-${KERNEL_VERSION}"
if [ ! -f "${KERNEL_SRC}/Makefile" ]
then
rm -rf "${KERNEL_SRC}"
curl --progress-bar -L -o "${KERNEL_SRC}.txz" "${KERNEL_SRC_URL}"
tar xf "${KERNEL_SRC}.txz" -C "${KERNEL_DIR}"
rm "${KERNEL_SRC}.txz"
fi
OUTPUT_DIR="${KRATA_DIR}/target/kernel"
mkdir -p "${OUTPUT_DIR}"
TARGET_ARCH="$(KRATA_ARCH_KERNEL_NAME=1 ./scripts/build/arch.sh)"
KERNEL_CONFIG_FILE="${KERNEL_DIR}/krata-${TARGET_ARCH}.config"
if [ ! -f "${KERNEL_CONFIG_FILE}" ]
then
echo "ERROR: kernel config file not found for ${TARGET_ARCH}" > /dev/stderr
exit 1
fi
cp "${KERNEL_CONFIG_FILE}" "${KERNEL_SRC}/.config"
make -C "${KERNEL_SRC}" ARCH="${TARGET_ARCH}" "${@}" olddefconfig
if [ "${TARGET_ARCH}" = "x86_64" ]
then
make -C "${KERNEL_SRC}" ARCH="${TARGET_ARCH}" "${@}" bzImage
elif [ "${TARGET_ARCH}" = "arm64" ]
then
make -C "${KERNEL_SRC}" ARCH="${TARGET_ARCH}" "${@}" Image.gz
fi
if [ "${TARGET_ARCH}" = "x86_64" ]
then
cp "${KERNEL_SRC}/arch/x86/boot/bzImage" "${OUTPUT_DIR}/kernel"
elif [ "${TARGET_ARCH}" = "arm64" ]
then
cp "${KERNEL_SRC}/arch/arm64/boot/Image.gz" "${OUTPUT_DIR}/kernel"
else
echo "ERROR: unable to determine what file is the vmlinuz for ${TARGET_ARCH}" > /dev/stderr
exit 1
fi

13
scripts/kernel/clean.sh Executable file
View File

@ -0,0 +1,13 @@
#!/bin/sh
set -e
REAL_SCRIPT="$(realpath "${0}")"
cd "$(dirname "${REAL_SCRIPT}")/../.."
KRATA_DIR="${PWD}"
KERNEL_DIR="${KRATA_DIR}/kernel"
# shellcheck disable=SC2010
for ITEM in $(ls "${KERNEL_DIR}" | grep "^linux-")
do
rm -rf "${KERNEL_DIR:?}/${ITEM}"
done

View File

@ -1,8 +0,0 @@
#!/bin/sh
set -e
REAL_SCRIPT="$(realpath "${0}")"
# shellcheck source-path=krata-debug-common.sh
. "$(dirname "${REAL_SCRIPT}")/krata-debug-common.sh"
KRATA_BUILD_INITRD=1 build_and_run kratactl "${@}"

View File

@ -1,8 +0,0 @@
#!/bin/sh
set -e
REAL_SCRIPT="$(realpath "${0}")"
# shellcheck source-path=krata-debug-common.sh
. "$(dirname "${REAL_SCRIPT}")/krata-debug-common.sh"
build_and_run kratanet "${@}"