libvirt_ffi 0.5.6 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dc49a5a9986110313860c3dd0390c7485e598b4407a52d35009381c1255ca9c5
4
- data.tar.gz: 6ebd4be029bba4e8a50dc0efc996d91e357e8d7616b1f64f66dde66503dcfc21
3
+ metadata.gz: bf7335e98f7be6c9145bad5d2fdc6322b9dda60f31ddb92afd2ce76ebfd95d34
4
+ data.tar.gz: 2c8fe7a75a8778709860128b466faa04d511ed1eb6eec0913a15bee9202a1d67
5
5
  SHA512:
6
- metadata.gz: 0d9716eca3d06aa6d0e3479e50c1bb93142968a9b34ad3df59a35a53dfab5b651f59d49e096a89362275700b2cdfd72f6b9832c6bbd187645fb3d236e6478afe
7
- data.tar.gz: cac12813a17c8ef4a6bd41ebbed45437d3c3908254866435bcb3bdeb17b457e65c6107c72b4fdd9b553699af2ee8608d2a6a56b6d4bc10ac0437d932e1acd15d
6
+ metadata.gz: 756e46ad598baa4996961a191398b924059333ea248ae4df26c950a340a8a683e44a3bef80995f07b1d2968e56be3a27965c882dafc891f879114c703ab1cd4c
7
+ data.tar.gz: eb82fede907f3c40474a894d428da233058ef502e6f0fe891c135c1733fe3bcb02f54df0951188bd0a8c779e1800b309ecc4b985d6ca8e504adbeff13f8143f3
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'ffi'
4
4
  require 'objspace'
5
- require 'libvirt/domain_callback_storage'
5
+ require 'libvirt/host_callback_storage'
6
6
  require 'libvirt/util'
7
7
  require 'libvirt/errors'
8
8
  require 'libvirt/ffi'
@@ -3,19 +3,37 @@
3
3
  module Libvirt
4
4
  class Connection
5
5
  DOMAIN_EVENT_IDS = FFI::Domain.enum_type(:event_id).symbols.dup.freeze
6
+ POOL_EVENT_IDS = FFI::Storage.enum_type(:event_id).symbols.dup.freeze
6
7
 
7
- STORAGE = DomainCallbackStorage.new
8
+ DOMAIN_STORAGE = HostCallbackStorage.new
9
+ POOL_STORAGE = HostCallbackStorage.new
8
10
 
9
11
  DOMAIN_EVENT_CALLBACKS = DOMAIN_EVENT_IDS.map do |event_id_sym|
10
12
  func = FFI::Domain.event_callback_for(event_id_sym) do |conn_ptr, dom_ptr, *args, op_ptr|
13
+ Util.log(:debug, "DOMAIN_EVENT_CALLBACKS[#{event_id_sym}]") do
14
+ "inside callback conn_ptr=#{conn_ptr}, pool_ptr=#{dom_ptr}, args=#{args}, op_ptr=#{op_ptr}"
15
+ end
11
16
  connection = Connection.load_ref(conn_ptr)
12
17
  domain = Domain.load_ref(dom_ptr)
13
- block, opaque = STORAGE.retrieve_from_pointer(op_ptr)
18
+ block, opaque = DOMAIN_STORAGE.retrieve_from_pointer(op_ptr)
14
19
  block.call(connection, domain, *args, opaque)
15
20
  end
16
21
  [event_id_sym, func]
17
22
  end.to_h.freeze
18
23
 
