File: //etc/xenicastart.sh
#!/bin/bash
# Ubuntu 20.04
# Xenixa autoStart file V1.0
# @Servarica.com - 2021
#
#
#####################################################################################################
#Start Main Program ..
#####################################################################################################
#
#
DATE=$(date -u +"%d-%m-%Y_%H-%M")
LOG_FILE="/opt/XenicaStart_""$DATE"".log"
# Prepare Disks if needed
if [ -f "/opt/diskexpanded.log" ]; then
partprobe /dev/xvda
resize2fs /dev/xvda3
fi
######################################################################
#run Boot Commands
i=0
xenica_list=($(xenstore-list vm-data))
for item in "${xenica_list[@]}"; do
if [[ ${item} == *"xenica_script"* ]]; then
# save the script to a file:
xenica_script=""
xenica_script=$(xenstore-read vm-data/${item})
if [[ -n "$xenica_script" ]]; then
if [[ "$i" -eq "0" ]]; then
# Log output to file and output on screen at the same time.
touch $LOG_FILE
exec > >(tee -ai $LOG_FILE) 2>&1
date
fi
echo;echo;echo
date -u +"Time: %H:%M:%S"
echo "Start Executing BootCommand: $item :"
echo "----------------------------------------------"
cd /opt/ || exit
filename="${item}.sh"
# create file named as 'xenica_script_*.sh':
touch "$filename"
# save content to it:
echo "$xenica_script" >"$filename"
# prepare file:
chmod +x "$filename"
# Now empty xenstore record before Run (to avoid infinit loop of reboot)
xenstore-write vm-data/${item} ""
xenstore-rm vm-data/${item}
# run it :
./"$filename"
# delete the file
rm -f "$filename"
echo "=============================================="
fi
fi
i=$((i+1))
done
echo;echo
#######################################################################
# ReBoot if needed
if [[ -f "/opt/need_to_reboot.log" ]]; then
echo "ReBooting now ..."
rm /opt/need_to_reboot.log
reboot
fi