Skip to content

Commit 7d08389

Browse files
authored
Modify sourcing context due to new library args
Fix / add: * adding list_fs_file function for listing files * speeding up list_disk function and adding faster 'lst_dsk' (which print virtual size) * fixing issue in get_vm_object_var when vmid does not exist but vmid+1 exist * fixing issue (forgot first param) in add_vm
1 parent ffde46b commit 7d08389

File tree

1 file changed

+151
-40
lines changed

1 file changed

+151
-40
lines changed

fbxvm-ctrl

Lines changed: 151 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#! /bin/bash
22
# NBA : Written by nbanba for managing Freebox Delta Virtual Machines
33
# NBA :
4-
# NBA : Licence : GNU - use, modify, redistribute, sell ... but Enjoy !
4+
# NBA : Licence : GPLv3 - use, modify, redistribute, sell ... but Enjoy !
55
# NBA :
66
# NBA : This software is not provide by Free (vendor of Freebox Delta) => use at your own risk !
77
##################################################################################################
@@ -43,7 +43,19 @@
4343
# NBA : --> correcting cloudinit userdata indentation
4444
# NBA : --> ensure disk_path is correctly encoded in base64
4545
# NBA : Also modifying cd_path in all functions to ensure it's correctly encoded in base64
46-
#
46+
#
47+
# NBA : 20221208
48+
# NBA : adding support of files download in library to download ISO or prebuilt VM image
49+
#
50+
# NBA : 20221222-20230123
51+
# NBA : adding list_fs_file function for listing files
52+
# NBA : speeding up list_disk function and adding faster 'lst_dsk' (which print virtual size)
53+
# NBA : fixing issue in get_vm_object_var when vmid does not exist but vmid+1 exist
54+
# NBA : fixing issue (forgot first param) in add_vm
55+
#
56+
# NBA : 20250405
57+
# NBA : modifying sourcing context due to new library args
58+
#
4759
##################################################################################################
4860
#
4961
#
@@ -68,7 +80,6 @@ purpl='\033[01;35m'
6880
PURPL='\033[35m'
6981
WHITE='\033[37m'
7082
white='\033[01;37m'
71-
white='\033[01;37m'
7283
norm='\033[00m'
7384

7485
vmid=$1
@@ -78,7 +89,10 @@ mode="$3"
7889

7990
# source fbx-delta-nba_bash_api.sh from the current directory
8091

92+
no_arg_sourcing_context () {
8193
source ${0/`basename $0`/}/fbx-delta-nba_bash_api.sh
94+
}
95+
no_arg_sourcing_context
8296

8397
# login
8498

