libvirt_ffi 0.2.1 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +78 -0
  3. data/Gemfile +7 -2
  4. data/Rakefile +6 -1
  5. data/bin/console +1 -0
  6. data/exe/libvirt +1 -0
  7. data/lib/libvirt.rb +14 -13
  8. data/lib/libvirt/base_info.rb +34 -0
  9. data/lib/libvirt/connection.rb +156 -47
  10. data/lib/libvirt/domain.rb +136 -8
  11. data/lib/libvirt/domain_callback_storage.rb +69 -0
  12. data/lib/libvirt/errors.rb +65 -0
  13. data/lib/libvirt/event.rb +60 -38
  14. data/lib/libvirt/ffi.rb +17 -0
  15. data/lib/libvirt/ffi/common.rb +8 -1
  16. data/lib/libvirt/ffi/domain.rb +796 -69
  17. data/lib/libvirt/ffi/error.rb +243 -0
  18. data/lib/libvirt/ffi/event.rb +30 -36
  19. data/lib/libvirt/ffi/helpers.rb +17 -0
  20. data/lib/libvirt/ffi/host.rb +122 -0
  21. data/lib/libvirt/ffi/storage.rb +149 -0
  22. data/lib/libvirt/ffi/stream.rb +74 -0
  23. data/lib/libvirt/node_info.rb +2 -41
  24. data/lib/libvirt/storage_pool.rb +70 -0
  25. data/lib/libvirt/storage_pool_info.rb +7 -0
  26. data/lib/libvirt/storage_volume.rb +51 -0
  27. data/lib/libvirt/storage_volume_info.rb +7 -0
  28. data/lib/libvirt/stream.rb +124 -0
  29. data/lib/libvirt/util.rb +75 -8
  30. data/lib/libvirt/version.rb +1 -1
  31. data/lib/libvirt/xml.rb +23 -0
  32. data/lib/libvirt/xml/disk.rb +59 -0
  33. data/lib/libvirt/xml/domain.rb +76 -0
  34. data/lib/libvirt/xml/generic.rb +252 -0
  35. data/lib/libvirt/xml/graphics.rb +14 -0
  36. data/lib/libvirt/xml/max_vcpu.rb +12 -0
  37. data/lib/libvirt/xml/memory.rb +14 -0
  38. data/lib/libvirt/xml/storage_pool.rb +24 -0
  39. data/lib/libvirt/xml/storage_volume.rb +32 -0
  40. data/lib/libvirt/xml/vcpu.rb +12 -0
  41. data/lib/libvirt_ffi.rb +2 -0
  42. data/libvirt.gemspec +5 -1
  43. data/test_usage/support/libvirt_async.rb +33 -31
  44. data/test_usage/support/log_formatter.rb +5 -10
  45. data/test_usage/test_domain.rb +43 -0
  46. data/test_usage/test_event_loop.rb +134 -33
  47. data/test_usage/test_libvirtd_restart.rb +63 -0
  48. data/test_usage/test_metadata.rb +104 -0
  49. data/test_usage/test_screenshot.rb +197 -0
  50. data/test_usage/test_storage.rb +52 -0
  51. metadata +46 -6
  52. data/lib/libvirt/error.rb +0 -6
  53. data/lib/libvirt/ffi/connection.rb +0 -84
  54. data/lib/libvirt/ffi/libvirt.rb +0 -17
  55. data/lib/libvirt/ffi/node_info.rb +0 -37
@@ -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
@@ -3,13 +3,20 @@
3
3
  module Libvirt
4
4
  module FFI
5
5
  module Common
6
+ # https://libvirt.org/html/libvirt-libvirt-common.html
7
+
6
8
  extend ::FFI::Library
9
+ extend Helpers
7
10
  ffi_lib Util.library_path
8
11
 
9
- # typedef void (*virFreeCallback) (
12
+ # typedef void (*virFreeCallback) (
10
13
  # void * opaque
11
14
  # )
12
15
  FREE_CALLBACK = callback :virFreeCallback, [:pointer], :void
16
+
17
+ def self.free_function(*args, &block)
18
+ callback_function(FREE_CALLBACK, *args, &block)
19
+ end
13
20
  end
14
21
  end
15
22
  end
@@ -3,18 +3,312 @@
3
3
  module Libvirt
4
4
  module FFI
5
5
  module Domain
6
+ # https://libvirt.org/html/libvirt-libvirt-domain.html
7
+
6
8
  extend ::FFI::Library
9
+ extend Helpers
7
10
  ffi_lib Util.library_path
8
11
 
9
12
  UUID_STRING_BUFLEN = 0x80 # RFC4122
10
13
 
