Update vmcreate1.sh

This commit is contained in:
2025-10-25 14:44:53 +00:00
parent aeb14e4b67
commit bab920dbc3

View File

@@ -4,6 +4,7 @@
# Сделать добавление кастомного полльзователя с паролем # Сделать добавление кастомного полльзователя с паролем
# Сделать замену тега влан # Сделать замену тега влан
# Сделать проверку наличия ВМ перед клонированием. Если создать ВМ не удалось, скипаем, а не выходим. # Сделать проверку наличия ВМ перед клонированием. Если создать ВМ не удалось, скипаем, а не выходим.
# ИЗУЧИТЬ!!!! /usr/share/pve-docs/examples/guest-example-hookscript.pl
storage="syno-tigra" storage="syno-tigra"
path="/mnt/pve/$storage/snippets" path="/mnt/pve/$storage/snippets"
@@ -16,12 +17,12 @@ show_help()
echo 'Arguments:' echo 'Arguments:'
echo '-h - show this help;' echo '-h - show this help;'
echo '-a - add hosts to HA affinity rules;' echo '-a - add hosts to HA affinity rules;'
echo "-k - embed custom public key or create new if 'pub_keyfile' not specified" echo "-k - embed custom public key or create new if `pub_keyfile` not specified"
echo "-u - specify user instead of default 'root'" echo "-u - specify user instead of default `root`"
echo "-p - UNDER CONSTRUCTION!!!!!! specify password instead of default" echo "-p - UNDER CONSTRUCTION!!!!!! specify password instead of default"
echo "-f - get IP addresses and Hostnames from 'flilename';" echo "-f - get IP addresses and Hostnames from `flilename`;"
echo "\nIf file not specified, script will use arguments as a list of IP addresses." 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 "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.*.*" echo "IP address should be 10.10.*.*"
@@ -41,7 +42,7 @@ done
# Если файл не задан, но флаг есть # Если файл не задан, но флаг есть
if [[ $# -eq 0 && -v $file && ! -f $file ]]; then if [[ $# -eq 0 && -v $file && ! -f $file ]]; then
input=y input=y
read -p "File $file does not exist. Use default 'hosts' file? Y/n: " input read -p "File `$file` does not exist. Use default `hosts` file? Y/n: " input
case $input in case $input in
y) file=hosts;; y) file=hosts;;
Y) file=hosts;; Y) file=hosts;;
@@ -49,7 +50,7 @@ if [[ $# -eq 0 && -v $file && ! -f $file ]]; then
N) read -p "Enter file name: " file;; N) read -p "Enter file name: " file;;
*) echo "Use '-f' flag for help"; exit 0;; *) echo "Use '-f' flag for help"; exit 0;;
esac esac
if [ ! -f hosts ]; then echo "File 'hosts' does not exist. Exiting."; exit 0; fi if [ ! -f hosts ]; then echo "File `hosts` does not exist. Exiting."; exit 0; fi
fi fi
# Если заданы аргументы И файл # Если заданы аргументы И файл
@@ -93,7 +94,7 @@ if [ $# -ne 0 ] then
printf "%03d\n" "$(echo -n $arg | cut -d '.' -f 4)" >> ./hosts.tmp printf "%03d\n" "$(echo -n $arg | cut -d '.' -f 4)" >> ./hosts.tmp
echo >> ./hosts.tmp echo >> ./hosts.tmp
else else
echo "Argument $arg is not a valid IPv4 address (10.10.*.*). Using next argument." echo "Argument `$arg` is not a valid IPv4 address (10.10.*.*). Using next argument."
fi fi
done done
fi fi
@@ -139,7 +140,7 @@ if [ -v pubkey ]; then
fi fi
else else
pubkey="key.pub" pubkey="key.pub"
if [[ ! -e key.pub ]]; then echo "'key.pub' does not exist. Please use '-k' flag to specify public key."; exit 0; fi if [[ ! -e key.pub ]]; then echo "`key.pub` does not exist. Please use `-k` flag to specify public key."; exit 0; fi
fi fi
keypair_generate (){ keypair_generate (){
@@ -189,7 +190,6 @@ do
qm set $vmnum --ipconfig0 ip=$ip/24,gw=10.10.35.1 qm set $vmnum --ipconfig0 ip=$ip/24,gw=10.10.35.1
qm cloudinit update $vmnum qm cloudinit update $vmnum
qm start $vmnum qm start $vmnum
echo "VMs from $1 successfully created"
if [ -v harule ] if [ -v harule ]
then then
ha-manager add vm:$vmnum --state started --max_relocate 2 ha-manager add vm:$vmnum --state started --max_relocate 2
@@ -203,6 +203,6 @@ done
rm hosts.tmp rm hosts.tmp
if [ -v $privkey ] if [ -v $privkey ]
then then
echo "\nGenerated private key $privkey" echo "\nGenerated private key `$privkey`"
echo "SAVE IT IMMEDIATELY!!!" echo "SAVE IT IMMEDIATELY!!!"
fi fi