#!/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”