11
- # int virConnectDomainEventRegisterAny(
14
+ EVENT_ID_TO_CALLBACK = {
15
+ LIFECYCLE: :virConnectDomainEventCallback,
16
+ REBOOT: :virConnectDomainEventGenericCallback,
17
+ RTC_CHANGE: :virConnectDomainEventRTCChangeCallback,
18
+ WATCHDOG: :virConnectDomainEventWatchdogCallback,
19
+ IO_ERROR: :virConnectDomainEventIOErrorCallback,
20
+ GRAPHICS: :virConnectDomainEventGraphicsCallback,
21
+ IO_ERROR_REASON: :virConnectDomainEventIOErrorReasonCallback,
22
+ CONTROL_ERROR: :virConnectDomainEventGenericCallback,
23
+ BLOCK_JOB: :virConnectDomainEventBlockJobCallback,
24
+ DISK_CHANGE: :virConnectDomainEventDiskChangeCallback,
25
+ TRAY_CHANGE: :virConnectDomainEventTrayChangeCallback,
26
+ PMWAKEUP: :virConnectDomainEventPMWakeupCallback,
27
+ PMSUSPEND: :virConnectDomainEventPMSuspendCallback,
28
+ BALLOON_CHANGE: :virConnectDomainEventBalloonChangeCallback,
29
+ PMSUSPEND_DISK: :virConnectDomainEventPMSuspendDiskCallback,
30
+ DEVICE_REMOVED: :virConnectDomainEventDeviceRemovedCallback,
31
+ BLOCK_JOB_2: :virConnectDomainEventBlockJobCallback,
32
+ TUNABLE: :virConnectDomainEventTunableCallback,
33
+ AGENT_LIFECYCLE: :virConnectDomainEventAgentLifecycleCallback,
34
+ DEVICE_ADDED: :virConnectDomainEventDeviceAddedCallback,
35
+ MIGRATION_ITERATION: :virConnectDomainEventMigrationIterationCallback,
36
+ JOB_COMPLETED: :virConnectDomainEventJobCompletedCallback,
37
+ DEVICE_REMOVAL_FAILED: :virConnectDomainEventDeviceRemovalFailedCallback,
38
+ METADATA_CHANGE: :virConnectDomainEventMetadataChangeCallback,
39
+ BLOCK_THRESHOLD: :virConnectDomainEventBlockThresholdCallback
40
+ }.freeze
41
+
42
+ # enum virDomainEventID
43
+ enum :event_id, [
44
+ :LIFECYCLE, 0x0, # virConnectDomainEventCallback
45
+ :REBOOT, 0x1, # virConnectDomainEventGenericCallback
46
+ :RTC_CHANGE, 0x2, # virConnectDomainEventRTCChangeCallback
47
+ :WATCHDOG, 0x3, # virConnectDomainEventWatchdogCallback
48
+ :IO_ERROR, 0x4, # virConnectDomainEventIOErrorCallback
49
+ :GRAPHICS, 0x5, # virConnectDomainEventGraphicsCallback
50
+ :IO_ERROR_REASON, 0x6, # virConnectDomainEventIOErrorReasonCallback
51
+ :CONTROL_ERROR, 0x7, # virConnectDomainEventGenericCallback
52
+ :BLOCK_JOB, 0x8, # virConnectDomainEventBlockJobCallback
53
+ :DISK_CHANGE, 0x9, # virConnectDomainEventDiskChangeCallback
54
+ :TRAY_CHANGE, 0xa, # virConnectDomainEventTrayChangeCallback
55
+ :PMWAKEUP, 0xb, # virConnectDomainEventPMWakeupCallback
56
+ :PMSUSPEND, 0xc, # virConnectDomainEventPMSuspendCallback
57
+ :BALLOON_CHANGE, 0xd, # virConnectDomainEventBalloonChangeCallback
58
+ :PMSUSPEND_DISK, 0xe, # virConnectDomainEventPMSuspendDiskCallback
59
+ :DEVICE_REMOVED, 0xf, # virConnectDomainEventDeviceRemovedCallback
60
+ :BLOCK_JOB_2, 0x10, # virConnectDomainEventBlockJobCallback
61
+ :TUNABLE, 0x11, # virConnectDomainEventTunableCallback
62
+ :AGENT_LIFECYCLE, 0x12, # virConnectDomainEventAgentLifecycleCallback
63
+ :DEVICE_ADDED, 0x13, # virConnectDomainEventDeviceAddedCallback
64
+ :MIGRATION_ITERATION, 0x14, # virConnectDomainEventMigrationIterationCallback
65
+ :JOB_COMPLETED, 0x15, # virConnectDomainEventJobCompletedCallback
66
+ :DEVICE_REMOVAL_FAILED, 0x16, # virConnectDomainEventDeviceRemovalFailedCallback
67
+ :METADATA_CHANGE, 0x17, # virConnectDomainEventMetadataChangeCallback
68
+ :BLOCK_THRESHOLD, 0x18 # virConnectDomainEventBlockThresholdCallback
69
+ ]
70
+
71
+ # enum virDomainState
72
+ enum :state, [
73
+ :NOSTATE, 0x0, # no state
74
+ :RUNNING, 0x1, # the domain is running
75
+ :BLOCKED, 0x2, # the domain is blocked on resource
76
+ :PAUSED, 0x3, # the domain is paused by user
77
+ :SHUTDOWN, 0x4, # the domain is being shut down
78
+ :SHUTOFF, 0x5, # the domain is shut off
79
+ :CRASHED, 0x6, # the domain is crashed
80
+ :PMSUSPENDED, 0x7 # the domain is suspended by guest power management
81
+ ]
82
+
83
+ # enum virDomainNostateReason
84
+ enum :nostate_reason, [
85
+ :UNKNOWN, 0x0
86
+ ]
87
+
88
+ # enum virDomainRunningReason
89
+ enum :running_reason, [
90
+ :UNKNOWN, 0x0,
91
+ :BOOTED, 0x1, # normal startup from boot
92
+ :MIGRATED, 0x2, # migrated from another host
93
+ :RESTORED, 0x3, # restored from a state file
94
+ :FROM_SNAPSHOT, 0x4, # restored from snapshot
95
+ :UNPAUSED, 0x5, # returned from paused state
96
+ :MIGRATION_CANCELED, 0x6, # returned from migration
97
+ :SAVE_CANCELED, 0x7, # returned from failed save process
98
+ :WAKEUP, 0x8, # returned from pmsuspended due to wakeup event
99
+ :CRASHED, 0x9, # resumed from crashed
100
+ :POSTCOPY, 0xa # running in post-copy migration mode
101
+ ]
102
+
103
+ # enum virDomainBlockedReason
104
+ enum :blocked_reason, [
105
+ :UNKNOWN, 0x0 # the reason is unknown
106
+ ]
107
+
108
+ # enum virDomainPausedReason
109
+ enum :paused_reason, [
110
+ :UNKNOWN, 0x0, # the reason is unknown
111
+ :USER, 0x1, # paused on user request
112
+ :MIGRATION, 0x2, # paused for offline migration
113
+ :SAVE, 0x3, # paused for save
114
+ :DUMP, 0x4, # paused for offline core dump
115
+ :IOERROR, 0x5, # paused due to a disk I/O error
116
+ :WATCHDOG, 0x6, # paused due to a watchdog event
117
+ :FROM_SNAPSHOT, 0x7, # paused after restoring from snapshot
118
+ :SHUTTING_DOWN, 0x8, # paused during shutdown process
119
+ :SNAPSHOT, 0x9, # paused while creating a snapshot
120
+ :CRASHED, 0xa, # paused due to a guest crash
121
+ :STARTING_UP, 0xb, # the domain is being started
122
+ :POSTCOPY, 0xc, # paused for post-copy migration
123
+ :POSTCOPY_FAILED, 0xd # paused after failed post-copy
124
+ ]
125
+
126
+ # enum virDomainShutdownReason
127
+ enum :shutdown_reason, [
128
+ :UNKNOWN, 0x0, # the reason is unknown
129
+ :USER, 0x1 # shutting down on user request
130
+ ]
131
+
132
+ # enum virDomainShutoffReason
133
+ enum :shutoff_reason, [
134
+ :UNKNOWN, 0x0, # the reason is unknown
135
+ :SHUTDOWN, 0x1, # normal shutdown
136
+ :DESTROYED, 0x2, # forced poweroff
137
+ :CRASHED, 0x3, # domain crashed
138
+ :MIGRATED, 0x4, # migrated to another host
139
+ :SAVED, 0x5, # saved to a file
140
+ :FAILED, 0x6, # domain failed to start
141
+ :FROM_SNAPSHOT, 0x7, # restored from a snapshot which was taken while domain was shutoff
142
+ :DAEMON, 0x8 # daemon decides to kill domain during reconnection processing
143
+ ]
144
+
145
+ # enum virDomainCrashedReason
146
+ enum :crashed_reason, [
147
+ :UNKNOWN, 0x0, # crashed for unknown reason
148
+ :PANICKED, 0x1 # domain panicked
149
+ ]
150
+
151
+ # enum virDomainPMSuspendedReason
152
+ enum :pmsuspended_reason, [
153
+ :UNKNOWN, 0x0
154
+ ]
155
+
156
+ # enum virDomainEventType
157
+ enum :event_type, [
158
+ :DEFINED, 0x0,
159
+ :UNDEFINED, 0x1,
160
+ :STARTED, 0x2,
161
+ :SUSPENDED, 0x3,
162
+ :RESUMED, 0x4,
163
+ :STOPPED, 0x5,
164
+ :SHUTDOWN, 0x6,
165
+ :PMSUSPENDED, 0x7,
166
+ :CRASHED, 0x8
167
+ ]
168
+
169
+ # enum virDomainEventDefinedDetailType
170
+ enum :event_defined_detail_type, [
171
+ :ADDED, 0x0, # Newly created config file
172
+ :UPDATED, 0x1, # Changed config file
173
+ :RENAMED, 0x2, # Domain was renamed
174
+ :FROM_SNAPSHOT, 0x3 # Config was restored from a snapshot
175
+ ]
176
+
177
+ # enum virDomainEventUndefinedDetailType
178
+ enum :event_undefined_detail_type, [
179
+ :REMOVED, 0x0, # Deleted the config file
180
+ :RENAMED, 0x1 # Domain was renamed
181
+ ]
182
+
183
+ # enum virDomainEventStartedDetailType
184
+ enum :event_started_detail_type, [
185
+ :BOOTED, 0x0, # Normal startup from boot
186
+ :MIGRATED, 0x1, # Incoming migration from another host
187
+ :RESTORED, 0x2, # Restored from a state file
188
+ :FROM_SNAPSHOT, 0x3, # Restored from snapshot
189
+ :WAKEUP, 0x4 # Started due to wakeup event
190
+ ]
191
+
192
+ # enum virDomainEventSuspendedDetailType
193
+ enum :event_suspended_detail_type, [
194
+ :PAUSED, 0x0, # Normal suspend due to admin pause
195
+ :MIGRATED, 0x1, # Suspended for offline migration
196
+ :IOERROR, 0x2, # Suspended due to a disk I/O error
197
+ :WATCHDOG, 0x3, # Suspended due to a watchdog firing
198
+ :RESTORED, 0x4, # Restored from paused state file
199
+ :FROM_SNAPSHOT, 0x5, # Restored from paused snapshot
200
+ :API_ERROR, 0x6, # suspended after failure during libvirt API call
201
+ :POSTCOPY, 0x7, # suspended for post-copy migration
202
+ :POSTCOPY_FAILED, 0x8 # suspended after failed post-copy
203
+ ]
204
+
205
+ # enum virDomainEventResumedDetailType
206
+ enum :event_resumed_detail_type, [
207
+ :UNPAUSED, 0x0, # Normal resume due to admin unpause
208
+ :MIGRATED, 0x1, # Resumed for completion of migration
209
+ :FROM_SNAPSHOT, 0x2, # Resumed from snapshot
210
+ :POSTCOPY, 0x3 # Resumed, but migration is still running in post-copy mode
211
+ ]
212
+
213
+ # enum virDomainEventStoppedDetailType
214
+ enum :event_stopped_detail_type, [
215
+ :SHUTDOWN, 0x0, # Normal shutdown
216
+ :DESTROYED, 0x1, # Forced poweroff from host
217
+ :CRASHED, 0x2, # Guest crashed
218
+ :MIGRATED, 0x3, # Migrated off to another host
219
+ :SAVED, 0x4, # Saved to a state file
220
+ :FAILED, 0x5, # Host emulator/mgmt failed
221
+ :FROM_SNAPSHOT, 0x6 # offline snapshot loaded
222
+ ]
223
+
224
+ # enum virDomainEventShutdownDetailType
225
+ enum :event_shutdown_detail_type, [
226
+ :FINISHED, 0x0, # Guest finished shutdown sequence
227
+ :GUEST, 0x1, # Domain finished shutting down after request from the guest itself (e.g. hardware-specific action)
228
+ :HOST, 0x2 # Domain finished shutting down after request from the host (e.g. killed by a signal)
229
+ ]
230
+
231
+ # enum virDomainEventPMSuspendedDetailType
232
+ enum :event_pmsuspended_detail_type, [
233
+ :MEMORY, 0x0, # Guest was PM suspended to memory
234
+ :DISK, 0x1 # Guest was PM suspended to disk
235
+ ]
236
+
237
+ # enum virDomainEventCrashedDetailType
238
+ enum :event_crashed_detail_type, [
239
+ :PANICKED, 0x0 # Guest was panicked
240
+ ]
241
+
242
+ # enum virDomainSaveRestoreFlags
243
+ enum :save_restore_flags, [
244
+ :BYPASS_CACHE, 0x1, # Avoid file system cache pollution
245
+ :RUNNING, 0x2, # Favor running over paused
246
+ :PAUSED, 0x4 # Favor paused over running
247
+ ]
248
+
249
+ # enum virDomainShutdownFlagValues
250
+ enum :shutdown_flags, [
251
+ :DEFAULT, 0x0, # hypervisor choice
252
+ :ACPI_POWER_BTN, 0x1, # Send ACPI event
253
+ :GUEST_AGENT, 0x2, # Use guest agent
254
+ :INITCTL, 0x4, # Use initctl
255
+ :SIGNAL, 0x8, # Send a signal
256
+ :PARAVIRT, 0x10 # Use paravirt guest control
257
+ ]
258
+
259
+ # enum virDomainUndefineFlagsValues
260
+ enum :undefine_flags_values, [
261
+ :MANAGED_SAVE, 0x1, # Also remove any managed save
262
+ :SNAPSHOTS_METADATA, 0x2, # If last use of domain, then also remove any snapshot metadata
263
+ :NVRAM, 0x4, # Also remove any nvram file
264
+ :KEEP_NVRAM, 0x8, # Keep nvram file
265
+ :CHECKPOINTS_METADATA, 0x10 # If last use of domain, then also remove any checkpoint metadata Future undefine control flags should come here.
266
+ ]
267
+
268
+ # enum virDomainDefineFlags
269
+ enum :define_flags, [
270
+ :DEFINE_VALIDATE, 0x1 # Validate the XML document against schema
271
+ ]
272
+
273
+ # enum virDomainMetadataType
274
+ enum :metadata_type, [
275
+ :DESCRIPTION, 0x0, # Operate on <description>
276
+ :TITLE, 0x1, # Operate on <title>
277
+ :ELEMENT, 0x2 # Operate on <metadata>
278
+ ]
279
+
280
+ # enum virDomainModificationImpact
281
+ enum :modification_impact, [
282
+ :AFFECT_CURRENT, 0x0, # Affect current domain state.
283
+ :AFFECT_LIVE, 0x1, # Affect running domain state.
284
+ :AFFECT_CONFIG, 0x2 # Affect persistent domain state. 1 << 2 is reserved for virTypedParameterFlags
285
+ ]
286
+
287
+ # enum virDomainXMLFlags
288
+ enum :xml_flags, [
289
+ :SECURE, 0x1, # dump security sensitive information too
290
+ :INACTIVE, 0x2, # dump inactive domain information
291
+ :UPDATE_CPU, 0x4, # update guest CPU requirements according to host CPU
292
+ :MIGRATABLE, 0x8 # dump XML suitable for migration
293
+ ]
294
+
295
+ # int virDomainFree (
296
+ # virDomainPtr domain
297
+ # )
298
+ attach_function :virDomainFree, [:pointer], :int
299
+
300
+ # int virDomainRef (
301
+ # virDomainPtr domain
302
+ # )
303
+ attach_function :virDomainRef, [:pointer], :int
304
+
305
+ # int virConnectDomainEventRegisterAny(
12
306
  # virConnectPtr conn,
