Skip to content

Instantly share code, notes, and snippets.

@Taurolyon
Last active February 3, 2024 07:46
Show Gist options
  • Save Taurolyon/fc5946ff40ece3d826bf5d4854470cb2 to your computer and use it in GitHub Desktop.
Save Taurolyon/fc5946ff40ece3d826bf5d4854470cb2 to your computer and use it in GitHub Desktop.
bash script to find and download the latest version of ROOTer (an OpenWRT derivative), and install it as a UEFI Proxmox VM.
#!/bin/bash
# by Jay M -- @Taurolyon
# Ref download: http://www.aturnofthenut.com/builds/GoldenOrb/x86-64-GO2023-09-01.zip
# inspired by https://computingforgeeks.com/install-and-configure-openwrt-vm-on-proxmox-ve/
set -o errexit
set -o nounset
set -o pipefail
log() {
local level=$1
local message=$2
local timestamp
timestamp=$(date +"%Y-%m-%d %T")
echo "$timestamp [$level] $message"
}
check_dependencies() {
local dependencies=(curl wget unzip find cp pvesh qm basename)
for cmd in "${dependencies[@]}"; do
if ! command -v "$cmd" &> /dev/null; then
log "ERROR" "Command not found in PATH: $cmd"
exit 1
fi
done
}
rootcheck() {
if [ "$EUID" -ne 0 ]; then
log "ERROR" "This script must be run as root!!"
exit 1
else
log "INFO" "User has elevated privilages; running script."
fi
}
create_proxmox_vm() {
local image=$1
local vm_name="Rooter"
local vm_id
vm_id=$(pvesh get /cluster/nextid)
local ram=2048
local cores=1
local bridge=vmbr1
local storage=local
log "INFO" "Creating Proxmox VM with ID $vm_id..."
qm create "$vm_id" --name "$vm_name" --memory "$ram" --cores "$cores" --cpu cputype=kvm64 --net0 virtio,bridge="$bridge" --bios ovmf --machine q35
qm importdisk "$vm_id" "$image" "$storage" --format qcow2
qm set "$vm_id" --scsihw virtio-scsi-pci --virtio0 "$storage:$vm_id/vm-$vm_id-disk-0.qcow2"
qm set "$vm_id" --efidisk0 "$storage:0,format=qcow2,efitype=4m"
qm set "$vm_id" --serial0 socket --vga std # serial0
qm set "$vm_id" --boot c --bootdisk virtio0
# Uncomment to set VM to start on boot
# qm set "$vm_id" --onboot 1
log "INFO" "Proxmox VM $vm_id created successfully."
}
download_image() {
local url="http://www.aturnofthenut.com/builds/GoldenOrb/"
local file_pattern='x86-64[^"]*.zip'
local file_url
local dir_name
file_url=$(curl -s "$url" | grep -io "$file_pattern" | head -n 1)
if [[ -z $file_url ]]; then
log "ERROR" "No file found on the website. Please check the URL or pattern."
exit 1
fi
log "INFO" "Downloading $file_url..."
wget -O "$file_url" "${url}${file_url}" || { log "ERROR" "Failed to download $file_url"; exit 1; }
log "INFO" "Downloaded $file_url!"
dir_name=$(basename "$file_url" .zip)
log "INFO" "Unzipping to $dir_name..."
mkdir -p "$dir_name"
unzip -o "$file_url" -d "$dir_name" || { log "ERROR" "Failed to unzip $file_url"; exit 1; }
log "INFO" "Unzipped $file_url!"
log "INFO" "Locating image and copying to working directory"
local img_file
img_file=$(find "$dir_name" -type f -name 'x86-64-UEFI*.img' | head -n 1)
if [[ -n $img_file ]]; then
# cp "$img_file" "rooter.img" || { log "ERROR" "Failed to copy image file"; exit 1; }
# log "INFO" "Image file copied to working directory as rooter.img"
qemu-img convert -f raw -O qcow2 "$img_file" "rooter.qcow2" || { log "ERROR" "Failed to convert image file"; exit 1; }
log "INFO" "Image file converted to qcow2 and copied to working directory as rooter.qcow2"
else
log "ERROR" "No img file found starting with x86-64-UEFI"
exit 1
fi
# CLEANUP
rm ./"$file_url"
rm -r ./"$dir_name"
}
main() {
rootcheck
check_dependencies
log "INFO" "Downloading Image..."
download_image
log "INFO" "Creating Proxmox VM with the image..."
create_proxmox_vm "rooter.qcow2"
# Final cleanup
rm ./rooter.qcow2
log "INFO" "Process completed successfully."
}
main "$@"
@Taurolyon
Copy link
Author

Taurolyon commented Feb 3, 2024

While this downloads and installs the VM, the VM currently does not boot. Fixed

kvm: -device virtio-blk-pci,drive=drive-virtio0,id=virtio0,bus=pci.0,addr=0xa,bootindex=100: Cannot get 'write' permission without 'resize': Image size is not a multiple of request alignment
TASK ERROR: start failed: QEMU exited with code 1

@Taurolyon
Copy link
Author

Taurolyon commented Feb 3, 2024

Added a fix to convert the img to qcow2. this avoids the alignment error with RAW.

TODO:

  • 1. user is root check
  • 2. remove redundant copy function ( x86-64-UEFI (...) .img to ../rooter.img )
    instead use qemu-img convert to automatically convert to qcow2 and save file to ../
  • 3. clean up downloaded files and unzip directory.

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