CubeOS in qemu

#!/bin/bash

Script to install eWeLink CUBE OS in QEMU for Raspberry Pi 5

Configuration: 2GB RAM, native aarch64 KVM acceleration, auto-start on boot

set -e

1. Install required dependencies

echo “[*] Updating packages and installing QEMU dependencies…”
sudo apt update
sudo apt install -y qemu-system-arm qemu-utils wget xz-utils curl

2. Create the working directory

WORKDIR=“/opt/cubeos_qemu”
sudo mkdir -p “$WORKDIR”
sudo chown -R $USER:$USER “$WORKDIR”
cd “$WORKDIR”

3. Fetch the latest CUBE OS image download link from GitHub Releases

echo “[*] Fetching the latest CUBE OS release URL…”
LATEST_URL=$(curl -s https://github.com | grep “browser_download_url” | grep “raspberrypi” | head -n 1 | cut -d ‘"’ -f 4)

if [ -z “$LATEST_URL” ]; then

Fallback URL if GitHub API rate limit is exceeded

LATEST_URL=“https://github.com
fi

IMG_XZ=$(basename “$LATEST_URL”)
IMG_RAW=“${IMG_XZ%.xz}”

4. Download and extract the image

if [ ! -f “$IMG_RAW” ]; then
echo “[] Downloading CUBE OS…"
wget -O “$IMG_XZ” “$LATEST_URL”
echo "[
] Extracting image file…”
unxz “$IMG_XZ”
else
echo “[+] Image file $IMG_RAW already exists. Skipping download.”
fi

5. Prepare UEFI firmware for ARM64 (Required for QEMU -M virt)

if [ ! -f “QEMU_EFI.fd” ]; then
echo “[] Setting up UEFI firmware…"
if [ -f “/usr/share/AAVMF/AAVMF_CODE.fd” ]; then
cp /usr/share/AAVMF/AAVMF_CODE.fd QEMU_EFI.fd
elif [ -f “/usr/share/qemu-efi-aarch64/QEMU_EFI.fd” ]; then
cp /usr/share/qemu-efi-aarch64/QEMU_EFI.fd QEMU_EFI.fd
else
echo "[
] Downloading fallback QEMU_EFI.fd…”
wget -O QEMU_EFI.fd “https://linaro.org
fi
fi

6. Resize the virtual disk by 10GB to provide storage for Docker add-ons

if [ ! -f “.disk_resized” ]; then
echo “[*] Resizing virtual disk…”
qemu-img resize “$IMG_RAW” +10G
touch .disk_resized
fi

7. Generate the standalone execution script for QEMU (2GB RAM configuration)

echo “[*] Generating launch script…”
cat << ‘EOF’ > start_vm.sh
#!/bin/bash
exec /usr/bin/qemu-system-aarch64
-cpu host
-enable-kvm
-M virt
-m 2048
-smp 4
-bios /opt/cubeos_qemu/QEMU_EFI.fd
-device virtio-blk-device,drive=hd0
-drive if=none,id=hd0,file=/opt/cubeos_qemu/raspberrypi4_64_prod.img,format=raw
-net dev,netdev=net0
-netdev user,id=net0,hostfwd=tcp::8080-:80,hostfwd=tcp::8123-:8123
-nographic
EOF

chmod +x start_vm.sh

8. Create and register the systemd service for persistent auto-start on reboot

echo “[*] Creating systemd service for auto-start on boot…”
sudo cat << EOF > /etc/systemd/system/cubeos.service
[Unit]
Description=Sonoff CubeOS QEMU Virtual Machine
After=network.target

[Service]
Type=simple
User=$USER
WorkingDirectory=/opt/cubeos_qemu
ExecStart=/opt/cubeos_qemu/start_vm.sh
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
EOF

Reload systemd configuration and enable the service

sudo systemctl daemon-reload
sudo systemctl enable cubeos.service

echo “[+] SETUP COMPLETE!”
echo “[*] Starting CUBE OS background service…”
sudo systemctl start cubeos.service

echo “[+] Service is running. Access the web interface at http://YOUR_RPI_IP:8080”

1 Like

Genius idea. Guess they went for Virtual Box because it is cross platform and so a mini-PC with windows could run it. I use virtual machine manager but was wondering about adding another VM for Cube.
Was thinking about doing it on my desktop computer which isn’t on all the time but a raspberry pi 5 is enough? I have a pi 5 4GB upstairs I rarely use and don’t run much on. If I understand the script you give the VM 2GB? 2GB left would be fine for the little I do I am pretty sure.

I tried HA in Docker and it ran smoothly. QEMU uses KVM, so it should perform better than Docker. I have a Raspberry Pi 5 with 4GB RAM where other services are running as well. I am using 1 GB RAM for CubeOS . Today, people want everything under one interface, which is why HA is so popular. First of all, until NSPP is fully local, I have to move in this direction. On Raspberry Pi, I can run Frigate, CubeOS, file sharing, torrents, and other specific requirements — the iHost cannot do that, even though I received several reasonable suggestions.

If I offer a device that can handle everything, customers are willing to pay extra. I had to create my own distribution for Raspberry Pi, where both I and the customer have service access. This is just another stage in expanding services. And unlike HA, CubeOS can even be operated by someone who is technically illite.