13
- # virDomainPtr dom,
14
- # int eventID,
15
- # virConnectDomainEventGenericCallback cb,
16
- # void * opaque,
17
- # virFreeCallback freecb
307
+ # virDomainPtr dom,
308
+ # int eventID,
309
+ # virConnectDomainEventGenericCallback cb,
310
+ # void * opaque,
311
+ # virFreeCallback freecb
18
312
  # )
19
313
  attach_function :virConnectDomainEventRegisterAny, [
20
314
  :pointer,
@@ -25,105 +319,538 @@ module Libvirt
25
319
  :pointer
26
320
  ], :int
27
321
 
28
- # int virConnectListAllDomains (
322
+ # int virConnectDomainEventDeregisterAny (
323
+ # virConnectPtr conn,
324
+ # int callbackID
325
+ # )
326
+ attach_function :virConnectDomainEventDeregisterAny, [:pointer, :int], :int
327
+
328
+ # int virConnectListAllDomains (
29
329
  # virConnectPtr conn,
30
- # virDomainPtr **domains,
31
- # unsigned int flags
330
+ # virDomainPtr **domains,
331
+ # unsigned int flags
32
332
  # )
33
333
  attach_function :virConnectListAllDomains, [:pointer, :pointer, :uint], :int
34
334
 
