Update vmcreate1.sh
This commit is contained in:
33
vmcreate1.sh
33
vmcreate1.sh
@@ -3,14 +3,20 @@
|
|||||||
# Сделать проверку, какие правила НА существуют, и вывести выбор /etc/pve/ha/rules.cfg
|
# Сделать проверку, какие правила НА существуют, и вывести выбор /etc/pve/ha/rules.cfg
|
||||||
# Сделать добавление кастомного полльзователя с паролем
|
# Сделать добавление кастомного полльзователя с паролем
|
||||||
# Сделать замену тега влан
|
# Сделать замену тега влан
|
||||||
|
|
||||||
|
storage="syno-tigra"
|
||||||
|
path="/mnt/pve/$storage/snippets"
|
||||||
|
|
||||||
show_help()
|
show_help()
|
||||||
echo 'Usage:'
|
echo 'Usage:'
|
||||||
echo 'vmcreate [-h][-a rule_name][-k pub_keyfile][-f filename]'
|
echo 'vmcreate [-h][-a rule_name][-k pub_keyfile][-u][-p][-f filename]'
|
||||||
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 "-f - get IP addresses and Hostnames from 'flilename';"
|
|
||||||
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 "-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 "\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.*.*"
|
||||||
@@ -23,6 +29,8 @@ while getopts "a:f:h" opt; do
|
|||||||
f) file="$OPTARG" ;;
|
f) file="$OPTARG" ;;
|
||||||
h) show_help ; exit 0;;
|
h) show_help ; exit 0;;
|
||||||
k) pubkey="$OPTARG";;
|
k) pubkey="$OPTARG";;
|
||||||
|
u) username="$OPTARG";;
|
||||||
|
p) password="$OPTARG";;
|
||||||
# *) noopts=true
|
# *) noopts=true
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
@@ -30,8 +38,7 @@ done
|
|||||||
# Если файл не задан, но флаг есть
|
# Если файл не задан, но флаг есть
|
||||||
if [[ $# -eq 0 && -v $file && ! -f $file ]]; then
|
if [[ $# -eq 0 && -v $file && ! -f $file ]]; then
|
||||||
input=y
|
input=y
|
||||||
echo "File $file does not exist. Use default 'hosts' file? Y/n: "
|
read -p "File $file does not exist. Use default 'hosts' file? Y/n: " input
|
||||||
read input
|
|
||||||
case $input in
|
case $input in
|
||||||
y) file=hosts;;
|
y) file=hosts;;
|
||||||
Y) file=hosts;;
|
Y) file=hosts;;
|
||||||
@@ -45,6 +52,12 @@ fi
|
|||||||
# Если заданы аргументы И файл
|
# Если заданы аргументы И файл
|
||||||
if [[ $# -ne 0 && -v file ]]; then echo "Please enter file OR arguments. Use '-f' flag for help"; exit 0; fi
|
if [[ $# -ne 0 && -v file ]]; then echo "Please enter file OR arguments. Use '-f' flag for help"; exit 0; fi
|
||||||
|
|
||||||
|
#Проверка файла на соответствие шаблону IPv4 адреса и hostname
|
||||||
|
if [ -e $file ]; then
|
||||||
|
for line in $(<$file); do
|
||||||
|
ipcheck (echo $line | cut -d '.' -f 1)
|
||||||
|
fi
|
||||||
|
|
||||||
# Если заданы аргументы, то создаем временный файл
|
# Если заданы аргументы, то создаем временный файл
|
||||||
if [ $# -ne 0 ] then
|
if [ $# -ne 0 ] then
|
||||||
touch hosts.tmp
|
touch hosts.tmp
|
||||||
@@ -54,8 +67,8 @@ if [ $# -ne 0 ] then
|
|||||||
then
|
then
|
||||||
echo -n "$arg " >> ./hosts.tmp
|
echo -n "$arg " >> ./hosts.tmp
|
||||||
echo -n "vm" >> ./hosts.tmp
|
echo -n "vm" >> ./hosts.tmp
|
||||||
printf "%05d\n" "$(echo -n $arg | cut -d '.' -f 3)" >> ./hosts.tmp
|
printf "%03d\n" "$(echo -n $arg | cut -d '.' -f 3)" >> ./hosts.tmp
|
||||||
printf "%05d\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 valid IP address (10.10.*.*). Using next argument."
|
echo "Argument $arg is not valid IP address (10.10.*.*). Using next argument."
|
||||||
@@ -116,7 +129,7 @@ for line in $(<$file)
|
|||||||
do
|
do
|
||||||
ip=$(echo $line | cut -d ' ' -f 1)
|
ip=$(echo $line | cut -d ' ' -f 1)
|
||||||
vlan=$(echo $ip | cut -d '.' -f 3)
|
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"
|
if [[ vlan -eq 0 ]]; then mask=23; gw="10.10.0.1"; else mask=24; gw="10.10.${vlan}.1"
|
||||||
hostname=$(echo $line | cut -d ' ' -f 2)
|
hostname=$(echo $line | cut -d ' ' -f 2)
|
||||||
vmnum=$(echo $ip | cut -d '.' -f 3,4 | sed 's/\.//')
|
vmnum=$(echo $ip | cut -d '.' -f 3,4 | sed 's/\.//')
|
||||||
if [[ $vlan -eq 0 ]]; then $vlan=100
|
if [[ $vlan -eq 0 ]]; then $vlan=100
|
||||||
@@ -127,10 +140,10 @@ do
|
|||||||
fi
|
fi
|
||||||
qm set $vmnum --tags 3,gfx
|
qm set $vmnum --tags 3,gfx
|
||||||
## preparing custom CloudInit snippets
|
## preparing custom CloudInit snippets
|
||||||
cp ./user.yaml /mnt/pve/syno-tigra/snippets/${vmnum}_user.yaml
|
cp ./user.yaml ${path}${vmnum}_user.yaml
|
||||||
sed -i "s/HOSTNAME/$hostname/g" /mnt/pve/syno-tigra/snippets/${vmnum}_user.yaml
|
sed -i "s/HOSTNAME/$hostname/g" ${path}${vmnum}_user.yaml
|
||||||
qm resize $vmnum scsi0 +50G
|
qm resize $vmnum scsi0 +50G
|
||||||
qm set $vmnum --cicustom "user=syno-tigra:snippets/${vmnum}_user.yaml"
|
qm set $vmnum --cicustom "user=${storage}:snippets/${vmnum}_user.yaml"
|
||||||
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
|
||||||
|
|||||||
Reference in New Issue
Block a user