Update vmcreate1.sh

This commit is contained in:
2025-10-26 07:29:24 +00:00
parent c63483cb01
commit 15300286d1

View File

@@ -20,7 +20,7 @@ show_help()
echo '-a - add hosts to HA affinity rules;'
echo "-k - embed custom public key or create new if `pub_keyfile` not specified"
echo "-u - specify user instead of default `root`"
echo "-p - UNDER CONSTRUCTION!!!!!! specify password instead of default"
echo "-p - specify password instead of default"
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 "In this case Hostname will be inherited from 2 last IP octets. Example for 10.10.35.20: `vm035020`."
@@ -46,7 +46,7 @@ if [[ -v pubkey ]]; then echo "Public key: $pubkey"; fi
if [[ -v username ]]; then echo "User: $file"; fi
# Если файл не задан, но флаг есть
if [[ $# -eq 0 && -v $file && ! -f $file ]]; then
if [[ $# -eq 0 && -v "$file" && ! -f "$file" ]]; then
input=y
read -p "File `$file` does not exist. Use default `hosts` file? Y/n: " input
case $input in
@@ -56,22 +56,22 @@ if [[ $# -eq 0 && -v $file && ! -f $file ]]; then
N) read -p "Enter file name: " file;;
*) echo "Use '-f' flag for help"; exit 0;;
esac
if [ ! -f file ]; then echo "File `$file` does not exist. Exiting."; exit 1; fi
if [[ ! -f "$file" ]]; then echo "File `$file` does not exist. Exiting."; exit 1; fi
fi
# Если заданы аргументы И файл
if [[ $# -ne 0 && -v file ]]; then echo "Please enter file OR arguments. Use '-f' flag for help"; exit 2; fi
if [[ $# -ne 0 && -v "$file" ]]; then echo "Please enter file OR arguments. Use '-f' flag for help"; exit 2; fi
# Проверка файла на соответствие шаблону IPv4 адреса и hostname, создаём временный файл из валидных строк
if [ -e $file ]; then
if [[ -e "$file" ]]; then
touch hosts.tmp
echo -n "" > hosts.tmp
for line in $(<$file); do
for line in $(cat "$file"); do
ip=$(echo $line | cut -d ' ' -f 1)
hostname=$(echo $line | cut -d ' ' -f 2)
if [ $? -eq 0 ]; then
if [[ $? -eq 0 ]]; then
hostcheck "$hostname"
if [ $? -eq 0 ];
if [[ $? -eq 0 ]];
then
echo $line >> hosts.tmp
else
@@ -88,7 +88,7 @@ if [ -e $file ]; then
fi
# Если заданы аргументы, то создаем временный файл
if [ $# -ne 0 ] then
if [[ $# -ne 0 ]] then
touch hosts.tmp
echo -n "" > hosts.tmp
for arg in "$@"; do
@@ -152,32 +152,47 @@ if [ -v pubkey ]; then
if [[ ! -e key.pub ]]; then echo "`key.pub` does not exist. Please use `-k` flag to specify public key."; exit 7; fi
fi
###DEBUG 1
echo "File `hosts.tmp`:"
cat hosts.tmp; echo
keypair_generate (){
ssh-keygen -t rsa -N "" -f $privkey
pubkey="$privkey".pub
}
mksnippet (){
cp ./user.yaml ${path}${vmnum}_user.yaml
sed -i "s/HOSTNAME/$hostname/g" ${path}${vmnum}_user.yaml
if [[ -v $username ]];
then sed -i "s|user: root|user: ${username}\nsudo: ALL=(ALL) NOPASSWD:ALL|"
snippet="${path}${vmnum}_user.yaml"
cp ./user.yaml "$snippet"
sed -i "s/HOSTNAME/$hostname/g" "$snippet"
if [[ -n $username ]];
then sed -i "s|user: root|user: ${username}\nsudo: ALL=(ALL) NOPASSWD:ALL|" "$snippet"
fi
if [[ -v $password ]];
if [[ -n $password ]];
phash=$(cat $password | mkpasswd -m sha-256 -s)
then sed -i "s|.*password.*| - ${phash}|"
then sed -i "s|.*password.*| - ${phash}|" "$snippet"
fi
sed -i "s|.*ssh-rsa.*| - ${pubkey}|" ${path}${vmnum}_user.yaml
sed -i "s|.*ssh-rsa.*| - ${pubkey}|" "$snippet"
}
for line in $(cat hosts.tmp)
do
ip=$(echo $line | cut -d ' ' -f 1)
vlan=$(echo $ip | cut -d '.' -f 3)
if [[ vlan -eq 0 ]]; then mask=23; gw="10.10.0.1"; else mask=24; gw="10.10.${vlan}.1"; fi
if [[ vlan -eq 0 ]]; then mask=23; gw="10.10.0.1"; vlan=100; else mask=24; gw="10.10.${vlan}.1"; fi
hostname=$(echo $line | cut -d ' ' -f 2)
vmnum=$(echo $ip | cut -d '.' -f 3,4 | sed 's/\.//')
vmnum="$vlan(printf "%03d\n" "$(echo -n $ip | cut -d '.' -f 4)")"
if [[ $vlan -eq 0 ]]; then $vlan=100; fi
mksnippet
#### DEBUG 2
echo "IP: $ip"
echo "hostname: $hostname"
echo "VMID: $vmnum"
echo "VLAN: $vlan"
echo "Snippet:"
cat "$snippet"
read -p "Press Enter"
####
echo -n "Now cloning VM $vmnum from a template....."
qm clone 5000 $vmnum --name $hostname --full &>/dev/null
if [ $? -eq 0 ]
@@ -185,7 +200,6 @@ do
else "ERROR"; exit 8
fi
qm set $vmnum --tags $node,gfx
mksnippet
qm resize $vmnum scsi0 +50G
qm set $vmnum --cicustom "user=${storage}:snippets/${vmnum}_user.yaml"
qm set $vmnum --ipconfig0 ip=$ip/24,gw=10.10.35.1