35
- # enum virDomainState {
36
- # VIR_DOMAIN_NOSTATE = 0 (0x0)
37
- # no state
38
- # VIR_DOMAIN_RUNNING = 1 (0x1)
39
- # the domain is running
40
- # VIR_DOMAIN_BLOCKED = 2 (0x2)
41
- # the domain is blocked on resource
42
- # VIR_DOMAIN_PAUSED = 3 (0x3)
43
- # the domain is paused by user
44
- # VIR_DOMAIN_SHUTDOWN = 4 (0x4)
45
- # the domain is being shut down
46
- # VIR_DOMAIN_SHUTOFF = 5 (0x5)
47
- # the domain is shut off
48
- # VIR_DOMAIN_CRASHED = 6 (0x6)
49
- # the domain is crashed
50
- # VIR_DOMAIN_PMSUSPENDED = 7 (0x7)
51
- # the domain is suspended by guest power management
52
- # VIR_DOMAIN_LAST = 8 (0x8)
53
- # NB: this enum value will increase over time as new events are added to the libvirt API.
54
- # It reflects the last state supported by this version of the libvirt API.
55
- # }
56
- enum :states, [
57
- :no_state, 0x0,
58
- :running, 0x1,
59
- :blocked, 0x2,
60
- :paused, 0x3,
61
- :shutdown, 0x4,
62
- :shutoff, 0x5,
63
- :crashed, 0x6,
64
- :pm_suspended, 0x7,
65
- :last, 0x8
66
- ]
67
-
68
- # int virDomainGetState (
335
+ # int virDomainGetState (
69
336
  # virDomainPtr domain,
