-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovision-vm
executable file
·96 lines (82 loc) · 2.12 KB
/
provision-vm
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
#!/usr/bin/env bash
set -eo pipefail
ram=1024
cpus=2
storage=/
disk=16
graphics=vnc,password=abc
blocking=--wait=-1
os=debian
while getopts ":r:c:smd:htWo:i" opt; do
case $opt in
r ) ram=$OPTARG;;
c ) cpus=$OPTARG;;
s ) storage=ssd/;;
m ) storage=magnetic/;;
d ) disk=$OPTARG;;
h ) graphics=none;;
t ) tty=console=ttyS0,115200;;
W ) blocking="";;
o ) os=$OPTARG;;
i ) ip=1;;
: ) echo "-$OPTARG requires an argument" >&2; exit 1;;
\?) echo "Invalid option: -$OPTARG" >&2; exit 1;;
esac
done
shift $((OPTIND - 1))
name=$1
if [ "$name" == "" ]; then
echo "Name required" >&2
exit 1
fi
if [ -n "$ip" ]; then
virsh domifaddr \
--source agent \
--domain "$name" \
--interface enp1s0 \
| awk '/ipv4/ { split($4,a,"/"); print a[1] }'
exit 0
fi
if [ "$os" == "debian" ]; then
iso=debian-12.9.0-amd64-netinst.iso
os_var=debian12
cfg=preseed.cfg
cfg_arg=auto=true
elif [ "$os" == "centos" ]; then
iso=CentOS-7-x86_64-Minimal-1810.iso
os_var=centos7.0
cfg=ks.cfg
cfg_arg=ks=file:/ks.cfg
else
echo "Invalid OS: '$os'. Valid options: debian, centos" >&2
exit 1
fi
config=$HOME/.virt-provisioning.json
if [ ! -f "$config" ]; then
cp config.template.json "$config"
echo "Please fill in configuration file: $config" >&2
exit 1
fi
temp_dir=$(mktemp -d)
trap 'rm -rf $temp_dir' EXIT
jinja2 \
-D "user=$USER" \
-D "timezone=$(realpath --relative-to /usr/share/zoneinfo /etc/localtime)" \
-D "fullname=$(getent passwd "$USER" | cut -d : -f 5 | cut -d , -f 1)" \
"cfg/$os.cfg.j2" \
"$config" \
> "$temp_dir/$cfg"
base_dir=$(<"$config" jq --raw-output .host.baseDir)
virt-install \
--name "$name" \
--os-variant=$os_var \
"--ram=$ram" \
"--vcpus=$cpus" \
--disk path="$base_dir/images/$storage$name.img,size=$disk" \
--location "$base_dir/isos/$iso" \
--network bridge:br0 \
--graphics $graphics \
--noautoconsole \
"$blocking" \
"--initrd-inject=$temp_dir/$cfg" \
--extra-args="$cfg_arg hostname=$name domain=$(hostname -d) $tty"