[gnome-boxes/wip/image-import: 1/10] Add InstalledMedia class



commit 3f9befe41568ba1f9970caaed1122d041c71dd69
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Wed Jun 5 04:21:02 2013 +0300

    Add InstalledMedia class
    
    A new subclass of InstallerMedia that represents ready-made/installed
    media in the form of raw or qcow2 images.
    
    This patch adds direct (runtime only) dependency on qemu-img binary.

 src/Makefile.am          |    1 +
 src/installed-media.vala |   92 ++++++++++++++++++++++++++++++++++++++++++++++
 src/installer-media.vala |    4 +-
 3 files changed, 95 insertions(+), 2 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 3c1b7c1..5035778 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -87,6 +87,7 @@ gnome_boxes_SOURCES =                         \
        editable-entry.vala                     \
        i-properties-provider.vala              \
        installer-media.vala                    \
+       installed-media.vala                    \
        iso-extractor.vala                      \
        libvirt-broker.vala                     \
        libvirt-machine.vala                    \
diff --git a/src/installed-media.vala b/src/installed-media.vala
new file mode 100644
index 0000000..296f4f6
--- /dev/null
+++ b/src/installed-media.vala
@@ -0,0 +1,92 @@
+// This file is part of GNOME Boxes. License: LGPLv2+
+
+using GVirConfig;
+
+private class Boxes.InstalledMedia : Boxes.InstallerMedia {
+    public override bool need_user_input_for_vm_creation { get { return false; } }
+    public override bool ready_to_create { get { return true; } }
+    public override bool live { get { return false; } }
+
+    public string format { get { return device_file.has_suffix (".qcow2")? "qcow2" : "raw"; } }
+
+    private string architecture {
+        owned get {
+            // Many distributors provide arch name on the image file so lets try to use that if possible
+            if (device_file.contains ("amd64") || device_file.contains ("x86_64"))
+                return "x86_64";
+            else {
+                string[] arch_list = { "i686", "i586", "i486", "i386" };
+                foreach (var arch in arch_list) {
+                    if (device_file.contains (arch))
+                        return arch;
+                }
+
+                debug ("Failed to guess architecture for media '%s', assuming 'x86_64'", device_file);
+
+                return "x86_64";
+            }
+        }
+    }
+
+    private bool converted;
+
+    public InstalledMedia (string path) throws GLib.Error {
+        if (!path.has_suffix (".qcow2") && !path.has_suffix (".img"))
+            throw new Boxes.Error.INVALID (_("Only QEMU QCOW Image (v2) and raw formats supported."));
+
+        device_file = path;
+        from_image = true;
+
+        resources = OSDatabase.get_default_resources ();
+        label_setup ();
+    }
+
+    public async void convert_to_native_format () throws GLib.Error {
+        if (device_file.has_suffix (".qcow2"))
+            return;
+
+        var converted_path = get_user_pkgcache (Path.get_basename (device_file) + ".qcow2");
+        string[] argv = { "qemu-img", "convert", "-O", "qcow2", device_file, converted_path };
+
+        debug ("Converting '%s' to 'qcow2' format", device_file);
+        yield exec (argv, null);
+        debug ("Finished converting '%s' to 'qcow2' format", device_file);
+
+        device_file = converted_path;
+        converted = true;
+    }
+
+    public override void clean_up () {
+        base.clean_up ();
+
+        if (!converted)
+            return;
+
+        var file = File.new_for_path (device_file);
+        try {
+            delete_file (file);
+        } catch (GLib.Error error) {
+            warning ("Failed to delete '%s': %s", device_file, error.message);
+        }
+    }
+
+    public override void setup_domain_config (Domain domain) {}
+
+    public override GLib.List<Pair<string,string>> get_vm_properties () {
+        var properties = new GLib.List<Pair<string,string>> ();
+
+        properties.append (new Pair<string,string> (_("System"), label));
+
+        return properties;
+    }
+
+    public override bool is_architecture_compatible (string architecture) {
+        var compatibility = compare_cpu_architectures (architecture, this.architecture);
+
+        return compatibility != CPUArchCompatibility.INCOMPATIBLE;
+    }
+
+    public override VMCreator get_vm_creator () {
+        return new VMImporter (this);
+    }
+}
diff --git a/src/installer-media.vala b/src/installer-media.vala
index de671b2..26028a8 100644
--- a/src/installer-media.vala
+++ b/src/installer-media.vala
@@ -32,7 +32,7 @@ private class Boxes.InstallerMedia : GLib.Object {
         }
     }
 
-    public bool live { get { return os_media == null || os_media.live; } }
+    public virtual bool live { get { return os_media == null || os_media.live; } }
 
     public InstallerMedia.from_iso_info (string           path,
                                          string           label,
@@ -97,7 +97,7 @@ private class Boxes.InstallerMedia : GLib.Object {
         return properties;
     }
 
-    public bool is_architecture_compatible (string architecture) {
+    public virtual bool is_architecture_compatible (string architecture) {
         if (os_media == null) // Unknown media
             return true;
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]