70
- # int *state,
71
- # int *reason,
72
- # unsigned int flags
337
+ # int *state,
338
+ # int *reason,
339
+ # unsigned int flags
73
340
  # )
74
341
  attach_function :virDomainGetState, [:pointer, :pointer, :pointer, :uint], :int
75
342
 
76
343
  # const char *virDomainGetName (
77
344
  # virDomainPtr domain
78
345
  # )
79
- attach_function :virDomainGetName, [:pointer], :string # strptr?
346
+ attach_function :virDomainGetName, [:pointer], :string
80
347
 
81
- # int virDomainGetMaxVcpus (
348
+ # int virDomainGetMaxVcpus (
82
349
  # virDomainPtr domain
83
350
  # )
84
351
  attach_function :virDomainGetMaxVcpus, [:pointer], :int
85
352
 
86
- # int virDomainGetVcpus (
353
+ # int virDomainGetVcpus (
87
354
  # virDomainPtr domain,
88
- # virVcpuInfoPtr info,
89
- # int maxinfo,
90
- # unsigned char * cpumaps,
91
- # int maplen
355
+ # virVcpuInfoPtr info,
356
+ # int maxinfo,
357
+ # unsigned char * cpumaps,
358
+ # int maplen
92
359
  # )
93
360
  attach_function :virDomainGetVcpus, [:pointer, :pointer, :int, :pointer, :int], :int
94
361
 
95
- # int virDomainGetUUIDString (
362
+ # int virDomainGetUUIDString (
96
363
  # virDomainPtr domain,
97
- # char * buf
364
+ # char * buf
98
365
  # )
99
366
  attach_function :virDomainGetUUIDString, [:pointer, :pointer], :int
100
367
 
101
- # unsigned long virDomainGetMaxMemory (
368
+ # unsigned long virDomainGetMaxMemory (
102
369
  # virDomainPtr domain
103
370
  # )
104
371
  attach_function :virDomainGetMaxMemory, [:pointer], :ulong
105
372
 
106
- # char *virDomainGetXMLDesc (
373
+ # char *virDomainGetXMLDesc (
107
374
  # virDomainPtr domain,
108
- # unsigned int flags
375
+ # unsigned int flags
109
376
  # )
110
- attach_function :virDomainGetXMLDesc, [:pointer, :uint], :string # strptr?
377
+ attach_function :virDomainGetXMLDesc, [:pointer, :xml_flags], :string
111
378
 
112
- # typedef int (*virConnectDomainEventCallback) (
379
+ # char *virDomainScreenshot (
380
+ # virDomainPtr domain,
381
+ # virStreamPtr stream,
382
+ # unsigned int screen,
383
+ # unsigned int flags
384
+ # )
385
+ attach_function :virDomainScreenshot, [:pointer, :pointer, :uint, :uint], :strptr
386
+
387
+ # typedef int (*virConnectDomainEventCallback) (
113
388
  # virConnectPtr conn,
114
- # virDomainPtr dom,
115
- # int event,
116
- # int detail,
117
- # void * opaque
389
+ # virDomainPtr dom,
390
+ # int event,
391
+ # int detail,
392
+ # void * opaque
118
393
  # )
