Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement custom OVMF firmware #646

Merged
merged 8 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docker/vm_supervisor-dev.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ RUN curl -fsSL -o /opt/firecracker/vmlinux.bin https://s3.amazonaws.com/spec.ccf
RUN ln /opt/firecracker/release-*/firecracker-v* /opt/firecracker/firecracker
RUN ln /opt/firecracker/release-*/jailer-v* /opt/firecracker/jailer

RUN pip3 install typing-extensions 'aleph-message==0.4.4'
RUN pip3 install typing-extensions 'aleph-message==0.4.7'

RUN mkdir -p /var/lib/aleph/vm/jailer

Expand Down
100 changes: 52 additions & 48 deletions examples/confidential_instance_message_from_aleph.json
Original file line number Diff line number Diff line change
@@ -1,51 +1,55 @@
{
"chain": "ETH",
"item_hash": "fake-hash-fake-hash-fake-hash-fake-hash-fake-hash-fake-hash-hash",
"sender": "0x9319Ad3B7A8E0eE24f2E639c40D8eD124C5520Ba",
"type": "INSTANCE",
"channel": "Fun-dApps",
"confirmed": true,
"content": {
"address": "0x9319Ad3B7A8E0eE24f2E639c40D8eD124C5520Ba",
"allow_amend": false,
"variables": {
"VM_CUSTOM_NUMBER": "32"
},
"environment": {
"reproducible": true,
"internet": true,
"aleph_api": true,
"shared_cache": true
},
"resources": {
"vcpus": 1,
"memory": 512,
"seconds": 30
},
"rootfs": {
"parent": {
"ref": "549ec451d9b099cad112d4aaa2c00ac40fb6729a92ff252ff22eef0b5c3cb613",
"use_latest": true
},
"persistence": "host",
"size_mib": 5000
},
"authorized_keys": [
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDj95BHGUx0/z2G/tTrEi8o49i70xvjcEUdSs3j4A33jE7pAphrfRVbuFMgFubcm8n9r5ftd/H8SjjTL4hY9YvWV5ZuMf92GUga3n4wgevvPlBszYZCy/idxFl0vtHYC1CcK9v4tVb9onhDt8FOJkf2m6PmDyvC+6tl6LwoerXTeeiKr5VnTB4KOBkammtFmix3d1X1SZd/cxdwZIHcQ7BNsqBm2w/YzVba6Z4ZnFUelBkQtMQqNs2aV51O1pFFqtZp2mM71D5d8vn9pOtqJ5QmY5IW6NypcyqKJZg5o6QguK5rdXLkc7AWro27BiaHIENl3w0wazp9EDO9zPAGJ6lz olivier@lanius"
],

"time": 1619017773.8950517
"chain": "ETH",
"item_hash": "fake-hash-fake-hash-fake-hash-fake-hash-fake-hash-fake-hash-hash",
"sender": "0x9319Ad3B7A8E0eE24f2E639c40D8eD124C5520Ba",
"type": "INSTANCE",
"channel": "Fun-dApps",
"confirmed": true,
"content": {
"address": "0x9319Ad3B7A8E0eE24f2E639c40D8eD124C5520Ba",
"allow_amend": false,
"variables": {
"VM_CUSTOM_NUMBER": "32"
},
"item_content": "{\"address\":\"0x9319Ad3B7A8E0eE24f2E639c40D8eD124C5520Ba\",\"allow_amend\":false,\"variables\":{\"VM_CUSTOM_NUMBER\":\"32\"},\"environment\":{\"reproducible\":true,\"internet\":true,\"aleph_api\":true,\"shared_cache\":true},\"resources\":{\"vcpus\":1,\"memory\":128,\"seconds\":30},\"rootfs\":{\"parent\":{\"ref\":\"549ec451d9b099cad112d4aaa2c00ac40fb6729a92ff252ff22eef0b5c3cb613\",\"use_latest\":true},\"persistence\":\"host\",\"size_mib\":20000},\"cloud_config\":{\"password\":\"password\",\"chpasswd\":{\"expire\":\"False\"}},\"volumes\":[{\"mount\":\"/opt/venv\",\"ref\":\"5f31b0706f59404fad3d0bff97ef89ddf24da4761608ea0646329362c662ba51\",\"use_latest\":false},{\"comment\":\"Working data persisted on the VM supervisor, not available on other nodes\",\"mount\":\"/var/lib/example\",\"name\":\"data\",\"persistence\":\"host\",\"size_mib\":5}],\"replaces\":\"0x9319Ad3B7A8E0eE24f2E639c40D8eD124C5520Ba\",\"time\":1619017773.8950517}",
"item_type": "inline",
"signature": "0x372da8230552b8c3e65c05b31a0ff3a24666d66c575f8e11019f62579bf48c2b7fe2f0bbe907a2a5bf8050989cdaf8a59ff8a1cbcafcdef0656c54279b4aa0c71b",
"size": 749,
"time": 1619017773.8950577,
"confirmations": [
{
"chain": "ETH",
"height": 12284734,
"hash": "0x67f2f3cde5e94e70615c92629c70d22dc959a118f46e9411b29659c2fce87cdc"
}
]
"environment": {
"hypervisor": "qemu",
"reproducible": true,
"internet": true,
"aleph_api": true,
"shared_cache": true,
"trusted_execution": {
"firmware": "88978bb4c2ff54400ce5f51c3a109e1af1ab03d1ea4409666917317ac513846b",
"policy": 1
}
},
"resources": {
"vcpus": 1,
"memory": 512,
"seconds": 30
},
"rootfs": {
"parent": {
"ref": "549ec451d9b099cad112d4aaa2c00ac40fb6729a92ff252ff22eef0b5c3cb613",
"use_latest": true
},
"persistence": "host",
"size_mib": 5000
},
"authorized_keys": [
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDj95BHGUx0/z2G/tTrEi8o49i70xvjcEUdSs3j4A33jE7pAphrfRVbuFMgFubcm8n9r5ftd/H8SjjTL4hY9YvWV5ZuMf92GUga3n4wgevvPlBszYZCy/idxFl0vtHYC1CcK9v4tVb9onhDt8FOJkf2m6PmDyvC+6tl6LwoerXTeeiKr5VnTB4KOBkammtFmix3d1X1SZd/cxdwZIHcQ7BNsqBm2w/YzVba6Z4ZnFUelBkQtMQqNs2aV51O1pFFqtZp2mM71D5d8vn9pOtqJ5QmY5IW6NypcyqKJZg5o6QguK5rdXLkc7AWro27BiaHIENl3w0wazp9EDO9zPAGJ6lz olivier@lanius"
],
"time": 1619017773.8950517
},
"item_content": "{\"address\":\"0x9319Ad3B7A8E0eE24f2E639c40D8eD124C5520Ba\",\"allow_amend\":false,\"variables\":{\"VM_CUSTOM_NUMBER\":\"32\"},\"environment\":{\"reproducible\":true,\"internet\":true,\"aleph_api\":true,\"shared_cache\":true},\"resources\":{\"vcpus\":1,\"memory\":128,\"seconds\":30},\"rootfs\":{\"parent\":{\"ref\":\"549ec451d9b099cad112d4aaa2c00ac40fb6729a92ff252ff22eef0b5c3cb613\",\"use_latest\":true},\"persistence\":\"host\",\"size_mib\":20000},\"cloud_config\":{\"password\":\"password\",\"chpasswd\":{\"expire\":\"False\"}},\"volumes\":[{\"mount\":\"/opt/venv\",\"ref\":\"5f31b0706f59404fad3d0bff97ef89ddf24da4761608ea0646329362c662ba51\",\"use_latest\":false},{\"comment\":\"Working data persisted on the VM supervisor, not available on other nodes\",\"mount\":\"/var/lib/example\",\"name\":\"data\",\"persistence\":\"host\",\"size_mib\":5}],\"replaces\":\"0x9319Ad3B7A8E0eE24f2E639c40D8eD124C5520Ba\",\"time\":1619017773.8950517}",
"item_type": "inline",
"signature": "0x372da8230552b8c3e65c05b31a0ff3a24666d66c575f8e11019f62579bf48c2b7fe2f0bbe907a2a5bf8050989cdaf8a59ff8a1cbcafcdef0656c54279b4aa0c71b",
"size": 749,
"time": 1619017773.8950577,
"confirmations": [
{
"chain": "ETH",
"height": 12284734,
"hash": "0x67f2f3cde5e94e70615c92629c70d22dc959a118f46e9411b29659c2fce87cdc"
}
]
}
2 changes: 1 addition & 1 deletion examples/volumes/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ RUN apt-get update && apt-get -y upgrade && apt-get install -y \
&& rm -rf /var/lib/apt/lists/*

RUN python3 -m venv /opt/venv
RUN /opt/venv/bin/pip install 'aleph-message==0.4.4'
RUN /opt/venv/bin/pip install 'aleph-message==0.4.7'

CMD mksquashfs /opt/venv /mnt/volume-venv.squashfs
2 changes: 1 addition & 1 deletion packaging/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ debian-package-code:
cp ../examples/instance_message_from_aleph.json ./aleph-vm/opt/aleph-vm/examples/instance_message_from_aleph.json
cp -r ../examples/data ./aleph-vm/opt/aleph-vm/examples/data
mkdir -p ./aleph-vm/opt/aleph-vm/examples/volumes
pip3 install --target ./aleph-vm/opt/aleph-vm/ 'aleph-message==0.4.4' 'eth-account==0.10' 'sentry-sdk==1.31.0' 'qmp==1.1.0' 'superfluid==0.2.1' 'sqlalchemy[asyncio]>=2.0' 'aiosqlite==0.19.0' 'alembic==1.13.1' 'aiohttp_cors==0.7.0' 'pyroute2==0.7.12'
pip3 install --target ./aleph-vm/opt/aleph-vm/ 'aleph-message==0.4.7' 'eth-account==0.10' 'sentry-sdk==1.31.0' 'qmp==1.1.0' 'superfluid==0.2.1' 'sqlalchemy[asyncio]>=2.0' 'aiosqlite==0.19.0' 'alembic==1.13.1' 'aiohttp_cors==0.7.0' 'pyroute2==0.7.12'
python3 -m compileall ./aleph-vm/opt/aleph-vm/

debian-package-resources: firecracker-bins vmlinux download-ipfs-kubo target/bin/sevctl
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ dependencies = [
"aiodns==3.1.0",
"setproctitle==1.3.3",
"pyyaml==6.0.1",
"aleph-message==0.4.4",
"aleph-message>=0.4.7",
"eth-account~=0.10",
"sentry-sdk==1.31.0",
"aioredis==1.3.1",
Expand Down
2 changes: 1 addition & 1 deletion runtimes/aleph-debian-12-python/create_disk_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ locale-gen en_US.UTF-8

echo "Pip installing aleph-sdk-python"
mkdir -p /opt/aleph/libs
pip3 install --target /opt/aleph/libs 'aleph-sdk-python==0.9.0' 'aleph-message==0.4.4' 'fastapi~=0.109.2'
pip3 install --target /opt/aleph/libs 'aleph-sdk-python==0.9.0' 'aleph-message==0.4.7' 'fastapi~=0.109.2'

# Compile Python code to bytecode for faster execution
# -o2 is needed to compile with optimization level 2 which is what we launch init1.py ("python -OO")
Expand Down
11 changes: 10 additions & 1 deletion src/aleph/vm/controllers/configuration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from enum import Enum
from pathlib import Path
from typing import Optional, Union
from typing import List, Optional, Union

from pydantic import BaseModel

Expand All @@ -18,6 +18,12 @@ class VMConfiguration(BaseModel):
init_timeout: float


class QemuVMHostVolume(BaseModel):
mount: str
path_on_host: Path
read_only: bool


class QemuVMConfiguration(BaseModel):
qemu_bin_path: str
cloud_init_drive_path: Optional[str]
Expand All @@ -27,6 +33,7 @@ class QemuVMConfiguration(BaseModel):
vcpu_count: int
mem_size_mb: int
interface_name: Optional[str]
host_volumes: List[QemuVMHostVolume]


class QemuConfidentialVMConfiguration(BaseModel):
Expand All @@ -38,9 +45,11 @@ class QemuConfidentialVMConfiguration(BaseModel):
vcpu_count: int
mem_size_mb: int
interface_name: Optional[str]
host_volumes: List[QemuVMHostVolume]
ovmf_path: Path
sev_session_file: Path
sev_dh_cert_file: Path
sev_policy: int


class HypervisorType(str, Enum):
Expand Down
18 changes: 17 additions & 1 deletion src/aleph/vm/controllers/qemu/instance.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import json
import logging
import shutil
Expand All @@ -17,6 +18,7 @@
Configuration,
HypervisorType,
QemuVMConfiguration,
QemuVMHostVolume,
save_controller_configuration,
)
from aleph.vm.controllers.firecracker.executable import (
Expand All @@ -34,11 +36,17 @@


class AlephQemuResources(AlephFirecrackerResources):
async def download_all(self) -> None:
async def download_runtime(self) -> None:
volume = self.message_content.rootfs
parent_image_path = await get_rootfs_base_path(volume.parent.ref)
self.rootfs_path = await self.make_writable_volume(parent_image_path, volume)

async def download_all(self):
await asyncio.gather(
self.download_runtime(),
self.download_volumes(),
)

async def make_writable_volume(self, parent_image_path, volume: Union[PersistentVolume, RootfsVolume]):
"""Create a new qcow2 image file based on the passed one, that we give to the VM to write onto"""
qemu_img_path: Optional[str] = shutil.which("qemu-img")
Expand Down Expand Up @@ -181,6 +189,14 @@ async def configure(self):
vcpu_count=vcpu_count,
mem_size_mb=mem_size_mb,
interface_name=interface_name,
host_volumes=[
QemuVMHostVolume(
mount=volume.mount,
path_on_host=volume.path_on_host,
read_only=volume.read_only,
)
for volume in self.resources.volumes
],
)

configuration = Configuration(
Expand Down
41 changes: 35 additions & 6 deletions src/aleph/vm/controllers/qemu_confidential/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
import logging
import shutil
from asyncio.subprocess import Process
from pathlib import Path
from typing import Callable, Optional

from aleph_message.models import ItemHash
from aleph_message.models.execution.environment import MachineResources
from aleph_message.models.execution.environment import AMDSEVPolicy, MachineResources

from aleph.vm.conf import settings
from aleph.vm.controllers.configuration import (
Configuration,
HypervisorType,
QemuConfidentialVMConfiguration,
QemuVMHostVolume,
save_controller_configuration,
)
from aleph.vm.controllers.qemu import AlephQemuInstance
Expand All @@ -21,19 +23,30 @@
logger,
)
from aleph.vm.network.interfaces import TapInterface
from aleph.vm.storage import get_existing_file

logger = logging.getLogger(__name__)


class AlephQemuConfidentialResources(AlephQemuResources):
# TODO: Implement download of the custom OVMF bootloader to use if specified, if not only use the default one.
pass
firmware_path: Path

async def download_firmware(self):
firmware = self.message_content.environment.trusted_execution.firmware
self.firmware_path = await get_existing_file(firmware)

Check warning on line 36 in src/aleph/vm/controllers/qemu_confidential/instance.py

View check run for this annotation

Codecov / codecov/patch

src/aleph/vm/controllers/qemu_confidential/instance.py#L35-L36

Added lines #L35 - L36 were not covered by tests

async def download_all(self):
await asyncio.gather(

Check warning on line 39 in src/aleph/vm/controllers/qemu_confidential/instance.py

View check run for this annotation

Codecov / codecov/patch

src/aleph/vm/controllers/qemu_confidential/instance.py#L39

Added line #L39 was not covered by tests
self.download_runtime(),
self.download_firmware(),
self.download_volumes(),
)


class AlephQemuConfidentialInstance(AlephQemuInstance):
vm_id: int
vm_hash: ItemHash
resources: AlephQemuResources
resources: AlephQemuConfidentialResources
enable_console: bool
enable_networking: bool
hardware_resources: MachineResources
Expand All @@ -45,6 +58,7 @@
persistent = True
_queue_cancellers: dict[asyncio.Queue, Callable] = {}
controller_configuration: Configuration
confidential_policy: int

def __repr__(self):
return f"<AlephQemuInstance {self.vm_id}>"
Expand All @@ -56,12 +70,17 @@
self,
vm_id: int,
vm_hash: ItemHash,
resources: AlephQemuResources,
resources: AlephQemuConfidentialResources,
enable_networking: bool = False,
confidential_policy: int = AMDSEVPolicy.NO_DBG,
enable_console: Optional[bool] = None,
hardware_resources: MachineResources = MachineResources(),
tap_interface: Optional[TapInterface] = None,
):
super().__init__(

Check warning on line 80 in src/aleph/vm/controllers/qemu_confidential/instance.py

View check run for this annotation

Codecov / codecov/patch

src/aleph/vm/controllers/qemu_confidential/instance.py#L80

Added line #L80 was not covered by tests
vm_id, vm_hash, resources, enable_networking, enable_console, hardware_resources, tap_interface
)
self.confidential_policy = confidential_policy

Check warning on line 83 in src/aleph/vm/controllers/qemu_confidential/instance.py

View check run for this annotation

Codecov / codecov/patch

src/aleph/vm/controllers/qemu_confidential/instance.py#L83

Added line #L83 was not covered by tests
super().__init__(vm_id, vm_hash, resources, enable_networking, hardware_resources, tap_interface)

async def setup(self):
Expand All @@ -76,6 +95,7 @@
cloud_init_drive = await self._create_cloud_init_drive()

image_path = str(self.resources.rootfs_path)
firmware_path = str(self.resources.firmware_path)

Check warning on line 98 in src/aleph/vm/controllers/qemu_confidential/instance.py

View check run for this annotation

Codecov / codecov/patch

src/aleph/vm/controllers/qemu_confidential/instance.py#L98

Added line #L98 was not covered by tests
vcpu_count = self.hardware_resources.vcpus
mem_size_mib = self.hardware_resources.memory
mem_size_mb = str(int(mem_size_mib / 1024 / 1024 * 1000 * 1000))
Expand All @@ -98,9 +118,18 @@
vcpu_count=vcpu_count,
mem_size_mb=mem_size_mb,
interface_name=interface_name,
ovmf_path="/home/olivier/custom-OVMF.fd",
ovmf_path=firmware_path,
sev_session_file=session_file_path,
sev_dh_cert_file=godh_file_path,
sev_policy=self.confidential_policy,
host_volumes=[
QemuVMHostVolume(
mount=volume.mount,
path_on_host=volume.path_on_host,
read_only=volume.read_only,
)
for volume in self.resources.volumes
],
)

configuration = Configuration(
Expand Down
Loading