Skip to content

Commit 66f7e09

Browse files
committed
tmp
1 parent 5669846 commit 66f7e09

20 files changed

+2013
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,8 @@ venv/*
5555
.env.local
5656

5757
.gitsigners
58+
59+
# Devtools
60+
devtools/bin
61+
devtools/programs
62+
devtools/built_programs

devtools/0_install_env.sh

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/bin/sh
2+
set -euf
3+
4+
arch=$(uname -m)
5+
image_bucket_url="https://s3.amazonaws.com/spec.ccfc.min/img/quickstart_guide/$arch"
6+
bin_dir=bin
7+
firecracker=firecracker
8+
kernel=vmlinux.bin
9+
mkdir -p ${bin_dir}
10+
11+
# Check architecture
12+
if [ ${arch} = "x86_64" ]; then
13+
kernel_url="${image_bucket_url}/kernels/vmlinux.bin"
14+
elif [ ${arch} = "aarch64" ]; then
15+
kernel_url="${image_bucket_url}/kernels/vmlinux.bin"
16+
else
17+
echo "Cannot run firecracker on $arch architecture."
18+
exit 1
19+
fi
20+
21+
# Dependencies
22+
apt update -y
23+
apt upgrade -y
24+
apt install squashfs-tools debootstrap -y
25+
26+
# Get the Firecracker binary
27+
if [ ! -f ${bin_dir}/${firecracker} ]; then
28+
echo "Installing Firecracker..."
29+
release_url="https://github.com/firecracker-microvm/firecracker/releases"
30+
latest=$(basename $(curl -fsSLI -o /dev/null -w %{url_effective} ${release_url}/latest))
31+
arch=$(uname -m)
32+
if curl -L ${release_url}/download/${latest}/firecracker-${latest}-${arch}.tgz | tar -xz; then
33+
bin_folder="release-${latest}-$(uname -m)"
34+
mv "${bin_folder}/firecracker-${latest}-$(uname -m)" ${bin_dir}/${firecracker}
35+
rm -r "${bin_folder}"
36+
echo "Firecracker installation complete."
37+
else
38+
echo "Failed to install Firecracker. Please check your network connection and try again."
39+
exit 1
40+
fi
41+
else
42+
echo "Firecracker is already installed."
43+
fi
44+
45+
# Get the vmlinux.bin
46+
if [ ! -f ${bin_dir}/${kernel} ]; then
47+
echo "Installing kernel..."
48+
if curl -L -o ${bin_dir}/${kernel} $kernel_url; then
49+
echo "${kernel} installation complete."
50+
else
51+
echo "Failed to install ${kernel}. Please check your network connection and try again."
52+
exit 1
53+
fi
54+
else
55+
echo "${kernel} is already installed."
56+
fi
57+
58+
# Docker installation
59+
if docker --version >/dev/null 2>&1; then
60+
echo "Docker is already installed."
61+
else
62+
apt install docker.io -y
63+
fi
64+
# Docker without sudo
65+
groupadd -f docker
66+
usermod -aG docker $(whoami)
67+
docker context use default
68+
69+
echo "Environment setup complete."

devtools/1a_build_custom_runtime.sh

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/sh
2+
set -euf
3+
4+
runtime=$1
5+
6+
# Utils
7+
setup=utils/setup.sh
8+
inittab=utils/inittab
9+
init=utils/init0.sh
10+
init_py=utils/init1.py
11+
loading_html=utils/loading.html
12+
13+
# Temp Files
14+
dest=/mnt/rootfs
15+
16+
# Target
17+
rootfs_dir=runtimes
18+
target=$rootfs_dir/$runtime.squashfs
19+
mkdir -p $rootfs_dir
20+
21+
# Cleanup previous run
22+
rm -rf $target
23+
rm -rf $dest
24+
mkdir $dest
25+
26+
echo "Downloading Debian Bullseye minimal"
27+
debootstrap --variant=minbase bullseye $dest http://deb.debian.org/debian/
28+
29+
echo "Run setup script"
30+
chmod +x $setup
31+
cp $setup $dest/setup.sh
32+
chroot $dest /bin/sh -c "./setup.sh && rm -f setup.sh"
33+
34+
# Reduce size
35+
rm -fr $dest/root/.cache
36+
rm -fr $dest/var/cache
37+
mkdir -p $dest/var/cache/apt/archives/partial
38+
rm -fr $dest/usr/share/doc
39+
rm -fr $dest/usr/share/man
40+
rm -fr $dest/var/lib/apt/lists
41+
42+
echo "Install init scripts"
43+
cp $inittab $dest/etc/inittab
44+
chmod +x $init
45+
chmod +x $init_py
46+
cp $init $dest/sbin/init
47+
cp $init_py $dest/root/init1.py
48+
cp $loading_html $dest/root/loading.html
49+
50+
echo "Creating squashfs image"
51+
mksquashfs $dest $target -noappend -quiet
52+
rm -rf $dest
53+
echo "Done"

devtools/1b_download_runtime.sh

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/sh
2+
set -euf
3+
4+
default_runtime=63f07193e6ee9d207b7d1fcf8286f9aee34e6f12f101d2ec77c1229f92964696
5+
get_message_url=https://api1.aleph.im/api/v0/messages/
6+
ipfs_gateway=https://ipfs.aleph.im/ipfs/
7+
rootfs_dir=runtimes
8+
mkdir -p $rootfs_dir
9+
10+
# Ask for runtime item hash
11+
read -p "Enter a runtime item hash (leave blank for default): " runtime
12+
if [ -z "${runtime}" ]; then
13+
runtime=$default_runtime
14+
echo "Using default runtime: ${runtime}"
15+
else
16+
echo "Using provided runtime: ${runtime}"
17+
fi
18+
19+
# Get item hash message
20+
message=$(curl -s -X GET "$get_message_url$runtime")
21+
ipfs_cid=$(echo "$message" | jq -r '.message.content.item_hash')
22+
#echo "IPFS CID: $ipfs_cid"
23+
24+
# Get runtime file
25+
runtime_file="${runtime}.squashfs"
26+
download_url="$ipfs_gateway$ipfs_cid"
27+
if [ ! -f "${rootfs_dir}/${runtime_file}" ]; then
28+
echo "Downloading ${runtime_file}..."
29+
curl -L $download_url -o ${rootfs_dir}/${runtime_file}
30+
echo "Download complete."
31+
else
32+
echo "${runtime_file} is already available."
33+
fi

devtools/1b_edit_runtime.sh

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/sh
2+
set -euf
3+
4+
apt install squashfs-tools -y
5+
6+
rootfs_dir=runtimes
7+
runtime=63f07193e6ee9d207b7d1fcf8286f9aee34e6f12f101d2ec77c1229f92964696.squashfs
8+
mount_dir=vm
9+
mkdir -p ${mount_dir}/ro
10+
mkdir -p ${mount_dir}/new
11+
12+
mount -o loop,ro -t squashfs ${rootfs_dir}/${runtime} ${mount_dir}/ro
13+
cp -ar ${mount_dir}/ro ${mount_dir}/new
14+
umount ${mount_dir}/ro
15+
16+
#mksquashfs ${mount_dir}/new new.squashfs -noappend -quiet

devtools/2_pack_program.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
set -euf
3+
4+
in=programs/$1
5+
out_dir=built_programs/$1
6+
mkdir -p $out_dir
7+
out=$out_dir/program.squashfs
8+
rm -f $out
9+
mksquashfs $in $out -noappend -quiet

devtools/3_pack_dependencies.sh

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/sh
2+
set -euf
3+
4+
if docker --version >/dev/null 2>&1; then
5+
echo "Docker is already installed."
6+
else
7+
apt install docker.io -y
8+
fi
9+
10+
if [ $# -ne 1 ]; then
11+
echo "Error: Please provide the application directory as an argument."
12+
echo "Usage: $0 <app_dir>"
13+
exit 1
14+
fi
15+
16+
app_dir=programs/$1
17+
deps_file="${app_dir}/requirements.txt"
18+
output_file="built_${app_dir}/packages.squashfs"
19+
20+
echo "Packing dependencies for ${app_dir} into ${output_file} using ${deps_file}"
21+
22+
if [ ! -d "$app_dir" ]; then
23+
echo "Error: The application directory '$app_dir' does not exist."
24+
exit 1
25+
fi
26+
if [ ! -f "$deps_file" ]; then
27+
echo "Error: The requirements file '$deps_file' does not exist."
28+
exit 1
29+
fi
30+
31+
rm -f "$output_file"
32+
touch "$output_file"
33+
34+
docker run --rm -t --platform linux/amd64 \
35+
-v "$(pwd)/${deps_file}:/opt/requirements.txt" \
36+
-v "$(pwd)/${output_file}:/opt/packages.squashfs" \
37+
debian:bookworm /bin/bash \
38+
-c "apt-get update -y;
39+
apt-get install python3-pip squashfs-tools -y;
40+
python3 -V;
41+
pip install -t /opt/packages -r /opt/requirements.txt;
42+
mksquashfs /opt/packages /opt/packages.squashfs -noappend -quiet"
43+
44+
echo "Dependencies packed into ${output_file} successfully."

devtools/5_run.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
set -euf
3+
4+
rm -f /tmp/v.sock
5+
rm -f /tmp/firecracker.socket
6+
./bin/firecracker --api-sock /tmp/firecracker.socket --config-file ./utils/vm_config_base.json
7+
rm -f /tmp/v.sock
8+
rm -f /tmp/firecracker.socket

devtools/config.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from pathlib import Path
2+
3+
from pydantic import BaseModel, PositiveInt
4+
5+
ROOTFS_PATH = "./runtimes/test.squashfs"
6+
KERNEL_PATH = "./bin/vmlinux.bin"
7+
VSOCK_PATH = "/tmp/v.sock"
8+
9+
10+
class BootSource(BaseModel):
11+
kernel_image_path: Path = Path(KERNEL_PATH)
12+
boot_args: str = "console=ttyS0 reboot=k panic=1 pci=off ro noapic nomodules random.trust_cpu=on"
13+
14+
@staticmethod
15+
def args(enable_console: bool = True, writable: bool = False):
16+
default = "reboot=k panic=1 pci=off noapic nomodules random.trust_cpu=on"
17+
if writable:
18+
default = default + " rw"
19+
else:
20+
default = default + " ro"
21+
if enable_console:
22+
return "console=ttyS0 " + default
23+
else:
24+
return default
25+
26+
27+
class Drive(BaseModel):
28+
drive_id: str = "rootfs"
29+
path_on_host: Path = Path(ROOTFS_PATH)
30+
is_root_device: bool = True
31+
is_read_only: bool = True
32+
33+
34+
class MachineConfig(BaseModel):
35+
vcpu_count: PositiveInt = 1
36+
mem_size_mib: PositiveInt = 512
37+
smt: bool = False
38+
39+
40+
class Vsock(BaseModel):
41+
vsock_id: str = "1"
42+
guest_cid: PositiveInt = 3
43+
uds_path: str = VSOCK_PATH
44+
45+
46+
class NetworkInterface(BaseModel):
47+
iface_id: str = "eth0"
48+
guest_mac: str = "AA:FC:00:00:00:01"
49+
host_dev_name: str
50+
51+
52+
class FirecrackerConfig(BaseModel):
53+
boot_source: BootSource
54+
drives: list[Drive]
55+
machine_config: MachineConfig
56+
vsock: Vsock | None
57+
network_interfaces: list[NetworkInterface] | None
58+
59+
class Config:
60+
allow_population_by_field_name = True
61+
62+
@staticmethod
63+
def alias_generator(x: str):
64+
return x.replace("_", "-")

devtools/draft.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import asyncio
2+
import logging
3+
from pathlib import Path
4+
5+
from run import MicroVM
6+
7+
logging.basicConfig(
8+
level=logging.DEBUG,
9+
format="%(relativeCreated)4f |V %(levelname)s | %(message)s",
10+
)
11+
logger = logging.getLogger(__name__)
12+
13+
vm = MicroVM(
14+
vm_id=0,
15+
firecracker_bin_path=Path("./bin/firecracker"),
16+
jailer_base_directory=Path("/tmp/jail"),
17+
use_jailer=False,
18+
jailer_bin_path=Path("./bin/release-v1.10.1-x86_64/jailer-v1.10.1-x86_64"),
19+
init_timeout=5.0,
20+
enable_log=True,
21+
)
22+
23+
config_path = Path("utils/vm_config_base.json")
24+
asyncio.run(vm.start(config_path))

devtools/reqs.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
msgpack==1.0.7
2+
systemd-python==235

0 commit comments

Comments
 (0)