Skip to content

Commit

Permalink
Armbian internals: deploy KVM instances
Browse files Browse the repository at this point in the history
  • Loading branch information
igorpecovnik committed Jan 10, 2025
1 parent 6b22dff commit a79b3d5
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 172 deletions.
122 changes: 122 additions & 0 deletions tools/modules/system/module_armbian_kvmtest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#declare -A module_options
module_options+=(
["module_armbian_kvmtest,author"]="@igorpecovnik"
["module_armbian_kvmtest,feature"]="module_armbian_kvmtest"
["module_armbian_kvmtest,desc"]="Deploy Armbian KVM instances"
["module_armbian_kvmtest,example"]="install remove help"
["module_armbian_kvmtest,port"]=""
["module_armbian_kvmtest,status"]="Active"
["module_armbian_kvmtest,arch"]=""
)
#
# Module Armbian self hosted Github runners
#
function module_armbian_kvmtest () {

local title="kvmtest"
local condition=$(which "$title" 2>/dev/null)

# read parameters from command install
local parameter
IFS=' ' read -r -a parameter <<< "${1}"
for feature in instances stop; do
for selected in ${parameter[@]}; do
IFS='=' read -r -a split <<< "${selected}"
[[ ${split[0]} == $feature ]] && eval "$feature=${split[1]}"
done
done

# default values if not defined
local instances="${instances:-01}"
local destination="${destination:-/var/lib/libvirt/images}"
local kvmprefix="${kvmprefix:-kvmtest}"

# we can generate per org or per repo
local registration_url="${organisation}"
local prefix="orgs"
if [[ -n "${owner}" && -n "${repository}" ]]; then
registration_url="${owner}/${repository}"
prefix=repos
fi

# we will mount qcow image
modprobe nbd max_part=8

local commands
IFS=' ' read -r -a commands <<< "${module_options["module_armbian_kvmtest,example"]}"

case "${parameter[0]}" in

"${commands[0]}")
# Install portainer with KVM support
# TBD

# download images
tempfolder=$(mktemp -d)
qcowimages=(
"https://imola.armbian.com/dl/uefi-x86/archive/Armbian_24.11.1_Uefi-x86_noble_current_6.6.60_minimal.img.qcow2.xz"
"https://imola.armbian.com/dl/uefi-x86/archive/Armbian_24.11.1_Uefi-x86_bookworm_current_6.6.60_minimal.img.qcow2.xz"
)
for qcowimage in ${qcowimages[@]}; do
curl --progress-bar -L $qcowimage | xz -d > ${tempfolder}/$(basename $qcowimage | sed "s/.xz//g")
done

mounttempfolder=$(mktemp -d)
# Deploy several instances
for i in $(seq -w 01 $instances); do
for qcowimage in ${qcowimages[@]}; do
local image=$i-$(basename $qcowimage | sed "s/.xz//g" | cut -d"_" -f4,5) # get image name
cp ${tempfolder}/$(basename $qcowimage | sed "s/.xz//g") ${destination}/$i-$(basename $qcowimage | sed "s/.xz//g") # make a copy
qemu-img resize ${destination}/$i-$(basename $qcowimage | sed "s/.xz//g") +10G # expand
qemu-nbd --connect=/dev/nbd0 ${destination}/$i-$(basename $qcowimage | sed "s/.xz//g") # connect to qemu image
printf "fix\n" | sudo parted ---pretend-input-tty /dev/nbd0 print >/dev/null # fix resize
mount /dev/nbd0p3 ${mounttempfolder} # 3rd partition on uefi images is rootfs
cat ${mounttempfolder}/etc/os-release | grep ARMBIAN_PRETTY_NAME | cut -d"=" -f2 | sed 's/"//g'
# commands for changing follows here
umount /dev/nbd0p3 # unmount
qemu-nbd --disconnect /dev/nbd0 >/dev/null # disconnect from qemu image
# install and start VM
virt-install \
--name ${kvmprefix}-$image \
--memory 3072 \
--vcpus 4 \
--autostart \
--disk ${destination}/$i-$(basename $qcowimage | sed "s/.xz//g"),bus=sata \
--import \
--os-variant ubuntu24.04 \
--network bridge=br1 \
--noautoconsole
done
done
rm -rf ${tempfolder} ${mounttempfolder}
;;
"${commands[1]}")
for i in {1..10}; do
for j in $(virsh list --all --name | grep ${kvmprefix}); do
virsh shutdown $j 2>/dev/null
for snapshot in $(virsh snapshot-list $j | tail -n +3 | head -n -1 | cut -d' ' -f2); do virsh snapshot-delete $j $snapshot; done
done
sleep 2
if [[ -z "$(virsh list --name | grep ${kvmprefix})" ]]; then break; fi
done
if [[ $i -lt 10 ]]; then
for j in $(virsh list --all --name | grep ${kvmprefix}); do virsh undefine $j; done
fi
;;
"${commands[6]}")
echo -e "\nUsage: ${module_options["module_armbian_kvmtest,feature"]} <command>"
echo -e "Commands: ${module_options["module_armbian_kvmtest,example"]}"
echo "Available commands:"
echo -e "\tinstall\t- Install $title. Parameters:"
echo -e "\t\t gh_token runner_name start stop label_primary label_secondary organisation owner repository"
echo -e "\t\t organisation and owner + repository are mutually exclusive"
echo -e "\tremove\t- Remove $title."
echo -e "\tstatus\t- Installation status $title."
echo
;;
*)
${module_options["module_armbian_kvmtest,feature"]} ${commands[6]}
;;
esac
}
#module_armbian_kvmtest $1 "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "$10"
172 changes: 0 additions & 172 deletions tools/modules/system/module_armbian_runners.sh

This file was deleted.

0 comments on commit a79b3d5

Please sign in to comment.