forked from cobbler/cobbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxencreate.py
executable file
·195 lines (155 loc) · 6.38 KB
/
xencreate.py
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
"""
Virtualization installation functions.
Currently somewhat Xen/paravirt specific, will evolve later.
Copyright 2006-2008 Red Hat, Inc and Others.
Michael DeHaan <michael.dehaan AT gmail>
Original version based on virtguest-install
Jeremy Katz <[email protected]>
Option handling added by Andrew Puch <[email protected]>
Simplified for use as library by koan, Michael DeHaan <michael.dehaan AT gmail>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
"""
import os, sys, time, stat
import tempfile
import random
import exceptions
import errno
import re
import virtinst
import app as koan
try:
import virtinst.DistroInstaller as DistroManager
except:
# older virtinst, this is probably ok
# but we know we can't do Xen fullvirt installs
pass
import traceback
def random_mac():
"""
from xend/server/netif.py
Generate a random MAC address.
Uses OUI 00-16-3E, allocated to
Xensource, Inc. Last 3 fields are random.
return: MAC address string
"""
mac = [ 0x00, 0x16, 0x3e,
random.randint(0x00, 0x7f),
random.randint(0x00, 0xff),
random.randint(0x00, 0xff) ]
return ':'.join(map(lambda x: "%02x" % x, mac))
def start_install(name=None,
ram=None,
disks=None,
uuid=None,
extra=None,
vcpus=None,
profile_data=None,
arch=None,
no_gfx=False,
fullvirt=False,
bridge=None,
virt_type=None,
virt_auto_boot=False,
qemu_driver_type=None,
qemu_net_type=None):
if profile_data.has_key("file"):
raise koan.InfoException("Xen does not work with --image yet")
if fullvirt:
# FIXME: add error handling here to explain when it's not supported
guest = virtinst.FullVirtGuest(installer=virtinst.PXEInstaller())
else:
guest = virtinst.ParaVirtGuest()
extra = extra.replace("&","&")
if not fullvirt:
guest.set_boot((profile_data["kernel_local"], profile_data["initrd_local"]))
# fullvirt OS's will get this from the PXE config (managed by Cobbler)
guest.extraargs = extra
else:
print "- fullvirt mode"
if profile_data.has_key("breed"):
breed = profile_data["breed"]
if breed != "other" and breed != "":
if breed in [ "debian", "suse", "redhat" ]:
guest.set_os_type("linux")
elif breed in [ "windows" ]:
guest.set_os_type("windows")
else:
guest.set_os_type("unix")
if profile_data.has_key("os_version"):
# FIXME: when os_version is not defined and it's linux, do we use generic24/generic26 ?
version = profile_data["os_version"]
if version != "other" and version != "":
try:
guest.set_os_variant(version)
except:
print "- virtinst library does not understand variant %s, treating as generic" % version
pass
guest.set_name(name)
guest.set_memory(ram)
guest.set_vcpus(vcpus)
guest.set_autostart(virt_auto_boot)
if not no_gfx:
guest.set_graphics("vnc")
else:
guest.set_graphics(False)
if uuid is not None:
guest.set_uuid(uuid)
for d in disks:
if d[1] != 0 or d[0].startswith("/dev"):
guest.disks.append(virtinst.XenDisk(d[0], size=d[1]))
else:
raise koan.InfoException("this virtualization type does not work without a disk image, set virt-size in Cobbler to non-zero")
counter = 0
if profile_data.has_key("interfaces"):
interfaces = profile_data["interfaces"].keys()
interfaces.sort()
counter = -1
vlanpattern = re.compile("[a-zA-Z0-9]+\.[0-9]+")
for iname in interfaces:
counter = counter + 1
intf = profile_data["interfaces"][iname]
if intf["interface_type"] in ("master","bond","bridge") or vlanpattern.match(iname) or iname.find(":") != -1:
continue
mac = intf["mac_address"]
if mac == "":
mac = random_mac()
if not bridge:
profile_bridge = profile_data["virt_bridge"]
intf_bridge = intf["virt_bridge"]
if intf_bridge == "":
if profile_bridge == "":
raise koan.InfoException("virt-bridge setting is not defined in cobbler")
intf_bridge = profile_bridge
else:
if bridge.find(",") == -1:
intf_bridge = bridge
else:
bridges = bridge.split(",")
intf_bridge = bridges[counter]
nic_obj = virtinst.XenNetworkInterface(macaddr=mac, bridge=intf_bridge)
guest.nics.append(nic_obj)
counter = counter + 1
else:
# for --profile you just get one NIC, go define a system if you want more.
# FIXME: can mac still be sent on command line in this case?
if bridge is None:
profile_bridge = profile_data["virt_bridge"]
else:
profile_bridge = bridge
if profile_bridge == "":
raise koan.InfoException("virt-bridge setting is not defined in cobbler")
nic_obj = virtinst.XenNetworkInterface(macaddr=random_mac(), bridge=profile_bridge)
guest.nics.append(nic_obj)
guest.start_install()
return "use virt-manager or reconnect with virsh console %s" % name