From ac369edea092ef40dcf021f3a6baabd63413ca2c Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Sun, 31 Dec 2017 13:35:15 -0200 Subject: Adds compress action --- kvmx | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'kvmx') diff --git a/kvmx b/kvmx index b132f56..7d8dbc2 100755 --- a/kvmx +++ b/kvmx @@ -1559,6 +1559,38 @@ function kvmx_serial { kvmx_console $* } +# Compress guest image +function kvmx_compress { + if kvmx_running; then + echo "$BASENAME: guest $VM is running" + exit 1 + fi + + # Avoid trying to convert guest using other images such as LVM volumes + if [ "$format" != "qcow2" ]; then + echo "$BASENAME: please convert guest $VM image to qcow2 and update your config" + exit 1 + fi + + # Size before compression + local size_before_bytes="`du $image | awk '{ print $1 }'`" + local size_before_human="`du -h $image | awk '{ print $1 }'`" + + qemu-img convert -O qcow2 -p -c $image $image.new && mv $image.new $image || exit 1 + + # Size after compression + local size_after_bytes="`du $image | awk '{ print $1 }'`" + local size_after_human="`du -h $image | awk '{ print $1 }'`" + + # Ratio + #local ratio="$(($size_after_bytes / $size_before_bytes))" + local ratio="$(echo "scale=4 ; $size_after_bytes / $size_before_bytes" | bc -l)" + + echo "size before: $size_before_human" + echo "size_after: $size_after_human" + echo "compression ratio: $ratio" +} + # Dispatch if type kvmx_$ACTION 2> /dev/null | grep -q "kvmx_$ACTION ()"; then __kvmx_initialize $* -- cgit v1.2.3