24
+ POOL_EVENT_CALLBACKS = POOL_EVENT_IDS.map do |event_id_sym|
25
+ func = FFI::Storage.event_callback_for(event_id_sym) do |conn_ptr, pool_ptr, *args, op_ptr|
26
+ Util.log(:debug, "POOL_EVENT_CALLBACKS[#{event_id_sym}]") do
27
+ "inside callback conn_ptr=#{conn_ptr}, pool_ptr=#{pool_ptr}, args=#{args}, op_ptr=#{op_ptr}"
28
+ end
29
+ connection = Connection.load_ref(conn_ptr)
30
+ pool = StoragePool.load_ref(pool_ptr)
31
+ block, opaque = POOL_STORAGE.retrieve_from_pointer(op_ptr)
32
+ block.call(connection, pool, *args, opaque)
33
+ end
34
+ [event_id_sym, func]
35
+ end.to_h.freeze
36
+
19
37
  def self.load_ref(conn_ptr)
20
38
  ref_result = FFI::Host.virConnectRef(conn_ptr)
21
39
  raise Errors::LibError, "Couldn't retrieve connection reference" if ref_result.negative?
@@ -163,7 +181,7 @@ module Libvirt
163
181
  event_id, event_id_sym = Util.parse_enum(enum, event_id)
164
182
  cb = DOMAIN_EVENT_CALLBACKS.fetch(event_id_sym)
165
183
 
166
- cb_data, cb_data_free_func = STORAGE.allocate_struct
184
+ cb_data, cb_data_free_func = DOMAIN_STORAGE.allocate_struct
167
185
 
