User Tools

Site Tools


Sidebar

js#vista.png msort nsort

virtualization:proxmox:pvefind

PVEFIND

A search utility to find:

 1: which Host node contains a given OpenVZ or LXC container or Qemu VM.
 2: what container or VM ID a given hostname has.
#!/bin/sh
#
# Find Host node name for CT or VM ID (node)
# Find CT or VM ID for container hostname (id)
#set -x

PVESH=`which pvesh`

usage(){
        echo "  USAGE: $0 CMD [OPTION]"
        echo
        echo "          node - Find Host node name for CT or VM ID"
        echo "          id - Find CT or VM ID for container hostname"
        echo
}

if [ "$#" -lt 2 ] && [ "$1" != "help" ]; then
        echo "  ERROR!: Missing options!"
        usage
        exit 1
fi

get_node (){
        HWNODES=$($PVESH ls nodes | cut -d " " -f 2)
        for hwnode in $HWNODES
        do
                LXCIDS=$($PVESH ls nodes/$hwnode/lxc 2>&1 | grep -v "no such resource" | cut -d " " -f 2)
                for lxcid in $LXCIDS
                do
                        #echo "checking $ctid against $1"
                        if [ "$lxcid" -eq "$1" ]; then
                                echo $hwnode
                                exit 0
                        fi
                done
                VMIDS=$($PVESH ls nodes/$hwnode/qemu | grep -v "no such resource" | cut -d " " -f 2)
                for vmid in $VMIDS
                do
                        #echo "checking $ctid against $1"
                        if [ "$vmid" -eq "$1" ]; then
                                echo $hwnode
                                exit 0
                        fi
                done
                VZIDS=$($PVESH ls nodes/$hwnode/openvz | grep -v "no such resource" | cut -d " " -f 2)
                for vzid in $VZIDS
                do
                        #echo "checking $ctid against $1"
                        if [ "$vzid" -eq "$1" ]; then
                                echo $hwnode
                                exit 0
                        fi
                done
        done
        echo "no such resource"
        exit 0
}

get_id (){
        HWNODES=$($PVESH ls nodes | cut -d " " -f 2)
        for hwnode in $HWNODES
        do
                LXCIDS=$($PVESH ls nodes/$hwnode/lxc 2>&1 | grep -v "no such resource" | cut -d " " -f 2)
                for lxcid in $LXCIDS
                do
                        hostname=$($PVESH get nodes/$hwnode/lxc/$lxcid/config 2>&1 | grep hostname | cut -d '"' -f 4)
                        if [ "$hostname" = "$1" ]
                        then
                                echo $lxcid
                                exit 0
                        fi
                done
                VMIDS=$($PVESH ls nodes/$hwnode/qemu 2>&1 | grep -v "no such resource" | cut -d " " -f 2)
                for vmid in $VMIDS
                do
                        hostname=$($PVESH get nodes/$hwnode/qemu/$vmid/config 2>&1 | grep name | cut -d '"' -f 4)
                        if [ "$hostname" = "$1" ]
                        then
                                echo $vmid
                                exit 0
                        fi
                done
                VZIDS=$($PVESH ls nodes/$hwnode/openvz 2>&1 | grep -v "no such resource" | cut -d " " -f 2)
                for vzid in $VZIDS
                do
                        hostname=$($PVESH get nodes/$hwnode/openvz/$vzid/config 2>&1 | grep hostname | cut -d '"' -f 4)
                        if [ "$hostname" = "$1" ]
                        then
                                echo $vzid
                                exit 0
                        fi
                done
        done
        echo "no such resource"
        exit 0
}

case $1 in
        id)
                get_id $2
                ;;
        node)
                get_node $2
                ;;
        help)
                usage
                exit 0
                ;;
        *)
                usage
                exit 1
                ;;
esac
virtualization/proxmox/pvefind.txt · Last modified: 2020/02/24 11:16 (external edit)