hi Qemu command line users,

`qemu-img` does not allow to have a light and tree-like view of a derived
image.

as I didn't find another solution on the Internet, I wrote this little bash
script which may be useful to some of you and which allows to obtain this :

$ qemu-img-tree /opt/virtual/test11.disk
/opt/virtual/test11.disk (QCOW2 - 995M / 64G)
  └─ /opt/virtual/src/11+++ (QCOW2 - 813M / 64G)
    └─ /opt/virtual/src/11++ (QCOW2 - 929M / 64G)
      └─ /opt/virtual/src/11+ (QCOW2 - 1,3G / 64G)
        └─ /opt/virtual/src/11 (QCOW2 - 8,8G / 64G)

this small script is based on `jq` and `numfmt` and displays the writeable
child images in red :

#!/usr/bin/bash
# helper
_exit(){ [ "$2" ] && echo $2 > /dev/stderr; exit $1; }
# check dependencies
type -a jq &> /dev/null || _exit 1 "jq (command-line JSON processor) is
missing"
type -a numfmt &> /dev/null || _exit 1 "numfmt (convert numbers from/to
human-readable) is missing"
# check image parameter
[ "$1" ] || _exit 1 "image file is missing (eg. $( basename "$0" )
image_file)"
# get image informations
image_info=$( qemu-img info --output=json "$1" )
if [ "$image_info" ]
then
# extract data
IFS=$'\n' read -srd '' virtual_size disk_size file_format backing_file < <(
jq -r '."virtual-size",."actual-size",."format",."full-backing-filename"'
<<< $image_info
)
# convert sizes
IFS=$'\n' read -srd '' virtual_size disk_size < <(
numfmt --to=iec $virtual_size $disk_size
)
# check backing file for write (red)
[ "$2" ] && [ -w "$1" ] && tput setaf 3
# print image informations (aligned/colored)
echo "$2$1 (${file_format^^} - $disk_size / $virtual_size)" && tput sgr0
# call script on backing file (if any)
[ "$backing_file" != "null" ] && "$0" "$backing_file" "  ${2:-└─ }"
fi

hope this help, regards, lacsaP.

Attachment: qemu-img-tree
Description: Binary data

Reply via email to