libvirt_ffi 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +78 -0
- data/Gemfile +7 -3
- data/Rakefile +6 -1
- data/bin/console +1 -0
- data/exe/libvirt +1 -0
- data/lib/libvirt/base_info.rb +34 -0
- data/lib/libvirt/connection.rb +98 -36
- data/lib/libvirt/domain.rb +105 -8
- data/lib/libvirt/domain_callback_storage.rb +13 -15
- data/lib/libvirt/errors.rb +65 -0
- data/lib/libvirt/event.rb +29 -19
- data/lib/libvirt/ffi/common.rb +8 -1
- data/lib/libvirt/ffi/domain.rb +529 -196
- data/lib/libvirt/ffi/error.rb +243 -0
- data/lib/libvirt/ffi/event.rb +30 -36
- data/lib/libvirt/ffi/helpers.rb +17 -0
- data/lib/libvirt/ffi/host.rb +122 -0
- data/lib/libvirt/ffi/storage.rb +149 -0
- data/lib/libvirt/ffi/stream.rb +19 -17
- data/lib/libvirt/ffi.rb +17 -0
- data/lib/libvirt/node_info.rb +2 -41
- data/lib/libvirt/storage_pool.rb +70 -0
- data/lib/libvirt/storage_pool_info.rb +7 -0
- data/lib/libvirt/storage_volume.rb +51 -0
- data/lib/libvirt/storage_volume_info.rb +7 -0
- data/lib/libvirt/stream.rb +21 -14
- data/lib/libvirt/util.rb +61 -8
- data/lib/libvirt/version.rb +1 -1
- data/lib/libvirt/xml/disk.rb +59 -0
- data/lib/libvirt/xml/domain.rb +76 -0
- data/lib/libvirt/xml/generic.rb +252 -0
- data/lib/libvirt/xml/graphics.rb +14 -0
- data/lib/libvirt/xml/max_vcpu.rb +12 -0
- data/lib/libvirt/xml/memory.rb +14 -0
- data/lib/libvirt/xml/storage_pool.rb +24 -0
- data/lib/libvirt/xml/storage_volume.rb +32 -0
- data/lib/libvirt/xml/vcpu.rb +12 -0
- data/lib/libvirt/xml.rb +23 -0
- data/lib/libvirt.rb +12 -12
- data/lib/libvirt_ffi.rb +2 -0
- data/libvirt.gemspec +5 -1
- data/test_usage/support/libvirt_async.rb +27 -35
- data/test_usage/support/log_formatter.rb +5 -10
- data/test_usage/test_domain.rb +43 -0
- data/test_usage/test_event_loop.rb +115 -39
- data/test_usage/test_libvirtd_restart.rb +63 -0
- data/test_usage/test_metadata.rb +104 -0
- data/test_usage/test_screenshot.rb +14 -13
- data/test_usage/test_storage.rb +52 -0
- metadata +42 -6
- data/lib/libvirt/error.rb +0 -6
- data/lib/libvirt/ffi/connection.rb +0 -94
- data/lib/libvirt/ffi/libvirt.rb +0 -17
- data/lib/libvirt/ffi/node_info.rb +0 -37
data/lib/libvirt/ffi/stream.rb
CHANGED
@@ -3,28 +3,31 @@
|
|
3
3
|
module Libvirt
|
4
4
|
module FFI
|
5
5
|
module Stream
|
6
|
+
# https://libvirt.org/html/libvirt-libvirt-stream.html
|
7
|
+
|
6
8
|
extend ::FFI::Library
|
9
|
+
extend Helpers
|
7
10
|
ffi_lib Util.library_path
|
8
11
|
|
9
12
|
# virStreamPtr virStreamNew (
|
10
13
|
# virConnectPtr conn,
|
11
|
-
#
|
14
|
+
# unsigned int flags
|
12
15
|
# )
|
13
16
|
attach_function :virStreamNew, [:pointer, :uint], :pointer
|
14
17
|
|
15
18
|
# typedef void (*virStreamEventCallback) (
|
16
19
|
# virStreamPtr stream,
|
17
|
-
#
|
18
|
-
#
|
20
|
+
# int events,
|
21
|
+
# void * opaque
|
19
22
|
# )
|
20
23
|
callback :virStreamEventCallback, [:pointer, :int, :pointer], :void
|
21
24
|
|
22
|
-
# int
|
25
|
+
# int virStreamEventAddCallback (
|
23
26
|
# virStreamPtr stream,
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
27
|
+
# int events,
|
28
|
+
# virStreamEventCallback cb,
|
29
|
+
# void * opaque,
|
30
|
+
# virFreeCallback ff
|
28
31
|
# )
|
29
32
|
attach_function :virStreamEventAddCallback, [
|
30
33
|
:pointer,
|
@@ -34,39 +37,38 @@ module Libvirt
|
|
34
37
|
FFI::Common::FREE_CALLBACK
|
35
38
|
], :int
|
36
39
|
|
37
|
-
# int
|
40
|
+
# int virStreamEventRemoveCallback (
|
38
41
|
# virStreamPtr stream
|
39
42
|
# )
|
40
43
|
attach_function :virStreamEventRemoveCallback, [:pointer], :int
|
41
44
|
|
42
|
-
# int
|
45
|
+
# int virStreamEventUpdateCallback (
|
43
46
|
# virStreamPtr stream,
|
44
47
|
# int events
|
45
48
|
# )
|
46
49
|
attach_function :virStreamEventUpdateCallback, [:pointer, :int], :int
|
47
50
|
|
48
|
-
# int
|
51
|
+
# int virStreamFinish (
|
49
52
|
# virStreamPtr stream
|
50
53
|
# )
|
51
54
|
attach_function :virStreamFinish, [:pointer], :int
|
52
55
|
|
53
|
-
# int
|
56
|
+
# int virStreamFree (
|
54
57
|
# virStreamPtr stream
|
55
58
|
# )
|
56
59
|
attach_function :virStreamFree, [:pointer], :int
|
57
60
|
|
58
|
-
# int
|
61
|
+
# int virStreamAbort (
|
59
62
|
# virStreamPtr stream
|
60
63
|
# )
|
61
64
|
attach_function :virStreamAbort, [:pointer], :int
|
62
65
|
|
63
|
-
# int
|
66
|
+
# int virStreamRecv (
|
64
67
|
# virStreamPtr stream,
|
65
|
-
#
|
66
|
-
#
|
68
|
+
# char *data,
|
69
|
+
# size_t nbytes
|
67
70
|
# )
|
68
71
|
attach_function :virStreamRecv, [:pointer, :pointer, :size_t], :int
|
69
|
-
|
70
72
|
end
|
71
73
|
end
|
72
74
|
end
|
data/lib/libvirt/ffi.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Libvirt
|
4
|
+
module FFI
|
5
|
+
# https://libvirt.org/html/index.html
|
6
|
+
# namespace for libvirt C bindings
|
7
|
+
|
8
|
+
require 'libvirt/ffi/helpers'
|
9
|
+
require 'libvirt/ffi/common'
|
10
|
+
require 'libvirt/ffi/error'
|
11
|
+
require 'libvirt/ffi/host'
|
12
|
+
require 'libvirt/ffi/domain'
|
13
|
+
require 'libvirt/ffi/event'
|
14
|
+
require 'libvirt/ffi/stream'
|
15
|
+
require 'libvirt/ffi/storage'
|
16
|
+
end
|
17
|
+
end
|
data/lib/libvirt/node_info.rb
CHANGED
@@ -1,46 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Libvirt
|
4
|
-
class NodeInfo
|
5
|
-
|
6
|
-
@node_info_ptr = node_info_ptr
|
7
|
-
@node_info_struct = FFI::NodeInfo::Struct.new(node_info_ptr)
|
8
|
-
end
|
9
|
-
|
10
|
-
def [](attr)
|
11
|
-
@node_info_struct[attr]
|
12
|
-
end
|
13
|
-
|
14
|
-
def model
|
15
|
-
@node_info_struct[:model].to_s
|
16
|
-
end
|
17
|
-
|
18
|
-
def cpus
|
19
|
-
@node_info_struct[:cpus]
|
20
|
-
end
|
21
|
-
|
22
|
-
def mhz
|
23
|
-
@node_info_struct[:mhz]
|
24
|
-
end
|
25
|
-
|
26
|
-
def nodes
|
27
|
-
@node_info_struct[:nodes]
|
28
|
-
end
|
29
|
-
|
30
|
-
def sockets
|
31
|
-
@node_info_struct[:sockets]
|
32
|
-
end
|
33
|
-
|
34
|
-
def cores
|
35
|
-
@node_info_struct[:cores]
|
36
|
-
end
|
37
|
-
|
38
|
-
def threads
|
39
|
-
@node_info_struct[:threads]
|
40
|
-
end
|
41
|
-
|
42
|
-
def memory
|
43
|
-
@node_info_struct[:memory]
|
44
|
-
end
|
4
|
+
class NodeInfo < BaseInfo
|
5
|
+
struct_class FFI::Host::NodeInfoStruct
|
45
6
|
end
|
46
7
|
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Libvirt
|
4
|
+
class StoragePool
|
5
|
+
def self.load_ref(pointer)
|
6
|
+
result = FFI::Storage.virStoragePoolRef(pointer)
|
7
|
+
raise Errors::LibError, "Couldn't retrieve storage pool reference" if result.negative?
|
8
|
+
|
9
|
+
new(pointer)
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(pointer)
|
13
|
+
@ptr = pointer
|
14
|
+
|
15
|
+
free = ->(obj_id) do
|
16
|
+
Util.log(:debug) { "Finalize Libvirt::StoragePool 0x#{obj_id.to_s(16)} @ptr=#{@ptr}," }
|
17
|
+
return unless @ptr
|
18
|
+
|
19
|
+
fr_result = FFI::Storage.virStoragePoolFree(@ptr)
|
20
|
+
warn "Couldn't free Libvirt::StoragePool (0x#{obj_id.to_s(16)}) pointer #{@ptr.address}" if fr_result.negative?
|
21
|
+
end
|
22
|
+
ObjectSpace.define_finalizer(self, free)
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_ptr
|
26
|
+
@ptr
|
27
|
+
end
|
28
|
+
|
29
|
+
def info
|
30
|
+
info_ptr = ::FFI::MemoryPointer.new(FFI::Storage::PoolInfoStruct.by_value)
|
31
|
+
result = FFI::Storage.virStoragePoolGetInfo(@ptr, info_ptr)
|
32
|
+
raise Errors::LibError, "Couldn't get storage pool info" if result.negative?
|
33
|
+
|
34
|
+
StoragePoolInfo.new(info_ptr)
|
35
|
+
end
|
36
|
+
|
37
|
+
def xml_desc(options_or_flags = nil)
|
38
|
+
flags = Util.parse_flags options_or_flags, FFI::Storage.enum_type(:xml_flags)
|
39
|
+
result = FFI::Storage.virStoragePoolGetXMLDesc(@ptr, flags)
|
40
|
+
raise Errors::LibError, "Couldn't get storage pool xml desc" if result.nil?
|
41
|
+
|
42
|
+
result
|
43
|
+
end
|
44
|
+
|
45
|
+
def list_all_volumes_qty
|
46
|
+
result = FFI::Storage.virStoragePoolListAllVolumes(@ptr, nil, 0)
|
47
|
+
raise Errors::LibError, "Couldn't retrieve volumes qty" if result.negative?
|
48
|
+
|
49
|
+
result
|
50
|
+
end
|
51
|
+
|
52
|
+
def list_all_volumes
|
53
|
+
size = list_all_volumes_qty
|
54
|
+
return [] if size.zero?
|
55
|
+
|
56
|
+
storage_volumes_ptr = ::FFI::MemoryPointer.new(:pointer, size)
|
57
|
+
result = FFI::Storage.virStoragePoolListAllVolumes(@ptr, storage_volumes_ptr, 0)
|
58
|
+
raise Errors::LibError, "Couldn't retrieve volumes list" if result.negative?
|
59
|
+
|
60
|
+
ptr = storage_volumes_ptr.read_pointer
|
61
|
+
ptr.get_array_of_pointer(0, size).map { |stv_ptr| StorageVolume.new(stv_ptr) }
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def dbg(&block)
|
67
|
+
Util.log(:debug, 'Libvirt::Domain', &block)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Libvirt
|
4
|
+
class StorageVolume
|
5
|
+
def self.load_ref(pointer)
|
6
|
+
result = FFI::Storage.virStorageVolRef(pointer)
|
7
|
+
raise Errors::LibError, "Couldn't retrieve storage volume reference" if result.negative?
|
8
|
+
|
9
|
+
new(pointer)
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(pointer)
|
13
|
+
@ptr = pointer
|
14
|
+
|
15
|
+
free = ->(obj_id) do
|
16
|
+
Util.log(:debug) { "Finalize Libvirt::StorageVolume 0x#{obj_id.to_s(16)} @ptr=#{@ptr}," }
|
17
|
+
return unless @ptr
|
18
|
+
|
19
|
+
fr_result = FFI::Storage.virStorageVolFree(@ptr)
|
20
|
+
warn "Couldn't free Libvirt::StorageVolume (0x#{obj_id.to_s(16)}) pointer #{@ptr.address}" if fr_result.negative?
|
21
|
+
end
|
22
|
+
ObjectSpace.define_finalizer(self, free)
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_ptr
|
26
|
+
@ptr
|
27
|
+
end
|
28
|
+
|
29
|
+
def info
|
30
|
+
info_ptr = ::FFI::MemoryPointer.new(FFI::Storage::VolumeInfoStruct.by_value)
|
31
|
+
result = FFI::Storage.virStorageVolGetInfo(@ptr, info_ptr)
|
32
|
+
raise Errors::LibError, "Couldn't get storage volume info" if result.negative?
|
33
|
+
|
34
|
+
StorageVolumeInfo.new(info_ptr)
|
35
|
+
end
|
36
|
+
|
37
|
+
def xml_desc(options_or_flags = nil)
|
38
|
+
flags = Util.parse_flags options_or_flags, FFI::Storage.enum_type(:xml_flags)
|
39
|
+
result = FFI::Storage.virStorageVolGetXMLDesc(@ptr, flags)
|
40
|
+
raise Errors::LibError, "Couldn't get storage volume xml desc" if result.nil?
|
41
|
+
|
42
|
+
result
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def dbg(&block)
|
48
|
+
Util.log(:debug, 'Libvirt::Domain', &block)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/libvirt/stream.rb
CHANGED
@@ -12,15 +12,17 @@ module Libvirt
|
|
12
12
|
@opaque = nil
|
13
13
|
|
14
14
|
free = ->(obj_id) do
|
15
|
+
Util.log(:debug) { "Finalize Libvirt::Stream 0x#{obj_id.to_s(16)} @stream_ptr=#{@stream_ptr}, @cb=#{@cb}, @opaque=#{@opaque}," }
|
15
16
|
return unless @stream_ptr
|
17
|
+
|
16
18
|
if @cb
|
17
19
|
rcb_result = FFI::Stream.virStreamEventRemoveCallback(@stream_ptr)
|
18
|
-
|
20
|
+
warn("Couldn't remove callback Libvirt::Stream (0x#{obj_id.to_s(16)}) pointer #{@stream_ptr.address}") if rcb_result.negative?
|
19
21
|
ab_result = FFI::Stream.virStreamAbort(@stream_ptr)
|
20
|
-
|
22
|
+
warn("Couldn't abort Libvirt::Stream (0x#{obj_id.to_s(16)}) pointer #{@stream_ptr.address}") if ab_result.negative?
|
21
23
|
end
|
22
24
|
fr_result = FFI::Stream.virStreamFree(@stream_ptr)
|
23
|
-
|
25
|
+
warn("Couldn't free Libvirt::Stream (0x#{obj_id.to_s(16)}) pointer #{@stream_ptr.address}") if fr_result.negative?
|
24
26
|
end
|
25
27
|
ObjectSpace.define_finalizer(self, free)
|
26
28
|
end
|
@@ -35,16 +37,16 @@ module Libvirt
|
|
35
37
|
def event_add_callback(events, opaque, &block)
|
36
38
|
dbg { "#event_add_callback events=#{events}, opaque=#{opaque}" }
|
37
39
|
|
38
|
-
raise
|
40
|
+
raise Errors::LibError, 'callback already added' unless @cb.nil?
|
39
41
|
|
40
42
|
@opaque = opaque
|
41
|
-
@cb =
|
43
|
+
@cb = FFI::Stream.callback_function(:virStreamEventCallback) do |_stream_ptr, evs, _op|
|
42
44
|
# stream = Stream.new(stream_ptr)
|
43
45
|
block.call(self, evs, @opaque)
|
44
46
|
end
|
45
47
|
|
46
48
|
result = FFI::Stream.virStreamEventAddCallback(@stream_ptr, events, @cb, nil, nil)
|
47
|
-
raise
|
49
|
+
raise Errors::LibError, "Couldn't add stream event callback" if result.negative?
|
48
50
|
|
49
51
|
true
|
50
52
|
end
|
@@ -54,7 +56,8 @@ module Libvirt
|
|
54
56
|
dbg { "#event_update_callback events=#{events}" }
|
55
57
|
|
56
58
|
result = FFI::Stream.virStreamEventUpdateCallback(@stream_ptr, events)
|
57
|
-
raise
|
59
|
+
raise Errors::LibError, "Couldn't remove stream event callback" if result.negative?
|
60
|
+
|
58
61
|
true
|
59
62
|
end
|
60
63
|
|
@@ -62,7 +65,8 @@ module Libvirt
|
|
62
65
|
dbg { '#event_remove_callback' }
|
63
66
|
|
64
67
|
result = FFI::Stream.virStreamEventRemoveCallback(@stream_ptr)
|
65
|
-
raise
|
68
|
+
raise Errors::LibError, "Couldn't remove stream event callback" if result.negative?
|
69
|
+
|
66
70
|
opaque = @opaque
|
67
71
|
@cb = nil
|
68
72
|
@opaque = nil
|
@@ -71,21 +75,24 @@ module Libvirt
|
|
71
75
|
|
72
76
|
def finish
|
73
77
|
result = FFI::Stream.virStreamFinish(@stream_ptr)
|
74
|
-
raise
|
78
|
+
raise Errors::LibError, "Couldn't remove stream event callback" if result.negative?
|
79
|
+
|
75
80
|
@cb = nil
|
76
81
|
@opaque = nil
|
77
82
|
end
|
78
83
|
|
79
84
|
def abort_stream
|
80
85
|
result = FFI::Stream.virStreamAbort(@stream_ptr)
|
81
|
-
raise
|
86
|
+
raise Errors::LibError, "Couldn't remove stream event callback" if result.negative?
|
87
|
+
|
82
88
|
@cb = nil
|
83
89
|
@opaque = nil
|
84
90
|
end
|
85
91
|
|
86
92
|
def free_stream
|
87
93
|
result = FFI::Stream.virStreamFree(@stream_ptr)
|
88
|
-
raise
|
94
|
+
raise Errors::LibError, "Couldn't free stream event callback" if result.negative?
|
95
|
+
|
89
96
|
@cb = nil
|
90
97
|
@opaque = nil
|
91
98
|
@stream_ptr = nil
|
@@ -97,14 +104,14 @@ module Libvirt
|
|
97
104
|
if result == -1
|
98
105
|
abort_stream
|
99
106
|
[-1, nil]
|
100
|
-
elsif result
|
107
|
+
elsif result.zero?
|
101
108
|
[0, nil]
|
102
109
|
elsif result == -2
|
103
110
|
[-2, nil]
|
104
|
-
elsif result
|
111
|
+
elsif result.positive?
|
105
112
|
[result, buffer.read_bytes(result)]
|
106
113
|
else
|
107
|
-
raise
|
114
|
+
raise Errors::LibError, "Invalid response from virStreamRecv #{result.inspect}"
|
108
115
|
end
|
109
116
|
end
|
110
117
|
|
data/lib/libvirt/util.rb
CHANGED
@@ -2,23 +2,36 @@
|
|
2
2
|
|
3
3
|
module Libvirt
|
4
4
|
module Util
|
5
|
-
|
5
|
+
UNIT_TO_BYTES = {
|
6
|
+
b: 1,
|
7
|
+
bytes: 1,
|
8
|
+
KB: 1_000,
|
9
|
+
KiB: 1_024,
|
10
|
+
k: 1_024,
|
11
|
+
MB: 1_000_000,
|
12
|
+
M: 1_048_576,
|
13
|
+
MiB: 1_048_576,
|
14
|
+
GB: 1_000_000_000,
|
15
|
+
G: 1_073_741_824,
|
16
|
+
GiB: 1_073_741_824,
|
17
|
+
TB: 1_000_000_000_000,
|
18
|
+
T: 1_099_511_627_776,
|
19
|
+
TiB: 1_099_511_627_776
|
20
|
+
}.freeze
|
6
21
|
|
7
|
-
|
8
|
-
|
9
|
-
end
|
22
|
+
class << self
|
23
|
+
attr_writer :logger
|
10
24
|
|
11
|
-
|
12
|
-
@logger
|
13
|
-
end
|
25
|
+
attr_reader :logger
|
14
26
|
|
15
27
|
def log(severity, prog = nil, &block)
|
16
28
|
return if @logger.nil?
|
29
|
+
|
17
30
|
@logger.public_send(severity, prog, &block)
|
18
31
|
end
|
19
32
|
|
20
33
|
def library_path
|
21
|
-
%w
|
34
|
+
%w[libvirt libvirt.so.0]
|
22
35
|
end
|
23
36
|
|
24
37
|
# @param [Integer] version_number ulong
|
@@ -36,13 +49,53 @@ module Libvirt
|
|
36
49
|
def parse_enum(enum, value)
|
37
50
|
if value.is_a?(Symbol)
|
38
51
|
raise ArgumentError, 'invalid enum value' unless enum.symbols.include?(value)
|
52
|
+
|
39
53
|
return [enum.find(value), value]
|
40
54
|
end
|
41
55
|
|
42
56
|
raise ArgumentError, 'invalid enum value' unless enum.symbol_map.values.include?(value)
|
57
|
+
|
43
58
|
[value, enum.symbol_map[value]]
|
44
59
|
end
|
45
60
|
|
61
|
+
# Bitwise OR integer flags calculation for C language.
|
62
|
+
# @param flags [Integer,Symbol,Array<Symbol>,Hash{Symbol=>Boolean},nil]
|
63
|
+
# @param enum [FFI::Enum]
|
64
|
+
# @param default [Integer] optional (default 0x0)
|
65
|
+
# @return [Integer] bitwise OR of keys
|
66
|
+
# @example Usage:
|
67
|
+
# parse_flags(nil, enum)
|
68
|
+
# parse_flags({MANAGED_SAVE: true, SNAPSHOTS_METADATA: true, NVRAM: false}, enum)
|
69
|
+
# parse_flags({managed_save: true, snapshots_metadata: true, keep_nvram: nil}, enum)
|
70
|
+
# parse_flags(3, enum)
|
71
|
+
# parse_flags([:MANAGED_SAVE, :SNAPSHOTS_METADATA], enum)
|
72
|
+
# parse_flags([:managed_save, :snapshots_metadata], enum)
|
73
|
+
#
|
74
|
+
def parse_flags(flags, enum, default: 0x0)
|
75
|
+
flags = default if flags.nil?
|
76
|
+
flags = enum[flags] if flags.is_a?(Symbol)
|
77
|
+
return flags if flags.is_a?(Integer)
|
78
|
+
|
79
|
+
result = 0x0
|
80
|
+
flags = flags.select { |_, v| v }.keys if flags.is_a?(Hash)
|
81
|
+
|
82
|
+
raise ArgumentError, 'flags must be an Integer or a Hash or an Array' unless flags.is_a?(Array)
|
83
|
+
|
84
|
+
flags.each do |key|
|
85
|
+
result |= enum[key.to_s.upcase.to_sym]
|
86
|
+
end
|
87
|
+
|
88
|
+
result
|
89
|
+
end
|
90
|
+
|
91
|
+
# @param value [Integer,String]
|
92
|
+
# @param unit [String,Symbol] default 'bytes'
|
93
|
+
# @return [Integer] memory in bytes
|
94
|
+
def parse_memory(value, unit)
|
95
|
+
unit ||= 'bytes'
|
96
|
+
multiplier = UNIT_TO_BYTES.fetch(unit.to_sym)
|
97
|
+
Integer(value) * multiplier
|
98
|
+
end
|
46
99
|
end
|
47
100
|
end
|
48
101
|
end
|
data/lib/libvirt/version.rb
CHANGED
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Libvirt
|
4
|
+
module Xml
|
5
|
+
class Disk < Generic
|
6
|
+
# https://libvirt.org/formatdomain.html#elementsDisks
|
7
|
+
|
8
|
+
attribute :type, type: :attr
|
9
|
+
attribute :device, type: :attr
|
10
|
+
attribute :model, type: :attr
|
11
|
+
attribute :raw_io, type: :attr, name: :rawio, cast: :bool
|
12
|
+
attribute :sg_io, type: :attr, name: :sgio
|
13
|
+
attribute :snapshot, type: :attr
|
14
|
+
attribute :source_file, name: :file, path: './source', type: :attr
|
15
|
+
attribute :source_dev, name: :dev, path: './source', type: :attr
|
16
|
+
attribute :source_dir, name: :dir, path: './source', type: :attr
|
17
|
+
attribute :source_protocol, name: :protocol, path: './source', type: :attr
|
18
|
+
attribute :source_pool, name: :pool, path: './source', type: :attr
|
19
|
+
attribute :source_volume, name: :volume, path: './source', type: :attr
|
20
|
+
attribute :source_mode, name: :mode, path: './source', type: :attr
|
21
|
+
attribute :source_name, name: :name, path: './source', type: :attr
|
22
|
+
attribute :source_type, name: :type, path: './source', type: :attr
|
23
|
+
attribute :source_managed, name: :managed, path: './source', type: :attr, cast: :bool
|
24
|
+
attribute :source_namespace, name: :namespace, path: './source', type: :attr
|
25
|
+
attribute :source_index, name: :index, path: './source', type: :attr
|
26
|
+
attribute :source_host_name, name: :name, path: './source/host', type: :attr
|
27
|
+
attribute :source_host_port, name: :port, path: './source/host', type: :attr
|
28
|
+
attribute :source_host_transport, name: :transport, path: './source/host', type: :attr
|
29
|
+
attribute :source_host_socket, name: :socket, path: './source/host', type: :attr
|
30
|
+
attribute :source_snapshot_name, name: :name, path: './source/snapshot', type: :attr
|
31
|
+
attribute :source_config_file, name: :file, path: './source/config', type: :attr
|
32
|
+
# TODO: source/auth
|
33
|
+
# TODO: source/encryption
|
34
|
+
# TODO: source/reservations
|
35
|
+
# TODO: source/initiator
|
36
|
+
# TODO: source/address
|
37
|
+
# TODO: source/slices
|
38
|
+
# TODO: backingStore
|
39
|
+
# TODO: mirror
|
40
|
+
# TODO: target
|
41
|
+
# TODO: iotune
|
42
|
+
# TODO: driver
|
43
|
+
# TODO: backenddomain
|
44
|
+
# TODO: boot
|
45
|
+
# TODO: encryption
|
46
|
+
# TODO: readonly
|
47
|
+
# TODO: shareable
|
48
|
+
# TODO: transient
|
49
|
+
# TODO: serial
|
50
|
+
# TODO: wwn
|
51
|
+
# TODO: vendor
|
52
|
+
# TODO: product
|
53
|
+
# TODO: address
|
54
|
+
# TODO: auth
|
55
|
+
# TODO: geometry
|
56
|
+
# TODO: blockio
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Libvirt
|
4
|
+
module Xml
|
5
|
+
class Domain < Generic
|
6
|
+
# https://libvirt.org/formatdomain.html
|
7
|
+
|
8
|
+
root_path './domain'
|
9
|
+
|
10
|
+
attribute :name
|
11
|
+
attribute :uuid
|
12
|
+
attribute :gen_id, path: './genid'
|
13
|
+
attribute :title
|
14
|
+
attribute :description
|
15
|
+
attribute :metadata, type: :raw
|
16
|
+
attribute :vcpu, type: :struct, class: MaxVcpu
|
17
|
+
attribute :vcpus, type: :struct, array: true, class: Vcpu, cast: ->(objects) { objects.sort_by(&:id) }
|
18
|
+
attribute :memory, type: :struct, class: Memory
|
19
|
+
attribute :current_memory, type: :struct, class: Memory
|
20
|
+
attribute :max_memory, type: :struct, class: Memory
|
21
|
+
attribute :resource_partitions, path: './resource/partition', array: true
|
22
|
+
attribute :on_power_off
|
23
|
+
attribute :on_reboot
|
24
|
+
attribute :on_crash
|
25
|
+
attribute :on_lock_failure
|
26
|
+
attribute :device_graphics, type: :struct, path: './devices/graphics', class: Graphics, array: true
|
27
|
+
attribute :device_disks, type: :struct, path: './devices/disk', class: Disk, array: true
|
28
|
+
# https://libvirt.org/formatdomain.html#elementsDevices
|
29
|
+
# todo devices/emulator
|
30
|
+
# todo devices/interface
|
31
|
+
# todo devices/filesystem
|
32
|
+
# todo devices/controller
|
33
|
+
# todo devices/lease
|
34
|
+
# todo devices/hostdev
|
35
|
+
# todo devices/redirdev
|
36
|
+
# todo devices/smartcard
|
37
|
+
# todo devices/input
|
38
|
+
# todo devices/hub
|
39
|
+
# todo devices/video
|
40
|
+
# todo devices/parallel
|
41
|
+
# todo devices/serial
|
42
|
+
# todo devices/console
|
43
|
+
# todo devices/channel
|
44
|
+
# todo devices/sound
|
45
|
+
# todo devices/watchdog
|
46
|
+
# todo devices/memballoon
|
47
|
+
# todo devices/rng
|
48
|
+
# todo devices/tpm
|
49
|
+
# todo devices/nvram
|
50
|
+
# todo devices/panic
|
51
|
+
# todo devices/shmem
|
52
|
+
# todo devices/memory
|
53
|
+
# todo devices/iommu
|
54
|
+
# todo devices/vsock
|
55
|
+
# todo os
|
56
|
+
# todo bootloader
|
57
|
+
# todo bootloader_args
|
58
|
+
# todo sysinfo
|
59
|
+
# todo iothreads
|
60
|
+
# todo iothreadids
|
61
|
+
# todo cputune
|
62
|
+
# todo memoryBacking
|
63
|
+
# todo memtune
|
64
|
+
# todo numatune
|
65
|
+
# todo blkiotune
|
66
|
+
# todo cpu
|
67
|
+
# todo pm
|
68
|
+
# todo features
|
69
|
+
# todo clock
|
70
|
+
# todo perf
|
71
|
+
# todo seclabel
|
72
|
+
# todo keywrap
|
73
|
+
# todo launchSecurity
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|