-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmakevm
More file actions
executable file
·349 lines (273 loc) · 11.8 KB
/
makevm
File metadata and controls
executable file
·349 lines (273 loc) · 11.8 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/env python3
# Creates and configures a new vm
#
# Dependencies (Debian packages):
# - python3-paramiko
# - nmap
#
import argparse
import os.path
import random
import re
import shlex
import shutil
import socket
import subprocess
import sys
import textwrap
import time
from ipaddress import ip_address
from ipaddress import IPv4Address
import paramiko
from ocflib.infra.net import ipv4_to_ipv6
from ocflib.infra.net import is_ocf_ip
from ocflib.infra.net import OCF_GATEWAY_V4
from ocflib.infra.net import OCF_GATEWAY_V6
from ocflib.infra.net import OCF_SUBNET_V4
from ocflib.misc.shell import bold
from ocflib.misc.shell import green
from ocflib.misc.shell import yellow
MAC_KVM_PREFIX = (0x52, 0x54, 0x00)
MAC_BYTES = 6
def exec_cmd(*args):
"""Executes the given shell command with some pretty colors. Script
exits if the command exits with a non-zero status code."""
print(bold(green('$ ' + ' '.join(map(shlex.quote, args)))))
subprocess.check_call(args)
def create_disk(vg, name, size):
"""Creates a logical volume."""
path = '/dev/{}/{}'.format(vg, name)
if os.path.exists(path):
print("Can't create new lv, {} already exists.".format(path))
print("Use 'lvremove {}' if you want to destroy it.".format(path))
sys.exit(2)
exec_cmd('lvcreate', '-L', str(size) + 'GB', '--name', name, vg)
def generate_mac_addr():
def random_byte(idx):
if idx < len(MAC_KVM_PREFIX):
return MAC_KVM_PREFIX[idx]
return random.randint(0, 255)
return ':'.join('{:02x}'.format(random_byte(i)) for i in range(MAC_BYTES))
def gen_ceph_disk_param(ceph_pool, size):
"""Generates the --disk argument to virt-install for a Ceph VM in the given pool"""
return 'pool={},size={}'.format(ceph_pool, size)
def gen_lvm_disk_param(name, vg):
"""Generates the --disk argument to virt-install for an LVM VM in the given VG"""
return '/dev/{}/{},cache=none'.format(vg, name)
def create_vm(name, memory, vcpus, disk_arg, os_type, os_variant, network, mac, skip_config):
"""Creates a new VM."""
# try to print info about the domain to see if it already exists
# we expect this command to fail
try:
subprocess.check_call(['virsh', 'dominfo', name], stderr=subprocess.DEVNULL)
print("Can't create new vm, domain {} already exists.".format(name))
sys.exit(2)
except subprocess.CalledProcessError:
pass # all good
if not skip_config:
print('If virt-viewer opens up', bold(yellow('please make sure to close it')),
'when the install is complete, or else the VM configuration cannot continue.')
exec_cmd('virt-install', '-r', str(memory), '--pxe', '--os-type', os_type,
'--os-variant', os_variant, '--disk', disk_arg, '--vcpus', str(vcpus),
'--network', network + ',mac=' + mac, '--graphics', 'vnc', '--serial',
'pty', '--name', name, '--wait', '0' if skip_config else '-1')
def get_running_vms():
output = subprocess.check_output(['virsh', 'list']).decode('utf-8')
return [line.split()[1] for line in output.splitlines()[2:-1]]
def get_ip(mac):
"""Get IP address for the given MAC address. This needs some work."""
while True:
try:
subprocess.check_call(['sudo', '-u', 'nobody', 'nmap', '-sn', str(OCF_SUBNET_V4)],
stdout=subprocess.DEVNULL)
cmd = "arp -an | grep '{}'".format(mac)
result = subprocess.check_output(cmd, shell=True).decode('utf-8').strip()
return result.split(' ')[1][1:-1]
except subprocess.CalledProcessError:
time.sleep(1)
def get_ssh_connection(mac, user, key_file):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# wait for the VM to be live
while True:
try:
print('Searching for autoconfigured IP for VM...')
ip = get_ip(mac)
print('Trying IP: {}'.format(ip))
client.connect(ip, username=user, key_filename=key_file, timeout=1)
return client
except (socket.timeout, socket.error):
pass
def get_net_iface(ssh):
"""Return the first available ethernet interface on the newly created VM.
In current versions of systemd/udev, they are supposed to be named starting
with the letters 'en', e.g. 'eno1', 'enp4s0'.
"""
with ssh.open_sftp() as sftp:
ifaces = sftp.listdir('/sys/class/net/')
return next(iface for iface in ifaces if iface.startswith('en'))
def configure_network(ssh, ipv4):
"""Set up the correct IPv4 and IPv6 static addresses."""
ipv6 = ipv4_to_ipv6(ipv4)
interface = get_net_iface(ssh)
content = textwrap.dedent(
"""\
auto lo
iface lo inet loopback
auto {iface}
iface {iface} inet static
\taddress {ipv4}
\tnetmask 255.255.255.0
\tgateway {gateway_v4}
iface {iface} inet6 static
\taddress {ipv6}
\tnetmask 64
\tgateway {gateway_v6}
""".format(iface=interface, ipv4=ipv4, gateway_v4=OCF_GATEWAY_V4,
ipv6=ipv6, gateway_v6=OCF_GATEWAY_V6))
stdin, _, _ = ssh.exec_command('cat > /etc/network/interfaces')
stdin.write(content)
stdin.close()
stdin.channel.shutdown(2)
def wait_drop_into_shell(ip, user, key_file):
# wait for the VM to be live
print('Waiting for VM to finish rebooting...', end='', flush=True)
while True:
try:
socket.create_connection((ip, 'ssh'), timeout=1).close()
break
except (socket.timeout, socket.error):
print('.', end='', flush=True)
time.sleep(1)
print()
print('You should now run puppet. You will now be dropped into a shell.')
exec_cmd('ssh', '-o', 'UserKnownHostsFile=/dev/null', '-o', 'GlobalKnownHostsFile=/dev/null',
'-o', 'StrictHostKeyChecking=no',
'-i', key_file, '-l', user, ip)
def confirm(prompt='Continue?', default=False):
choices = 'Yn' if default else 'yN'
response = input('{} [{}] '.format(prompt, choices)).lower()
if response in ('y', 'yes'):
return True
if response in ('n', 'no'):
return False
return default
def _main(args):
parser = argparse.ArgumentParser(
description='Create and configure new VMs using libvirt CLI tools',
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument('-m', '--memory', type=int, default=4096,
help='amount of memory (in MB)')
parser.add_argument('-c', '--vcpus', type=int, default=2,
help='number of vCPUs')
parser.add_argument('-s', '--storage', type=int, default=30,
help='amount of disk storage (in GB)')
parser.add_argument('-V', '--vg', type=str, default='vg',
help='LVM volume group to use for storage')
parser.add_argument('-C', '--ceph', action='store_true', default=False,
help='use Ceph storage instead of LVM')
parser.add_argument('-P', '--ceph-pool', type=str, default='vm',
help='Ceph Pool to use for storage')
parser.add_argument('--os-type', type=str, default='linux',
help='os type')
parser.add_argument('--os-variant', type=str, default='debian10')
parser.add_argument('--network', type=str, default='bridge=br0',
help='network configuration')
parser.add_argument('--preseed-user', type=str, default='root',
help='user created in preseed')
parser.add_argument('--preseed-ssh-key', type=str, default='/opt/share/kvm/makevm-ssh-key',
help='SSH private key for user created in preseed')
parser.add_argument('--skip-config', action='store_true', default=False,
help="don't configure the new VM")
parser.add_argument('hostname', type=str,
help='hostname for the new VM')
parser.add_argument('ip', type=str,
help='static IPv4 address for the new VM')
args = parser.parse_args()
# sanity checks
if not 512 <= args.memory <= 16384:
print('Warning: You want {} *MB* of memory.'.format(args.memory))
if not confirm():
print('Cancelled.')
sys.exit(2)
if not 5 <= args.storage <= 50:
print('Warning: You want {} *GB* of storage.'.format(args.storage))
if not confirm():
print('Cancelled.')
sys.exit(2)
if not re.match(r'^[a-z\-0-9]{1,20}$', args.hostname):
print("Warning: Your hostname is '{}'.".format(args.hostname))
print('You probably should NOT include the domain name.')
if not confirm():
print('Cancelled.')
sys.exit(2)
if args.ceph and not shutil.which('ceph'):
print("Warning: You are trying to use Ceph but I couldn't find the `ceph` executable.")
if not confirm():
print('Cancelled.')
sys.exit(2)
if args.ceph and args.vg != 'vg':
print('Warning: You have Ceph backing storage, but specified a non-default VG.')
if not confirm():
print('Cancelled.')
sys.exit(2)
if not args.ceph and args.ceph_pool != 'vm':
print('Warning: You have LVM backing storage, but specified a Ceph pool.')
if not confirm():
print('Cancelled.')
sys.exit(2)
# Check to make sure the IP address provided is an OCF IPv4 address
ip_addr = ip_address(args.ip)
if not is_ocf_ip(ip_addr) or not isinstance(ip_addr, IPv4Address):
print("Warning: Your IP is '{}'.".format(args.ip))
print('It should probably be in {}.'.format(OCF_SUBNET_V4))
if not confirm():
print('Cancelled.')
sys.exit(2)
if os.geteuid() != 0:
print('You are not root.')
sys.exit(1)
print('Creating new VM with the following details:')
print('\tHostname: {}'.format(args.hostname))
print('\tIP Address: {}'.format(args.ip))
print('\tOS Type: {}'.format(args.os_type))
print('\tOS Variant: {}'.format(args.os_variant))
print('\tDisk Space: {} GB'.format(args.storage))
if args.ceph:
print('\tBacking Storage: Ceph')
print('\tCeph Pool: {}'.format(args.ceph_pool))
else:
print('\tBacking Storage: LVM')
print('\tVolume Group: {}'.format(args.vg))
print('\tMemory: {} MB'.format(args.memory))
print('\tvCPUs: {}'.format(args.vcpus))
print('\tNetwork: {}'.format(args.network))
if not confirm():
print('Cancelled.')
sys.exit(2)
mac = generate_mac_addr()
disk_param = None
if args.ceph:
disk_param = gen_ceph_disk_param(args.ceph_pool, args.storage)
else:
create_disk(args.vg, args.hostname, args.storage)
disk_param = gen_lvm_disk_param(args.hostname, args.vg)
create_vm(args.hostname, args.memory, args.vcpus, disk_param, args.os_type,
args.os_variant, args.network, mac, args.skip_config)
if args.skip_config:
print('VM created, skipping configuration. Have fun!')
sys.exit(0)
print('Connecting to VM via SSH...')
client = get_ssh_connection(mac, args.preseed_user, args.preseed_ssh_key)
print('Setting hostname to {}...'.format(args.hostname))
client.exec_command('echo {} > /etc/hostname'.format(args.hostname))
client.exec_command('hostname -F /etc/hostname')
print('Configuring static IP {}...'.format(args.ip))
configure_network(client, ip_addr)
print('Restarting VM...')
client.exec_command('shutdown -r now')
print('{} is now minimally configured.'.format(args.hostname))
wait_drop_into_shell(args.ip, args.preseed_user, args.preseed_ssh_key)
if __name__ == '__main__':
_main(sys.argv[1:])