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.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +78 -0
  3. data/Gemfile +7 -3
  4. data/Rakefile +6 -1
  5. data/bin/console +1 -0
  6. data/exe/libvirt +1 -0
  7. data/lib/libvirt/base_info.rb +34 -0
  8. data/lib/libvirt/connection.rb +98 -36
  9. data/lib/libvirt/domain.rb +105 -8
  10. data/lib/libvirt/domain_callback_storage.rb +13 -15
  11. data/lib/libvirt/errors.rb +65 -0
  12. data/lib/libvirt/event.rb +29 -19
  13. data/lib/libvirt/ffi/common.rb +8 -1
  14. data/lib/libvirt/ffi/domain.rb +529 -196
  15. data/lib/libvirt/ffi/error.rb +243 -0
  16. data/lib/libvirt/ffi/event.rb +30 -36
  17. data/lib/libvirt/ffi/helpers.rb +17 -0
  18. data/lib/libvirt/ffi/host.rb +122 -0
  19. data/lib/libvirt/ffi/storage.rb +149 -0
  20. data/lib/libvirt/ffi/stream.rb +19 -17
  21. data/lib/libvirt/ffi.rb +17 -0
  22. data/lib/libvirt/node_info.rb +2 -41
  23. data/lib/libvirt/storage_pool.rb +70 -0
  24. data/lib/libvirt/storage_pool_info.rb +7 -0
  25. data/lib/libvirt/storage_volume.rb +51 -0
  26. data/lib/libvirt/storage_volume_info.rb +7 -0
  27. data/lib/libvirt/stream.rb +21 -14
  28. data/lib/libvirt/util.rb +61 -8
  29. data/lib/libvirt/version.rb +1 -1
  30. data/lib/libvirt/xml/disk.rb +59 -0
  31. data/lib/libvirt/xml/domain.rb +76 -0
  32. data/lib/libvirt/xml/generic.rb +252 -0
  33. data/lib/libvirt/xml/graphics.rb +14 -0
  34. data/lib/libvirt/xml/max_vcpu.rb +12 -0
  35. data/lib/libvirt/xml/memory.rb +14 -0
  36. data/lib/libvirt/xml/storage_pool.rb +24 -0
  37. data/lib/libvirt/xml/storage_volume.rb +32 -0
  38. data/lib/libvirt/xml/vcpu.rb +12 -0
  39. data/lib/libvirt/xml.rb +23 -0
  40. data/lib/libvirt.rb +12 -12
  41. data/lib/libvirt_ffi.rb +2 -0
  42. data/libvirt.gemspec +5 -1
  43. data/test_usage/support/libvirt_async.rb +27 -35
  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 +115 -39
  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 +14 -13
  50. data/test_usage/test_storage.rb +52 -0
  51. metadata +42 -6
  52. data/lib/libvirt/error.rb +0 -6
  53. data/lib/libvirt/ffi/connection.rb +0 -94
  54. data/lib/libvirt/ffi/libvirt.rb +0 -17
  55. data/lib/libvirt/ffi/node_info.rb +0 -37
