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

Radxa notes to run LocalAI #307

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
102 changes: 102 additions & 0 deletions content/en/blog/radxa-trusted-boot-localai.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
---
title: "Running AI Models Securely at the Edge"
date: 2024-11-10T12:53:13+01:00
author: Mauro Morales ([X](https://x.com/mauromrls)) ([GitHub](https://github.com/mauromorales))
---

{{< alert title="Requirements" color="info" >}}
- [Radxa X4](https://radxa.com/products/x/x4/) with at least 8GB of RAM and [proper cooling](https://radxa.com/products/accessories/heatsink-for-x4)
- Radxa X4 Beta Firmware including the Secure Boot feature
{{< /alert >}}

## Prepare the Radxa X4

{{< alert title="Warning" color="warning" >}}
- Without proper cooling, the Radxa X4 will overheat and might shut down during the firmware update process, which could brick the device.
- Radxa's Firmware to enable Secure Boot is still in beta as of writing of this document. Use it at your own risk.
{{< /alert >}}

Once you have a Radxa X4 and the proper cooling, we first need to flash the Radxa X4 Beta Firmware to enable Secure Boot. Follow these steps:

1. Go to this [Radxa X4 Beta Firmware](https://forum.radxa.com/t/enabling-secureboot/22704/15) and request access to the firmware.
2. Extract the contents of the zip file
{{< figure src="https://github.com/user-attachments/assets/d1614177-2e88-4a01-b514-4d1572b28a7a" title="An elephant at sunset" >}}
3. Format a USB drive as FAT32
![Format USB Drive](https://github.com/user-attachments/assets/69088541-3314-4593-9495-6569c4d4a413)
3. Rename the top directory to `EFI` and copy the contents to the USB drive
![Copy Contents to USB Drive](https://github.com/user-attachments/assets/3b2e73aa-fa4c-4ec6-b1ee-c5d76453d198)
5. Follow the instructions on the Radxa Documentation to [Upgrade the BIOS](https://docs.radxa.com/en/x/x4/bios/update-bios)
6. Do a G3 power cycle (unplug the power cable, wait for 10 seconds, and plug it back in)

## Create an Ubuntu 24.04 LTS UKI Image with Full Firmware Support

{{< alert title="Note" color="info" >}}
- This guide assumes that you have checked out the [Kairos repo](https://github.com/kairos-io/kairos) and are working from the root of the repo.
- If you'd like to also have Kubernetes on the image, make sure set the `VARIANT=standard` and give one of the possible values for `K3S_VERSION` related to the Kairos version you're using.
{{< /alert >}}

Create the file `images/Dockerfile.full-firmware` with the following content:

```Dockerfile
FROM ubuntu:24.04
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cant we use an existing image to base this off ? A simple FROM kairos whatever and then install the kernel and link it?

so basically do it the other way around, from a kairos image ADD the kernel missing stuff, instead of generating a ubuntu image to then transform it into kairos? Seems easier

RUN apt-get update
RUN apt-get install -y --no-install-recommends \
linux-image-generic-hwe-24.04
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
```

Then, build the image:

```bash
docker build -t ubuntu-full-firmware -f images/Dockerfile.full-firmware ./images
```

Now we are going to use our newly created image and convert it to a Kairos image for Trusted Boot.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would drop the trusted mention here, after all this will transform it into a generic kairos image which is valid for trusted or non-trusted workflows no?


{{< alert title="Note" color="info" >}}
I'm using ttl.sh as the Docker registry. You can use any other registry you prefer, but this one is very useful for temporary images.
{{< /alert >}}

```bash
docker build --build-arg="RELEASE=1.0.0" \ # This should either be the version of Kairos that you're based on, or your own version
--build-arg="BASE_IMAGE=ubuntu-full-firmware" \
--build-arg="VARIANT=core" \ # If you choose standard, you need to set K3S_VERSION in the same way
--build-arg="FLAVOR=ubuntu" \
--build-arg="FLAVOR_RELEASE=24.04" \
-t ttl.sh/kairos-radxa-uki:24h \
-f images/Dockerfile.kairos-ubuntu ./images
```

---

```bash
docker build -t kairos-radxa -f images/Dockerfile.kairos-radxa ./images/
```

```bash
MY_ORG="Acme Corp"
# Generate the keys
docker run -v $PWD/keys:/work/keys -ti --rm quay.io/kairos/osbuilder-tools:latest genkey "$MY_ORG" --expiration-in-days 365 -o /work/keys
```

```bash
CONTAINER_IMAGE=ttl.sh/kairos-radxa:24h
docker run -ti --rm -v $PWD/build:/result \
-v $PWD/keys/:/keys \
-v /var/run/docker.sock:/var/run/docker.sock \
quay.io/kairos/osbuilder-tools:latest build-uki $CONTAINER_IMAGE -t iso -d /result/ -k /keys
```


## Create a bootable image and flash it to the Radxa X4

Follow the instructions on https://kairos.io/docs/installation/trustedboot/ to create a bootable image.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can add the full steps here for reference and make it a e2e example, wdyt? docs mutate and would be nice to give people something replicate.

We can also merge as-is and improve later, when at it I will update the article, or whoever gets at it first :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mudler makes sense, I was hesitating between doing one and the other, I'll get back to it as soon as I'm in front of my desk


## Install LocalAI

1. Log into the machine with the user you created.
2. Download the [LocalAI binary](https://github.com/mudler/LocalAI/releases/download/v2.22.1/local-ai-Linux-x86_64)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use a bundle in teh cc directly? https://github.com/kairos-io/community-bundles/tree/main/LocalAI

3. Make it executable with `chmod +x local-ai-Linux-x86_64`
4. Run `./local-ai-Linux-x86_64` to start the LocalAI service

Now you can go to the IP:8080 of your Radxa X4 and install the models you need and chat with the LocalAI service.