Files
vmcreate/vmcreate1.sh
2025-10-24 15:06:10 +00:00

134 lines
4.1 KiB
Bash

#!/bin/bash
show_help()
echo 'Usage:'
echo 'vmcreate [-h][-a rule_name][-k pub_keyfile][-f filename]'
echo 'Arguments:'
echo '-h - show this help;'
echo '-a - add hosts to HA affinity rules;'
echo "-f - get IP addresses and Hostnames from 'flilename';"
echo "-k - embed custom public key or create new if 'pub_keyfile' not specified"
echo "\nIf file not specified, script will use arguments as a list of IP addresses."
echo "In this case Hostname will be inherited from 2 last IP octets. Example for 10.10.35.20: 'vm035020'."
echo "IP address should be 10.10.*.*"
# Обрабатываем опции
while getopts "a:f:h" opt; do
case $opt in
a) harule=$OPTARG; ;;
f) file="$OPTARG" ;;
h) show_help ; exit 0;;
# *) noopts=true
esac
done
if [[ $# -eq 0 && -v $file && ! -f $file ]]; then
local input=y
echo "File $file does not exist. Use default 'hosts' file? Y/n: "
read input
case $input in
y) file=hosts;;
Y) file=hosts;;
n) read -p "Enter file name: " file;;
N) read -p "Enter file name: " file;;
*) echo "Use '-f' flag for help"; exit 0;;
esac
if [ ! -f hosts ]; then echo "File 'hosts' does not exist. Exiting."; exit 0; fi
fi
# Если заданы аргументы И файл
if [[ $# -ne 0 && -v file ]]; then echo "Please enter file OR arguments. Use '-f' flag for help"; exit 0; fi
# Если заданы аргументы, то создаем временный файл
if [ $# -ne 0 ] then
touch hosts.tmp
for arg in "$@"; do
ipcheck "$arg"
if [ $? -eq 0 ]
then
echo -n "$arg " >> ./hosts.tmp
echo -n "vm" >> ./hosts.tmp
printf "%05d\n" "$(echo -n $arg | cut -d '.' -f 3)" >> ./hosts.tmp
printf "%05d\n" "$(echo -n $arg | cut -d '.' -f 4)" >> ./hosts.tmp
echo >> ./hosts.tmp
else
echo "Argument $arg is not valid IP address (10.10.*.*). Using next argument."
fi
done
fi
function ip_check() {
# Функция написана гуглом
local ip=$1
local stat=1
oldIFS=$IFS
# Check if the IP matches the general IPv4 pattern
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
# Split the IP into octets
IFS='.' read -r -a octets <<< "$ip"
# Check if each octet is within the valid range (0-255)
if [[ ${octets[0]} -eq 10 && ${octets[1]} -eq 10 \
&& ${octets[2]} -le 254 && ${octets[3]} -le 254 ]]; then
stat=0
fi
fi
return $stat
IFS=$oldIFS
}
function hostame_generate(){
}
keypair_generate (){
#указать в конце функции переменную для вывода названия приватного ключа
privkey=
}
embed_key(){
#
}
for line in $(<$hosts)
do
ip=$(echo $line | cut -d ' ' -f 1)
hostname=$(echo $line | cut -d ' ' -f 2)
vmnum=$(echo $ip | cut -d '.' -f 3,4 | sed 's/\.//')
qm clone 5000 $vmnum --name $hostname --full
if [ $? -eq 0 ]
then echo "clone OK"
else "clone ERROR"; exit 1
fi
qm set $vmnum --tags 3,gfx
## preparing custom CloudInit snippets
cp ./user.yaml /mnt/pve/syno-tigra/snippets/${vmnum}_user.yaml
sed -i "s/HOSTNAME/$hostname/g" /mnt/pve/syno-tigra/snippets/${vmnum}_user.yaml
qm resize $vmnum scsi0 +50G
qm set $vmnum --cicustom "user=syno-tigra:snippets/${vmnum}_user.yaml"
qm set $vmnum --ipconfig0 ip=$ip/24,gw=10.10.35.1
qm cloudinit update $vmnum
qm start $vmnum
echo "VMs from $1 successfully created"
if [ haadd ]
then
ha-manager add vm:$vmnum --state started --max_relocate 2
ha-manager rules add node-affinity $ha_rule --resources vm:$vmnum --nodes pve1,pve2,pve3 --strict 1
# ОБЯЗАТЕЛЬНО ПРОВЕРИТЬ КОМАНДУ!!!
fi
echo "HA rules added"
done
# Финальная часть
if [ -e hosts.tmp]
then
rm hosts.tmp
fi
if [ -v $privkey ]
then
echo "Generated private key $privkey"
echo "SAVE IT IMMEDIATELY!!!"
fi