50 lines
1.5 KiB
Bash
50 lines
1.5 KiB
Bash
#!/bin/bash
|
|
show_help()
|
|
echo 'Usage:'
|
|
echo 'vmcreate [-h][-a][-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'."
|
|
|
|
make_file ()
|
|
|
|
ip_check ()
|
|
|
|
hostame_generate()
|
|
|
|
|
|
while getopts "a:f:h" opt; do
|
|
case $opt in
|
|
a) haadd=true ;;
|
|
f) file="$OPTARG" ;;
|
|
h) show_help ; exit 0;;
|
|
*) noargs
|
|
esac
|
|
done
|
|
|
|
cat ./hosts | while read line
|
|
do
|
|
ip=$(echo $line | cut -d ' ' -f 1)
|
|
hostname=$(echo $line | cut -d ' ' -f 2)
|
|
vmnum=$(echo $ip | cut -d '.' -f 3)(echo $ip | cut -d '.' -f 3)
|
|
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
|
|
|
|
done
|
|
echo "VMs from 'hosts' successfully created" |