#!/bin/bash TEMPLATE_VM_ID=202404001 PROFS_CPU_CORES_MULTIPLIER=4 PROFS_RAM_MULTIPLIER=2 INPUT_FILE=$1 POOL_NAME=$2 NEXT_VM_ID=$3 if ! command -v jq &> /dev/null; then echo "jq is not installed. Please install it using 'apt install jq' to continue." exit 1 fi if ! command -v mariadb &> /dev/null; then echo "mariadb is not installed. Please install it using 'apt install mariadb-client' to continue." exit 1 fi if [ -z "$INPUT_FILE" ] | [ -z "$POOL_NAME" ] | [ -z "$NEXT_VM_ID" ]; then echo "Usage: $0 " echo "Input file format:" echo -e "\tusername1" echo -e "\tusername2" echo -e "\t..." exit 1 fi POOL_EXISTS=$(pvesh get /pools --output-format=json | jq -r ".[] | select(.poolid == \"$POOL_NAME\") | .poolid") if [ "$POOL_EXISTS" == "$POOL_NAME" ]; then echo "Resource pool '$POOL_NAME' exists." else echo "Resource pool '$POOL_NAME' does not exist, creating..." pvesh create /pools --poolid $POOL_NAME fi while read -r username; do USERNAME=$(echo "$line") echo "Creating VM for $username in $POOL_NAME" qm clone $TEMPLATE_VM_ID $NEXT_VM_ID --name "$username" --full false --pool $POOL_NAME qm set $NEXT_VM_ID --args "-fw_cfg name=\"opt/fr.iut-fbleau/hostname\",string=\"vm-$username\" -fw_cfg name=\"opt/fr.iut-fbleau/domainname\",string=\"vm.iut-fbleau.fr\"" if [ "$POOL_NAME" == "profs" ]; then CURRENT_CORES=$(qm config $NEXT_VM_ID | grep "^cores" | awk '{print $2}') CURRENT_RAM=$(qm config $NEXT_VM_ID | grep "^memory" | awk '{print $2}') NEW_CORES=$((CURRENT_CORES * PROFS_CPU_CORES_MULTIPLIER)) NEW_RAM=$((CURRENT_RAM * PROFS_RAM_MULTIPLIER)) qm set $NEXT_VM_ID --cores $NEW_CORES --memory $NEW_RAM fi MAC_ADDRESS=$(grep -Po 'net\d+: virtio=\K[^,]+' /etc/pve/qemu-server/$NEXT_VM_ID.conf) mariadb --host=gimli.iut-fbleau.fr --user=proxmox --password="..." -e "CALL add_vm('$MAC_ADDRESS', 'vm-$username')" kea pvesh set /access/acl --path /vms/$NEXT_VM_ID --roles etu --users "$username@arda.lan" ((NEXT_VM_ID++)) done < "$INPUT_FILE"