Skip to content

Instantly share code, notes, and snippets.

@Jelle-S
Last active February 28, 2025 15:18
Show Gist options
  • Save Jelle-S/b0c5c9880532d10c936cc2771f4092e2 to your computer and use it in GitHub Desktop.
Save Jelle-S/b0c5c9880532d10c936cc2771f4092e2 to your computer and use it in GitHub Desktop.
Install Home Assistant on VirtualBox
# Installs Home Assistant on VirtualBox following https://www.home-assistant.io/installation/linux
# Install VirtualBox
sudo apt-get -y install virtualbox
# Create a new VM
VBoxManage createvm --name homeassistant --register
# Select OS type "Other Linux (64-bit)"
VBoxManage modifyvm homeassistant --ostype Linux_64
# Set RAM to 6GB (modify to your needs, TODO: make this an option, default value 2GB), video memory to 16MB (TODO: make this an option, default value 16MB)
VBoxManage modifyvm homeassistant --memory 6144 --vram 16
# 2 vCPUs (TODO: make this an option, default value 2)
VBoxManage modifyvm homeassistant --cpus 2
# Download the latest vdi image (TODO: Always use the latest version)
wget https://github.com/home-assistant/operating-system/releases/download/7.2/haos_ova-7.2.vdi.zip
sudo apt-get -y install unzip
unzip haos_ova-7.2.vdi.zip
rm haos_ova-7.2.vdi.zip
# Resize the harddisk to 100GB (TODO: make this an option, default value 32GB)
VBoxManage modifyhd "haos_ova-7.2.vdi" --resize 102400
# Move the vdi image to a location of choice (TODO: make this an option, default value TBD)
mv haos_ova-7.2.vdi VirtualBox\ VMs/homeassistant/
# Add the SATA controller
VBoxManage storagectl homeassistant --name "SATA Controller" --add sata --controller IntelAHCI
# Attach the vdi image (TODO: use the vdi path option)
VBoxManage storageattach homeassistant --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium ~/VirtualBox\ VMs/homeassistant/haos_ova-7.2.vdi
# Create the bridged adapter (TODO: Figure out a way to detect the netwerk adapter, or add it as an option)
VBoxManage modifyvm homeassistant --nic1 bridged --nictype1 82540EM --bridgeadapter1 enp2s0
# Enable EFI
VBoxManage modifyvm homeassistant --firmware efi
# Set Intel HD Audio as Audio controller
VBoxManage modifyvm homeassistant --audiocontroller hda
# Start Home Assistant headless
VBoxManage startvm homeassistant --type headless
# TODO: Figure out the guest box's IP address and output it
@sanandru
Copy link

auto start

nano ~/.bashrc

(sleep 20 && VBoxManage startvm homeassistant --type headless) &

@OldSurferDude
Copy link

"Figure out the guest box's IP address and output it"
If you enable RDP (Remoed Desktop) with:
vboxmanage modifyvm homeassistant --vrde on
(You'll probably have to reboot the virtual machine unless you ran this command before starting the vm)
Then you can RDP to the guest using the host's IP address. The RDP would have to originate from a machine with a GUI
Once in the guest at the ha> prompt, execute
net info
This should provide the guest's IP address

@OldSurferDude
Copy link

OldSurferDude commented Jan 25, 2024

The options of which you write could be setting bash variables at the beginning of the script.
One of them could be the name for the virtual machine, eg.
VM=HomeAssistant
and then replace homeassistant with $VM
If you want the wifi interface it can be extracted into the variable $HAIF with the command:
export HAIF=$(nmcli d | grep wifi | awk '{print $1;}')
I don't believe you need to have the --nictype when using bridgedadapter thus

# Create the bridged adapter (DONE: Figure out a way to detect the netwerk adapter, or add it as an option)
# Network medium, NM: **wifi** or **ethernet**
NM=wifi
VM=HomeAssistant
export HAIF=$(nmcli d | grep $NM | awk '{print $1;}')
VBoxManage modifyvm $VM --nic1 bridged --bridgeadapter1 $HAIF

@cn3m0
Copy link

cn3m0 commented Jan 16, 2025

#!/bin/bash

# Description:
# This script automates the deployment of a Home Assistant virtual machine (VM) in VirtualBox.
# It checks the latest Home Assistant OS release version from GitHub, compares it to the locally
# installed version, and creates a new VM if the latest version is not already installed.
#
# Key Features:
# - Dynamically fetches the latest Home Assistant OS release version using GitHub API.
# - Compares it with the currently installed local version.
# - Automatically downloads, extracts, and configures the latest version as a VirtualBox VM.
# - Ensures that VM configuration parameters (e.g., memory, CPUs) are pre-set.
# - Supports user confirmation before proceeding with download and deployment.

# Variables
repo="home-assistant/operating-system"
vm_dir="/home/user/vms"
network_adapter="wlp2s0" # Adjust according to your system's network adapter

# Fetch the latest and local release versions
latest_release=$(curl --silent "https://api.github.com/repos/${repo}/releases/latest" | jq -r .tag_name | sed 's/^v//')
local_release=$(VBoxManage list vms | grep -oP 'ha_\K[0-9]+\.[0-9]+')

# Display the current local and latest versions
echo "Current local version: ${local_release:-No local version found}" 
echo "Latest version on GitHub: ${latest_release}"

# Prompt user for confirmation
read -p "Do you want to download and install the latest version (${latest_release})? (y/n): " confirm
if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then
  echo "Operation canceled by user."
  exit 0
fi

# Exit if the local version is already up-to-date
if [[ "$local_release" == "$latest_release" ]]; then
  echo "The local version is already up-to-date."
  exit 0
fi

# Set VM name and directory
vm_name="ha_${latest_release}"
id=$(date +%Y%m%d-%H%M%S)
mkdir -p "${vm_dir}/${vm_name}"

# Create and configure the new VM
VBoxManage createvm --name ${vm_name} --register
VBoxManage modifyvm ${vm_name} --ostype Linux_64
VBoxManage modifyvm ${vm_name} --memory 6144 --vram 16
VBoxManage modifyvm ${vm_name} --cpus 2
VBoxManage modifyvm ${vm_name} --nic1 bridged --nictype1 82540EM --bridgeadapter1 ${network_adapter}
VBoxManage modifyvm ${vm_name} --firmware efi
VBoxManage modifyvm ${vm_name} --audiocontroller hda
VBoxManage modifyvm ${vm_name} --boot1 disk --boot2 none --boot3 none --boot4 none

# Download the release file
echo "Downloading the release image..."
cd /tmp
wget "https://github.com/home-assistant/operating-system/releases/download/${latest_release}/haos_ova-${latest_release}.vdi.zip"

# Extract and clean up
echo "Extracting the release image..."
unzip "haos_ova-${latest_release}.vdi.zip"
rm "haos_ova-${latest_release}.vdi.zip"

# Move the VDI file with a unique ID
echo "Moving the VDI file to the VM directory..."
mv "haos_ova-${latest_release}.vdi" "${vm_dir}/${vm_name}/haos_ova-${latest_release}-${id}.vdi"

# Configure the VM's storage controller and attach the VDI file
echo "Configuring VM storage controllers..."
VBoxManage storagectl ${vm_name} --name "SATA Controller" --add sata --controller IntelAHCI
VBoxManage storageattach ${vm_name} --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium "${vm_dir}/${vm_name}/haos_ova-${latest_release}-${id}.vdi"

# Start the VM
echo "Starting the VM..."
VBoxManage startvm ${vm_name} --type headless

echo "The Home Assistant VM ${vm_name} has been successfully created and started."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment