From 156ad20d2752024169b86ee02a0382b0c3eb0c8a Mon Sep 17 00:00:00 2001 From: root Date: Wed, 13 Jul 2022 19:58:22 +0000 Subject: [PATCH] -listimages2 creates a .csv and lists only use images -master.list useful for seeing which labs are using a particular image -still needs work --- listimages2 | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100755 listimages2 diff --git a/listimages2 b/listimages2 new file mode 100755 index 0000000..b005a3a --- /dev/null +++ b/listimages2 @@ -0,0 +1,55 @@ +#!/bin/bash +#get all used images and their backing files +#list all unused qemu images in eve-ng +#will not include "wiped" nodes +#there cannot be any running nodes +# +#very "hacky" +# +#find /opt/unetlab/tmp -type f -name \*.qcow2 -exec qemu-img info {} \; > test +#set record delimiter to blank line +#sed -i s/\ \ \ \ corrupt:\ false// test +#sed -i s/\ \ \ \ corrupt:\ true// test +#parse w/ blank line for record separateor and newline for field separator +# 1 is image file (provisioned device) +# 2 is for the source +#awk 'BEGIN {FS="\n"; RS=""} {print $1 $6}' test > test2 +#awk 'BEGIN {FS="\n"; RS=""} {print $6}' test > test2 +#clean up leaving two files +#sed -i 's/image: //' test2 +#sed -i 's/backing file://' test2 +# list of all used images +#cat test2 | sort -k 2 -u +#cat test2 | sort -u +# +#get just the directory names for qemu (lists only used versions) +#cat test2 | cut -f 6 -d '/' +# +#cat test2 | sort -u | cut -f 6 -d '/' > test3 +# +#list installed qemu versions +#ls -l /opt/unetlab/addons/qemu/ | cut -b 42- > test4 +#grep -Fxv `ls -l /opt/unetlab/addons/qemu/ | cut -b 42-` test3 +#grep -Fxv -f test3 test4 +echo "Please enter username" +read USERNAME +echo "Please enter password" +read -s PASSWORD +rm /list.json +rm master.list +curl -k -s -b /tmp/cookie -c /tmp/cookie -X POST -d '{"username":"'"$USERNAME"'","password":"'"$PASSWORD"'"}' https://127.0.0.1/api/auth/login +ls -R /opt/unetlab/labs/*.unl > labs_list +sed -i 's/\ /%20/g' labs_list +sed -i 's/\/opt\/unetlab//g' labs_list +while read i; do + curl -k -s -c /tmp/cookie -b /tmp/cookie -X GET -H 'Content-type: application/json' https://127.0.0.1/api$i/nodes | jq -r '.[] | .. | objects.image' > images.list + echo >> images.list + sed ':a;N;$!ba;s~\n~,"'"$i"'"\n~g' images.list >> master.list + + #echo "$i"; + +done < labs_list +# list used images +grep -v null master.list | cut -f1 -d ',' | sort -u +echo +echo master.list contains a .csv of images and corresponding labs