168
186
  result = FFI::Domain.virConnectDomainEventRegisterAny(
169
187
  @conn_ptr,
@@ -178,7 +196,7 @@ module Libvirt
178
196
  raise Errors::LibError, "Couldn't register domain event callback"
179
197
  end
180
198
 
181
- STORAGE.store_struct(
199
+ DOMAIN_STORAGE.store_struct(
182
200
  cb_data,
183
201
  connection_pointer: @conn_ptr,
184
202
  callback_id: result,
@@ -195,10 +213,42 @@ module Libvirt
195
213
  raise Errors::LibError, "Couldn't deregister domain event callback" if result.negative?
196
214
 
197
215
  # virConnectDomainEventDeregisterAny will call free func
198
- # So we don't need to remove object from STORAGE here.
216
+ # So we don't need to remove object from DOMAIN_STORAGE here.
199
217
  true
200
218
  end
201
219
 
220
+ def register_storage_pool_event_callback(event_id, storage_pool = nil, opaque = nil, &block)
221
+ dbg { "#register_storage_pool_event_callback event_id=#{event_id}" }
222
+
223
+ enum = FFI::Storage.enum_type(:event_id)
224
+ event_id, event_id_sym = Util.parse_enum(enum, event_id)
225
+ cb = POOL_EVENT_CALLBACKS.fetch(event_id_sym)
226
+
227
+ cb_data, cb_data_free_func = POOL_STORAGE.allocate_struct
228
+
229
+ result = FFI::Storage.virConnectStoragePoolEventRegisterAny(
230
+ @conn_ptr,
231
+ storage_pool&.to_ptr,
232
+ event_id,
233
+ cb,
234
+ cb_data.pointer,
235
+ cb_data_free_func
236
+ )
237
+ if result.negative?
238
+ cb_data.pointer.free
239
+ raise Errors::LibError, "Couldn't register storage pool event callback"
240
+ end
241
+
242
+ POOL_STORAGE.store_struct(
243
+ cb_data,
244
+ connection_pointer: @conn_ptr,
245
+ callback_id: result,
246
+ cb: block,
247
+ opaque: opaque
248
+ )
249
+ result
250
+ end
251
+
202
252
  def lib_version
203
253
  version_ptr = ::FFI::MemoryPointer.new(:ulong)
204
254
  result = FFI::Host.virConnectGetLibVersion(@conn_ptr, version_ptr)
@@ -38,7 +38,7 @@ module Libvirt
38
38
  end
39
39
 
40
40
  def uuid
41
- buff = ::FFI::MemoryPointer.new(:char, FFI::Domain::UUID_STRING_BUFLEN)
41
+ buff = ::FFI::MemoryPointer.new(:char, Util::UUID_STRING_BUFLEN)
42
42
  result = FFI::Domain.virDomainGetUUIDString(@dom_ptr, buff)
43
43
  raise Errors::LibError, "Couldn't get domain uuid" if result.negative?
44
44
 
@@ -46,7 +46,10 @@ module Libvirt
46
46
  end
47
47
 
48
48
  def name
49
- FFI::Domain.virDomainGetName(@dom_ptr)
49
+ result = FFI::Domain.virDomainGetName(@dom_ptr)
50
+ raise Errors::LibError, "Couldn't retrieve storage pool name" if result.nil?
51
+
52
+ result
50
53
  end
51
54
 
52
55
  def max_vcpus
@@ -9,8 +9,6 @@ module Libvirt
9
9
  extend Helpers
10
10
  ffi_lib Util.library_path
11
11
 
12
- UUID_STRING_BUFLEN = 0x80 # RFC4122
13
-
14
12
  EVENT_ID_TO_CALLBACK = {
15
13
  LIFECYCLE: :virConnectDomainEventCallback,
16
14
  REBOOT: :virConnectDomainEventGenericCallback,
@@ -812,6 +810,10 @@ module Libvirt
812
810
  detail_enum[detail]
813
811
  end
814
812
 
813
+ # Converts state reason of domain from integer to symbol name.
814
+ # @param state [Symbol] enum :state (virDomainState)
815
+ # @param reason [Integer]
816
+ # @return [Symbol]
815
817
  def self.state_reason(state, reason)
816
818
  reason_enum = enum_type(:"#{state.to_s.downcase}_reason")
817
819
  reason_enum[reason]
@@ -31,6 +31,14 @@ module Libvirt
31
31
  :threads, :uint
32
32
  end
33
33
 
34
+ # enum virConnectCloseReason
35
+ enum :close_reason, [
36
+ :ERROR, 0x0, # Misc I/O error
37
+ :EOF, 0x1, # End-of-file from server
38
+ :KEEPALIVE, 0x2, # Keepalive timer triggered
39
+ :CLIENT, 0x3 # Client requested it
40
+ ]
41
+
34
42
  # int virGetVersion (
35
43
  # unsigned long *libVer,
36
44
  # const char *type,
@@ -49,7 +57,7 @@ module Libvirt
49
57
  # int reason,
50
58
  # void * opaque
51
59
  # )
52
- callback :virConnectCloseFunc, [:pointer, :int, :pointer], :void
60
+ callback :virConnectCloseFunc, [:pointer, :close_reason, :pointer], :void
53
61
 
54
62
  # virConnectPtr virConnectOpen (const char * name)
55
63
  attach_function :virConnectOpen, [:string], :pointer
@@ -9,6 +9,11 @@ module Libvirt
9
9
  extend Helpers
10
10
  ffi_lib Util.library_path
11
11
 
12
+ EVENT_ID_TO_CALLBACK = {
13
+ LIFECYCLE: :virConnectStoragePoolEventLifecycleCallback,
14
+ REFRESH: :virConnectStoragePoolEventGenericCallback
15
+ }.freeze
16
+
12
17
  # enum virStoragePoolState
13
18
  enum :pool_state, [
14
19
  :INACTIVE, 0x0, # Not running
@@ -63,6 +68,23 @@ module Libvirt
63
68
  :INACTIVE, 0x1 # dump inactive pool/volume information
64
69
  ]
65
70
 
71
+ # enum virStoragePoolEventID
72
+ enum :event_id, [
73
+ :LIFECYCLE, 0x0, # virConnectStoragePoolEventLifecycleCallback
74
+ :REFRESH, 0x1 # virConnectStoragePoolEventGenericCallback
75
+ ]
76
+
77
+ # enum virStoragePoolEventLifecycleType
78
+ enum :event_lifecycle_type, [
79
+ :DEFINED, 0x0,
80
+ :UNDEFINED, 0x1,
81
+ :STARTED, 0x2,
82
+ :STOPPED, 0x3,
83
+ :CREATED, 0x4,
84
+ :DELETED, 0x5,
85
+ :LAST, 0x6
86
+ ]
87
+
66
88
  # struct virStoragePoolInfo {
67
89
  # int state # virStoragePoolState flags
68
90
  # unsigned long long capacity # Logical size bytes
@@ -87,6 +109,26 @@ module Libvirt
87
109
  :allocation, :ulong_long
88
110
  end
89
111
 
112
+ # typedef void (*virConnectStoragePoolEventGenericCallback) (
113
+ # virConnectPtr conn,
114
+ # virStoragePoolPtr pool,
115
+ # void * opaque
116
+ # )
117
+ callback :virConnectStoragePoolEventGenericCallback,
118
+ [:pointer, :pointer, :pointer],
119
+ :void
120
+
121
+ # typedef void (*virConnectStoragePoolEventLifecycleCallback) (
122
+ # virConnectPtr conn,
123
+ # virStoragePoolPtr pool,
124
+ # int event,
125
+ # int detail,
126
+ # void * opaque
127
+ # )
128
+ callback :virConnectStoragePoolEventLifecycleCallback,
129
+ [:pointer, :pointer, :event_lifecycle_type, :int, :pointer],
130
+ :void
131
+
90
132
  # int virConnectListAllStoragePools (
91
133
  # virConnectPtr conn,
92
134
  # virStoragePoolPtr ** pools,
@@ -144,6 +186,49 @@ module Libvirt
144
186
  # unsigned int flags
145
187
  # )
146
188
  attach_function :virStorageVolGetXMLDesc, [:pointer, :xml_flags], :string
189
+
190
+ # int virConnectStoragePoolEventRegisterAny (
191
+ # virConnectPtr conn,
192
+ # virStoragePoolPtr pool,
193
+ # int eventID,
194
+ # virConnectStoragePoolEventGenericCallback cb,
195
+ # void * opaque,
196
+ # virFreeCallback freecb
197
+ # )
198
+ attach_function :virConnectStoragePoolEventRegisterAny,
199
+ [:pointer, :pointer, :event_id, :pointer, :pointer, FFI::Common::FREE_CALLBACK],
200
+ :int
201
+
202
+ # int virConnectStoragePoolEventDeregisterAny (
203
+ # virConnectPtr conn,
204
+ # int callbackID
205
+ # )
206
+ attach_function :virConnectStoragePoolEventDeregisterAny,
207
+ [:pointer, :int],
208
+ :int
209
+
210
+ # int virStoragePoolGetUUIDString (virStoragePoolPtr pool,
211
+ # char * buf)
212
+ attach_function :virStoragePoolGetUUIDString, [:pointer, :pointer], :int
213
+
214
+ # const char * virStoragePoolGetName (virStoragePoolPtr pool)
215
+ attach_function :virStoragePoolGetName, [:pointer], :string
216
+
217
+ module_function
218
+
219
+ # Creates event callback function for provided event_id
220
+ # @param event_id [Integer,Symbol]
221
+ # @yield connect_ptr, domain_ptr, *args, opaque_ptr
222
+ # @return [FFI::Function]
223
+ def event_callback_for(event_id, &block)
224
+ event_id_sym = event_id.is_a?(Symbol) ? event_id : enum_type(:event_id)[event_id]
225
+
226
+ callback_name = EVENT_ID_TO_CALLBACK.fetch(event_id_sym)
227
+ callback_function(callback_name) do |*args|
228
+ Util.log(:debug, name) { ".event_callback_for #{event_id_sym} CALLBACK #{args.map(&:to_s).join(', ')}," }
229
+ block.call(*args)
230
+ end
231
+ end
147
232
  end
148
233
  end
149
234
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Libvirt
4
- class DomainCallbackStorage
4
+ class HostCallbackStorage
5
5
  class CallbackDataStruct < ::FFI::Struct
6
6
  layout :connection_pointer, :pointer,
7
7
  :callback_id, :int
@@ -12,7 +12,7 @@ module Libvirt
12
12
  end
13
13
 
14
14
  # @return [Array<2>]
15
- # cb_data [Libvirt::DomainCallbackStorage::CallbackDataStruct],
15
+ # cb_data [Libvirt::HostCallbackStorage::CallbackDataStruct],
16
16
  # cb_data_free_func [FFI::Function]
17
17
  def allocate_struct
18
18
  dbg { '#allocate_struct' }
@@ -20,7 +20,7 @@ module Libvirt
20
20
  cb_data_ptr = ::FFI::MemoryPointer.new(:char, CallbackDataStruct.size, false)
21
21
  cb_data = CallbackDataStruct.new(cb_data_ptr)
22
22
  cb_data_free_func = FFI::Common.free_function do |pointer|
23
- dbg { 'Libvirt::DomainCallbackStorage cb_data_free_func triggered' }
23
+ dbg { 'Libvirt::HostCallbackStorage cb_data_free_func triggered' }
24
24
  remove_struct(pointer)
25
25
  end
26
26
  [cb_data, cb_data_free_func]
@@ -63,7 +63,7 @@ module Libvirt
63
63
  private
64
64
 
65
65
  def dbg(&block)
66
- Util.log(:debug, 'Libvirt::DomainCallbackStorage', &block)
66
+ Util.log(:debug, 'Libvirt::HostCallbackStorage', &block)
67
67
  end
68
68
  end
69
69
  end
@@ -61,6 +61,21 @@ module Libvirt
61
61
  ptr.get_array_of_pointer(0, size).map { |stv_ptr| StorageVolume.new(stv_ptr) }
62
62
  end
63
63
 
64
+ def uuid
65
+ buff = ::FFI::MemoryPointer.new(:char, Util::UUID_STRING_BUFLEN)
66
+ result = FFI::Storage.virStoragePoolGetUUIDString(@ptr, buff)
67
+ raise Errors::LibError, "Couldn't get storage pool uuid" if result.negative?
68
+
69
+ buff.read_string
70
+ end
71
+
72
+ def name
73
+ result = FFI::Storage.virStoragePoolGetName(@ptr)
74
+ raise Errors::LibError, "Couldn't retrieve storage pool name" if result.nil?
75
+
76
+ result
77
+ end
78
+
64
79
  private
65
80
 
66
81
  def dbg(&block)
@@ -19,6 +19,8 @@ module Libvirt
19
19
  TiB: 1_099_511_627_776
20
20
  }.freeze
21
21
 
22
+ UUID_STRING_BUFLEN = 0x80 # RFC4122
23
+
22
24
  class << self
23
25
  attr_writer :logger
24
26
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Libvirt
4
- VERSION = '0.5.6'
4
+ VERSION = '0.6.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libvirt_ffi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.6
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Talakevich
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-23 00:00:00.000000000 Z
11
+ date: 2020-05-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi
@@ -61,7 +61,6 @@ files:
61
61
  - lib/libvirt/base_info.rb
62
62
  - lib/libvirt/connection.rb
63
63
  - lib/libvirt/domain.rb
64
- - lib/libvirt/domain_callback_storage.rb
65
64
  - lib/libvirt/errors.rb
66
65
  - lib/libvirt/event.rb
67
66
  - lib/libvirt/ffi.rb
@@ -73,6 +72,7 @@ files:
73
72
  - lib/libvirt/ffi/host.rb
74
73
  - lib/libvirt/ffi/storage.rb
75
74
  - lib/libvirt/ffi/stream.rb
75
+ - lib/libvirt/host_callback_storage.rb
76
76
  - lib/libvirt/node_info.rb
77
77
  - lib/libvirt/storage_pool.rb
78
78
  - lib/libvirt/storage_pool_info.rb