-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvm.sh
More file actions
110 lines (93 loc) · 2.84 KB
/
vm.sh
File metadata and controls
110 lines (93 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash
VM_ID_START=801
MAC_CONFIG="../config/macs.conf"
interfaces=$(pvesh get /nodes/server/network \
--output-format json | \
jq -r '.[] | select(.iface | startswith("ctf")) | .iface')
get_mac() {
local name=$1
mac=$(grep "^$name=" "$MAC_CONFIG" | cut -d '=' -f 2)
if [[ -z "$mac" ]]; then
echo "WARN: MAC для $name не найден в $MAC_CONFIG — генерирую случайный"
mac="52:54:00:$(openssl rand -hex 3 | sed 's/\(..\)/\1:/g; s/:$//')"
fi
echo "$mac"
}
clone_vm() {
local template_id=$1
local jury_template_id=$2
vm_count=$(echo "$interfaces" | wc -w)
# teams
for ((i = 1; i < vm_count; i++)); do
new_vm_id=$((VM_ID_START + i))
mac=$(get_mac "team$i")
qm clone "$template_id" "$new_vm_id" --name "team$i"
qm set "$new_vm_id" --net0 "virtio=$mac,bridge=ctf$i"
done
# jury
jury_id=$((VM_ID_START + vm_count))
mac=$(get_mac "jury")
qm clone "$jury_template_id" "$jury_id" --name "jury"
qm set "$jury_id" --net0 "virtio=$mac,bridge=ctf_org"
}
delete_vms() {
vm_count=$(echo "$interfaces" | wc -w)
echo "Deleting cloned VMs..."
# Delete team VMs
for ((i = 1; i < vm_count; i++)); do
vm_id=$((VM_ID_START + i))
echo "Deleting VM ID $vm_id..."
qm stop "$vm_id" 2>/dev/null || true
qm destroy "$vm_id"
done
# Delete jury VM
jury_id=$((VM_ID_START + vm_count))
echo "Deleting jury VM ID $jury_id..."
qm stop "$jury_id" 2>/dev/null || true
qm destroy "$jury_id"
}
start_vms() {
vm_count=$(echo "$interfaces" | wc -w)
# teams
for ((i = 1; i < vm_count; i++)); do
vm_id=$((VM_ID_START + i))
qm start "$vm_id"
done
# jury
jury_id=$((VM_ID_START + vm_count))
qm start "$jury_id"
}
stop_vms() {
vm_count=$(echo "$interfaces" | wc -w)
# teams
for ((i = 1; i < vm_count; i++)); do
vm_id=$((VM_ID_START + i))
qm stop "$vm_id" 2>/dev/null || true
done
# jury
jury_id=$((VM_ID_START + vm_count))
qm stop "$jury_id" 2>/dev/null || true
}
if [[ $1 == "clone" ]]; then
template_id=$2
jury_template_id=$3
if [[ -z "$template_id" ]]; then
echo -e "Incorrect template_id.\nexample: $0 clone 100 101"
exit 1
fi
if ! [[ "$template_id" =~ ^[0-9]+$ ]] || ! [[ "$jury_template_id" =~ ^[0-9]+$ ]]; then
echo -e "template_id must be positive.\nexample: $0 clone 100 101"
exit 2
fi
echo "Cloning VMs from template $template_id and jury from $jury_template_id..."
clone_vm "$template_id" "$jury_template_id"
elif [[ $1 == "delete" ]]; then
delete_vms
elif [[ $1 == "start" ]]; then
start_vms
elif [[ $1 == "stop" ]]; then
stop_vms
else
echo -e "Invalid command.\nusage: $0 <clone|delete> [template_id] [jury_template_id]"
exit 3
fi