From 3cf711596ab9c97a594ece318eb19b1daf3668d5 Mon Sep 17 00:00:00 2001 From: sang Date: Thu, 16 Jan 2025 15:40:48 +0800 Subject: [PATCH] docs(readme): Uninstall script will remove pve lxc ip tags --- README.md | 12 ++++++++++++ uninstall.sh | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/README.md b/README.md index eb7d302..30b3f36 100644 --- a/README.md +++ b/README.md @@ -50,3 +50,15 @@ rm -f /lib/systemd/system/lxc-iptag.service rm -f /usr/local/etc/lxc-iptag.conf rm -f /usr/local/bin/lxc-iptag ``` + +If you want to remove all lxc-iptag related datas (includes all ip tags), run the following command: + +This script will: + +- Stop and disable `lxc-iptag` systemd service +- Remove pve all lxc ip tags +- Delete all lxc-iptag related systemd unit, config file and script + +```sh +curl -sL https://github.com/gitsang/lxc-iptag/raw/main/uninstall.sh | bash +``` diff --git a/uninstall.sh b/uninstall.sh index f442dc2..3e5101f 100644 --- a/uninstall.sh +++ b/uninstall.sh @@ -6,6 +6,40 @@ set -xe systemctl stop lxc-iptag.service systemctl disable lxc-iptag.service +# clean ip tags +is_valid_ipv4() { + local ip=$1 + local regex="^([0-9]{1,3}\.){3}[0-9]{1,3}$" + + if [[ $ip =~ $regex ]]; then + IFS='.' read -r -a parts <<< "$ip" + for part in "${parts[@]}"; do + if ! [[ $part =~ ^[0-9]+$ ]] || ((part < 0 || part > 255)); then + return 1 + fi + done + return 0 + else + return 1 + fi +} +vmid_list=$(pct list 2>/dev/null | grep -v VMID | awk '{print $1}') +for vmid in ${vmid_list}; do + next_tags=() + + # Parse current tags + mapfile -t current_tags < <(pct config "${vmid}" | grep tags | awk '{print $2}' | sed 's/;/\n/g') + for current_tag in "${current_tags[@]}"; do + if ! is_valid_ipv4 "${current_tag}"; then + next_tags+=("${current_tag}") + fi + done + + # Set tags + echo "Setting ${vmid} tags from ${current_tags[*]} to ${next_tags[*]}" + pct set "${vmid}" -tags "$(IFS=';'; echo "${next_tags[*]}")" +done + # remove lxc-iptag rm -f /lib/systemd/system/lxc-iptag.service rm -f /usr/local/etc/lxc-iptag.conf