]> gitweb.fluxo.info Git - kvmx.git/commitdiff
Adds compress action
authorSilvio Rhatto <rhatto@riseup.net>
Sun, 31 Dec 2017 15:35:15 +0000 (13:35 -0200)
committerSilvio Rhatto <rhatto@riseup.net>
Sun, 31 Dec 2017 15:35:15 +0000 (13:35 -0200)
kvmx

diff --git a/kvmx b/kvmx
index b132f56779f9e36407ed69cb1758e6e56053a309..7d8dbc2d4313b8943bc679faafd31e6bdefe4bef 100755 (executable)
--- 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 $*