Commits

Kirill Simonov committed b4cc6fe

Chain and compress VM images.

Comments (0)

Files changed (2)

     shutil.copy(src_filename, dst_filename)
 
 
+def mv(src_filename, dst_filename):
+    # Rename a file.
+    debug("moving: %s => %s" % (src_filename, dst_filename))
+    os.rename(src_filename, dst_filename)
+
+
 def rm(filename):
     # Remove a file.
     debug("removing: %s" % filename)
 
 
 from job import (job, settings, run, pipe, exe, log, debug, warn, fatal, prompt,
-                 ls, cp, rm, mktree, rmtree)
+                 ls, cp, mv, rm, mktree, rmtree)
 import os, os.path, urllib2, socket, datetime, time, re, tempfile
 
 
         # Run `kvm-img create -f qcow2 <img_path> <DISK_SIZE>`.
         run("kvm-img create -f qcow2 %s %s" % (self.img_path, DISK_SIZE))
 
+    def compress(self, backing_name=None):
+        # Run `kvm-img convert -c ...`.
+        opts = "convert -c -f qcow2 -O qcow2"
+        if backing_name:
+            opts += " -o backing_file=%s.qcow2" % backing_name
+        run("kvm-img %s %s.qcow2 %s-compressed.qcow2"
+            % (opts, self.name, self.name), cd=IMG_DIR)
+        mv(IMG_DIR+"/%s-compressed.qcow2" % self.name,
+           IMG_DIR+"/%s.qcow2" % self.name)
+
     def kvm(self, opts=None):
         # Run `kvm`.
         net_model = "virtio"
             self.kvm_img()
             self.kvm("-cdrom %s -boot d" % iso_path)
             rm(iso_path)
+            self.compress()
         except:
             if os.path.exists(self.img_path):
                 rm(self.img_path)
             self.kvm_img()
             self.kvm("-cdrom %s -boot d" % iso_path)
             rm(iso_path)
+            self.compress()
         except:
             if os.path.exists(self.img_path):
                 rm(self.img_path)
             self.kvm_img()
             self.kvm("-cdrom %s -boot d" % iso_path)
             rm(iso_path)
+            self.compress()
         except:
             if os.path.exists(self.img_path):
                 rm(self.img_path)
         log("building VM: `%s`..." % self.name)
         start_time = datetime.datetime.now()
         try:
-            cp(parent_vm.img_path, self.img_path)
+            run("kvm-img create -b %s.qcow2 -f qcow2 %s.qcow2"
+                % (parent_vm.name, self.name), cd=IMG_DIR)
             self.kvm("-daemonize")
             time.sleep(60.0)
             self.put(DATA_ROOT+"/vm/%s-update.sh" % self.name, "/root/update.sh")
             self.run("rm /root/update.sh")
             self.run("poweroff")
             self.wait()
+            self.compress(parent_vm.name)
             self.kvm("-daemonize")
             time.sleep(60.0)
             self.ctl("savevm %s" % self.state)
                      " /v %s /t REG_SZ /d 'C:\INSTALL\UPDATE.CMD' /f" % self.name)
             self.run("shutdown /r /t 0 /f")
             self.wait()
+            self.compress(parent_vm.name)
             self.kvm("-daemonize")
             time.sleep(120.0)
             self.ctl("savevm %s" % self.state)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.