340a7ab6ec6458dcb8baa73db78079fe20e1b6a1
[apex.git] / lib / configure-vm
1 #!/usr/bin/env python
2
3 import argparse
4 import math
5 import os
6 import random
7
8 import libvirt
9
10 templatedir = os.getenv('LIB', '/var/opt/opnfv/lib') + '/installer/'
11
12 MAX_NUM_MACS = math.trunc(0xff/2)
13
14
15 def generate_baremetal_macs(count=1):
16     """Generate an Ethernet MAC address suitable for baremetal testing."""
17     # NOTE(dprince): We generate our own bare metal MAC address's here
18     # instead of relying on libvirt so that we can ensure the
19     # locally administered bit is set low. (The libvirt default is
20     # to set the 2nd MSB high.) This effectively allows our
21     # fake baremetal VMs to more accurately behave like real hardware
22     # and fixes issues with bridge/DHCP configurations which rely
23     # on the fact that bridges assume the MAC address of the lowest
24     # attached NIC.
25     # MACs generated for a given machine will also be in sequential
26     # order, which matches how most BM machines are laid out as well.
27     # Additionally we increment each MAC by two places.
28     macs = []
29
30     if count > MAX_NUM_MACS:
31         raise ValueError("The MAX num of MACS supported is %i." % MAX_NUM_MACS)
32
33     base_nums = [0x00,
34                random.randint(0x00, 0xff),
35                random.randint(0x00, 0xff),
36                random.randint(0x00, 0xff),
37                random.randint(0x00, 0xff)]
38     base_mac = ':'.join(map(lambda x: "%02x" % x, base_nums))
39
40     start = random.randint(0x00, 0xff)
41     if (start + (count * 2)) > 0xff:
42         # leave room to generate macs in sequence
43         start = 0xff - count * 2
44     for num in range(0, count*2, 2):
45         mac = start + num
46         macs.append(base_mac + ":" + ("%02x" % mac))
47     return macs
48
49 def main():
50     parser = argparse.ArgumentParser(
51         description="Configure a kvm virtual machine for the seed image.")
52     parser.add_argument('--name', default='seed',
53         help='the name to give the machine in libvirt.')
54     parser.add_argument('--image',
55         help='Use a custom image file (must be qcow2).')
56     parser.add_argument('--diskbus', default='sata',
57         help='Choose an alternate bus type for the disk')
58     parser.add_argument('--baremetal-interface', nargs='+', default=['brbm'],
59         help='The interface which bare metal nodes will be connected to.')
60     parser.add_argument('--engine', default='kvm',
61         help='The virtualization engine to use')
62     parser.add_argument('--arch', default='i686',
63         help='The architecture to use')
64     parser.add_argument('--memory', default='2097152',
65         help="Maximum memory for the VM in KB.")
66     parser.add_argument('--cpus', default='1',
67         help="CPU count for the VM.")
68     parser.add_argument('--bootdev', default='hd',
69         help="What boot device to use (hd/network).")
70     parser.add_argument('--seed', default=False, action='store_true',
71         help='Create a seed vm with two interfaces.')
72     parser.add_argument('--ovsbridge', default="",
73         help='Place the seed public interface on this ovs bridge.')
74     parser.add_argument('--libvirt-nic-driver', default='virtio',
75         help='The libvirt network driver to use')
76     parser.add_argument('--enable-serial-console', action="store_true",
77             help='Enable a serial console')
78     parser.add_argument('--direct-boot',
79             help='Enable directboot to <value>.{vmlinux & initrd}')
80     parser.add_argument('--kernel-arg', action="append", dest='kernel_args',
81             help='Kernel arguments, use multiple time for multiple args.')
82     parser.add_argument('--uri', default='qemu:///system',
83         help='The server uri with which to connect.')
84     args = parser.parse_args()
85     with file(templatedir + '/domain.xml', 'rb') as f:
86         source_template = f.read()
87     imagefile = '/var/lib/libvirt/images/seed.qcow2'
88     if args.image:
89         imagefile = args.image
90     imagefile = os.path.realpath(imagefile)
91     params = {
92         'name': args.name,
93         'imagefile': imagefile,
94         'engine': args.engine,
95         'arch': args.arch,
96         'memory': args.memory,
97         'cpus': args.cpus,
98         'bootdev': args.bootdev,
99         'network': '',
100         'enable_serial_console': '',
101         'direct_boot': '',
102         'kernel_args': '',
103         }
104     if args.image is not None:
105         params['imagefile'] = args.image
106
107     # Configure the bus type for the target disk device
108     params['diskbus'] = args.diskbus
109     nicparams = {
110         'nicdriver': args.libvirt_nic_driver,
111         'ovsbridge': args.ovsbridge,
112         }
113     if args.seed:
114         if args.ovsbridge:
115             params['network'] = """
116       <interface type='bridge'>
117         <source bridge='%(ovsbridge)s'/>
118         <virtualport type='openvswitch'/>
119         <model type='%(nicdriver)s'/>
120       </interface>""" % nicparams
121         else:
122             params['network'] = """
123       <!-- regular natted network, for access to the vm -->
124       <interface type='network'>
125         <source network='default'/>
126         <model type='%(nicdriver)s'/>
127       </interface>""" % nicparams
128
129     macs = generate_baremetal_macs(len(args.baremetal_interface))
130
131     params['bm_network'] = ""
132     for bm_interface, mac in zip(args.baremetal_interface, macs):
133         bm_interface_params = {
134             'bminterface': bm_interface,
135             'bmmacaddress': mac,
136             'nicdriver': args.libvirt_nic_driver,
137             }
138         params['bm_network'] += """
139           <!-- bridged 'bare metal' network on %(bminterface)s -->
140           <interface type='network'>
141             <mac address='%(bmmacaddress)s'/>
142             <source network='%(bminterface)s'/>
143             <model type='%(nicdriver)s'/>
144           </interface>""" % bm_interface_params
145
146     if args.enable_serial_console:
147         params['enable_serial_console'] = """
148         <serial type='pty'>
149           <target port='0'/>
150         </serial>
151         <console type='pty'>
152           <target type='serial' port='0'/>
153         </console>
154         """
155     if args.direct_boot:
156         params['direct_boot'] = """
157         <kernel>/var/lib/libvirt/images/%(direct_boot)s.vmlinuz</kernel>
158         <initrd>/var/lib/libvirt/images/%(direct_boot)s.initrd</initrd>
159         """ % { 'direct_boot': args.direct_boot }
160     if args.kernel_args:
161         params['kernel_args'] = """
162         <cmdline>%s</cmdline>
163         """ % ' '.join(args.kernel_args)
164
165     libvirt_template = source_template % params
166     conn=libvirt.open(args.uri)
167     a = conn.defineXML(libvirt_template)
168     print ("Created machine %s with UUID %s" % (args.name, a.UUIDString()))
169
170 if __name__ == '__main__':
171     main()