From 0d16d5e84e321bd705f575d6610f865fa892f09c Mon Sep 17 00:00:00 2001
From: Alain Takoudjou <alain.takoudjou@nexedi.com>
Date: Wed, 17 May 2017 18:50:51 +0200
Subject: [PATCH] kvm resilient: promise check disk corruption on kvm clone

Check that backup kvm disk is not corrupted
---
 software/kvm/common.cfg                       |  6 +++---
 .../kvm/instance-kvm-import.cfg.jinja2.in     | 21 +++++++++++++++++++
 software/kvm/instance.cfg.in                  |  1 +
 software/kvm/template/kvm-export.sh.jinja2    |  1 +
 4 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/software/kvm/common.cfg b/software/kvm/common.cfg
index 8f1984a57..0870b5bee 100644
--- a/software/kvm/common.cfg
+++ b/software/kvm/common.cfg
@@ -89,7 +89,7 @@ command =
 [template]
 recipe = slapos.recipe.template
 url = ${:_profile_base_location_}/instance.cfg.in
-md5sum = 5c159864ce41394486867c27d65c2f50 
+md5sum = 01bad94ed1358b27d24a6f3745408f7d 
 output = ${buildout:directory}/template.cfg
 mode = 0644
 
@@ -124,7 +124,7 @@ on-update = true
 recipe = hexagonit.recipe.download
 ignore-existing = true
 url = ${:_profile_base_location_}/instance-kvm-import.cfg.jinja2.in
-md5sum = 5e21ad759c148d26134ecefbb4d3d9f1
+md5sum = 30f74f1958bd77e647a1bbe70e286e5a
 mode = 0644
 download-only = true
 on-update = true
@@ -152,7 +152,7 @@ recipe = hexagonit.recipe.download
 ignore-existing = true
 url = ${:_profile_base_location_}/template/kvm-export.sh.jinja2
 filename = kvm-export.sh.jinja2
-md5sum = c18e958bf374d5fa35dbb952686595fb
+md5sum = b617d64de73de1eed518185f310bbc82
 download-only = true
 mode = 0755
 
diff --git a/software/kvm/instance-kvm-import.cfg.jinja2.in b/software/kvm/instance-kvm-import.cfg.jinja2.in
index a1d42c9d6..b52d0f9d9 100644
--- a/software/kvm/instance-kvm-import.cfg.jinja2.in
+++ b/software/kvm/instance-kvm-import.cfg.jinja2.in
@@ -4,6 +4,9 @@ eggs-directory = {{ eggs_directory }}
 develop-eggs-directory = {{ develop_eggs_directory }}
 offline = true
 
+parts += 
+  kvm-disk-image-corruption-promise
+
 # Here, we don't need KVM to run to import data, so we don't
 # even extend the kvm instance profile.
 extends = 
@@ -59,3 +62,21 @@ context =
     raw gzip_binary {{ gzip_binary }}
 
 backup-disk-path = ${directory:backup}/virtual.qcow2
+
+[kvm-disk-image-corruption-promise]
+# Check that disk image is not corrupted
+recipe = collective.recipe.template
+input = inline:#!/bin/sh
+  QEMU_IMAGE="${directory:srv}/virtual.qcow2"
+  if [ ! -s "$QEMU_IMAGE" ]; then
+    exit 0
+  fi
+  {{ qemu_location }}/bin/qemu-img check $QEMU_IMAGE
+  RETURN_CODE=$?
+  if [ $RETURN_CODE -eq 0 ] || [ $RETURN_CODE -eq 3 ]; then
+    exit 0
+  else
+    exit 1
+  fi
+output = ${directory:promises}/kvm-disk-image-corruption
+mode = 700
\ No newline at end of file
diff --git a/software/kvm/instance.cfg.in b/software/kvm/instance.cfg.in
index 137c6adfe..3920808a8 100644
--- a/software/kvm/instance.cfg.in
+++ b/software/kvm/instance.cfg.in
@@ -149,6 +149,7 @@ extensions = jinja2.ext.do
 context =
     key develop_eggs_directory buildout:develop-eggs-directory
     key eggs_directory buildout:eggs-directory
+    key qemu_location kvm:location
     raw template_kvm_import ${template-kvm-import-script:location}/${template-kvm-import-script:filename}
     raw pbsready_import_template ${pbsready-import:output}
     key slapparameter_dict slap-configuration:configuration
diff --git a/software/kvm/template/kvm-export.sh.jinja2 b/software/kvm/template/kvm-export.sh.jinja2
index 42d7589e0..6c0296bcf 100644
--- a/software/kvm/template/kvm-export.sh.jinja2
+++ b/software/kvm/template/kvm-export.sh.jinja2
@@ -2,6 +2,7 @@
 #
 # Create a backup of the disk image of the virtual machine
 #
+set -e
 LC_ALL=C
 export LC_ALL
 BACKUP_DIR={{ directory['backup'] }}
-- 
2.30.9