This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
vmsetup.sh
executable file
·157 lines (133 loc) · 5.89 KB
/
vmsetup.sh
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/bash
#set -x # debug mode
set -e
# =============================================================================================
# global vars
# force english messages
export LANG=C
export LC_ALL=C
declare CONFIG_FILE=template_deploy.conf
if [[ ! -f "${CONFIG_FILE}" ]] ;then
echo "ERROR: File ${CONFIG_FILE} doesn't exists"
exit 1
else
source ${CONFIG_FILE}
fi
TEMPLATE_NAME_FULL="${TEMPLATE_NAME}-${VERSION}"
if [[ -f ${TEMPLATE_NAME_FULL}.id ]] && [[ ${TEMPLATE_RECREATE} != true ]];then
echo "${TEMPLATE_NAME_FULL} exists. Recreating not asked."
sleep 2
TEMPLATE_VMID=$(cat ./${TEMPLATE_NAME_FULL}.id)
TEMPLATE_CREATE="false"
elif [[ ! -f ${TEMPLATE_NAME_FULL}.id ]];then
echo "${TEMPLATE_VMID}" > ${TEMPLATE_NAME_FULL}.id
TEMPLATE_CREATE="true"
elif [[ -f ${TEMPLATE_NAME_FULL}.id ]] && [[ ${TEMPLATE_RECREATE} == true ]];then
echo "${TEMPLATE_NAME_FULL} exists. Recreating asked !!"
TEMPLATE_VMID=$(cat ./${TEMPLATE_NAME_FULL}.id)
qm destroy ${TEMPLATE_VMID} --purge
rm ./${TEMPLATE_NAME_FULL}.id
sleep 3
TEMPLATE_VMID=$(pvesh get /cluster/nextid)
echo "${TEMPLATE_VMID}" > ${TEMPLATE_NAME_FULL}.id
TEMPLATE_CREATE="true"
else
#write Template VM_ID in a file.
TEMPLATE_CREATE="false"
fi
# =============================================================================================
# main()
# pve storage exist ?
echo -n "Check if vm storage ${TEMPLATE_VMSTORAGE} exist... "
pvesh get /storage/${TEMPLATE_VMSTORAGE} --noborder --noheader &> /dev/null || {
echo -e "[failed]"
exit 1
}
echo "[ok]"
# pve storage snippet ok ?
echo -n "Check if snippet storage ${SNIPPET_STORAGE} exist... "
pvesh get /storage/${SNIPPET_STORAGE} --noborder --noheader &> /dev/null || {
echo -e "[failed]"
exit 1
}
echo "[ok]"
# pve storage snippet enable
pvesh get /storage/${SNIPPET_STORAGE} --noborder --noheader | grep -q snippets || {
echo "You musr activate content snippet on storage: ${SNIPPET_STORAGE}"
exit 1
}
# copy files
echo "Copy hook-script and ignition config to snippet storage..."
snippet_storage="$(pvesh get /storage/${SNIPPET_STORAGE} --noborder --noheader | grep ^path | awk '{print $NF}')"
cp -av ${TEMPLATE_IGNITION} hook-fcos.sh ${snippet_storage}/snippets
sed -e "/^COREOS_TMPLT/ c\COREOS_TMPLT=${snippet_storage}/snippets/${TEMPLATE_IGNITION}" -i ${snippet_storage}/snippets/hook-fcos.sh
chmod 755 ${snippet_storage}/snippets/hook-fcos.sh
# storage type ? (https://pve.proxmox.com/wiki/Storage)
echo -n "Get storage \"${TEMPLATE_VMSTORAGE}\" type... "
case "$(pvesh get /storage/${TEMPLATE_VMSTORAGE} --noborder --noheader | grep ^type | awk '{print $2}')" in
dir|nfs|cifs|glusterfs|cephfs) TEMPLATE_VMSTORAGE_type="file"; echo "[file]"; ;;
lvm|lvmthin|iscsi|iscsidirect|rbd|zfs|zfspool) TEMPLATE_VMSTORAGE_type="block"; echo "[block]" ;;
*)
echo "[unknown]"
exit 1
;;
esac
# download fcos vdisk
[[ ! -e fedora-coreos-${VERSION}-${PLATEFORM}.x86_64.qcow2 ]]&& {
echo "Download fedora coreos..."
wget -q --show-progress \
${BASEURL}/prod/streams/${STREAMS}/builds/${VERSION}/x86_64/fedora-coreos-${VERSION}-${PLATEFORM}.x86_64.qcow2.xz
xz -dv fedora-coreos-${VERSION}-${PLATEFORM}.x86_64.qcow2.xz
}
# create a new VM
if [[ ${TEMPLATE_CREATE} == "true" ]];then
echo "Create fedora coreos vm ${VMID}"
qm create ${TEMPLATE_VMID} --name ${TEMPLATE_NAME_FULL}
qm set ${TEMPLATE_VMID} --memory ${TEMPLATE_MEMORY} \
--cpu ${TEMPLATE_CPU_TYPE} \
--cores ${TEMPLATE_CPU_CORE} \
--agent enabled=1 \
--autostart ${TEMPLATE_AUTOSTAR} \
--onboot ${TEMPLATE_ONBOOT} \
--ostype l26 \
--tablet 0 \
--boot c --bootdisk scsi0
template_vmcreated=$(date +%Y-%m-%d)
qm set ${TEMPLATE_VMID} --description "Fedora CoreOS - Geco-iT Template
- Version : ${VERSION}
- Cloud-init : true
Creation date : ${template_vmcreated}
"
qm set ${TEMPLATE_VMID} --net0 virtio,bridge=vmbr0
#qm set ${TEMPLATE_VMID} --net1 virtio,bridge=vmbr1
echo -e "\nCreate Cloud-init vmdisk..."
qm set ${TEMPLATE_VMID} --ide2 ${TEMPLATE_VMSTORAGE}:cloudinit
# import fedora disk
if [[ "x${TEMPLATE_VMSTORAGE_type}" = "xfile" ]]
then
vmdisk_name="${TEMPLATE_VMID}/vm-${TEMPLATE_VMID}-disk-0.qcow2"
vmdisk_format="--format qcow2"
else
vmdisk_name="vm-${TEMPLATE_VMID}-disk-0"
vmdisk_format=""
fi
qm importdisk ${TEMPLATE_VMID} fedora-coreos-${VERSION}-${PLATEFORM}.x86_64.qcow2 ${TEMPLATE_VMSTORAGE} ${vmdisk_format}
qm set ${TEMPLATE_VMID} --scsihw virtio-scsi-pci --scsi0 ${TEMPLATE_VMSTORAGE}:${vmdisk_name}${VMDISK_OPTIONS}
# set hook-script
qm set ${TEMPLATE_VMID} -hookscript ${SNIPPET_STORAGE}:snippets/hook-fcos.sh
# convert vm template
echo -n "Convert VM ${TEMPLATE_VMID} in proxmox vm template... "
qm template ${TEMPLATE_VMID} &> /dev/null || true
echo "[done]"
fi
echo
echo
if [[ ${TEMPLATE_CREATE} == "true" ]] && [[ ${TEMPLATE_RECREATE} == "false" ]];then
echo "SUCCESS: ${TEMPLATE_NAME_FULL} with ID: ${TEMPLATE_VMID} created on \"${TEMPLATE_VMSTORAGE}\" storage"
elif [[ ${TEMPLATE_CREATE} == "true" ]] && [[ ${TEMPLATE_CREATE} == "true" ]];then
echo "SUCCESS: ${TEMPLATE_NAME_FULL} with ID: ${TEMPLATE_VMID} Re-created on \"${TEMPLATE_VMSTORAGE}\" storage"
elif [[ ${TEMPLATE_CREATE} == "false" ]];then
echo "SUCCESS: ${TEMPLATE_NAME_FULL} with ID: ${TEMPLATE_VMID} updated"
fi
echo
echo