[gnome-boxes/wip/image-import: 18/27] Add InstalledMedia class



commit 4af0a3ce9e261f4af9c361c773986f59361eba30
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. It also provides means to
    convert raw image to our native (qcow2) format.
    
    This patch adds direct (runtime only) dependency on qemu-img binary.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=690757

 src/Makefile.am          |    1 +
 src/installed-media.vala |   64 ++++++++++++++++++++++++++++++++++++++++++++++
 src/installer-media.vala |    2 +-
 3 files changed, 66 insertions(+), 1 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..20facc1
--- /dev/null
+++ b/src/installed-media.vala
@@ -0,0 +1,64 @@
+// 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"; } }
+
+    protected override 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'.", device_file);
+                return null;
+            }
+        }
+    }
+
+    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 (string destination_path) throws GLib.Error {
+        string[] argv = { "qemu-img", "convert", "-O", "qcow2", device_file, destination_path };
+
+        var converting = !device_file.has_suffix (".qcow2");
+
+        debug ("Copying '%s' to '%s'%s.",
+               device_file,
+               destination_path,
+               converting? " while converting it to 'qcow2' format" : "");
+        yield exec (argv, null);
+        debug ("Finished copying '%s' to '%s'", device_file, destination_path);
+    }
+
+    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;
+    }
+
+}
diff --git a/src/installer-media.vala b/src/installer-media.vala
index c687c10..d739d8e 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; } }
 
     protected virtual string? architecture {
         owned get {


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