Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
igorpecovnik committed Jan 16, 2025
1 parent 7895208 commit 3e0fd27
Showing 1 changed file with 73 additions and 59 deletions.
132 changes: 73 additions & 59 deletions tools/modules/system/module_armbian_kvmtest.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#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 restore list help"
["module_armbian_kvmtest,example"]="install remove save restore list help"
["module_armbian_kvmtest,port"]=""
["module_armbian_kvmtest,status"]="Active"
["module_armbian_kvmtest,arch"]=""
["module_armbian_kvmtest,arch"]="x86-64"
)
#
# Module deploy Armbian QEMU KVM instances
Expand All @@ -19,49 +18,48 @@ function module_armbian_kvmtest () {
# read additional parameters from command line
local parameter
IFS=' ' read -r -a parameter <<< "${1}"
for feature in instances provisioning firstconfig keyword arch distro bridge; do
for feature in instances provisioning firstconfig keyword arch kvmprefix network bridge memory vcpus; do
for selected in ${parameter[@]}; do
IFS='=' read -r -a split <<< "${selected}"
[[ ${split[0]} == $feature ]] && eval "$feature=${split[1]}"
done
done

local arch="${arch:-x86}" # VM architecture
local bridge="${bridge:-br0}" # Bridge number
local distro="${distro:-stable}" # Stable or rolling images
local network="${network:-default}"
if [[ -n "${bridge}" ]]; then network="bridge=${bridge}"; fi
local instances="${instances:-01}" # number of instances
local destination="${destination:-/var/lib/libvirt/images}"
local kvmprefix="${kvmprefix:-kvmtest}"
local memory="${memory:-3072}"
local vcpus="${vcpus:-2}"
local startingip="${startingip:-10.0.60.60}"
local keyword=$(echo $keyword | sed "s/_/|/g") # convert

if [[ ${distro} == stable ]]; then
# use point releases
qcowimages=(
https://imola.armbian.com/dl/uefi-${arch}/archive/Armbian_24.11.1_Uefi-${arch}_noble_current_6.6.60_minimal.img.qcow2.xz
https://imola.armbian.com/dl/uefi-${arch}/archive/Armbian_24.11.1_Uefi-${arch}_bookworm_current_6.6.60_minimal.img.qcow2.xz
)
else
# those targets we are updating every day
qcowimages=(
"https://dl.armbian.com/nightly/uefi-${arch}/Bullseye_current_minimal-qcow2"
"https://dl.armbian.com/nightly/uefi-${arch}/Bookworm_current_minimal-qcow2"
"https://dl.armbian.com/nightly/uefi-${arch}/Trixie_current_minimal-qcow2"
"https://dl.armbian.com/nightly/uefi-${arch}/Focal_current_minimal-qcow2"
"https://dl.armbian.com/nightly/uefi-${arch}/Jammy_current_minimal-qcow2"
"https://dl.armbian.com/nightly/uefi-${arch}/Noble_current_minimal-qcow2"
"https://dl.armbian.com/nightly/uefi-${arch}/Oracular_current_minimal-qcow2"
)
fi
qcowimages=(
"https://dl.armbian.com/nightly/uefi-${arch}/Bullseye_current_minimal-qcow2"
"https://dl.armbian.com/nightly/uefi-${arch}/Bookworm_current_minimal-qcow2"
"https://dl.armbian.com/nightly/uefi-${arch}/Trixie_current_minimal-qcow2"
"https://dl.armbian.com/nightly/uefi-${arch}/Focal_current_minimal-qcow2"
"https://dl.armbian.com/nightly/uefi-${arch}/Jammy_current_minimal-qcow2"
"https://dl.armbian.com/nightly/uefi-${arch}/Noble_current_minimal-qcow2"
"https://dl.armbian.com/nightly/uefi-${arch}/Oracular_current_minimal-qcow2"
)

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

case "${parameter[0]}" in

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

# Install portainer with KVM support and / KVM support only
# TBD - need to be added to armbian-config
pkg_install virtinst libvirt-daemon-system libvirt-clients qemu-kvm qemu-utils dnsmasq

# start network
virsh net-start default 2>/dev/null
virsh net-autostart default

if ! pkg_installed xz-utils; then
pkg_install xz-utils
Expand All @@ -72,7 +70,7 @@ function module_armbian_kvmtest () {
trap '{ rm -rf -- "$tempfolder"; }' EXIT
for qcowimage in ${qcowimages[@]}; do
[[ ! $qcowimage =~ ${keyword/,/|} ]] && continue # skip not needed ones
curl --progress-bar -L $qcowimage | xz -d > ${tempfolder}/$(basename $qcowimage | sed "s/.xz//g")
curl --progress-bar -L $qcowimage | xz -d > ${tempfolder}/$(basename $qcowimage | sed "s/-qcow2/.qcow2/g")
done

# we will mount qcow image
Expand All @@ -84,22 +82,20 @@ function module_armbian_kvmtest () {
for i in $(seq -w 01 $instances); do
for qcowimage in ${qcowimages[@]}; do
[[ ! $qcowimage =~ ${keyword/,/|} ]] && continue # skip not needed ones
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
local filename=$(basename $qcowimage | sed "s/-qcow2/.qcow2/g") # identify filename
local domain=$i-${kvmprefix}-$(basename $qcowimage | sed "s/-qcow2//g") # without qcow2
local image="$i"-"${kvmprefix}"-"${filename}" # get image name
cp ${tempfolder}/${filename} ${destination}/${image} # make a copy under different number
qemu-img resize ${destination}/${image} +10G # expand
qemu-nbd --connect=/dev/nbd0 ${destination}/${image} # 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
# Check if it reads
cat ${mounttempfolder}/etc/os-release | grep ARMBIAN_PRETTY_NAME | cut -d"=" -f2 | sed 's/"//g'
# Move this to main system
#mkdir -p ${mounttempfolder}/etc/shadow-maint/useradd-post.d/
#echo "#!/bin/sh" > ${mounttempfolder}/etc/shadow-maint/useradd-post.d/01groups
#echo "usermod -a -G docker \"\$SUBJECT\"" >> ${mounttempfolder}/etc/shadow-maint/useradd-post.d/01groups
#chmod a+x ${mounttempfolder}/etc/shadow-maint/useradd-post.d/01groups
# commands for changing follows here
local ip_address=$(awk -F\. '{ print $1"."$2"."$3"."$4+'$i' }' <<< $startingip )
# this part needs be changed in build framework
j=$(( j + 1 ))
local ip_address=$(awk -F\. '{ print $1"."$2"."$3"."$4+'$j' }' <<< $startingip )

# script that is executed at firstrun
if [[ -f ${provisioning} ]]; then
echo "INSTANCE=$i" > ${mounttempfolder}/root/provisioning.sh
Expand All @@ -108,6 +104,12 @@ function module_armbian_kvmtest () {
fi

# first config
if [[ ${firstconfig} ]]; then
echo "first config"
if [[ -f ${firstconfig} ]]; then
cat "${firstconfig}" >> ${mounttempfolder}/root/.not_logged_in_yet
fi
else
cat <<- EOF >> ${mounttempfolder}/root/.not_logged_in_yet
PRESET_NET_CHANGE_DEFAULTS="1"
PRESET_NET_ETHERNET_ENABLED="1"
Expand All @@ -126,32 +128,31 @@ function module_armbian_kvmtest () {
PRESET_DEFAULT_REALNAME="Armbian user"
PRESET_USER_SHELL="bash"
EOF
fi

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 \
--name ${domain} \
--memory ${memory} \
--vcpus 2 \
--autostart \
--disk ${destination}/$i-$(basename $qcowimage | sed "s/.xz//g"),bus=sata \
--disk ${destination}/${image},bus=sata \
--import \
--os-variant ubuntu24.04 \
--network bridge=${bridge} \
--network default \
--noautoconsole
# create snapshot of initial state
virsh snapshot-create-as --domain ${kvmprefix}-$image --name "initial-state"
done
done
rm -rf ${tempfolder} ${mounttempfolder}
;;
"${commands[1]}")
for i in {1..10}; do
for j in $(virsh list --all --name | grep ${kvmprefix}); 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
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
Expand All @@ -160,40 +161,53 @@ function module_armbian_kvmtest () {
fi
;;
"${commands[2]}")
for j in $(virsh list --all --name | grep ${kvmprefix}); do
# create snapshot
virsh snapshot-create-as --domain ${j} --name "initial-state"
done
;;
"${commands[3]}")
for j in $(virsh list --all --name | grep ${kvmprefix}); do
virsh shutdown $j 2>/dev/null
virsh snapshot-revert --domain $j --snapshotname "initial-state" --running
virsh shutdown $j 2>/dev/null
for i in {1..20}; do
sleep 2
if [[ "$(virsh domstate $j | grep "shut off")" == "shut off" ]]; then break; fi
done
virsh start $j 2>/dev/null
done
;;
"${commands[3]}")
"${commands[4]}")
for qcowimage in ${qcowimages[@]}; do
[[ ! $qcowimage =~ ${keyword/,/|} ]] && continue # skip not needed ones
echo $qcowimage
done
done
;;
"${commands[6]}")
"${commands[5]}")
echo -e "\nUsage: ${module_options["module_armbian_kvmtest,feature"]} <command> [switches]"
echo -e "Commands: ${module_options["module_armbian_kvmtest,example"]}"
echo -e "Available commands:\n"
echo -e "\tinstall\t- Install $title."
echo -e "\tremove\t- Remove all virtual machines $title."
echo -e "\trestore\t- Restore intial state of VM $title."
echo -e "\tsave\t- Save state of all VM $title."
echo -e "\trestore\t- Restore all saved state of VM $title."
echo -e "\tlist\t- Show available VM machines $title."
echo -e "\tkeyword\t- Use only certain. keyword=Jammy_Noble for example"
echo -e "\tstatus\t- Installation status $title."
echo -e "\nAvailable switches:\n"
echo -e "\tbridge\t- Network bridge br0,br1,...\n"
echo -e "\tkvmprefix\t- Name prefix (default = kvmtest)"
echo -e "\tmemory\t\t- KVM memory (default = 2048)"
echo -e "\tvcpus\t\t- Virtual CPUs (default = 2)"
echo -e "\tbridge\t\t- Use network bridge br0,br1,... instead of default inteface"
echo -e "\tinstances\t- Repetitions if more then 1"
echo -e "\tprovisioning\t- File of command that is executed at first run."
echo -e "\tfirstconfig\t- Armbian first config."
echo -e "\tkeyword\t\t- Select only certain image, example: Focal_Jammy VM image."
echo -e "\tarch\t\t- architecture of VM image."
echo -e "\tdistro\t\t- stable or rolling."
echo
;;
*)
${module_options["module_armbian_kvmtest,feature"]} ${commands[6]}
${module_options["module_armbian_kvmtest,feature"]} ${commands[5]}
;;
esac
}
#module_armbian_kvmtest $1 $2 "$3" "$4" "$5" "$6" "$7" "$8" "$9" "$10"

0 comments on commit 3e0fd27

Please sign in to comment.