libvirt_ffi 0.6.1 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,185 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require 'bundler/setup'
5
- require 'libvirt'
6
- require 'logger'
7
- require 'active_support/all'
8
- require 'async'
9
- require 'get_process_mem'
10
- require 'gc_tracer'
11
-
12
- require_relative 'support/libvirt_async'
13
- require_relative 'support/log_formatter'
14
-
15
- GC::Tracer.start_logging(
16
- nil,
17
- gc_stat: false,
18
- gc_latest_gc_info: false,
19
- rusage: false,
20
- events: [:end_mark, :end_sweep]
21
- )
22
-
23
- Libvirt.logger = Logger.new(STDOUT, formatter: LogFormatter.new)
24
- Libvirt.logger.level = ENV['DEBUG'] ? :debug : :info
25
-
26
- IMPL = LibvirtAsync::Implementations.new
27
- OBJECTS = {
28
- hv: nil,
29
- domains: [],
30
- cb_ids: []
31
- }.freeze
32
-
33
- Async do
34
- ASYNC_REACTOR = Async::Task.current.reactor
35
-
36
- puts "Lib version #{Libvirt.lib_version}"
37
- puts "Gem version #{Libvirt::VERSION}"
38
-
39
- IMPL.start
40
-
41
- OBJECTS[:hv] = Libvirt::Connection.new('qemu+tcp://localhost:16510/system')
42
- OBJECTS[:hv].open
43
- res = OBJECTS[:hv].set_keep_alive(2, 1)
44
- Libvirt.logger.info { "set_keep_alive #{res}" }
45
-
46
- puts "Connection version #{OBJECTS[:hv].version.inspect}"
47
- puts "Connection lib_version #{OBJECTS[:hv].lib_version.inspect}"
48
- puts "Connection hostname #{OBJECTS[:hv].hostname.inspect}"
49
- puts "Connection max_vcpus #{OBJECTS[:hv].max_vcpus.inspect}"
50
- puts "Connection capabilities #{OBJECTS[:hv].capabilities.inspect}"
51
- node_info = OBJECTS[:hv].node_info
52
- puts "Connection nodeInfo #{node_info}"
53
- puts "NodeInfo model #{node_info.model.inspect}"
54
- puts "NodeInfo cpus #{node_info.cpus.inspect}"
55
- puts "NodeInfo mhz #{node_info.mhz.inspect}"
56
- puts "NodeInfo nodes #{node_info.nodes.inspect}"
57
- puts "NodeInfo sockets #{node_info.sockets.inspect}"
58
- puts "NodeInfo cores #{node_info.cores.inspect}"
59
- puts "NodeInfo threads #{node_info.threads.inspect}"
60
- puts "NodeInfo memory #{node_info.memory.inspect}"
61
-
62
- Libvirt::Connection::DOMAIN_EVENT_IDS.map do |event_id|
63
- op = OpenStruct.new(a: 'b', event_id: event_id)
64
- callback_id = OBJECTS[:hv].register_domain_event_callback(event_id, nil, op) do |conn, dom, *args, opaque|
65
- Libvirt.logger.info { "DOMAIN EVENT #{event_id} conn=#{conn}, dom=#{dom}, args=#{args}, opaque=#{opaque}" }
66
- end
67
- Libvirt.logger.info { "Registered domain event callback event_id=#{event_id} callback_id=#{callback_id}" }
68
- OBJECTS[:cb_ids] << callback_id
69
- end
70
-
71
- puts "domains qty #{OBJECTS[:hv].list_all_domains_qty}"
72
-
73
- OBJECTS[:domains] = OBJECTS[:hv].list_all_domains
74
- puts "Domains (#{OBJECTS[:domains].size}): #{OBJECTS[:domains]}"
75
-
76
- d = OBJECTS[:domains].first
77
- puts "Domain uuid #{d.uuid.inspect}"
78
- puts "Domain name #{d.name.inspect}"
79
- puts "Domain get_state #{d.get_state.inspect}"
80
- puts "Domain get_cpus #{d.max_vcpus.inspect}"
81
- puts "Domain max_memory #{d.max_memory.inspect}"
82
- # puts "Domain xml_desc #{d.xml_desc.inspect}"
83
-
84
- # ASYNC_REACTOR.every(10) do
85
- # LibvirtAsync::Util.create_task(nil, ASYNC_REACTOR) { IMPL.print_debug_info }.run
86
- # end
87
-
88
- ASYNC_REACTOR.every(5) do
89
- Libvirt.logger.info { "MEM USAGE: #{GetProcessMem.new.mb} MB" }
90
- # Libvirt.logger.info { "GC.start" }
91
- # GC.start
92
- # Libvirt.logger.info { "MEM USAGE: #{GetProcessMem.new.mb} MB" }
93
- end
94
-
95
- # ASYNC_REACTOR.after(20) do
96
- # Libvirt.logger.info { 'START Cleaning up!' }
97
- #
98
- # LibvirtAsync::Util.create_task(nil, ASYNC_REACTOR) do
99
- #
100
- # OBJECTS[:cb_ids].each do |callback_id|
101
- # Libvirt.logger.info { "Start retrieving callback_id=#{callback_id}" }
102
- # opaque = OBJECTS[:hv].deregister_domain_event_callback(callback_id)
103
- # Libvirt.logger.info { "Retrieved opaque=#{opaque}" }
104
- # end
105
- # Libvirt.logger.info { 'Cleaning up!' }
106
- # OBJECTS[:hv] = nil
107
- # OBJECTS[:domains] = []
108
- # OBJECTS[:cb_ids] = []
109
- # Libvirt.logger.info { "GC.start 1" }
110
- # GC.start
111
- #
112
- # ASYNC_REACTOR << LibvirtAsync::Util.create_task(nil, ASYNC_REACTOR) do
113
- # Libvirt.logger.info { "GC.start 2" }
114
- # GC.start
115
- # end.fiber
116
- #
117
- # end.run
118
- # end
119
-
120
- # puts 'undefine DOM'
121
- # d.undefine
122
- # ASYNC_REACTOR.sleep 5
123
-
124
- # begin
125
- # puts 'DOM starting...'
126
- # d.start
127
- # puts 'DOM started'
128
- # rescue Libvirt::Errors::LibError => e
129
- # STDERR.puts "error starting\n#{e.class}\n#{e.message}", e.error_data
130
- # end
131
- #
132
- # ASYNC_REACTOR.sleep 5
133
- # puts "DOMAIN state #{d.get_state} before save_memory"
134
- # d.save_memory
135
- # puts "DOMAIN state #{d.get_state} after save_memory"
136
- #
137
- # ASYNC_REACTOR.sleep 5
138
- # puts "DOMAIN state #{d.get_state} before start"
139
- # d.start
140
- # puts "DOMAIN state #{d.get_state} after start"
141
- #
142
- # #ASYNC_REACTOR.sleep 5
143
- # puts "DOMAIN state #{d.get_state} before resume"
144
- # d.resume
145
- # puts "DOMAIN state #{d.get_state} after resume"
146
-
147
- # ASYNC_REACTOR.sleep 10
148
- # puts "DOMAIN state #{d.get_state} before shutdown"
149
- # d.shutdown(1)
150
- # puts "DOMAIN state #{d.get_state} after shutdown"
151
- #
152
- # ASYNC_REACTOR.sleep 10
153
- # puts "DOMAIN state #{d.get_state}"
154
- # d.start
155
- # puts 'DOM start'
156
- #
157
- # ASYNC_REACTOR.sleep 10
158
- # puts "DOMAIN state #{d.get_state}"
159
- # d.reboot
160
- # puts 'DOM reboot'
161
- #
162
- # ASYNC_REACTOR.sleep 5
163
- # puts "DOMAIN state #{d.get_state}"
164
- # d.suspend
165
- # puts 'DOM suspend'
166
- #
167
- # ASYNC_REACTOR.sleep 5
168
- # puts "DOMAIN state #{d.get_state}"
169
- # d.resume
170
- # puts 'DOM resume'
171
- #
172
- # ASYNC_REACTOR.sleep 5
173
- # puts "DOMAIN state #{d.get_state}"
174
- # d.reset
175
- # puts 'DOM reset'
176
- #
177
- # ASYNC_REACTOR.sleep 5
178
- # puts "DOMAIN state #{d.get_state}"
179
- # d.power_off
180
- # puts 'DOM power_off'
181
- #
182
- # ASYNC_REACTOR.sleep 5
183
- # d.start
184
- # puts 'DOM start'
185
- end
@@ -1,63 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require 'bundler/setup'
5
- require 'libvirt'
6
- require 'logger'
7
- require 'active_support/all'
8
- require 'async'
9
- require 'get_process_mem'
10
- require 'gc_tracer'
11
-
12
- require_relative 'support/libvirt_async'
13
- require_relative 'support/log_formatter'
14
-
15
- GC::Tracer.start_logging(
16
- nil,
17
- gc_stat: false,
18
- gc_latest_gc_info: false,
19
- rusage: false,
20
- events: [:end_mark, :end_sweep]
21
- )
22
-
23
- Libvirt.logger = Logger.new(STDOUT, formatter: LogFormatter.new)
24
- Libvirt.logger.level = ENV['DEBUG'] ? :debug : :info
25
-
26
- IMPL = LibvirtAsync::Implementations.new
27
- OBJECTS = {
28
- hv: nil
29
- }.freeze
30
-
31
- def async_task(run, parent = nil, &block)
32
- task = LibvirtAsync::Util.create_task(parent, ASYNC_REACTOR, &block)
33
- case run
34
- when :now
35
- task.run
36
- when :later
37
- task.reactor << task.fiber
38
- else
39
- raise ArgumentError, "invalid run #{run}"
40
- end
41
- end
42
-
43
- Async do
44
- ASYNC_REACTOR = Async::Task.current.reactor
45
-
46
- puts "Lib version #{Libvirt.lib_version}"
47
- puts "Gem version #{Libvirt::VERSION}"
48
-
49
- IMPL.start
50
-
51
- OBJECTS[:hv] = Libvirt::Connection.new('qemu+tcp://localhost:16510/system')
52
- OBJECTS[:hv].open
53
- OBJECTS[:hv].register_close_callback do |conn, reason, _op|
54
- puts "Im closing conn=#{conn}, reason=#{reason}"
55
- end
56
- # OBJECTS[:hv].set_keep_alive(2, 1)
57
-
58
- ASYNC_REACTOR.every(5) do
59
- async_task(:now) do
60
- puts "list_all_domains_qty #{OBJECTS[:hv].list_all_domains_qty}"
61
- end
62
- end
63
- end
@@ -1,104 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require 'bundler/setup'
5
- require 'libvirt'
6
- require 'logger'
7
- require 'active_support/all'
8
- require 'async'
9
-
10
- require_relative 'support/libvirt_async'
11
- require_relative 'support/log_formatter'
12
-
13
- require 'libvirt/xml'
14
-
15
- Libvirt.logger = Logger.new(STDOUT, formatter: LogFormatter.new)
16
- Libvirt.logger.level = ENV['DEBUG'] ? :debug : :info
17
-
18
- IMPL = LibvirtAsync::Implementations.new
19
-
20
- STDOUT.sync = true
21
- STDERR.sync = true
22
-
23
- def log_error(error, skip_backtrace: false, causes: [])
24
- STDERR.puts "<#{error.class}>: #{error.message}", error.backtrace
25
- if error.cause && error.cause != error && !causes.include?(error.cause)
26
- causes.push(error)
27
- log_error(error.cause, skip_backtrace: skip_backtrace, causes: causes)
28
- end
29
- end
30
-
31
- def libvirt_safe(rescue_value = nil)
32
- yield
33
- rescue Libvirt::Errors::LibError => e
34
- STDERR.puts "<#{e.class}>: #{e.message}"
35
- rescue_value
36
- end
37
-
38
- Async do
39
- ASYNC_REACTOR = Async::Task.current.reactor
40
-
41
- puts "Lib version #{Libvirt.lib_version}"
42
- puts "Gem version #{Libvirt::VERSION}"
43
-
44
- IMPL.start
45
-
46
- conn = Libvirt::Connection.new('qemu+tcp://localhost:16510/system')
47
- conn.open
48
-
49
- puts "Connection version #{conn.version.inspect}"
50
- puts "Connection lib_version #{conn.lib_version.inspect}"
51
- puts "Connection hostname #{conn.hostname.inspect}"
52
-
53
- dom = conn.list_all_domains.first
54
- puts "Domain #{dom.uuid} #{dom.name} #{dom.get_state}"
55
-
56
- libvirt_safe do
57
- dom.start
58
- end
59
-
60
- dom.set_metadata("test title #{Process.pid}", type: :TITLE, flags: :AFFECT_CONFIG)
61
- dom.set_metadata("test desc #{Process.pid}", type: :DESCRIPTION, flags: :AFFECT_CONFIG)
62
-
63
- puts "domain title", libvirt_safe{ dom.get_metadata(type: :TITLE, flags: :AFFECT_CONFIG) }
64
- puts "domain description", libvirt_safe { dom.get_metadata(type: :DESCRIPTION, flags: :AFFECT_CONFIG) }
65
-
66
- puts "full XML title", Libvirt::Xml::Domain.load(dom.xml_desc).title
67
- puts "full XML description", Libvirt::Xml::Domain.load(dom.xml_desc).description
68
-
69
- namespace = 'https://example.com'
70
- old_metadata = dom.get_metadata(
71
- uri: namespace, flags: :AFFECT_CONFIG
72
- )
73
-
74
- puts "Old Metadata", old_metadata
75
-
76
- new_metadata = "<pid>#{Process.pid}</pid>"
77
- key = 'example'
78
- dom.set_metadata new_metadata,
79
- key: key,
80
- uri: namespace,
81
- flags: :AFFECT_CONFIG
82
-
83
- puts "new metadata", dom.get_metadata(
84
- uri: namespace, flags: :AFFECT_CONFIG
85
- )
86
-
87
- puts "full XML metadata", Libvirt::Xml::Domain.load(dom.xml_desc).metadata
88
-
89
- puts "domain shutdown", libvirt_safe { dom.shutdown }
90
- ASYNC_REACTOR.sleep 5
91
- puts dom.get_state
92
-
93
- puts "domain start", libvirt_safe { dom.start }
94
- ASYNC_REACTOR.sleep 2
95
- puts dom.get_state
96
-
97
- puts "full XML metadata", Libvirt::Xml::Domain.load(dom.xml_desc).metadata
98
- puts "full XML title", Libvirt::Xml::Domain.load(dom.xml_desc).title
99
- puts "full XML description", Libvirt::Xml::Domain.load(dom.xml_desc).description
100
-
101
- rescue StandardError => e
102
- log_error(e)
103
- exit 1
104
- end
@@ -1,197 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require 'bundler/setup'
5
- require 'libvirt'
6
- require 'logger'
7
- require 'active_support/all'
8
- require 'async'
9
- require 'get_process_mem'
10
-
11
- require_relative 'support/libvirt_async'
12
- require_relative 'support/log_formatter'
13
-
14
- Libvirt.logger = Logger.new(STDOUT, formatter: LogFormatter.new)
15
- Libvirt.logger.level = ENV['DEBUG'] ? :debug : :info
16
-
17
- LibvirtAsync.logger = Logger.new(STDOUT, formatter: LogFormatter.new)
18
- LibvirtAsync.logger.level = ENV['LIBVIRT_DEBUG'] ? :debug : :info
19
-
20
- def print_usage(msg)
21
- mem = GetProcessMem.new
22
- STDOUT.puts "#{msg} [#{mem.mb}MB]"
23
- end
24
-
25
- def run_gc(msg)
26
- print_usage "#{msg} before GC.start"
27
- GC.start
28
- print_usage "#{msg} after GC.start"
29
- end
30
-
31
- IMPL = LibvirtAsync::Implementations.new
32
- CONNS = [].freeze
33
- DOMS = [].freeze
34
- STREAMS = { stream: nil }.freeze
35
-
36
- class ScreenshotOpaque
37
- CALLBACK = proc do |s, ev, op|
38
- # run_gc('ScreenshotOpaque CALLBACK start')
39
- next unless (Libvirt::Stream::EVENT_READABLE & ev) != 0
40
-
41
- begin
42
- code, data = s.recv(1024)
43
- rescue Libvirt::Errors::LibError => e
44
- op.on_libvirt_error(s, e)
45
- next
46
- end
47
- # run_gc('ScreenshotOpaque CALLBACK after recv')
48
-
49
- case code
50
- when 0
51
- op.on_complete(s)
52
- when -1
53
- op.on_recv_error(s)
54
- when -2
55
- print_usage "Opaque::CALLBACK #{op.filepath} wait for data"
56
- else
57
- op.on_receive(data)
58
- end
59
- end
60
-
61
- attr_reader :filepath
62
-
63
- def initialize(filepath, finish_cb)
64
- @filepath = filepath
65
- @f = File.open(@filepath, 'wb')
66
- @finish_cb = finish_cb
67
- end
68
-
69
- def on_complete(stream)
70
- print_usage "Opaque#on_complete #{@filepath}"
71
- success, reason = finish_stream(stream)
72
- finish(success, reason)
73
- end
74
-
75
- def on_receive(data)
76
- print_usage "Opaque#on_receive #{@filepath} #{data&.size}"
77
- @f.write(data)
78
- end
79
-
80
- def on_recv_error(stream)
81
- print_usage "Opaque#on_recv_error #{@filepath}"
82
- success, reason = finish_stream(stream)
83
- finish(success, reason)
84
- end
85
-
86
- def on_libvirt_error(stream, e)
87
- print_usage "Opaque#on_libvirt_error #{@filepath} #{e}"
88
- success, reason = finish_stream(stream)
89
- finish(success, reason)
90
- end
91
-
92
- private
93
-
94
- def finish_stream(stream)
95
- print_usage "Opaque#finish_stream stream.event_remove_callback #{@filepath}"
96
- stream.event_remove_callback
97
- result = begin
98
- print_usage "Opaque#finish_stream stream.finish #{@filepath}"
99
- stream.finish
100
- [true, nil]
101
- rescue Libvirt::Errors::LibError => e
102
- warn "Opaque#finish_stream stream.finish exception rescued #{e.class} #{e.message}"
103
- [false, e.message]
104
- end
105
- print_usage "Opaque#finish_stream ends #{@filepath}"
106
- result
107
- end
108
-
109
- def finish(success, reason)
110
- print_usage "Opaque#finish success=#{success} #{@filepath}"
111
-
112
- @f.close
113
- @f = nil
114
- @finish_cb.call(success, reason)
115
- @finish_cb = nil
116
- end
117
- end
118
-
119
- def save_screenshot(c, domain, i)
120
- stream = c.stream(Libvirt::Stream::NONBLOCK)
121
-
122
- opaque_cb = proc do |success|
123
- puts "Stream #{i} complete success=#{success}"
124
- print_usage "after stream #{i} complete stream=#{STREAMS["stream#{i}"]}"
125
- run_gc("Stream #{i} complete before remove stream")
126
- print_usage "after stream #{i} complete and GC.start"
127
- STREAMS["stream#{i}"] = nil
128
- run_gc("Stream #{i} complete before remove stream")
129
- print_usage "after stream #{i} delete and GC.start"
130
- end
131
-
132
- opaque = ScreenshotOpaque.new("tmp/screenshots_test#{i}.pnm", opaque_cb)
133
-
134
- STREAMS["stream#{i}"] = stream
135
-
136
- print_usage "test_screenshot_mem #{i} before stream start"
137
- domain.screenshot(stream, 0)
138
- stream.event_add_callback(
139
- Libvirt::Stream::EVENT_READABLE,
140
- opaque,
141
- &ScreenshotOpaque::CALLBACK
142
- )
143
- run_gc("Stream #{i} after add event")
144
- end
145
-
146
- Async do
147
- ASYNC_REACTOR = Async::Task.current.reactor
148
-
149
- puts "Lib version #{Libvirt.lib_version}"
150
- puts "Gem version #{Libvirt::VERSION}"
151
-
152
- IMPL.start
153
-
154
- c = Libvirt::Connection.new('qemu+tcp://localhost:16510/system')
155
- c.open
156
- res = c.set_keep_alive(2, 1)
157
- Libvirt.logger.info { "set_keep_alive #{res}" }
158
- CONNS.push(c)
159
-
160
- domain = c.list_all_domains.first
161
- DOMS.push(domain)
162
-
163
- print_usage 'First generation'
164
- 5.times do |i|
165
- save_screenshot(c, domain, 100 + i)
166
- end
167
-
168
- ASYNC_REACTOR.after(15) do
169
- Async::Task.new(ASYNC_REACTOR, nil) do
170
- print_usage 'Second generation'
171
-
172
- con = CONNS.first
173
- dom = DOMS.first
174
- 5.times do |i|
175
- save_screenshot(con, dom, 200 + i)
176
- end
177
- end.run
178
- end
179
-
180
- ASYNC_REACTOR.after(30) do
181
- Async::Task.new(ASYNC_REACTOR, nil) do
182
- print_usage 'Third generation'
183
-
184
- con = CONNS.first
185
- dom = DOMS.first
186
- 5.times do |i|
187
- save_screenshot(con, dom, 300 + i)
188
- end
189
- end.run
190
- end
191
-
192
- ASYNC_REACTOR.every(5) do
193
- Async::Task.new(ASYNC_REACTOR, nil) do
194
- run_gc 'PERIODIC'
195
- end.run
196
- end
197
- end
@@ -1,52 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require 'bundler/setup'
5
- require 'libvirt'
6
- require 'logger'
7
- require 'active_support/all'
8
- require 'async'
9
-
10
- require_relative 'support/libvirt_async'
11
- require_relative 'support/log_formatter'
12
-
13
- require 'libvirt/xml'
14
-
15
- Libvirt.logger = Logger.new(STDOUT, formatter: LogFormatter.new)
16
- Libvirt.logger.level = ENV['DEBUG'] ? :debug : :info
17
-
18
- IMPL = LibvirtAsync::Implementations.new
19
-
20
- Async do
21
- ASYNC_REACTOR = Async::Task.current.reactor
22
-
23
- puts "Lib version #{Libvirt.lib_version}"
24
- puts "Gem version #{Libvirt::VERSION}"
25
-
26
- IMPL.start
27
-
28
- conn = Libvirt::Connection.new('qemu+tcp://localhost:16510/system')
29
- conn.open
30
-
31
- puts "Connection version #{conn.version.inspect}"
32
- puts "Connection lib_version #{conn.lib_version.inspect}"
33
- puts "Connection hostname #{conn.hostname.inspect}"
34
-
35
- pools = conn.list_all_storage_pools
36
- puts "Connection storage pools qty #{pools.size}"
37
-
38
- pools.each.with_index do |pool, i|
39
- puts "Storage pool #{i} info", pool.info.to_h
40
- puts "Storage pool #{i} xml", Libvirt::Xml::StoragePool.load(pool.xml_desc).to_h
41
- end
42
-
43
- pools.each.with_index do |pool, i|
44
- volumes = pool.list_all_volumes
45
- puts "Storage pool #{i} volumes qty #{volumes.size}"
46
-
47
- volumes.each.with_index do |vol, j|
48
- puts "Storage pool #{i} volume #{j} info", vol.info.to_h
49
- puts "Storage pool #{i} volume #{j} xml", Libvirt::Xml::StorageVolume.load(vol.xml_desc).to_h
50
- end
51
- end
52
- end