-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqemu.rb
90 lines (76 loc) · 1.93 KB
/
qemu.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
require 'choice'
require './Virtmod.rb'
require 'libvirt'
Choice.options do
header ''
header 'Specific options:'
option :action do
short '-a'
long '--action=ACTION'
desc 'Specify the action -- create or delete or status'
valid %w[create delete status]
end
option :domain do
short '-d'
long '--domain=DOMAIN'
desc 'Specify the Domain/Name for the VM'
end
option :arch do
short '-r'
long '--arch=ARCH'
desc 'Specify the Domain/Name for the VM'
valid %w[i686 x86_64]
end
option :help do
long '--help'
desc 'shows this message'
end
end
if Choice.choices[:action] == 'status'
conn = Libvirt::open('qemu:///system')
#conn.list_defined_domains.each do |domname|
# puts " Domain #{domname}"
conn.list_domains.each do |domid|
dom = conn.lookup_domain_by_id(domid)
puts " ##### Active Domains #####"
puts " Domain #{dom.name} Running"
end
conn.list_defined_domains.each do |domname|
puts " ##### InActive Domains #####"
puts " Domain #{domname} Stopped"
end
end
if Choice.choices[:action] == 'create'
name = Choice.choices[:domain]
arch = Choice.choices[:arch]
dom_xml = Virtmod.vm_create_xml(name,arch)
#puts dump_xml
puts "Conncting to LibVirt"
conn = Libvirt::open('qemu:///system')
dom = conn.define_domain_xml(dom_xml)
dom.create
conn.close
end
if Choice.choices[:action] == 'delete'
name = Choice.choices[:domain]
conn = Libvirt::open('qemu:///system')
vms = Hash.new
conn.list_domains.each do |domid|
dom = conn.lookup_domain_by_id(domid)
vms[dom.name] = dom.xml_desc
end
if vms["#{name}"]
puts "VM is running, so stopping . . . "
del_xml = vms["#{name}"]
dom = conn.define_domain_xml(del_xml)
dom.destroy
dom.undefine
else
puts "VM is switched off, so deleting . . . "
conn.list_domains.each do |domname|
dom = conn.lookup_domain_by_name(name)
vms[dom.name] = dom.xml_desc
end
dom.undefine
end
end