-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron-callback.sh
executable file
·181 lines (158 loc) · 6.94 KB
/
cron-callback.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/bash
# You may change this directory
svm_workdir="${svm_workdir:-./data}"
ver=1.0.62
_self_bin_name="$0"
function where_is_him () {
SOURCE="$1"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
echo -n "$DIR"
}
function where_am_i () {
_my_path=`type -p ${_self_bin_name}`
[[ "$_my_path" = "" ]] && where_is_him "$_self_bin_name" || where_is_him "$_my_path"
}
_script_path=`where_am_i`
function echo2 () {
echo "$@" 1>&2
}
function generate_metadata () {
local name=$1
echo "local-hostname: $name"
}
function generate_userdata () {
local username=$1
local password=$2
local name=$3
# TODO: allow public key?
echo "#cloud-config
system_info:
default_user:
name: $username
home: /home/$username
password: $password
chpasswd: { expire: False }
hostname: $name
# allow password login
ssh_pwauth: True
"
}
function download_cloud_img_if_not_exist () {
local cloudimg="$1"
[[ -f "base/$cloudimg" ]] && return
declare -A knowledge
knowledge["focal-server-cloudimg-amd64.img"]=https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img
knowledge["Arch-Linux-x86_64-cloudimg.qcow2"]=https://geo.mirror.pkgbuild.com/images/latest/Arch-Linux-x86_64-cloudimg.qcow2
knowledge["win10pro-22h2-virtio-uefi.qcow2"]=https://recolic.net/hms.php?/systems/win10pro-22h2-virtio-uefi.qcow2
knowledge["win10-tiny10-virtio-uefi.qcow2"]=https://recolic.net/hms.php?/systems/win10-tiny10-virtio-uefi.qcow2
[ ! "${knowledge[$cloudimg]+abc}" ] && echo2 "Unknown cloudimg $cloudimg. cannot download it." && return 1
echo2 "+ Downloading cloudimg $cloudimg..."
if aria2c --version >/dev/null; then
aria2c -o "base/$cloudimg" "${knowledge[$cloudimg]}" || ! echo2 "Failed to download ubuntu cloudimg" || return $?
elif wget --version >/dev/null; then
wget -O "base/$cloudimg" "${knowledge[$cloudimg]}" || ! echo2 "Failed to download ubuntu cloudimg" || return $?
elif curl --version >/dev/null; then
curl -L -o "base/$cloudimg" "${knowledge[$cloudimg]}" || ! echo2 "Failed to download ubuntu cloudimg" || return $?
fi
}
function create_vm_if_not_exist () {
# TODO: support create VM from existing qcow2 snapshot
local name=$1
local cloudimg=$2
local disk=$3
local username=$4
local password="$5"
# Check if disk img already exists.
[[ -f "vm/$name/disk.img" ]] && return
rm -rf "vm/$name" ; mkdir -p "vm/$name"
echo2 "+ Creating VM image $name with options $@..."
if [ "$disk" != "" ]; then
# create from cloudimg
download_cloud_img_if_not_exist "$cloudimg" || return $?
generate_metadata "$name" > "vm/$name/meta-data" || return $?
generate_userdata "$username" "$password" "$name" > "vm/$name/user-data" || return $?
( cd "vm/$name" ; genisoimage -output initimg.iso -volid cidata -joliet -rock user-data meta-data ) || return $?
qemu-img create -f qcow2 -F qcow2 -b "../../base/$cloudimg" "vm/$name/disk.img" || return $?
qemu-img resize "vm/$name/disk.img" "$disk" || return $?
else
# create from baseimg
download_cloud_img_if_not_exist "$cloudimg" || return $?
qemu-img create -f qcow2 -F qcow2 -b "../../base/$cloudimg" "vm/$name/disk.img" || return $?
fi
}
function start_vm_if_not_running () {
local name=$1
local options_txt="$2"
read -a options <<< "$options_txt"
# For tracking started instance
local uuid=`uuidgen --namespace @oid --name "qemu.$name" --sha1`
# Check if qemu already running for this instance.
ps aux | grep -F "uuid $uuid" | grep qemu > /dev/null 2>&1 && return 0
# start it
[[ ! -f "vm/$name/disk.img" ]] && echo2 "In start_vm, disk image vm/$name/disk.img doesn't exist. Did init_vm fail?" && return 1
echo2 "+ Starting VM $name with options_txt '$options_txt' and uuid $uuid..."
[[ -f "vm/$name/initimg.iso" ]] && options+=(-cdrom "vm/$name/initimg.iso")
nohup qemu-system-x86_64 --uuid "$uuid" -drive file="vm/$name/disk.img",if=virtio -cpu host --enable-kvm -net nic,model=virtio-net-pci "${options[@]}" >> tmp/qemu.log 2>&1 & disown
}
function do_init () {
while IFS= read -r line; do
# Ignore lines starting with #
if [[ "$line" =~ ^\# ]]; then
continue
fi
# Trim leading and trailing whitespaces
line=$(echo "$line" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
# Check if the line is non-empty
if [ -n "$line" ]; then
# Parse the line as "name;cloudimg;disk;username;password", trim space
IFS=';' read -r name cloudimg disk username password <<< "$(echo "$line" | tr -s '[:space:]' ';')"
# 2 options or 5 options allowed, otherwise bad config line.
if [ -n "$name" ] && [ -n "$cloudimg" ] && [ -n "$disk" ] && [ -n "$username" ] && [ -n "$password" ]; then
create_vm_if_not_exist "$name" "$cloudimg" "$disk" "$username" "$password" || echo2 "Failed to create_vm_if_not_exist. $?"
elif [ -n "$name" ] && [ -n "$cloudimg" ] && [ ! -n "$disk" ] && [ ! -n "$username" ] && [ ! -n "$password" ]; then
create_vm_if_not_exist "$name" "$cloudimg" || echo2 "Failed to create_vm_if_not_exist. $?"
else
echo2 "Error: Bad configuration line: $line"
fi
fi
done < "$_script_path/init.settings"
}
function do_start () {
while IFS= read -r line; do
# Ignore lines starting with #
if [[ "$line" =~ ^\# ]]; then
continue
fi
# Trim leading and trailing whitespaces
line=$(echo "$line" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
# Check if the line is non-empty
if [ -n "$line" ]; then
# Parse the line as "name;options", only trim space in name, options can contain ;
name=$(echo "$line" | sed -e 's/[[:space:]]*;.*$//' -e 's/^[[:space:]]*//')
options=$(echo "$line" | sed 's/^[^;]*;//')
# Check if the name is empty
if [ -n "$name" ]; then
start_vm_if_not_running "$name" "$options" || echo2 "Failed to start_vm_if_not_running. $?"
else
echo2 "Error: Bad configuration line: $line"
fi
fi
done < "$_script_path/runtime.settings"
}
# Check if current script is already running. Stupid flock is very unreliable.
for pid in $(pidof -x "$0"); do
if [ $pid != $$ ]; then
echo "$0 : Process is already running with PID $pid"
exit 1
fi
done
mkdir -p "$svm_workdir"
cd "$svm_workdir" || exit $?
mkdir -p base vm tmp
do_init
do_start