aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSilvio Rhatto <rhatto@riseup.net>2017-12-31 13:35:15 -0200
committerSilvio Rhatto <rhatto@riseup.net>2017-12-31 13:35:15 -0200
commitac369edea092ef40dcf021f3a6baabd63413ca2c (patch)
tree13b990ec106da4c202df5238d17940c39924dadd
parent0fd5bcd6ebb162ea3efce7d3bfd3c013157bc717 (diff)
downloadkvmx-ac369edea092ef40dcf021f3a6baabd63413ca2c.tar.gz
kvmx-ac369edea092ef40dcf021f3a6baabd63413ca2c.tar.bz2
Adds compress action
-rwxr-xr-xkvmx32
1 files changed, 32 insertions, 0 deletions
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 $*