-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlibvirt_storage.rb
158 lines (136 loc) · 5.4 KB
/
libvirt_storage.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
require 'chef/knife'
class Chef
class Knife
class LibvirtStoragePoolList < Knife
deps do
require 'libvirt'
end
banner "knife libvirt storage pool list (options)"
#TODO re-enable arguments with defaults from knife.rb
# option :host,
# :short => "-h HOST",
# :long => "--host HOST",
# :description => "The Libvirt host",
# :proc => Proc.new { |h| Chef::Config[:knife][:libvirt_host] = h}
#
# option :libvirt_tls_path,
# :short => "-t PATH",
# :long => "--tls-path PATH",
# :description => "The path to your libvirt TLS keys",
# :proc => Proc.new { |t| Chef::Config[:knife][:libvirt_tls_path] = t},
# :default => Chef::Config[:knife][:libvirt_tls_path]
def run
host = Chef::Config[:knife][:libvirt_host]
uri = "qemu+tls://#{host}/system?pkipath=#{Chef::Config[:knife][:libvirt_tls_path]}/#{host}"
connection = Libvirt::open(uri)
puts connection.list_storage_pools
end
end
class LibvirtStoragePoolShow < Knife
deps do
require 'chef/knife/bootstrap'
Chef::Knife::Bootstrap.load_deps
require 'libvirt'
require 'nokogiri'
end
banner "knife libvirt storage pool show POOL (options)"
def to_gb(kb)
(kb/1073741824.0).round(2)
end
def run
host = "qemu+tls://#{Chef::Config[:knife][:libvirt_host]}/system?pkipath=#{Chef::Config[:knife][:libvirt_tls_path]}/#{Chef::Config[:knife][:libvirt_host]}"
connection = Libvirt::open(host)
@name_args.each do |pool_name|
pool = connection.lookup_storage_pool_by_name(pool_name)
puts "Name: #{pool.name}"
xml = Nokogiri::XML(pool.xml_desc)
puts "Path: #{xml.xpath('//target/path').inner_text}"
puts "UUID: #{pool.uuid}"
puts "---------------"
puts "Volumes: #{pool.num_of_volumes}"
puts "Autostart: #{pool.autostart?}"
puts "Persistent: #{pool.persistent?}"
puts "Capacity: #{to_gb(pool.info.capacity)} GB"
puts "Allocated: #{to_gb(pool.info.allocation)} GB"
puts "Available: #{to_gb(pool.info.available)} GB"
puts "======================="
puts
end
end
end
class LibvirtStorageVolumeList < Knife
deps do
require 'chef/knife/bootstrap'
require 'libvirt'
Chef::Knife::Bootstrap.load_deps
end
banner "knife libvirt storage volume list (options)"
def to_mb(kb)
(kb/1024.0).round(2)
end
def run
host = "qemu+tls://#{Chef::Config[:knife][:libvirt_host]}/system?pkipath=#{Chef::Config[:knife][:libvirt_tls_path]}/#{Chef::Config[:knife][:libvirt_host]}"
connection = Libvirt::open(host)
connection.list_storage_pools.each do |pool_name|
puts "#{pool_name}:"
puts "-----------------------"
storage_pool = connection.lookup_storage_pool_by_name(pool_name)
storage_pool.list_volumes.each do |volume|
vol = storage_pool.lookup_volume_by_name(volume)
puts "#{volume} #{to_mb(vol.info.allocation)} MB/#{to_mb(vol.info.capacity)} MB"
end
puts "======================="
end
end
class LibvirtStorageVolumeShow < Knife
deps do
require 'chef/knife/bootstrap'
Chef::Knife::Bootstrap.load_deps
require 'libvirt'
end
banner "knife libvirt storage volume show POOL VOLUME (options)"
def to_mb(kb)
(kb/1024.0).round(2)
end
def run
host = "qemu+tls://#{Chef::Config[:knife][:libvirt_host]}/system?pkipath=#{Chef::Config[:knife][:libvirt_tls_path]}/#{Chef::Config[:knife][:libvirt_host]}"
connection = Libvirt::open(host)
@name_args.each_slice(2) do |pool_name, volume_name|
volume = connection.lookup_storage_pool_by_name(pool_name).lookup_volume_by_name(volume_name)
puts "Name: #{volume.name}"
puts "Pool: #{volume.pool.name}"
puts "---------------"
puts "Capacity: #{to_mb(volume.info.capacity)} MB"
puts "Allocated: #{to_mb(volume.info.allocation)} MB"
puts "Path: #{volume.path}"
puts "Key: #{volume.key}"
puts "======================="
puts
end
end
class LibvirtStorageMediaList < Knife
deps do
require 'libvirt'
end
banner "knife libvirt storage media list (options)"
def bytes_to_mb(bytes)
(bytes/1048576.0).round(2)
end
def run
host = Chef::Config[:knife][:libvirt_host]
uri = "qemu+tls://#{host}/system?pkipath=#{Chef::Config[:knife][:libvirt_tls_path]}/#{host}"
connection = Libvirt::open(uri)
storage_pool = connection.lookup_storage_pool_by_name("media")
storage_pool.refresh
storage_pool.list_volumes.each do |volume|
vol = storage_pool.lookup_volume_by_name(volume)
puts "#{volume} #{bytes_to_mb(vol.info.allocation)} MB"
puts "#{vol.path}"
puts "#{File.dirname vol.path}"
end
end
end
end
end
end
end