-
Notifications
You must be signed in to change notification settings - Fork 5
/
start-virt.sh
executable file
·140 lines (123 loc) · 4.17 KB
/
start-virt.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
#!/bin/bash
#
# Create TD VM from libvirt template
#
set -e
CURR_DIR=$(readlink -f "$(dirname "$0")")
GUEST_IMG="tdx-guest-ubuntu22.04.qcow2"
GUEST_NAME="tdx-guest"
GUEST_ROOTDIR=/tmp/libvirt-vms
TEMPLATE="${CURR_DIR}/tdx-libvirt-ubuntu-host.xml.template"
FORCE=false
VCPU_NUM=1
MEM_SIZE=4
usage() {
cat << EOM
Usage: $(basename "$0") [OPTION]...
-i <guest image file> Default is tdx-guest-ubuntu22.04.qcow2 under current directory
-n <guest name> Name of TD guest
-t <template file> Default is ./tdx-libvirt-ubuntu-host.xml.template
-f Force recreate
-v <vcpu number> VM vCPU number
-m <memory size in GB> VM memory size in GB
-h Show this help
EOM
}
pre-check() {
# Check whether current user belong to libvirt
if [[ ! $(id -nG "$USER") == *"libvirt"* ]]; then
echo WARNING! Please add user "$USER" into group "libvirt" via \"sudo usermod -aG libvirt "$USER"\"
return 1
fi
}
process_args() {
while getopts ":i:n:t:v:m:fh" option; do
case "$option" in
i) GUEST_IMG=$OPTARG;;
n) GUEST_NAME=$OPTARG;;
t) TEMPLATE=$OPTARG;;
v) VCPU_NUM=$OPTARG;;
m) MEM_SIZE=$OPTARG;;
f) FORCE=true;;
h) usage
exit 0
;;
*)
echo "Invalid option '-$OPTARG'"
usage
exit 1
;;
esac
done
echo "====================================================================="
echo " Use template : ${TEMPLATE}"
echo " Guest name : ${GUEST_NAME}"
echo " Guest XML : ${GUEST_ROOTDIR}/${GUEST_NAME}.xml"
echo " Guest Image : ${GUEST_ROOTDIR}/${GUEST_NAME}.qcow2"
echo " Force Recreate : ${FORCE}"
echo "====================================================================="
if [[ ! -f ${GUEST_IMG} ]]; then
echo "Error: Guest image ${GUEST_IMG} does not exist"
exit 1
fi
# If the domain exists for a shutdown VM, it is safe to delete it.
# This will also allow the VM to be created later without erroring out.
if virsh list --state-shutoff | grep -q "tdx-guest "; then
echo ">Clean up domain for previously shutdown guest"
virsh undefine "${GUEST_NAME}" || true
fi
if [[ ${FORCE} == "true" ]]; then
echo "> Clean up the old guest... "
virsh destroy "${GUEST_NAME}" || true
sleep 2
virsh undefine "${GUEST_NAME}" || true
sleep 2
rm "${GUEST_ROOTDIR}/${GUEST_NAME}.xml" -fr || true
fi
if [[ -f ${GUEST_ROOTDIR}/${GUEST_NAME}.xml ]]; then
rm -rf "${GUEST_ROOTDIR}/${GUEST_NAME}.xml" || true
fi
if [[ ! -f ${TEMPLATE} ]]; then
echo "Template ${TEMPLATE} does not exist".
echo "Please specify via -t"
exit 1
fi
# Validate the number of vCPUs
if ! [[ ${VCPU_NUM} =~ ^[0-9]+$ && ${VCPU_NUM} -gt 0 ]]; then
echo "Error: Invalid number of vCPUs: ${VCPU_NUM}"
usage
exit 1
fi
# Validate the size of memory
if ! [[ ${MEM_SIZE} =~ ^[0-9]+$ && ${MEM_SIZE} -gt 0 ]]; then
echo "Error: Invalid memory size: ${MEM_SIZE}"
usage
exit 1
fi
}
create-vm() {
mkdir -p ${GUEST_ROOTDIR}/
echo "> Create ${GUEST_ROOTDIR}/${GUEST_NAME}.qcow2..."
cp "${GUEST_IMG}" "${GUEST_ROOTDIR}/${GUEST_NAME}.qcow2"
echo "> Create ${GUEST_ROOTDIR}/${GUEST_NAME}.xml..."
cp "${TEMPLATE}" "${GUEST_ROOTDIR}/${GUEST_NAME}.xml"
echo "> Modify configurations..."
sed -i "s/.*<name>.*/<name>${GUEST_NAME}<\/name>/" "${GUEST_ROOTDIR}/${GUEST_NAME}.xml"
sed -i "s#/path/to/image#${GUEST_ROOTDIR}/${GUEST_NAME}.qcow2#" "${GUEST_ROOTDIR}/${GUEST_NAME}.xml"
sed -i "s/REPLACE_VCPU_NUM/${VCPU_NUM}/g" "${GUEST_ROOTDIR}/${GUEST_NAME}.xml"
sed -i "s/REPLACE_MEM_SIZE/${MEM_SIZE}/g" "${GUEST_ROOTDIR}/${GUEST_NAME}.xml"
}
start-vm() {
echo "> Create VM domain..."
virsh define "${GUEST_ROOTDIR}/${GUEST_NAME}.xml"
sleep 2
echo "> Start VM..."
virsh start "${GUEST_NAME}"
sleep 2
echo "> Connect console..."
virsh console "${GUEST_NAME}"
}
pre-check
process_args "$@"
create-vm
start-vm