[ruby-libvirt 2/2] Add volume upload example

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

 



---
 examples/upload_volume.rb | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100644 examples/upload_volume.rb

diff --git a/examples/upload_volume.rb b/examples/upload_volume.rb
new file mode 100644
index 0000000..a051d46
--- /dev/null
+++ b/examples/upload_volume.rb
@@ -0,0 +1,36 @@
+require 'libvirt'
+
+abort("No file to upload given") unless ARGV.length > 0
+
+file_path = ARGV[0]
+
+c = Libvirt::open("qemu:///system")
+
+vol_xml = <<EOF
+<volume>
+  <name>#{File.basename(file_path)}</name>
+  <allocation unit="G">0</allocation>
+  <capacity unit="b">#{File.size(file_path)}</capacity>
+</volume>
+EOF
+
+pool = c.lookup_storage_pool_by_name("default")
+volume = pool.create_volume_xml(vol_xml)
+stream = c.stream
+
+image_file = File.open(file_path, "rb")
+volume.upload(stream, 0, image_file.size)
+stream.sendall do |_opaque, n|
+begin
+  r = image_file.read(n)
+  # This works with and without 03f18670c79e6664fb424d6731f95ea2be4531f4
+  r ? [r.length, r] : [0, ""]
+  # This works with 03f18670c79e6664fb424d6731f95ea2be4531f4 and matches the
+  # docs
+  r ? [0, r] : [0, ""]
+  rescue Exception => e
+    $stderr.puts "Got exception #{e}"
+    [-1, ""]
+  end
+end
+stream.finish
-- 
2.8.1

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]