-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvmgenerator.rb
executable file
·158 lines (137 loc) · 4.37 KB
/
vmgenerator.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
#!/usr/bin/env ruby
# extend string class to add a numeric check
class String
def is_number?
true if Float(self) rescue false
end
end
# initialize things to have baseline empty vars
vm_Name = ''
vm_RamUnit = ''
vm_Memory = ''
vm_Bridge = ''
vm_Cpus = ''
# call out for uuid generation
uuid = `uuidgen`
# create a random mac from hex digits, then split with colons
wholemac = (1..6).collect { "%02x" % (rand 255) }.join(":")
# loop to let this question happen multiple times, if answers make no sense
while vm_Name.empty? do
over_answer = ''
out_filename = ''
puts "Name for you new VM:"
vm_Name = gets.chomp
out_filename = "#{vm_Name}.xml"
# checking for the existence of filenames that match the chosen name
if !Dir.glob(out_filename).empty?
puts "#{vm_Name}.xml is already taken. Overwrite [n]?"
over_answer = gets.chomp.downcase[0]
#puts "over_answer: #{over_answer}"
if !(over_answer == 'y')
vm_Name = ''
end
#puts "over_answer 2nd: #{over_answer}"
end
#puts "name: #{vm_Name}"
end
while !(vm_RamUnit =~ /G|K|M/) do
puts "RAM unit (K/M/G) [G]:"
vm_RamUnit = gets.chomp.upcase[0]
vm_RamUnit = 'G' if vm_RamUnit.nil? || vm_RamUnit.empty?
puts "#{vm_RamUnit}? Try again (K/M/G)" unless vm_RamUnit =~ /G|K|M/
end
while !vm_Memory.is_number? do
puts "Memory capacity [1]:"
vm_Memory = gets.chomp
vm_Memory = '1' if vm_Memory.empty?
# TODO Calculate how many real VMs there are on the box,
# and/or how many non-oversubscribed are left
puts "#{vm_Memory} RAM? Try again" unless vm_Memory.is_number?
end
while !vm_Cpus.is_number? do
puts "vCPUs [1]:"
vm_Cpus = gets.chomp
vm_Cpus = '1' if vm_Cpus.empty?
puts "#{vm_Cpus} cpus? Try again" unless vm_Cpus.is_number?
end
while vm_Bridge.empty? do
puts "Network Bridge [br0]:"
vm_Bridge = gets.chomp
vm_Bridge = 'br0' if vm_Bridge.empty?
end
puts "name: #{vm_Name}"
puts "unit: #{vm_RamUnit}"
puts "memory: #{vm_Memory}"
puts "cpus: #{vm_Cpus}"
puts "bridge: #{vm_Bridge}"
puts "mac: #{wholemac}"
puts "uuid: #{uuid}"
puts "filename: #{out_filename}"
file_contents = "<domain type='kvm'>
<name>#{vm_Name}</name>
<uuid>#{uuid}</uuid>
<memory unit='#{vm_RamUnit}iB'>#{vm_Memory}</memory>
<currentMemory unit='#{vm_RamUnit}iB'>#{vm_Memory}</currentMemory>
<vcpu placement='static'>#{vm_Cpus}</vcpu>
<os>
<type arch='x86_64'>hvm</type>
<boot dev='hd'/>
</os>
<features>
<acpi/>
</features>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<source file='/vm/iso/centos7-min.iso'/>
<target dev='hdc' bus='ide'/>
<readonly/>
<address type='drive' controller='0' bus='1' unit='0'/>
</disk>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/vm/#{vm_Name}.qcow2'/>
<target dev='hda' bus='ide'/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
<controller type='usb' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
</controller>
<controller type='pci' index='0' model='pci-root'/>
<controller type='ide' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<interface type='bridge'>
<mac address='#{wholemac}'/>
<source bridge='#{vm_Bridge}'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<serial type='pty'>
<target port='0'/>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'>
<listen type='address' address='0.0.0.0'/>
</graphics>
<video>
<model type='cirrus' vram='9216' heads='1'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</memballoon>
</devices>
</domain> "
open(out_filename, 'w') { |f|
f.puts file_contents
}