@@ -3,40 +3,14 @@
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
- # enum virDomainEventID
12
- enum :event_id, [
13
- :LIFECYCLE, 0, # (0x0) virConnectDomainEventCallback
14
- :REBOOT, 1, # (0x1) virConnectDomainEventGenericCallback
15
- :RTC_CHANGE, 2, # (0x2) virConnectDomainEventRTCChangeCallback
16
- :WATCHDOG, 3, # (0x3) virConnectDomainEventWatchdogCallback
17
- :IO_ERROR, 4, # (0x4) virConnectDomainEventIOErrorCallback
18
- :GRAPHICS, 5, # (0x5) virConnectDomainEventGraphicsCallback
19
- :IO_ERROR_REASON, 6, # (0x6) virConnectDomainEventIOErrorReasonCallback
20
- :CONTROL_ERROR, 7, # (0x7) virConnectDomainEventGenericCallback
21
- :BLOCK_JOB, 8, # (0x8) virConnectDomainEventBlockJobCallback
22
- :DISK_CHANGE, 9, # (0x9) virConnectDomainEventDiskChangeCallback
23
- :TRAY_CHANGE, 10, # (0xa) virConnectDomainEventTrayChangeCallback
24
- :PMWAKEUP, 11, # (0xb) virConnectDomainEventPMWakeupCallback
25
- :PMSUSPEND, 12, # (0xc) virConnectDomainEventPMSuspendCallback
26
- :BALLOON_CHANGE, 13, # (0xd) virConnectDomainEventBalloonChangeCallback
27
- :PMSUSPEND_DISK, 14, # (0xe) virConnectDomainEventPMSuspendDiskCallback
28
- :DEVICE_REMOVED, 15, # (0xf) virConnectDomainEventDeviceRemovedCallback
29
- :BLOCK_JOB_2, 16, # (0x10) virConnectDomainEventBlockJobCallback
30
- :TUNABLE, 17, # (0x11) virConnectDomainEventTunableCallback
31
- :AGENT_LIFECYCLE, 18, # (0x12) virConnectDomainEventAgentLifecycleCallback
32
- :DEVICE_ADDED, 19, # (0x13) virConnectDomainEventDeviceAddedCallback
33
- :MIGRATION_ITERATION, 20, # (0x14) virConnectDomainEventMigrationIterationCallback
34
- :JOB_COMPLETED, 21, # (0x15) virConnectDomainEventJobCompletedCallback
35
- :DEVICE_REMOVAL_FAILED, 22, # (0x16) virConnectDomainEventDeviceRemovalFailedCallback
36
- :METADATA_CHANGE, 23, # (0x17) virConnectDomainEventMetadataChangeCallback
37
- :BLOCK_THRESHOLD, 24 # (0x18) virConnectDomainEventBlockThresholdCallback
38
- ]
39
-
40
14
  EVENT_ID_TO_CALLBACK = {
41
15
  LIFECYCLE: :virConnectDomainEventCallback,
42
16
  REBOOT: :virConnectDomainEventGenericCallback,
@@ -63,37 +37,278 @@ module Libvirt
63
37
  DEVICE_REMOVAL_FAILED: :virConnectDomainEventDeviceRemovalFailedCallback,
64
38
  METADATA_CHANGE: :virConnectDomainEventMetadataChangeCallback,
65
39
  BLOCK_THRESHOLD: :virConnectDomainEventBlockThresholdCallback
66
- }
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
+ ]
67
70
 
68
71
  # enum virDomainState
69
72
  enum :state, [
70
- :NOSTATE, 0x0, # no state
71
- :RUNNING, 0x1, # the domain is running
72
- :BLOCKED, 0x2, # the domain is blocked on resource
73
- :PAUSED, 0x3, # the domain is paused by user
74
- :SHUTDOWN, 0x4, # the domain is being shut down
75
- :SHUTOFF, 0x5, # the domain is shut off
76
- :CRASHED, 0x6, # the domain is crashed
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
77
80
  :PMSUSPENDED, 0x7 # the domain is suspended by guest power management
78
81
  ]
79
82
 
