diff options
-rwxr-xr-x | kvmx | 32 |
1 files changed, 32 insertions, 0 deletions
@@ -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 $* |