2018-04-20 14:12:01 -04:00
|
|
|
#!/bin/bash
|
|
|
|
|
2018-04-26 12:25:32 -04:00
|
|
|
# set -x
|
|
|
|
|
|
|
|
storage_path="/storage/images/maas"
|
2018-04-26 13:35:10 -04:00
|
|
|
storage_format="qcow2"
|
2018-04-20 14:12:01 -04:00
|
|
|
compute="maas-node"
|
2019-03-01 21:11:47 +00:00
|
|
|
node_count=20
|
|
|
|
node_start=1
|
|
|
|
node_cpus=4
|
|
|
|
node_ram=4096
|
2018-04-20 14:12:01 -04:00
|
|
|
nic_model="virtio"
|
|
|
|
network="maas"
|
|
|
|
|
|
|
|
create_vms() {
|
2019-03-13 02:09:09 +00:00
|
|
|
create_storage & build_vms
|
2018-04-20 14:12:01 -04:00
|
|
|
}
|
|
|
|
|
2018-04-26 13:35:10 -04:00
|
|
|
|
2018-04-20 14:12:01 -04:00
|
|
|
wipe_vms() {
|
|
|
|
destroy_vms
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
create_storage() {
|
2019-03-01 21:11:47 +00:00
|
|
|
for ((machine="$node_start"; machine<=node_count; machine++)); do
|
2018-04-26 13:35:10 -04:00
|
|
|
printf -v maas_node %s-%02d "$compute" "$machine"
|
|
|
|
mkdir -p "$storage_path/$maas_node"
|
|
|
|
/usr/bin/qemu-img create -f "$storage_format" "$storage_path/$maas_node/$maas_node-d1.img" 40G &
|
|
|
|
/usr/bin/qemu-img create -f "$storage_format" "$storage_path/$maas_node/$maas_node-d2.img" 20G &
|
|
|
|
/usr/bin/qemu-img create -f "$storage_format" "$storage_path/$maas_node/$maas_node-d3.img" 20G &
|
2018-04-20 14:12:01 -04:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
build_vms() {
|
2019-03-01 21:11:47 +00:00
|
|
|
for ((virt="$node_start"; virt<=node_count; virt++)); do
|
2018-04-26 13:35:10 -04:00
|
|
|
printf -v virt_node %s-%02d "$compute" "$virt"
|
2019-03-01 21:11:47 +00:00
|
|
|
ram="$node_ram"
|
|
|
|
vcpus="$node_cpus"
|
2018-04-20 14:12:01 -04:00
|
|
|
bus="scsi"
|
2018-04-26 13:35:10 -04:00
|
|
|
macaddr=$(printf '52:54:00:63:%02x:%02x\n' "$((RANDOM%256))" "$((RANDOM%256))")
|
2018-04-20 14:12:01 -04:00
|
|
|
|
2019-03-13 02:09:09 +00:00
|
|
|
virt-install -v --noautoconsole \
|
|
|
|
--print-xml \
|
|
|
|
--autostart \
|
|
|
|
--boot network,hd,menu=on \
|
|
|
|
--graphics spice \
|
|
|
|
--video qxl,vram=1024 \
|
|
|
|
--channel spicevmc \
|
|
|
|
--name "$virt_node" \
|
|
|
|
--ram "$ram" \
|
|
|
|
--vcpus "$vcpus" \
|
|
|
|
--cpu host-passthrough,cache.mode=passthrough \
|
2018-04-26 12:25:32 -04:00
|
|
|
--controller "$bus",model=virtio-scsi,index=0 \
|
2018-04-26 13:35:10 -04:00
|
|
|
--disk path="$storage_path/$virt_node/$virt_node-d1.img,format=$storage_format,size=40,bus=$bus,cache=writeback" \
|
|
|
|
--disk path="$storage_path/$virt_node/$virt_node-d2.img,format=$storage_format,size=20,bus=$bus,cache=writeback" \
|
|
|
|
--disk path="$storage_path/$virt_node/$virt_node-d3.img,format=$storage_format,size=20,bus=$bus,cache=writeback" \
|
2019-03-11 04:53:49 +00:00
|
|
|
--network=network=$network,mac="$macaddr",model=$nic_model > "$virt_node.xml"
|
2018-04-26 13:35:10 -04:00
|
|
|
virsh define "$virt_node.xml"
|
2019-03-13 02:09:09 +00:00
|
|
|
# virsh start "$virt_node"
|
2018-04-20 14:12:01 -04:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy_vms() {
|
2019-03-01 21:11:47 +00:00
|
|
|
for ((node="$node_start"; node<=node_count; node++)); do
|
2018-04-26 13:35:10 -04:00
|
|
|
printf -v compute_node %s-%02d "$compute" "$node"
|
|
|
|
|
2018-04-20 14:12:01 -04:00
|
|
|
# If the domain is running, this will complete, else throw a warning
|
2018-04-26 13:35:10 -04:00
|
|
|
virsh --connect qemu:///system destroy "$compute_node"
|
2018-04-20 14:12:01 -04:00
|
|
|
|
|
|
|
# Actually remove the VM
|
2018-04-26 13:35:10 -04:00
|
|
|
virsh --connect qemu:///system undefine "$compute_node"
|
2018-04-20 14:12:01 -04:00
|
|
|
|
|
|
|
# Remove the three storage volumes from disk
|
|
|
|
for disk in {1..3}; do
|
2018-04-26 13:35:10 -04:00
|
|
|
virsh vol-delete --pool "$compute_node" "$compute_node-d${disk}.img"
|
2018-04-20 14:12:01 -04:00
|
|
|
done
|
2018-04-26 13:35:10 -04:00
|
|
|
rm -rf "$storage_path/$compute_node/"
|
2018-04-20 14:12:01 -04:00
|
|
|
sync
|
2018-04-26 13:35:10 -04:00
|
|
|
rm -f "$compute_node.xml" \
|
|
|
|
"/etc/libvirt/qemu/$compute_node.xml" \
|
|
|
|
"/etc/libvirt/storage/$compute_node.xml" \
|
|
|
|
"/etc/libvirt/storage/autostart/$compute_node.xml"
|
2018-04-20 14:12:01 -04:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2018-04-26 13:35:10 -04:00
|
|
|
while getopts ":cw" opt; do
|
2018-04-20 14:12:01 -04:00
|
|
|
case $opt in
|
|
|
|
c)
|
|
|
|
create_vms
|
|
|
|
;;
|
|
|
|
w)
|
|
|
|
wipe_vms
|
|
|
|
;;
|
|
|
|
\?)
|
|
|
|
echo "Invalid option: -$OPTARG" >&2
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|