80
- # int virDomainFree (
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 (
81
296
  # virDomainPtr domain
82
297
  # )
83
298
  attach_function :virDomainFree, [:pointer], :int
84
299
 
85
- # int virDomainRef (
300
+ # int virDomainRef (
86
301
  # virDomainPtr domain
87
302
  # )
88
303
  attach_function :virDomainRef, [:pointer], :int
89
304
 
90
- # int virConnectDomainEventRegisterAny(
305
+ # int virConnectDomainEventRegisterAny(
91
306
  # virConnectPtr conn,
92
- # virDomainPtr dom,
93
- # int eventID,
94
- # virConnectDomainEventGenericCallback cb,
95
- # void * opaque,
96
- # virFreeCallback freecb
307
+ # virDomainPtr dom,
308
+ # int eventID,
309
+ # virConnectDomainEventGenericCallback cb,
310
+ # void * opaque,
311
+ # virFreeCallback freecb
97
312
  # )
98
313
  attach_function :virConnectDomainEventRegisterAny, [
99
314
  :pointer,
@@ -104,92 +319,92 @@ module Libvirt
104
319
  :pointer
105
320
  ], :int
106
321
 
107
- # int virConnectDomainEventDeregisterAny (
322
+ # int virConnectDomainEventDeregisterAny (
108
323
  # virConnectPtr conn,
109
- # int callbackID
324
+ # int callbackID
110
325
  # )
111
326
  attach_function :virConnectDomainEventDeregisterAny, [:pointer, :int], :int
112
327
 
113
- # int virConnectListAllDomains (
328
+ # int virConnectListAllDomains (
114
329
  # virConnectPtr conn,
115
- # virDomainPtr **domains,
116
- # unsigned int flags
330
+ # virDomainPtr **domains,
331
+ # unsigned int flags
117
332
  # )
118
333
  attach_function :virConnectListAllDomains, [:pointer, :pointer, :uint], :int
119
334
 
120
- # int virDomainGetState (
335
+ # int virDomainGetState (
121
336
  # virDomainPtr domain,
122
- # int *state,
123
- # int *reason,
124
- # unsigned int flags
337
+ # int *state,
338
+ # int *reason,
339
+ # unsigned int flags
125
340
  # )
126
341
  attach_function :virDomainGetState, [:pointer, :pointer, :pointer, :uint], :int
127
342
 
128
343
  # const char *virDomainGetName (
129
344
  # virDomainPtr domain
130
345
  # )
131
- attach_function :virDomainGetName, [:pointer], :string # strptr?
346
+ attach_function :virDomainGetName, [:pointer], :string
132
347
 
133
- # int virDomainGetMaxVcpus (
348
+ # int virDomainGetMaxVcpus (
134
349
  # virDomainPtr domain
135
350
  # )
136
351
  attach_function :virDomainGetMaxVcpus, [:pointer], :int
137
352
 
138
- # int virDomainGetVcpus (
353
+ # int virDomainGetVcpus (
139
354
  # virDomainPtr domain,
140
- # virVcpuInfoPtr info,
141
- # int maxinfo,
142
- # unsigned char * cpumaps,
143
- # int maplen
355
+ # virVcpuInfoPtr info,
356
+ # int maxinfo,
357
+ # unsigned char * cpumaps,
358
+ # int maplen
144
359
  # )
145
360
  attach_function :virDomainGetVcpus, [:pointer, :pointer, :int, :pointer, :int], :int
146
361
 
147
- # int virDomainGetUUIDString (
362
+ # int virDomainGetUUIDString (
148
363
  # virDomainPtr domain,
149
- # char * buf
364
+ # char * buf
150
365
  # )
151
366
  attach_function :virDomainGetUUIDString, [:pointer, :pointer], :int
152
367
 
153
- # unsigned long virDomainGetMaxMemory (
368
+ # unsigned long virDomainGetMaxMemory (
154
369
  # virDomainPtr domain
155
370
  # )
156
371
  attach_function :virDomainGetMaxMemory, [:pointer], :ulong
157
372
 
158
- # char *virDomainGetXMLDesc (
373
+ # char *virDomainGetXMLDesc (
159
374
  # virDomainPtr domain,
160
- # unsigned int flags
375
+ # unsigned int flags
161
376
  # )
162
- attach_function :virDomainGetXMLDesc, [:pointer, :uint], :string # strptr?
377
+ attach_function :virDomainGetXMLDesc, [:pointer, :xml_flags], :string
163
378
 
164
379
  # char *virDomainScreenshot (
165
380
  # virDomainPtr domain,
166
- # virStreamPtr stream,
167
- # unsigned int screen,
168
- # unsigned int flags
381
+ # virStreamPtr stream,
382
+ # unsigned int screen,
383
+ # unsigned int flags
169
384
  # )
170
385
  attach_function :virDomainScreenshot, [:pointer, :pointer, :uint, :uint], :strptr
171
386
 
172
- # typedef int (*virConnectDomainEventCallback) (
387
+ # typedef int (*virConnectDomainEventCallback) (
173
388
  # virConnectPtr conn,
174
- # virDomainPtr dom,
175
- # int event,
176
- # int detail,
177
- # void * opaque
389
+ # virDomainPtr dom,
390
+ # int event,
391
+ # int detail,
392
+ # void * opaque
178
393
  # )
179
- callback :virConnectDomainEventCallback, [:pointer, :pointer, :int, :int, :pointer], :int
394
+ callback :virConnectDomainEventCallback, [:pointer, :pointer, :event_type, :int, :pointer], :int
180
395
 
181
396
  # typedef void (*virConnectDomainEventGenericCallback) (
182
397
  # virConnectPtr conn,
183
- # virDomainPtr dom,
184
- # void * opaque
398
+ # virDomainPtr dom,
399
+ # void * opaque
185
400
  # )
186
401
  callback :virConnectDomainEventGenericCallback, [:pointer, :pointer, :pointer], :void
187
402
 
188
403
  # typedef void (*virConnectDomainEventRTCChangeCallback) (
189
404
  # virConnectPtr conn,
190
- # virDomainPtr dom,
191
- # long long utcoffset,
192
- # void * opaque
405
+ # virDomainPtr dom,
406
+ # long long utcoffset,
407
+ # void * opaque
193
408
  # )
194
409
  callback :virConnectDomainEventRTCChangeCallback, [
195
410
  :pointer,
@@ -198,11 +413,11 @@ module Libvirt
198
413
  :pointer
199
414
  ], :void
200
415
 
201
- # typedef void (*virConnectDomainEventWatchdogCallback) (
416
+ # typedef void (*virConnectDomainEventWatchdogCallback) (
202
417
  # virConnectPtr conn,
203
- # virDomainPtr dom,
204
- # int action,
205
- # void * opaque
418
+ # virDomainPtr dom,
419
+ # int action,
420
+ # void * opaque
206
421
  # )
207
422
  callback :virConnectDomainEventWatchdogCallback, [
208
423
  :pointer,
@@ -213,11 +428,11 @@ module Libvirt
213
428
 
214
429
  # typedef void (*virConnectDomainEventIOErrorCallback) (
215
430
  # virConnectPtr conn,
216
- # virDomainPtr dom,
217
- # const char * srcPath,
218
- # const char * devAlias,
219
- # int action,
220
- # void * opaque
431
+ # virDomainPtr dom,
432
+ # const char * srcPath,
433
+ # const char * devAlias,
434
+ # int action,
435
+ # void * opaque
221
436
  # )
222
437
  callback :virConnectDomainEventIOErrorCallback, [
223
438
  :pointer,
@@ -228,35 +443,35 @@ module Libvirt
228
443
  :pointer
229
444
  ], :void
230
445
 
231
- # typedef void (*virConnectDomainEventGraphicsCallback) (
446
+ # typedef void (*virConnectDomainEventGraphicsCallback) (
232
447
  # virConnectPtr conn,
233
- # virDomainPtr dom,
234
- # int phase,
235
- # const virDomainEventGraphicsAddress * local,
236
- # const virDomainEventGraphicsAddress * remote,
237
- # const char * authScheme,
238
- # const virDomainEventGraphicsSubject * subject,
239
- # void * opaque
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
240
455
  # )
241
456
  callback :virConnectDomainEventGraphicsCallback, [
242
457
  :pointer,
243
458
  :pointer,
244
459
  :int,
245
- # virDomainEventGraphicsAddress
246
- # virDomainEventGraphicsAddress
460
+ :pointer,
461
+ :pointer,
247
462
  :string,
248
- # virDomainEventGraphicsSubject
463
+ :pointer,
249
464
  :pointer
250
465
  ], :void
251
466
 
252
- # typedef void (*virConnectDomainEventIOErrorReasonCallback) (
467
+ # typedef void (*virConnectDomainEventIOErrorReasonCallback) (
253
468
  # virConnectPtr conn,
254
- # virDomainPtr dom,
255
- # const char * srcPath,
256
- # const char * devAlias,
257
- # int action,
258
- # const char * reason,
259
- # void * opaque
469
+ # virDomainPtr dom,
470
+ # const char * srcPath,
471
+ # const char * devAlias,
472
+ # int action,
473
+ # const char * reason,
474
+ # void * opaque
260
475
  # )
261
476
  callback :virConnectDomainEventIOErrorReasonCallback, [
262
477
  :pointer,
@@ -268,13 +483,13 @@ module Libvirt
268
483
  :pointer
269
484
  ], :void
270
485
 
271
- # typedef void (*virConnectDomainEventBlockJobCallback) (
486
+ # typedef void (*virConnectDomainEventBlockJobCallback) (
272
487
  # virConnectPtr conn,
273
- # virDomainPtr dom,
274
- # const char * disk,
275
- # int type,
276
- # int status,
277
- # void * opaque
488
+ # virDomainPtr dom,
489
+ # const char * disk,
490
+ # int type,
491
+ # int status,
492
+ # void * opaque
278
493
  # )
279
494
  callback :virConnectDomainEventBlockJobCallback, [
280
495
  :pointer,
@@ -285,14 +500,14 @@ module Libvirt
285
500
  :pointer
286
501
  ], :void
287
502
 
288
- # typedef void (*virConnectDomainEventDiskChangeCallback) (
503
+ # typedef void (*virConnectDomainEventDiskChangeCallback) (
289
504
  # virConnectPtr conn,
290
- # virDomainPtr dom,
291
- # const char * oldSrcPath,
292
- # const char * newSrcPath,
293
- # const char * devAlias,
294
- # int reason,
295
- # void * opaque
505
+ # virDomainPtr dom,
506
+ # const char * oldSrcPath,
507
+ # const char * newSrcPath,
508
+ # const char * devAlias,
509
+ # int reason,
510
+ # void * opaque
296
511
  # )
297
512
  callback :virConnectDomainEventDiskChangeCallback, [
298
513
  :pointer,
@@ -304,12 +519,12 @@ module Libvirt
304
519
  :pointer
305
520
  ], :void
306
521
 
307
- # typedef void (*virConnectDomainEventTrayChangeCallback) (
522
+ # typedef void (*virConnectDomainEventTrayChangeCallback) (
308
523
  # virConnectPtr conn,
309
- # virDomainPtr dom,
310
- # const char * devAlias,
311
- # int reason,
312
- # void * opaque
524
+ # virDomainPtr dom,
525
+ # const char * devAlias,
526
+ # int reason,
527
+ # void * opaque
313
528
  # )
314
529
  callback :virConnectDomainEventTrayChangeCallback, [
315
530
  :pointer,
@@ -319,11 +534,11 @@ module Libvirt
319
534
  :pointer
320
535
  ], :void
321
536
 
322
- # typedef void (*virConnectDomainEventPMWakeupCallback) (
537
+ # typedef void (*virConnectDomainEventPMWakeupCallback) (
323
538
  # virConnectPtr conn,
324
- # virDomainPtr dom,
325
- # int reason,
326
- # void * opaque
539
+ # virDomainPtr dom,
540
+ # int reason,
541
+ # void * opaque
327
542
  # )
328
543
  callback :virConnectDomainEventPMWakeupCallback, [
329
544
  :pointer,
@@ -332,11 +547,11 @@ module Libvirt
332
547
  :pointer
333
548
  ], :void
334
549
 
335
- # typedef void (*virConnectDomainEventPMSuspendCallback) (
550
+ # typedef void (*virConnectDomainEventPMSuspendCallback) (
336
551
  # virConnectPtr conn,
337
- # virDomainPtr dom,
338
- # int reason,
339
- # void * opaque
552
+ # virDomainPtr dom,
553
+ # int reason,
554
+ # void * opaque
340
555
  # )
341
556
  callback :virConnectDomainEventPMSuspendCallback, [
342
557
  :pointer,
@@ -345,11 +560,11 @@ module Libvirt
345
560
  :pointer
346
561
  ], :void
347
562
 
348
- # typedef void (*virConnectDomainEventBalloonChangeCallback) (
563
+ # typedef void (*virConnectDomainEventBalloonChangeCallback) (
349
564
  # virConnectPtr conn,
350
- # virDomainPtr dom,
351
- # unsigned long long actual,
352
- # void * opaque
565
+ # virDomainPtr dom,
566
+ # unsigned long long actual,
567
+ # void * opaque
353
568
  # )
354
569
  callback :virConnectDomainEventBalloonChangeCallback, [
355
570
  :pointer,
@@ -358,11 +573,11 @@ module Libvirt
358
573
  :pointer
359
574
  ], :void
360
575
 
361
- # typedef void (*virConnectDomainEventPMSuspendDiskCallback) (
576
+ # typedef void (*virConnectDomainEventPMSuspendDiskCallback) (
362
577
  # virConnectPtr conn,
363
- # virDomainPtr dom,
364
- # int reason,
365
- # void * opaque
578
+ # virDomainPtr dom,
579
+ # int reason,
580
+ # void * opaque
366
581
  # )
367
582
  callback :virConnectDomainEventPMSuspendDiskCallback, [
368
583
  :pointer,
@@ -371,11 +586,11 @@ module Libvirt
371
586
  :pointer
372
587
  ], :void
373
588
 
374
- # typedef void (*virConnectDomainEventDeviceRemovedCallback) (
589
+ # typedef void (*virConnectDomainEventDeviceRemovedCallback) (
375
590
  # virConnectPtr conn,
376
- # virDomainPtr dom,
377
- # const char * devAlias,
378
- # void * opaque
591
+ # virDomainPtr dom,
592
+ # const char * devAlias,
593
+ # void * opaque
379
594
  # )
380
595
  callback :virConnectDomainEventDeviceRemovedCallback, [
381
596
  :pointer,
@@ -384,27 +599,27 @@ module Libvirt
384
599
  :pointer
385
600
  ], :void
386
601
 
387
- # typedef void (*virConnectDomainEventTunableCallback) (
602
+ # typedef void (*virConnectDomainEventTunableCallback) (
388
603
  # virConnectPtr conn,
389
- # virDomainPtr dom,
390
- # virTypedParameterPtr params,
391
- # int nparams,
392
- # void * opaque
604
+ # virDomainPtr dom,
605
+ # virTypedParameterPtr params,
606
+ # int nparams,
607
+ # void * opaque
393
608
  # )
394
609
  callback :virConnectDomainEventTunableCallback, [
395
610
  :pointer,
396
611
  :pointer,
397
- # virTypedParameterPtr
612
+ :pointer,
398
613
  :int,
399
614
  :pointer
400
615
  ], :void
401
616
 
402
- # typedef void (*virConnectDomainEventAgentLifecycleCallback) (
617
+ # typedef void (*virConnectDomainEventAgentLifecycleCallback) (
403
618
  # virConnectPtr conn,
404
- # virDomainPtr dom,
405
- # int state,
406
- # int reason,
407
- # void * opaque
619
+ # virDomainPtr dom,
620
+ # int state,
621
+ # int reason,
622
+ # void * opaque
408
623
  # )
409
624
  callback :virConnectDomainEventAgentLifecycleCallback, [
410
625
  :pointer,
@@ -414,11 +629,11 @@ module Libvirt
414
629
  :pointer
415
630
  ], :void
416
631
 
417
- # typedef void (*virConnectDomainEventDeviceAddedCallback) (
632
+ # typedef void (*virConnectDomainEventDeviceAddedCallback) (
418
633
  # virConnectPtr conn,
419
- # virDomainPtr dom,
420
- # const char * devAlias,
421
- # void * opaque
634
+ # virDomainPtr dom,
635
+ # const char * devAlias,
636
+ # void * opaque
422
637
  # )
423
638
  callback :virConnectDomainEventDeviceAddedCallback, [
424
639
  :pointer,
@@ -427,11 +642,11 @@ module Libvirt
427
642
  :pointer
428
643
  ], :void
429
644
 
430
- # typedef void (*virConnectDomainEventMigrationIterationCallback) (
645
+ # typedef void (*virConnectDomainEventMigrationIterationCallback) (
431
646
  # virConnectPtr conn,
432
- # virDomainPtr dom,
433
- # int iteration,
434
- # void * opaque
647
+ # virDomainPtr dom,
648
+ # int iteration,
649
+ # void * opaque
435
650
  # )
436
651
  callback :virConnectDomainEventMigrationIterationCallback, [
437
652
  :pointer,
@@ -440,26 +655,26 @@ module Libvirt
440
655
  :pointer
441
656
  ], :void
442
657
 
443
- # typedef void (*virConnectDomainEventJobCompletedCallback) (
658
+ # typedef void (*virConnectDomainEventJobCompletedCallback) (
444
659
  # virConnectPtr conn,
445
- # virDomainPtr dom,
446
- # virTypedParameterPtr params,
447
- # int nparams,
448
- # void * opaque
660
+ # virDomainPtr dom,
661
+ # virTypedParameterPtr params,
662
+ # int nparams,
663
+ # void * opaque
449
664
  # )
450
665
  callback :virConnectDomainEventJobCompletedCallback, [
451
666
  :pointer,
452
667
  :pointer,
453
- # virTypedParameterPtr
668
+ :pointer,
454
669
  :int,
455
670
  :pointer
456
671
  ], :void
457
672
 
458
- # typedef void (*virConnectDomainEventDeviceRemovalFailedCallback) (
673
+ # typedef void (*virConnectDomainEventDeviceRemovalFailedCallback) (
459
674
  # virConnectPtr conn,
460
- # virDomainPtr dom,
461
- # const char * devAlias,
462
- # void * opaque
675
+ # virDomainPtr dom,
676
+ # const char * devAlias,
677
+ # void * opaque
463
678
  # )
464
679
  callback :virConnectDomainEventDeviceRemovalFailedCallback, [
465
680
  :pointer,
@@ -468,12 +683,12 @@ module Libvirt
468
683
  :pointer
469
684
  ], :void
470
685
 
471
- # typedef void (*virConnectDomainEventMetadataChangeCallback) (
686
+ # typedef void (*virConnectDomainEventMetadataChangeCallback) (
472
687
  # virConnectPtr conn,
473
- # virDomainPtr dom,
474
- # int type,
475
- # const char * nsuri,
476
- # void * opaque
688
+ # virDomainPtr dom,
689
+ # int type,
690
+ # const char * nsuri,
691
+ # void * opaque
477
692
  # )
478
693
  callback :virConnectDomainEventMetadataChangeCallback, [
479
694
  :pointer,
@@ -482,14 +697,14 @@ module Libvirt
482
697
  :pointer
483
698
  ], :void
484
699
 
485
- # typedef void (*virConnectDomainEventBlockThresholdCallback) (
700
+ # typedef void (*virConnectDomainEventBlockThresholdCallback) (
486
701
  # virConnectPtr conn,
487
- # virDomainPtr dom,
488
- # const char * dev,
489
- # const char * path,
490
- # unsigned long long threshold,
491
- # unsigned long long excess,
492
- # void * opaque
702
+ # virDomainPtr dom,
703
+ # const char * dev,
704
+ # const char * path,
705
+ # unsigned long long threshold,
706
+ # unsigned long long excess,
707
+ # void * opaque
493
708
  # )
494
709
  callback :virConnectDomainEventBlockThresholdCallback, [
495
710
  :pointer,
@@ -501,22 +716,140 @@ module Libvirt
501
716
  :pointer
502
717
  ], :void
503
718
 
504
- # @param event_id [Integer]
719
+ # int virDomainReboot (
720
+ # virDomainPtr domain,
721
+ # unsigned int flags
722
+ # )
723
+ attach_function :virDomainReboot, [:pointer, :uint], :int
724
+
725
+ # int virDomainShutdownFlags (
726
+ # virDomainPtr domain,
727
+ # unsigned int flags
728
+ # )
729
+ attach_function :virDomainShutdownFlags, [:pointer, :shutdown_flags], :int
730
+
731
+ # int virDomainDestroyFlags (
732
+ # virDomainPtr domain,
733
+ # unsigned int flags
734
+ # )
735
+ attach_function :virDomainDestroyFlags, [:pointer, :uint], :int
736
+
737
+ # int virDomainReset (
738
+ # virDomainPtr domain,
739
+ # unsigned int flags
740
+ # )
741
+ attach_function :virDomainReset, [:pointer, :uint], :int
742
+
743
+ # int virDomainSuspend (
744
+ # virDomainPtr domain
745
+ # )
746
+ attach_function :virDomainSuspend, [:pointer], :int
747
+
748
+ # int virDomainResume (
749
+ # virDomainPtr domain
750
+ # )
751
+ attach_function :virDomainResume, [:pointer], :int
752
+
753
+ # int virDomainCreateWithFlags (
754
+ # virDomainPtr domain,
755
+ # unsigned int flags
756
+ # )
757
+ attach_function :virDomainCreateWithFlags, [:pointer, :uint], :int
758
+
759
+ # int virDomainManagedSave (
760
+ # virDomainPtr dom,
761
+ # unsigned int flags
762
+ # )
763
+ attach_function :virDomainManagedSave, [:pointer, :save_restore_flags], :int
764
+
765
+ # int virDomainUndefineFlags (
766
+ # virDomainPtr domain,
767
+ # unsigned int flags
768
+ # )
769
+ attach_function :virDomainUndefineFlags, [:pointer, :uint], :int
770
+
771
+ # virDomainPtr virDomainDefineXMLFlags (
772
+ # virConnectPtr conn,
773
+ # const char * xml,
774
+ # unsigned int flags
775
+ # )
776
+ attach_function :virDomainDefineXMLFlags, [:pointer, :string, :uint], :pointer
777
+
778
+ # int virDomainSetMetadata (
779
+ # virDomainPtr domain,
780
+ # int type,
781
+ # const char * metadata,
782
+ # const char * key,
783
+ # const char * uri,
784
+ # unsigned int flags
785
+ # )
786
+ attach_function :virDomainSetMetadata,
787
+ [:pointer, :metadata_type, :string, :string, :string, :modification_impact],
788
+ :int
789
+
790
+ # char * virDomainGetMetadata (
791
+ # virDomainPtr domain,
792
+ # int type,
793
+ # const char * uri,
794
+ # unsigned int flags
795
+ # )
796
+ attach_function :virDomainGetMetadata,
797
+ [:pointer, :metadata_type, :string, :modification_impact],
798
+ :string
799
+
800
+ # Converts detail from lifecycle callback from integer to symbol name.
801
+ # @param event [Symbol] enum :event_type (virDomainEventType)
802
+ # @param detail [Integer]
803
+ # @return [Symbol]
804
+ def self.event_detail_type(event, detail)
805
+ detail_enum = enum_type(:"event_#{event.to_s.downcase}_detail_type")
806
+ detail_enum[detail]
807
+ end
808
+
809
+ def self.state_reason(state, reason)
810
+ reason_enum = enum_type(:"#{state.to_s.downcase}_reason")
811
+ reason_enum[reason]
812
+ end
813
+
814
+ # Creates event callback function for provided event_id
815
+ # @param event_id [Integer,Symbol]
505
816
  # @yield connect_ptr, domain_ptr, *args, opaque_ptr
506
- def self.event_callback(event_id, &block)
507
- event_id_sym = enum_type(:event_id)[event_id]
817
+ # @return [FFI::Function]
818
+ def self.event_callback_for(event_id, &block)
819
+ event_id_sym = event_id.is_a?(Symbol) ? event_id : enum_type(:event_id)[event_id]
820
+
821
+ case event_id_sym
822
+ when :LIFECYCLE
823
+ event_callback(&block)
824
+ else
825
+ event_callback_base(event_id_sym, &block)
826
+ end
827
+ end
828
+
829
+ # Creates generic event callback function for provided event_id_sym
830
+ # @param event_id_sym [Symbol]
831
+ # @yield connect_ptr, domain_ptr, *args, opaque_ptr
832
+ # @return [FFI::Function]
833
+ def self.event_callback_base(event_id_sym, &block)
508
834
  callback_name = EVENT_ID_TO_CALLBACK.fetch(event_id_sym)
509
- callback_info = find_type(callback_name)
510
- ::FFI::Function.new(callback_info.result_type, callback_info.param_types) do |*args|
835
+ callback_function(callback_name) do |*args|
511
836
  Util.log(:debug) { "Libvirt::Domain #{event_id_sym} CALLBACK #{args.map(&:to_s).join(', ')}," }
512
837
  block.call(*args)
513
- # Only callback for lifecycle must return 0.
514
- # Return value of other callbacks are ignored.
515
- # So we just pass zero everywhere.
516
- 0
517
838
  end
518
839
  end
519
840
 
841
+ # Creates event callback function for lifecycle event_id
842
+ # @param event_id_sym [Symbol]
843
+ # @yield connect_ptr, domain_ptr, event, detail, opaque_ptr
844
+ # @return [FFI::Function]
845
+ def self.event_callback(&block)
846
+ callback_function(:virConnectDomainEventCallback) do |conn, dom, event, detail, opaque|
847
+ detail_sym = event_detail_type(event, detail)
848
+ Util.log(:debug) { "Libvirt::Domain LIFECYCLE CALLBACK #{conn}, #{dom}, #{event}, #{detail_sym}, #{opaque}," }
849
+ block.call(conn, dom, event, detail_sym, opaque)
850
+ 0
851
+ end
852
+ end
520
853
  end
521
854
  end
522
855
  end