Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve code quality in virt-manager hook scripts #14

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ Target = virt-manager
Description = Configure virt-manager
When = PostTransaction
Exec = /usr/share/libalpm/scripts/biglinux-virt-manager-config-install
Depends = libvirt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/bash

network_file=/etc/libvirt/qemu/networks/default.xml

# Adiciona os grupos libvirt e libvirt-qemu a todos usuarios acima de 1000
for user in $(awk -F':' '{ if ($3 >= 1000 && $1 != "nobody") print $1 }' /etc/passwd); do
usermod -a -G libvirt $user
Expand All @@ -9,37 +11,26 @@ for user in $(awk -F':' '{ if ($3 >= 1000 && $1 != "nobody") print $1 }' /etc/pa
done

# Backup do default.xml
if [ ! -e "/etc/libvirt/qemu/networks/default.xml.bkp" ] && [ -e "/etc/libvirt/qemu/networks/default.xml" ];then
cp /etc/libvirt/qemu/networks/default.xml /etc/libvirt/qemu/networks/default.xml.bkp
if [ ! -e "${network_file}.bkp" ] && [ -e "$network_file" ];then
cp "$network_file" "${network_file}.bkp"
fi

# Verifica se o range de ip usado pelo libvirt não está sendo usado por outro rede
libvirtNetRange=$(grep -oP "(?<=ip address=')[0-9]+\.[0-9]+\.[0-9]+" /etc/libvirt/qemu/networks/default.xml)
libvirtNetRange=$(grep -oP "(?<=ip address=')[0-9]+\.[0-9]+\.[0-9]+" "$network_file")
if [ -n "$(ip -o -4 a | grep $libvirtNetRange)" ]; then
newLibvirtNetRange=$(awk -F. '{print $1 "." $2 "." $3+1}' <<< $libvirtNetRange)
sed -i "s/$libvirtNetRange/$newLibvirtNetRange/g" /etc/libvirt/qemu/networks/default.xml
sed -i "s/$libvirtNetRange/$newLibvirtNetRange/g" "$network_file"
fi

# firewallBackend=$(awk '/^firewall_backend/ { gsub(/"/, "", $3); print $3 }' /etc/libvirt/network.conf)
# if [ "$firewallBackend" != "iptables" ];then
# sed -i '/^firewall_backend/d' /etc/libvirt/network.conf
# echo 'firewall_backend = "iptables"' | tee -a /etc/libvirt/network.conf > /dev/null
# systemctl restart libvirtd.service
# fi

# Aceita bridge em todas as redes
if [ -e /etc/qemu/bridge.conf ] && [ -z "$(grep 'allow all' /etc/qemu/bridge.conf)" ]; then
echo "allow all" | tee -a /etc/qemu/bridge.conf > /dev/null
fi

# Starta o libvirtd
# systemctl start virtqemud.service
systemctl start libvirtd.service

# Configura, starta e coloca no autostart a rede do libvirt
network=$(LANG=C virsh net-list | grep default)
if [ -z "$network" ];then
virsh net-define /etc/libvirt/qemu/networks/default.xml
virsh net-define "$network_file"
fi
network=$(LANG=C virsh net-list | grep default)
if [ "$(awk '{print $2}' <<< $network)" != "active" ];then
Expand All @@ -50,6 +41,5 @@ if [ "$(awk '{print $3}' <<< $network)" != "yes" ];then
virsh net-autostart default
fi

# Habilita o libvirtd para iniciar no boot
# systemctl enable virtqemud.service
systemctl enable libvirtd.service
# Enable and start libvirtd
systemctl enable --now libvirtd.service
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#!/usr/bin/bash

network_file=/etc/libvirt/qemu/networks/default.xml

# Adiciona os grupos libvirt e libvirt-qemu a todos usuarios acima de 1000
for user in $(awk -F':' '{ if ($3 >= 1000 && $1 != "nobody") print $1 }' /etc/passwd); do
gpasswd -d $user libvirt
gpasswd -d $user libvirt-qemu
done

# Apaga backup do default.xml
if [ -e "/etc/libvirt/qemu/networks/default.xml" ]; then
rm "/etc/libvirt/qemu/networks/default.xml"
if [ -e "$network_file" ]; then
rm "$network_file"
fi
if [ -e "/etc/libvirt/qemu/networks/default.xml.bkp" ]; then
rm "/etc/libvirt/qemu/networks/default.xml.bkp"
if [ -e "${network_file}.bkp" ]; then
rm "{$network_file}.bkp"
fi

# Revert bridge ao original
Expand Down