Update vmcreate1.sh

This commit is contained in:
2025-10-26 13:27:30 +00:00
parent 2e876fd79a
commit 24cc5a3351

View File

@@ -109,13 +109,7 @@ if [[ $# -ne 0 ]]; then
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo -n "$arg " >> ./hosts.tmp echo -n "$arg " >> ./hosts.tmp
echo -n "vm" >> ./hosts.tmp echo -n "vm" >> ./hosts.tmp
vlan=$(echo -n $arg | cut -d '.' -f 3) get_vmid "$arg" >> hosts.tmp
if [[ $vlan -eq 0 ]]; then
echo -n "100" >> ./hosts.tmp
else
printf "%02d\n" "$vlan" >> ./hosts.tmp
fi
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.*.*). Aborting." echo "Argument `$arg` is not a valid IPv4 address (10.10.*.*). Aborting."
@@ -198,7 +192,7 @@ keypair_generate (){
} }
mksnippet (){ mksnippet (){
snippet="${path}${vmnum}_user.yaml" snippet="${path}${vmid}_user.yaml"
cp ./user.yaml "$snippet" cp ./user.yaml "$snippet"
sed -i "s/HOSTNAME/$hostname/g" "$snippet" sed -i "s/HOSTNAME/$hostname/g" "$snippet"
if [[ -n $username ]]; then if [[ -n $username ]]; then
@@ -224,37 +218,34 @@ do
gw="10.10.${vlan}.1" gw="10.10.${vlan}.1"
fi fi
hostname=$(echo $line | cut -d ' ' -f 2) hostname=$(echo $line | cut -d ' ' -f 2)
vmnum="$vlan(printf "%03d\n" "$(echo -n $ip | cut -d '.' -f 4)")" vmid=$(get_vmid "$ip")
if [[ $vlan -eq 0 ]]; then
$vlan=100
fi
mksnippet mksnippet
#### DEBUG 2 #### DEBUG 2
echo "IP: $ip" echo "IP: $ip"
echo "hostname: $hostname" echo "hostname: $hostname"
echo "VMID: $vmnum" echo "VMID: $vmid"
echo "VLAN: $vlan" echo "VLAN: $vlan"
echo "$snippet:" echo "$snippet:"
cat "$snippet" cat "$snippet"
read -p "Press Enter" read -p "Press Enter"
#### ####
echo -n "Now cloning VM $vmnum from a template....." echo -n "Now cloning VM $vmnum from a template....."
qm clone 5000 $vmnum --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"
else else
echo "ERROR" echo "ERROR"
exit 8 exit 8
fi fi
qm set $vmnum --tags $node,$tag qm set $vmid --tags $node,$tag
qm resize $vmnum scsi0 +"$size"G qm resize $vmid scsi0 +"$size"G
qm set $vmnum --cicustom "user=${storage}:snippets/${vmnum}_user.yaml" qm set $vmid --cicustom "user=${storage}:snippets/${vmnum}_user.yaml"
qm set $vmnum --ipconfig0 ip="$ip"/"$mask",gw="$gw" qm set $vmid --ipconfig0 ip="$ip"/"$mask",gw="$gw"
qm cloudinit update $vmnum qm cloudinit update $vmid
qm start $vmnum qm start $vmid
if [ -v harule ]; then if [ -v harule ]; then
ha-manager add vm:$vmnum --state started --max_relocate 2 ha-manager add vm:$vmid --state started --max_relocate 2
ha-manager rules add node-affinity $harule --resources vm:$vmnum --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"
# ОБЯЗАТЕЛЬНО ПРОВЕРИТЬ КОМАНДУ!!! # ОБЯЗАТЕЛЬНО ПРОВЕРИТЬ КОМАНДУ!!!
fi fi