diff --git a/vmcreate1.sh b/vmcreate1.sh index afa5223..0b07cb8 100644 --- a/vmcreate1.sh +++ b/vmcreate1.sh @@ -3,14 +3,20 @@ # Сделать проверку, какие правила НА существуют, и вывести выбор /etc/pve/ha/rules.cfg # Сделать добавление кастомного полльзователя с паролем # Сделать замену тега влан + +storage="syno-tigra" +path="/mnt/pve/$storage/snippets" + show_help() 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 '-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 "-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 "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.*.*" @@ -23,6 +29,8 @@ while getopts "a:f:h" opt; do f) file="$OPTARG" ;; h) show_help ; exit 0;; k) pubkey="$OPTARG";; + u) username="$OPTARG";; + p) password="$OPTARG";; # *) noopts=true esac done @@ -30,8 +38,7 @@ done # Если файл не задан, но флаг есть if [[ $# -eq 0 && -v $file && ! -f $file ]]; then input=y - echo "File $file does not exist. Use default 'hosts' file? Y/n: " - read input + read -p "File $file does not exist. Use default 'hosts' file? Y/n: " input case $input in 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 +#Проверка файла на соответствие шаблону IPv4 адреса и hostname +if [ -e $file ]; then + for line in $(<$file); do + ipcheck (echo $line | cut -d '.' -f 1) +fi + # Если заданы аргументы, то создаем временный файл if [ $# -ne 0 ] then touch hosts.tmp @@ -54,8 +67,8 @@ if [ $# -ne 0 ] then then echo -n "$arg " >> ./hosts.tmp echo -n "vm" >> ./hosts.tmp - printf "%05d\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 3)" >> ./hosts.tmp + printf "%03d\n" "$(echo -n $arg | cut -d '.' -f 4)" >> ./hosts.tmp echo >> ./hosts.tmp else echo "Argument $arg is not valid IP address (10.10.*.*). Using next argument." @@ -116,7 +129,7 @@ for line in $(<$file) 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" + 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) vmnum=$(echo $ip | cut -d '.' -f 3,4 | sed 's/\.//') if [[ $vlan -eq 0 ]]; then $vlan=100 @@ -127,10 +140,10 @@ do 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 + cp ./user.yaml ${path}${vmnum}_user.yaml + sed -i "s/HOSTNAME/$hostname/g" ${path}${vmnum}_user.yaml 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 cloudinit update $vmnum qm start $vmnum