119
- def self.domain_event_id_lifecycle_callback(&block)
120
- ::FFI::Function.new(:int, [:pointer, :pointer, :int, :int]) do |_conn, dom, event, detail, op|
121
- Util.log(:debug) { "DOMAIN_EVENT_CALLBACK LIFECYCLE dom=#{dom}, event=#{event}, detail=#{detail}, op=#{op}" }
122
- block.call(dom, event, detail, op)
123
- 0
394
+ callback :virConnectDomainEventCallback, [:pointer, :pointer, :event_type, :int, :pointer], :int
395
+
396
+ # typedef void (*virConnectDomainEventGenericCallback) (
397
+ # virConnectPtr conn,
398
+ # virDomainPtr dom,
399
+ # void * opaque
400
+ # )
401
+ callback :virConnectDomainEventGenericCallback, [:pointer, :pointer, :pointer], :void
402
+
403
+ # typedef void (*virConnectDomainEventRTCChangeCallback) (
404
+ # virConnectPtr conn,
405
+ # virDomainPtr dom,
406
+ # long long utcoffset,
407
+ # void * opaque
408
+ # )
409
+ callback :virConnectDomainEventRTCChangeCallback, [
410
+ :pointer,
411
+ :pointer,
412
+ :long_long,
413
+ :pointer
414
+ ], :void
415
+
416
+ # typedef void (*virConnectDomainEventWatchdogCallback) (
417
+ # virConnectPtr conn,
418
+ # virDomainPtr dom,
419
+ # int action,
420
+ # void * opaque
421
+ # )
422
+ callback :virConnectDomainEventWatchdogCallback, [
423
+ :pointer,
424
+ :pointer,
425
+ :int,
426
+ :pointer
427
+ ], :void
428
+
429
+ # typedef void (*virConnectDomainEventIOErrorCallback) (
430
+ # virConnectPtr conn,
431
+ # virDomainPtr dom,
432
+ # const char * srcPath,
433
+ # const char * devAlias,
434
+ # int action,
435
+ # void * opaque
436
+ # )
437
+ callback :virConnectDomainEventIOErrorCallback, [
438
+ :pointer,
439
+ :pointer,
440
+ :string,
441
+ :string,
442
+ :int,
443
+ :pointer
444
+ ], :void
445
+
446
+ # typedef void (*virConnectDomainEventGraphicsCallback) (
447
+ # virConnectPtr conn,
448
+ # virDomainPtr dom,
449
+ # int phase,
450
+ # const virDomainEventGraphicsAddress * local,
451
+ # const virDomainEventGraphicsAddress * remote,
452
+ # const char * authScheme,
453
+ # const virDomainEventGraphicsSubject * subject,
454
+ # void * opaque
455
+ # )
456
+ callback :virConnectDomainEventGraphicsCallback, [
457
+ :pointer,
458
+ :pointer,
459
+ :int,
460
+ :pointer,
461
+ :pointer,
462
+ :string,
463
+ :pointer,
464
+ :pointer
465
+ ], :void
466
+
467
+ # typedef void (*virConnectDomainEventIOErrorReasonCallback) (
468
+ # virConnectPtr conn,
469
+ # virDomainPtr dom,
470
+ # const char * srcPath,
471
+ # const char * devAlias,
472
+ # int action,
473
+ # const char * reason,
474
+ # void * opaque
475
+ # )
476
+ callback :virConnectDomainEventIOErrorReasonCallback, [
477
+ :pointer,
478
+ :pointer,
479
+ :string,
480
+ :string,
481
+ :int,
482
+ :string,
483
+ :pointer
484
+ ], :void
485
+
486
+ # typedef void (*virConnectDomainEventBlockJobCallback) (
487
+ # virConnectPtr conn,
488
+ # virDomainPtr dom,
489
+ # const char * disk,
490
+ # int type,
491
+ # int status,
492
+ # void * opaque
493
+ # )
494
+ callback :virConnectDomainEventBlockJobCallback, [
495
+ :pointer,
496
+ :pointer,
497
+ :string,
498
+ :int,
499
+ :int,
500
+ :pointer
501
+ ], :void
502
+
503
+ # typedef void (*virConnectDomainEventDiskChangeCallback) (
504
+ # virConnectPtr conn,
505
+ # virDomainPtr dom,
506
+ # const char * oldSrcPath,
507
+ # const char * newSrcPath,
508
+ # const char * devAlias,
509
+ # int reason,
510
+ # void * opaque
511
+ # )
512
+ callback :virConnectDomainEventDiskChangeCallback, [
513
+ :pointer,
514
+ :pointer,
515
+ :string,
516
+ :string,
517
+ :string,
518
+ :int,
519
+ :pointer
520
+ ], :void
521
+
522
+ # typedef void (*virConnectDomainEventTrayChangeCallback) (
523
+ # virConnectPtr conn,
524
+ # virDomainPtr dom,
525
+ # const char * devAlias,
526
+ # int reason,
527
+ # void * opaque
528
+ # )
529
+ callback :virConnectDomainEventTrayChangeCallback, [
530
+ :pointer,
531
+ :pointer,
532
+ :string,
533
+ :int,
534
+ :pointer
535
+ ], :void
536
+
537
+ # typedef void (*virConnectDomainEventPMWakeupCallback) (
538
+ # virConnectPtr conn,
539
+ # virDomainPtr dom,
540
+ # int reason,
541
+ # void * opaque
542
+ # )
543
+ callback :virConnectDomainEventPMWakeupCallback, [
544
+ :pointer,
545
+ :pointer,
546
+ :int,
547
+ :pointer
548
+ ], :void
549
+
550
+ # typedef void (*virConnectDomainEventPMSuspendCallback) (
551
+ # virConnectPtr conn,
552
+ # virDomainPtr dom,
553
+ # int reason,
554
+ # void * opaque
555
+ # )
556
+ callback :virConnectDomainEventPMSuspendCallback, [
557
+ :pointer,
558
+ :pointer,
559
+ :int,
560
+ :pointer
561
+ ], :void
562
+
563
+ # typedef void (*virConnectDomainEventBalloonChangeCallback) (
564
+ # virConnectPtr conn,
565
+ # virDomainPtr dom,
566
+ # unsigned long long actual,
567
+ # void * opaque
568
+ # )
569
+ callback :virConnectDomainEventBalloonChangeCallback, [
570
+ :pointer,
571
+ :pointer,
572
+ :ulong_long,
573
+ :pointer
574
+ ], :void
575
+
576
+ # typedef void (*virConnectDomainEventPMSuspendDiskCallback) (
577
+ # virConnectPtr conn,
578
+ # virDomainPtr dom,
579
+ # int reason,
580
+ # void * opaque
581
+ # )
582
+ callback :virConnectDomainEventPMSuspendDiskCallback, [
583
+ :pointer,
584
+ :pointer,
585
+ :int,
586
+ :pointer
587
+ ], :void
588
+
589
+ # typedef void (*virConnectDomainEventDeviceRemovedCallback) (
590
+ # virConnectPtr conn,
591
+ # virDomainPtr dom,
592
+ # const char * devAlias,
593
+ # void * opaque
594
+ # )
595
+ callback :virConnectDomainEventDeviceRemovedCallback, [
596
+ :pointer,
597
+ :pointer,
598
+ :string,
599
+ :pointer
600
+ ], :void
601
+
602
+ # typedef void (*virConnectDomainEventTunableCallback) (
603
+ # virConnectPtr conn,
604
+ # virDomainPtr dom,
605
+ # virTypedParameterPtr params,
606
+ # int nparams,
607
+ # void * opaque
608
+ # )
609
+ callback :virConnectDomainEventTunableCallback, [
610
+ :pointer,
611
+ :pointer,
612
+ :pointer,
613
+ :int,
614
+ :pointer
615
+ ], :void
616
+
617
+ # typedef void (*virConnectDomainEventAgentLifecycleCallback) (
618
+ # virConnectPtr conn,
619
+ # virDomainPtr dom,
620
+ # int state,
621
+ # int reason,
622
+ # void * opaque
623
+ # )
624
+ callback :virConnectDomainEventAgentLifecycleCallback, [
625
+ :pointer,
626
+ :pointer,
627
+ :int,
628
+ :int,
629
+ :pointer
630
+ ], :void
631
+
632
+ # typedef void (*virConnectDomainEventDeviceAddedCallback) (
633
+ # virConnectPtr conn,
634
+ # virDomainPtr dom,
635
+ # const char * devAlias,
636
+ # void * opaque
637
+ # )
638
+ callback :virConnectDomainEventDeviceAddedCallback, [
639
+ :pointer,
640
+ :pointer,
641
+ :string,
642
+ :pointer
643
+ ], :void
644
+
645
+ # typedef void (*virConnectDomainEventMigrationIterationCallback) (
646
+ # virConnectPtr conn,
647
+ # virDomainPtr dom,
648
+ # int iteration,
649
+ # void * opaque
650
+ # )
651
+ callback :virConnectDomainEventMigrationIterationCallback, [
652
+ :pointer,
653
+ :pointer,
654
+ :int,
655
+ :pointer
656
+ ], :void
657
+
658
+ # typedef void (*virConnectDomainEventJobCompletedCallback) (
659
+ # virConnectPtr conn,
660
+ # virDomainPtr dom,
661
+ # virTypedParameterPtr params,
662
+ # int nparams,
663
+ # void * opaque
664
+ # )
665
+ callback :virConnectDomainEventJobCompletedCallback, [
666
+ :pointer,
667
+ :pointer,
668
+ :pointer,
669
+ :int,
670
+ :pointer
671
+ ], :void
672
+
673
+ # typedef void (*virConnectDomainEventDeviceRemovalFailedCallback) (
674
+ # virConnectPtr conn,
675
+ # virDomainPtr dom,
676
+ # const char * devAlias,
677
+ # void * opaque
678
+ # )
679
+ callback :virConnectDomainEventDeviceRemovalFailedCallback, [
680
+ :pointer,
681
+ :pointer,
682
+ :string,
683
+ :pointer
684
+ ], :void
685
+
686
+ # typedef void (*virConnectDomainEventMetadataChangeCallback) (
687
+ # virConnectPtr conn,
688
+ # virDomainPtr dom,
689
+ # int type,
690
+ # const char * nsuri,
691
+ # void * opaque
692
+ # )
693
+ callback :virConnectDomainEventMetadataChangeCallback, [
694
+ :pointer,
695
+ :pointer,
696
+ :metadata_type,
697
+ :string,
698
+ :pointer
699
+ ], :void
700
+
701
+ # typedef void (*virConnectDomainEventBlockThresholdCallback) (
702
+ # virConnectPtr conn,
703
+ # virDomainPtr dom,
704
+ # const char * dev,
705
+ # const char * path,
706
+ # unsigned long long threshold,
707
+ # unsigned long long excess,
708
+ # void * opaque
709
+ # )
710
+ callback :virConnectDomainEventBlockThresholdCallback, [
711
+ :pointer,
712
+ :pointer,
713
+ :string,
714
+ :string,
715
+ :ulong_long,
716
+ :ulong_long,
717
+ :pointer
718
+ ], :void
719
+
720
+ # int virDomainReboot (
721
+ # virDomainPtr domain,
722
+ # unsigned int flags
723
+ # )
724
+ attach_function :virDomainReboot, [:pointer, :uint], :int
725
+
726
+ # int virDomainShutdownFlags (
727
+ # virDomainPtr domain,
728
+ # unsigned int flags
729
+ # )
730
+ attach_function :virDomainShutdownFlags, [:pointer, :shutdown_flags], :int
731
+
732
+ # int virDomainDestroyFlags (
733
+ # virDomainPtr domain,
734
+ # unsigned int flags
735
+ # )
736
+ attach_function :virDomainDestroyFlags, [:pointer, :uint], :int
737
+
738
+ # int virDomainReset (
739
+ # virDomainPtr domain,
740
+ # unsigned int flags
741
+ # )
742
+ attach_function :virDomainReset, [:pointer, :uint], :int
743
+
744
+ # int virDomainSuspend (
745
+ # virDomainPtr domain
746
+ # )
747
+ attach_function :virDomainSuspend, [:pointer], :int
748
+
749
+ # int virDomainResume (
750
+ # virDomainPtr domain
751
+ # )
752
+ attach_function :virDomainResume, [:pointer], :int
753
+
754
+ # int virDomainCreateWithFlags (
755
+ # virDomainPtr domain,
756
+ # unsigned int flags
757
+ # )
758
+ attach_function :virDomainCreateWithFlags, [:pointer, :uint], :int
759
+
760
+ # int virDomainManagedSave (
761
+ # virDomainPtr dom,
762
+ # unsigned int flags
763
+ # )
764
+ attach_function :virDomainManagedSave, [:pointer, :save_restore_flags], :int
765
+
766
+ # int virDomainUndefineFlags (
767
+ # virDomainPtr domain,
768
+ # unsigned int flags
769
+ # )
770
+ attach_function :virDomainUndefineFlags, [:pointer, :uint], :int
771
+
772
+ # virDomainPtr virDomainDefineXMLFlags (
773
+ # virConnectPtr conn,
774
+ # const char * xml,
775
+ # unsigned int flags
776
+ # )
777
+ attach_function :virDomainDefineXMLFlags, [:pointer, :string, :uint], :pointer
778
+
779
+ # int virDomainSetMetadata (
780
+ # virDomainPtr domain,
781
+ # int type,
782
+ # const char * metadata,
783
+ # const char * key,
784
+ # const char * uri,
785
+ # unsigned int flags
786
+ # )
787
+ attach_function :virDomainSetMetadata,
788
+ [:pointer, :metadata_type, :string, :string, :string, :modification_impact],
789
+ :int
790
+
791
+ # char * virDomainGetMetadata (
792
+ # virDomainPtr domain,
793
+ # int type,
794
+ # const char * uri,
795
+ # unsigned int flags
796
+ # )
797
+ attach_function :virDomainGetMetadata,
798
+ [:pointer, :metadata_type, :string, :modification_impact],
799
+ :string
800
+
801
+ # Converts detail from lifecycle callback from integer to symbol name.
802
+ # @param event [Symbol] enum :event_type (virDomainEventType)
803
+ # @param detail [Integer]
804
+ # @return [Symbol]
805
+ def self.event_detail_type(event, detail)
806
+ detail_enum = enum_type(:"event_#{event.to_s.downcase}_detail_type")
807
+ detail_enum[detail]
808
+ end
809
+
810
+ def self.state_reason(state, reason)
811
+ reason_enum = enum_type(:"#{state.to_s.downcase}_reason")
812
+ reason_enum[reason]
813
+ end
814
+
815
+ # Creates event callback function for provided event_id
816
+ # @param event_id [Integer,Symbol]
817
+ # @yield connect_ptr, domain_ptr, *args, opaque_ptr
818
+ # @return [FFI::Function]
819
+ def self.event_callback_for(event_id, &block)
820
+ event_id_sym = event_id.is_a?(Symbol) ? event_id : enum_type(:event_id)[event_id]
821
+
822
+ case event_id_sym
823
+ when :LIFECYCLE
824
+ event_callback(&block)
825
+ else
826
+ event_callback_base(event_id_sym, &block)
827
+ end
828
+ end
829
+
830
+ # Creates generic event callback function for provided event_id_sym
831
+ # @param event_id_sym [Symbol]
832
+ # @yield connect_ptr, domain_ptr, *args, opaque_ptr
833
+ # @return [FFI::Function]
834
+ def self.event_callback_base(event_id_sym, &block)
835
+ callback_name = EVENT_ID_TO_CALLBACK.fetch(event_id_sym)
836
+ callback_function(callback_name) do |*args|
837
+ Util.log(:debug) { "Libvirt::Domain #{event_id_sym} CALLBACK #{args.map(&:to_s).join(', ')}," }
838
+ block.call(*args)
124
839
  end
125
840
  end
126
841
 
842
+ # Creates event callback function for lifecycle event_id
843
+ # @param event_id_sym [Symbol]
844
+ # @yield connect_ptr, domain_ptr, event, detail, opaque_ptr
845
+ # @return [FFI::Function]
846
+ def self.event_callback(&block)
847
+ callback_function(:virConnectDomainEventCallback) do |conn, dom, event, detail, opaque|
848
+ detail_sym = event_detail_type(event, detail)
849
+ Util.log(:debug) { "Libvirt::Domain LIFECYCLE CALLBACK #{conn}, #{dom}, #{event}, #{detail_sym}, #{opaque}," }
850
+ block.call(conn, dom, event, detail_sym, opaque)
851
+ 0
852
+ end
853
+ end
127
854
  end
128
855
  end
129
856
  end