-
Notifications
You must be signed in to change notification settings - Fork 0
/
temporary
68 lines (58 loc) · 2.03 KB
/
temporary
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
#!/bin/bash
export ssh_keyfile=/tmp/keys_internal_servers
export username=admin
function check_exists() {
echo "Checking if $1 exists.."
if [[ $(qm status $1 2>/dev/null) ]]; then
echo "VMID already exists. Proceeding to delete.."
if [[ $(qm destroy $1) ]]; then
return 0
else
return 1
fi
else
return 0
fi
}
function create_template() {
echo "Creating template $2 ($1)"
qm create $1 --name $2 --ostype l26
qm set $1 --net0 virtio,bridge=vmbr0
qm set $1 --serial0 socket --vga serial0
qm set $1 --memory 2048 --cores 2 --cpu host
qm set $1 --scsi0 ${template_storage}:0,import-from="$(pwd)/$3",discard=on
qm set $1 --boot order=scsi0 --scsihw virtio-scsi-single
qm set $1 --agent enabled=1,fstrim_cloned_disks=1
qm set $1 --ide2 ${template_storage}:cloudinit
qm set $1 --ipconfig0 "ip6=auto,ip=dhcp"
qm set $1 --sshkeys ${ssh_keyfile}
qm set $1 --ciuser ${username}
qm disk resize $1 scsi0 8G
qm template $1
rm $3
}
if [[ $HOSTNAME == "titan.freddan.io" ]]; then
export template_storage=vm-storage
export template_vmids=("3000" "3001")
elif [[ $HOSTNAME == "hive.freddan.io" ]]; then
export template_storage=local-lvm
export template_vmids=("2000" "2001")
elif [[ $HOSTNAME == "nano.freddan.io" ]]; then
export template_storage=local-lvm
export template_vmids=("1000" "1001")
fi
declare -A disk_images
disk_images["alma9.3"]="${template_vmids[0]}|https://repo.almalinux.org/almalinux/9.3/cloud/x86_64/images/AlmaLinux-9-GenericCloud-latest.x86_64.qcow2"
wget -q --show-progress https://raw.githubusercontent.com/fredrikscode/ssh-keys/main/internal_servers -O $ssh_keyfile
for name in "${!disk_images[@]}"
do
value="${disk_images[$name]}"
IFS="|" read -ra values <<< "$value"
vmid="${values[0]}"
url="${values[1]}"
check_exists "$vmid"
echo "Downloading disk image.. "
wget -q --show-progress $url
create_template $vmid $name $(basename -- $url)
done
rm $ssh_keyfile