@@ -92,14 +106,16 @@ print_vm_summary () {
92106

93107
local i=0
94108
answer=$(call_freebox_api "/$API/")
109+
# caching json results in env to avoid performance issue
95110
dump_json_keys_values "$answer" >/dev/null
96111
while [[ $(get_json_value_for_key "$answer" "result[$i].id") != "" ]]
97112
do
98113
name=$(get_json_value_for_key "$answer" "result[$i].name")
99114
id=$(get_json_value_for_key "$answer" "result[$i].id")
100115
mac=$(get_json_value_for_key "$answer" "result[$i].mac")
101116
state=$(get_json_value_for_key "$answer" "result[$i].status")
102-
if [ ! -z "$name" ];
117+
#if [ ! -z "$name" ];
118+
if [ ! -z "$id" ];
103119
then
104120
echo -e "VM-$i:\t${red}id: ${id}${norm} $([ "$state" == "running" ] && echo -e "\t status: ${green}${state}${norm}" || echo -e "\t status: ${purpl}${state}${norm}") \tname: $name \tmac_address: $mac"
105121
fi
@@ -113,30 +129,34 @@ get_vm_object_var () {
113129

114130
local i=0
115131
answer=$(call_freebox_api "/$API/")
132+
# caching json results in env to avoid performance issue
116133
dump_json_keys_values "$answer" >/dev/null
117134
#dump_json_keys_values "$answer"
118135

119-
while [[ $(get_json_value_for_key "$answer" "result[$i].name") != "" ]]
136+
#while [[ $(get_json_value_for_key "$answer" "result[$i].name") != "" ]]
137+
while [[ $(get_json_value_for_key "$answer" "result[$i].id") != "" ]]
120138
do
121-
mac[$i]=$(get_json_value_for_key "$answer" "result[$i].mac")
122-
userdata[$i]=$(get_json_value_for_key "$answer" "result[$i].cloudinit_userdata")
123-
cd_path[$i]=$(get_json_value_for_key "$answer" "result[$i].cd_path")
124-
id[$i]=$(get_json_value_for_key "$answer" "result[$i].id")
125-
os[$i]=$(get_json_value_for_key "$answer" "result[$i].os")
126-
cloudinit[$i]=$(get_json_value_for_key "$answer" "result[$i].enable_cloudinit")
127-
disk_path[$i]=$(get_json_value_for_key "$answer" "result[$i].disk_path")
128-
vcpus[$i]=$(get_json_value_for_key "$answer" "result[$i].vcpus")
129-
memory[$i]=$(get_json_value_for_key "$answer" "result[$i].memory")
130-
name[$i]=$(get_json_value_for_key "$answer" "result[$i].name")
131-
cloudinit_hostname[$i]=$(get_json_value_for_key "$answer" "result[$i].cloudinit_hostname")
132-
state[$i]=$(get_json_value_for_key "$answer" "result[$i].status")
133-
usb_0[$i]=$(get_json_value_for_key "$answer" "result[$i].bind_usb_ports[0]")
134-
usb_1[$i]=$(get_json_value_for_key "$answer" "result[$i].bind_usb_ports[1]")
135-
usb[$i]=$(echo "[\"${usb_0[$i]}\",\"${usb_1[$i]}\"]")
136-
enable_screen[$i]=$(get_json_value_for_key "$answer" "result[$i].enable_screen")
137-
disk_type[$i]=$(get_json_value_for_key "$answer" "result[$i].disk_type")
138-
139-
vm_object_[$i]="{\"mac\":${mac[$i]},\"cloudinit_userdata\":${userdata[$i]},\"cd_path\":${cd_path[$i]},\"id\":${id[$i]},\"os\":${os[$i]},\"enable_cloudinit\":${cloudinit[$i]},\"disk_path\":${disk_path[$i]},\"vcpus\":${vcpus[$i]},\"memory\":${memory[$i]},\"name\":${name[$i]},\"cloudinit_hostname\":${cloudinit_hostname[$i]},\"status\":${state[$i]},\"bind_usb_ports\":${usb[$i]},\"enable_screen\":${enable_screen[$i]},\"disk_type\":${disk_type[$i]}}"
139+
local j=$(get_json_value_for_key "$answer" "result[$i].id")
140+
mac[$j]=$(get_json_value_for_key "$answer" "result[$i].mac")
141+
userdata[$j]=$(get_json_value_for_key "$answer" "result[$i].cloudinit_userdata")
142+
cd_path[$j]=$(get_json_value_for_key "$answer" "result[$i].cd_path")
143+
id[$j]=$(get_json_value_for_key "$answer" "result[$i].id")
144+
os[$j]=$(get_json_value_for_key "$answer" "result[$i].os")
145+
cloudinit[$j]=$(get_json_value_for_key "$answer" "result[$i].enable_cloudinit")
146+
disk_path[$j]=$(get_json_value_for_key "$answer" "result[$i].disk_path")
147+
vcpus[$j]=$(get_json_value_for_key "$answer" "result[$i].vcpus")
148+
memory[$j]=$(get_json_value_for_key "$answer" "result[$i].memory")
149+
name[$j]=$(get_json_value_for_key "$answer" "result[$i].name")
150+
cloudinit_hostname[$j]=$(get_json_value_for_key "$answer" "result[$i].cloudinit_hostname")
151+
state[$j]=$(get_json_value_for_key "$answer" "result[$i].status")
152+
usb_0[$j]=$(get_json_value_for_key "$answer" "result[$i].bind_usb_ports[0]")
153+
usb_1[$j]=$(get_json_value_for_key "$answer" "result[$i].bind_usb_ports[1]")
154+
usb[$j]=$(echo "[\"${usb_0[$j]}\",\"${usb_1[$j]}\"]")
155+
enable_screen[$j]=$(get_json_value_for_key "$answer" "result[$i].enable_screen")
156+
disk_type[$j]=$(get_json_value_for_key "$answer" "result[$i].disk_type")
157+
158+
vm_object_[$j]="{\"mac\":${mac[$j]},\"cloudinit_userdata\":${userdata[$j]},\"cd_path\":${cd_path[$j]},\"id\":${id[$j]},\"os\":${os[$j]},\"enable_cloudinit\":${cloudinit[$j]},\"disk_path\":${disk_path[$j]},\"vcpus\":${vcpus[$j]},\"memory\":${memory[$j]},\"name\":${name[$j]},\"cloudinit_hostname\":${cloudinit_hostname[$j]},\"status\":${state[$j]},\"bind_usb_ports\":${usb[$j]},\"enable_screen\":${enable_screen[$j]},\"disk_type\":${disk_type[$j]}}"
159+
#vm_object_[$i]="{\"mac\":${mac[$i]},\"cloudinit_userdata\":${userdata[$i]},\"cd_path\":${cd_path[$i]},\"id\":${id[$i]},\"os\":${os[$i]},\"enable_cloudinit\":${cloudinit[$i]},\"disk_path\":${disk_path[$i]},\"vcpus\":${vcpus[$i]},\"memory\":${memory[$i]},\"name\":${name[$i]},\"cloudinit_hostname\":${cloudinit_hostname[$i]},\"status\":${state[$i]},\"bind_usb_ports\":${usb[$i]},\"enable_screen\":${enable_screen[$i]},\"disk_type\":${disk_type[$i]}}"
140160

141161
#echo -e "${vm_object_[$i]}\n"
142162
((i++))
@@ -241,14 +261,13 @@ fi
241261

242262

243263
create_vm_variables () {
244-
[ -z "$1" ] && echo "function feed_vm_variables take at least 'id' and 'param' argument" && exit 34
264+
[ -z "$1" ] && echo "function create_vm_variables take at least 'id' and 'param' argument" && exit 34
245265

246266
local param=("${@}")
247267
local idparam=0
248268
local nameparam=("")
249269
local valueparam=("")
250270
vm_object_create=("")
251-
252271
while [[ "${param[$idparam]}" != "" ]]
253272
do
254273
nameparam=$(echo "${param[$idparam]}"|cut -d= -f1)
@@ -359,33 +378,122 @@ del_disk () {
359378
#del_freebox_api "/fs/tasks/${task_id}"
360379
}
361380

381+
382+
# Adding this function as a replacemnt of "list_vm_disk ()" which suffer of performance issues
383+
# and which not providing disk image virtual size
384+
list_vm_dsk () {
385+
#export action=listdisk && source fbx-delta-nba_bash_api.sh
386+
export action=listdisk
387+
echo -e "\n${WHITE}VIRTUAL MACHINE DISK LIST (qcow2, raw, iso, img): ${norm}\n"
388+
local dsk_file_path=$(echo -n "$1"|base64)
389+
local answer=$(call_freebox_api "/fs/ls/${dsk_file_path}" 2>&1)
390+
local idx=(`dump_json_keys_values ${answer} |egrep ].index |cut -d' ' -f3`)
391+
local name=(`dump_json_keys_values ${answer} |egrep ].name |cut -d' ' -f3`)
392+
local size=(`dump_json_keys_values ${answer} |egrep ].size |cut -d' ' -f3`)
393+
local modification=(`dump_json_keys_values ${answer} |egrep ].modification |cut -d' ' -f3`)
394+
local mimetype=(`dump_json_keys_values ${answer} |egrep ].mimetype |cut -d' ' -f3`)
395+
396+
local i=0 j=0
397+
while [[ "${name[$i]}" != "" ]];
398+
do
399+
if [[ "${mimetype[$i]}" == "application/x-qemu-disk" ||\
400+
"${mimetype[$i]}" == "application/vnd.freebox.raw-disk-image" ||\
401+
"${mimetype[$i]}" == "application/x-raw-disk-image" ||\
402+
"${mimetype[$i]}" == "application/x-cd-image" ]]
403+
then
404+
#echo name=${name[$i]}
405+
modification[$i]=$(date "+%Y%m%d-%H:%M:%S" -d@${modification[$i]})
406+
#local nc=$(echo -n "${name[$i]}"|wc -m) savename=${name[$i]}
407+
#[[ "$nc" -gt "4" && "$nc" -lt "10" ]] && name[$i]="${name[$i]}\t\t\t"
408+
#[[ "$nc" -gt "9" && "$nc" -lt "17" ]] && name[$i]="${name[$i]}\t\t"
409+
#[[ "$nc" -gt "16" && "$nc" -lt "26" ]] && name[$i]="${name[$i]}\t"
410+
#[[ "$nc" -gt "25" ]] && name[$i]="${name[$i]}"
411+
#local vszdmp=$(add_freebox_api /vm/disk/info "{\"disk_path\": \"${dsk_file_path}$(echo -n /${savename}|base64)\"}" 2>/dev/null)
412+
local vszdmp=$(add_freebox_api /vm/disk/info "{\"disk_path\": \"${dsk_file_path}$(echo -n /${name[$i]}|base64)\"}" 2>/dev/null)
413+
local vsize=$(get_json_value_for_key "$vszdmp" "result.virtual_size")
414+
local sizeprint="" vsizeprint=""
415+
[[ "${size[$i]}" -lt "10240" ]] && sizeprint="${size[$i]} B"
416+
[[ "${size[$i]}" -ge "10240" ]] && sizeprint="$((${size[$i]}/1024)) K"
417+
[[ "${size[$i]}" -gt "1048576" ]] && sizeprint="$((${size[$i]}/1048576)) M"
418+
[[ "${size[$i]}" -gt "10737418240" ]]&& sizeprint="$((${size[$i]}/1073741824)) G"
419+
[[ "${vsize}" -lt "10240" ]] && vsizeprint="$((${vsize})) B"
420+
[[ "${vsize}" -ge "10240" ]] && vsizeprint="$((${vsize}/1024)) K"
421+
[[ "${vsize}" -gt "1048576" ]] && vsizeprint="$((${vsize}/1048576)) M"
422+
[[ "${vsize}" -gt "10737418240" ]] && vsizeprint="$((${vsize}/1073741824)) G"
423+
[[ "${vsize}" == "" ]] && vsizeprint="RUNNING"
424+
#echo -e "$j: \t${RED}idx: ${idx[$i]}${norm} \tname: ${GREEN}${name[$i]}${norm}\tsize: ${PURPL}${sizeprint}${norm}\tvirt: ${PURPL}${vsizeprint}${norm}\t${modification[$i]}${norm}"
425+
echo -e "$j: \t${RED}idx: ${idx[$i]}${norm} \tsize: ${PURPL}${sizeprint}${norm}\tvirt: ${PURPL}${vsizeprint}${norm}\t${modification[$i]}${norm}\tname: ${GREEN}${name[$i]}${norm}"
426+
((j++))
427+
fi
428+
((i++))
429+
done
430+
export action=''
431+
echo
432+
}
433+
434+
435+
436+
362437
list_vm_disk () {
363438
echo -e "\n${WHITE}VIRTUAL MACHINE DISK LIST : ${norm}\n"
364439
local vm_disk_path=$(echo -n "$1"|base64)
365-
#echo vm_disk_path=$vm_disk_path
440+
#echo vm_disk_path=$vm_disk_path # debug
366441
local answer=$(call_freebox_api "/fs/ls/${vm_disk_path}" 2>&1)
367-
#echo $answer
368-
local i=0
442+
# NBA 20230122: for better performances caching json results in env
443+
dump_json_keys_values "$answer" >/dev/null
444+
local i=0 j=0
369445
while [[ $(get_json_value_for_key "$answer" "result[$i].name") != "" ]]
370446
do
371-
local type=$(get_json_value_for_key "$answer" "result[$i].type")
372447
local index=$(get_json_value_for_key "$answer" "result[$i].index")
373-
local link=$(get_json_value_for_key "$answer" "result[$i].link")
374-
local modification=$(get_json_value_for_key "$answer" "result[$i].modification")
375-
local hidden=$(get_json_value_for_key "$answer" "result[$i].hidden")
448+
# local modification=$(get_json_value_for_key "$answer" "result[$i].modification")
376449
local mimetype=$(get_json_value_for_key "$answer" "result[$i].mimetype")
377450
local name=$(get_json_value_for_key "$answer" "result[$i].name")
378-
local path=$(get_json_value_for_key "$answer" "result[$i].path")
379451
local size=$(get_json_value_for_key "$answer" "result[$i].size")
380-
if [[ "$mimetype" == "application/x-qemu-disk" ]]
452+
if [[ "$mimetype" == "application/x-qemu-disk" ||\
453+
"$mimetype" == "application/vnd.freebox.raw-disk-image" ||\
454+
"$mimetype" == "application/x-raw-disk-image" ||\
455+
"$mimetype" == "application/x-cd-image" ]]
381456
then
382-
echo -e "DISK-$i: \t${RED}index: ${index}${norm}\tname: ${GREEN}${name}${norm}\tsize: ${PURPL}${size} bytes${norm}"
457+
#echo -e "DISK-$j: \t${RED}index: ${index}${norm}\tname: ${GREEN}${name}${norm}\tsize: ${PURPL}${size} bytes${norm}"
458+
echo -e "DISK-$j: \t${RED}index: ${index}${norm}\tsize: ${PURPL}${size} bytes${norm}\tname: ${GREEN}${name}${norm}"
459+
((j++))
383460
fi
384461
((i++))
385462
done
386463
echo
387464
}
388465

466+
# adding function to list file and directory - performance issue by design
467+
# NBA 20230122: for better performances caching json results in env
468+
# NBA 20230122: from today, speed became acceptable
469+
list_fs_file () {
470+
local fs_file_path=$(echo -n "$1"|base64)
471+
echo -e "\n${WHITE}LIST CONTENT IN : ${1} ${norm}\n"
472+
#echo fs_file_path=$fs_file_path # debug
473+
local answer=$(call_freebox_api "/fs/ls/${fs_file_path}" 2>&1)
474+
475+
# caching json results in env to avoid performance issue
476+
dump_json_keys_values "$answer" >/dev/null
477+
478+
local i=0 j=0
479+
while [[ $(get_json_value_for_key "$answer" "result[$i].name") != "" ]]
480+
do
481+
local type=$(get_json_value_for_key "$answer" "result[$i].type")
482+
local index=$(get_json_value_for_key "$answer" "result[$i].index")
483+
local link=$(get_json_value_for_key "$answer" "result[$i].link")
484+
local modification=$(get_json_value_for_key "$answer" "result[$i].modification")
485+
local hidden=$(get_json_value_for_key "$answer" "result[$i].hidden")
486+
local mimetype=$(get_json_value_for_key "$answer" "result[$i].mimetype")
487+
local name=$(get_json_value_for_key "$answer" "result[$i].name")
488+
local size=$(get_json_value_for_key "$answer" "result[$i].size")
489+
echo -e "${RED}idx: ${index}${norm}\tname: ${GREEN}${name}${norm}\tsize: ${PURPL}${size} bytes${norm}\t${GREEN}${type}${norm}\t${WHITE}${hidden}\t${modification}${norm}"
490+
((i++))
491+
done
492+
echo
493+
}
494+
495+
496+
389497
add_disk () {
390498
feeds_vmdisk_variables ${@:2}
391499

@@ -456,7 +564,8 @@ resize_disk () {
456564

457565
add_vm () {
458566
local idvm="$1"
459-
create_vm_variables ${vmid} ${@:3}
567+
#create_vm_variables ${vmid} ${@:3}
568+
create_vm_variables ${vmid} ${@:2}
460569

461570
#echo -e "\ncall_freebox_api \"/$API\" \"${vm_object_create}\"" # debug call_freebox_api
462571

@@ -555,7 +664,7 @@ if [[ -z "$1" ]];
555664
- console : connect VM console
556665
"
557666
echo -e "OBJECT : - vm : freebox delta virtual machine
558-
- disk : freebox delta virtual machine
667+
- disk : freebox delta virtual machine disk
559668
"
560669
echo -e "PARAM : - name= : name of this VM - VM-only (string, max 31 characters)
561670
- vcpu= : number of virtual CPUs to allocate to this VM - VM-only (integer)
@@ -771,8 +880,10 @@ echo -e "VERB|VM ID : ${WHITE}${vmid}${norm}"
771880
# verb & action
772881

773882
case "${vmid}" in
774-
list) print_vm_summary && exit 29 ; ;;
775-
listdisk) list_vm_disk ${@:2} && exit 33 ; ;;
883+
#list) print_vm_summary && exit 29 ; ;;
884+
list) list_vm && exit 29 ; ;;
885+
#listdisk) list_vm_disk ${@:2} && exit 33 ; ;;
886+
listdisk) list_vm_dsk ${@:2} && exit 33 ; ;;
776887
add) add ${@:2}; ;;
777888
del) del ${@:2}; ;;
778889
resize) resize_disk ${@:2}; ;;

0 commit comments

Comments
 (0)