From 71fa85fcfa6870f7a25fa37ffec6cf6138b12b12 Mon Sep 17 00:00:00 2001
From: Alain Takoudjou <talino@tiolive.com>
Date: Mon, 23 Mar 2015 15:41:42 +0100
Subject: [PATCH] Avoid getting wrong storage path when creating kvm external
 disk

---
 slapos/recipe/kvm/template/kvm_run.in | 30 ++++++++++++++++++++++-----
 software/kvm/common.cfg               |  2 +-
 software/kvm/instance-kvm.cfg.jinja2  |  1 +
 3 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/slapos/recipe/kvm/template/kvm_run.in b/slapos/recipe/kvm/template/kvm_run.in
index ca1d5b454..aaf8fb06a 100644
--- a/slapos/recipe/kvm/template/kvm_run.in
+++ b/slapos/recipe/kvm/template/kvm_run.in
@@ -36,11 +36,8 @@ external_disk_number = %(external-disk-number)s
 external_disk_size = '%(external-disk-size)s'
 disk_storage_dict = {}
 disk_storage_list = """%(disk-storage-list)s""".split('\n')
-for storage in disk_storage_list:
-  if storage:
-    key, val = storage.split(' ')
-    disk_storage_dict[key.strip()] = val.strip()
-map_storage_list = ['data%%r' %% (i + 1) for i in range(0, len(disk_storage_dict))]
+map_storage_list = []
+etc_directory = '%(etc-directory)s'.strip()
 
 def md5Checksum(file_path):
     with open(file_path, 'rb') as fh:
@@ -71,6 +68,22 @@ def getSocketStatus(host, port):
     break
   return s
 
+def getMapStorageList(disk_storage_dict):
+  map_disk_file = os.path.join(etc_directory, '.data-disk-ids')
+  id_list = []
+  if os.path.exists(map_disk_file):
+    with open(map_disk_file, 'r') as mf:
+      # ID are writen in one line: data1 data3 data2 ...
+      content = mf.readline()
+      if content:
+        id_list = [id for id in content.split(' ') if disk_storage_dict.has_key(id)]
+  for key in disk_storage_dict:
+    if not key in id_list:
+      id_list.append(key)
+  with open(map_disk_file, 'w') as mf:
+    mf.write(' '.join(id_list))
+  return id_list
+
 # Download existing hard drive if needed at first boot
 if not os.path.exists(disk_path) and virtual_hard_drive_url != '':
   print('Downloading virtual hard drive...')
@@ -109,6 +122,13 @@ if not os.path.exists(disk_path):
 
 # Check and create external disk
 additional_disk_list = []
+for storage in sorted(disk_storage_list):
+  if storage:
+    key, val = storage.split(' ')
+    disk_storage_dict[key.strip()] = val.strip()
+
+map_storage_list = getMapStorageList(disk_storage_dict)
+
 if disk_storage_dict:
   if int(external_disk_number) > 0:
     index = 0
diff --git a/software/kvm/common.cfg b/software/kvm/common.cfg
index ec440a66c..dd3c7d21f 100644
--- a/software/kvm/common.cfg
+++ b/software/kvm/common.cfg
@@ -93,7 +93,7 @@ mode = 0644
 recipe = hexagonit.recipe.download
 url = ${:_profile_base_location_}/instance-kvm.cfg.jinja2
 mode = 644
-md5sum = 10af5beb60655add4de26f7ae60e32aa
+md5sum = 5506e1df6ba32c6ead647636ebece79e
 download-only = true
 on-update = true
 
diff --git a/software/kvm/instance-kvm.cfg.jinja2 b/software/kvm/instance-kvm.cfg.jinja2
index 55799e3ed..3e343030c 100644
--- a/software/kvm/instance-kvm.cfg.jinja2
+++ b/software/kvm/instance-kvm.cfg.jinja2
@@ -103,6 +103,7 @@ qemu-path =  {{ qemu_executable_location }}
 qemu-img-path = {{ qemu_img_executable_location }}
 6tunnel-path = {{ sixtunnel_executable_location }}
 
+etc-directory = ${directory:etc}
 disk-storage-list = 
 {% for key, path in storage_dict.items() -%}
 {{ '  ' ~ key ~ ' ' ~ path }}
-- 
2.30.9