Update vmcreate.sh

This commit is contained in:
2025-10-28 15:11:07 +00:00
parent 72991247a5
commit 173b3c9bf8

View File

@@ -98,7 +98,7 @@ mksnippet () {
fi fi
if [ $password ]; then if [ $password ]; then
phash=$(cat $password | mkpasswd -m sha-256 -s) phash=$(cat $password | mkpasswd -m sha-256 -s)
sed -i "s|.*password.*|password: ${phash}|" "$snippet" sed -i "s|.*password.*|password: ${password}|" "$snippet"
fi fi
if [ $pubkey ]; then if [ $pubkey ]; then
sed -i "s|.*ssh-rsa.*| - $(cat ${pubkey})|" "$snippet" sed -i "s|.*ssh-rsa.*| - $(cat ${pubkey})|" "$snippet"
@@ -246,12 +246,6 @@ do
fi fi
done done
### DEBUG 1
echo "DEBUG cat hosts.tmp:"
cat hosts.tmp; echo
###
# Подготовительные операции закончены, приступаем к выполнению. # Подготовительные операции закончены, приступаем к выполнению.
read -p "Script is ready to create $(cat hosts.tmp | wc -l) VMs. Press Enter" read -p "Script is ready to create $(cat hosts.tmp | wc -l) VMs. Press Enter"
@@ -270,16 +264,7 @@ do
hostname=$(echo -n $line | cut -d ';' -f 2) hostname=$(echo -n $line | cut -d ';' -f 2)
vmid=$(get_vmid "$ip") vmid=$(get_vmid "$ip")
mksnippet mksnippet
#### DEBUG 2 echo -n "Now cloning VM $vmid from a template....."
echo "IP: $ip"
echo "hostname: $hostname"
echo "VMID: $vmid"
echo "VLAN: $vlan"
echo "TAG: $tag"
echo "$snippet:"
cat "$snippet"
####
echo -n "Now cloning VM $vmnum from a template....."
qm clone 5000 $vmid --name $hostname --full > /dev/null qm clone 5000 $vmid --name $hostname --full > /dev/null
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "OK" echo "OK"
@@ -294,10 +279,8 @@ do
sed -i "s|tag=35|tag=$vlan|" /etc/pve/qemu-server/${vmid}.conf sed -i "s|tag=35|tag=$vlan|" /etc/pve/qemu-server/${vmid}.conf
# sed -i "s|vmbr0|vmbr1|" /etc/pve/qemu-server/${vmid}.conf # sed -i "s|vmbr0|vmbr1|" /etc/pve/qemu-server/${vmid}.conf
qm cloudinit update $vmid qm cloudinit update $vmid
### DEBUG echo "Snippet:"
echo "DEBUG: ${vmid}.conf" cat $snippet
cat /etc/pve/qemu-server/${vmid}.conf
###
# # Migrate VM to target node # # Migrate VM to target node
# echo -n "Migrating VM $vmid to pve${node}....." # echo -n "Migrating VM $vmid to pve${node}....."
# qm migrate $vmid pve${node} && # qm migrate $vmid pve${node} &&
@@ -307,12 +290,16 @@ do
# echo "ERROR" # echo "ERROR"
# exit 9 # exit 9
# fi # fi
# qm start $vmid ВРЕМЕННО ОТКЛЮЧЕНО read -p "Press enter to start VM"
qm start $vmid &&
if [ $? -eq 0 ]; then
echo "VM ${vmid} started successfully"
fi
if [ $harule ]; then if [ $harule ]; then
ha-manager add vm:$vmid --state started --max_relocate 2 ha-manager add vm:$vmid --state started --max_relocate 2 &&
ha-manager rules add node-affinity $harule --resources vm:$vmid --nodes pve1,pve2,pve3 --strict 1 ha-manager rules add node-affinity $harule --resources vm:$vmid --nodes pve1,pve2,pve3 --strict 1 &&
echo "HA rule added" echo "HA rule added"
# ОБЯЗАТЕЛЬНО ПРОВЕРИТЬ КОМАНДУ!!! if [ $? -ne0 ]; then echo "Error creating HA rule"
fi fi
done done