Support bytesPerInode on RAID (Curtis Doty <Curtis at GreenKey.net> #175288).
[pykickstart.git] / pykickstart / writer.py
bloba210111b139883c12aec2eec33e5ae5f230510ea
2 # writer.py: Kickstart file writer.
4 # Chris Lumens <clumens@redhat.com>
6 # Copyright 2005 Red Hat, Inc.
8 # This software may be freely redistributed under the terms of the GNU
9 # general public license.
11 # You should have received a copy of the GNU General Public License
12 # along with this program; if not, write to the Free Software
13 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 import string
16 from constants import *
18 class KickstartWriter:
19 def __init__(self, ksdata):
20 # All the printing handlers in the order they should be called.
21 self.handlers = [self.doPlatform,
23 self.doAuthconfig, self.doAutoPart, self.doAutoStep,
24 self.doBootloader, self.doClearPart, self.doDevice,
25 self.doDeviceProbe, self.doDisplayMode,
26 self.doDriverDisk, self.doFirewall, self.doFirstboot,
27 self.doIgnoreDisk, self.doInteractive, self.doKeyboard,
28 self.doLang, self.doMediaCheck, self.doMethod,
29 self.doMonitor, self.doNetwork, self.doReboot,
30 self.doRootPw, self.doSELinux, self.doSkipX,
31 self.doTimezone, self.doUpgrade, self.doVnc,
32 self.doXConfig, self.doZeroMbr, self.doZFCP,
34 self.doPartition, self.doLogicalVolume,
35 self.doVolumeGroup, self.doRaid,
37 self.doScripts, self.doPackages]
39 self.ksdata = ksdata
41 # Return a string representation of the ksdata suitable for writing to
42 # a file.
43 def write(self):
44 return string.join(filter (lambda str: str != None and str != "",
45 map (lambda fn: fn(), self.handlers)), "\n")
47 def doPlatform(self):
48 if self.ksdata.platform != "":
49 return "#platform=%s" % self.ksdata.platform
51 def doAuthconfig(self):
52 if self.ksdata.authconfig != "" and self.ksdata.upgrade == False:
53 return "# System authorization information\nauth %s" % self.ksdata.authconfig
55 def doAutoPart(self):
56 if self.ksdata.autopart:
57 return "autopart"
59 def doAutoStep(self):
60 if self.ksdata.autostep["autoscreenshot"]:
61 return "autostep --autoscreenshot"
63 def doBootloader(self):
64 str = "# System bootloader configuration\nbootloader"
66 if self.ksdata.bootloader["appendLine"] != "":
67 str = str + " --append=\"%s\"" % self.ksdata.bootloader["appendLine"]
68 if self.ksdata.bootloader["location"]:
69 str = str + " --location=%s" % self.ksdata.bootloader["location"]
70 if self.ksdata.bootloader["forceLBA"] == True:
71 str = str + " --lba32"
72 if self.ksdata.bootloader["password"] != "":
73 str = str + " --password=%s" % self.ksdata.bootloader["password"]
74 if self.ksdata.bootloader["md5pass"] != "":
75 str = str + " --md5pass=%s" % self.ksdata.bootloader["md5pass"]
76 if self.ksdata.bootloader["upgrade"] == True:
77 str = str + " --upgrade"
78 if len(self.ksdata.bootloader["driveorder"]) > 0:
79 str = str + " --driveorder=%s" % \
80 string.join(self.ksdata.bootloader["driveorder"], ",")
82 if str != "bootloader":
83 return str
85 def doClearPart(self):
86 if self.ksdata.clearpart["type"] == CLEARPART_TYPE_NONE:
87 clearstr = "--none"
88 elif self.ksdata.clearpart["type"] == CLEARPART_TYPE_LINUX:
89 clearstr = "--linux"
90 elif self.ksdata.clearpart["type"] == CLEARPART_TYPE_ALL:
91 clearstr = "--all"
92 else:
93 clearstr = ""
95 if self.ksdata.clearpart["initAll"] == True:
96 initstr = "--initlabel"
97 else:
98 initstr = ""
100 if len(self.ksdata.clearpart["drives"]) > 0:
101 drivestr = "--drives=" + \
102 string.join (self.ksdata.clearpart["drives"], ",")
103 else:
104 drivestr = ""
106 return "# Partition clearing information\nclearpart %s %s %s" % (clearstr, initstr, drivestr)
108 def doDevice(self):
109 if self.ksdata.device != "":
110 return "device %s" % self.ksdata.device
112 def doDeviceProbe(self):
113 if self.ksdata.deviceprobe != "":
114 return "deviceprobe %s" % self.ksdata.deviceprobe
116 def doDisplayMode(self):
117 if self.ksdata.displayMode == DISPLAY_MODE_CMDLINE:
118 return "cmdline"
119 elif self.ksdata.displayMode == DISPLAY_MODE_GRAPHICAL:
120 return "# Use graphical install\ngraphical"
121 elif self.ksdata.displayMode == DISPLAY_MODE_TEXT:
122 return "# Use text mode install\ntext"
124 def doDriverDisk(self):
125 if self.ksdata.driverdisk != "":
126 return "driverdisk %s" % self.ksdata.driverdisk
128 def doFirewall(self):
129 if self.ksdata.upgrade == True:
130 return
132 extra = []
133 filteredPorts = []
135 if self.ksdata.firewall["enabled"]:
136 # It's possible we have words in the ports list instead of
137 # port:proto (s-c-kickstart may do this). So, filter those
138 # out into their own list leaving what we expect.
139 for port in self.ksdata.firewall["ports"]:
140 if port == "ssh":
141 extra.append("--ssh")
142 elif port == "telnet":
143 extra.append("--telnet")
144 elif port == "smtp":
145 extra.append("--smtp")
146 elif port == "http":
147 extra.append("--http")
148 elif port == "ftp":
149 extra.append("--ftp")
150 else:
151 filteredPorts.append(port)
153 # All the port:proto strings go into a comma-separated list.
154 portstr = string.join (filteredPorts, ",")
155 if len(portstr) > 0:
156 portstr = "--ports=" + portstr
157 else:
158 portstr = ""
160 extrastr = string.join (extra, " ")
162 truststr = string.join (self.ksdata.firewall["trusts"], ",")
163 if len(truststr) > 0:
164 truststr = "--trust=" + truststr
166 # The output port list consists only of port:proto for
167 # everything that we don't recognize, and special options for
168 # those that we do.
169 return "# Firewall configuration\nfirewall --enabled %s %s %s" % (extrastr, portstr, truststr)
170 else:
171 return "# Firewall configuration\nfirewall --disabled"
173 def doFirstboot(self):
174 str = "# Run the Setup Agent on first boot\n"
176 if self.ksdata.firstboot == FIRSTBOOT_SKIP:
177 return str + "firstboot --disable"
178 elif self.ksdata.firstboot == FIRSTBOOT_DEFAULT:
179 return str + "firstboot --enable"
180 elif self.ksdata.firstboot == FIRSTBOOT_RECONFIG:
181 return str + "firstboot --reconfig"
183 def doIgnoreDisk(self):
184 if len(self.ksdata.ignoredisk) > 0:
185 str = string.join (self.ksdata.ignoredisk, ",")
186 return "ignoredisk --drives= %s" % str
188 def doInteractive(self):
189 if self.ksdata.interactive == True:
190 return "# Use interactive kickstart installation method\ninteractive"
192 def doKeyboard(self):
193 if self.ksdata.keyboard != "":
194 return "# System keyboard\nkeyboard %s" % self.ksdata.keyboard
196 def doLang(self):
197 if self.ksdata.lang != "":
198 return "# System language\nlang %s" % self.ksdata.lang
200 def doLogicalVolume(self):
201 if self.ksdata.upgrade == True:
202 return
204 str = ""
206 for part in self.ksdata.lvList:
207 str = str + "logvol %s" % part.mountpoint
209 if part.bytesPerInode > 0:
210 str = str + " --bytes-per-inode= %d" % part.bytesPerInode
211 if part.fsopts != "":
212 str = str + " --fsoptions=\"%s\"" % part.fsopts
213 if part.fstype != "":
214 str = str + " --fstype=\"%s\"" % part.fstype
215 if part.grow == True:
216 str = str + " --grow"
217 if part.maxSizeMB > 0:
218 str = str + " --maxsize=%d" % part.maxSizeMB
219 if part.format == False:
220 str = str + " --noformat"
221 if part.percent > 0:
222 str = str + " --percent=%d" % part.percent
223 if part.recommended == True:
224 str = str + " --recommended"
225 if part.size > 0:
226 str = str + " --size=%d" % part.size
227 if part.preexist == True:
228 str = str + " --useexisting"
230 str = str + " --name=%s --vgname=%s\n" % (part.name, part.vgname)
232 return str.rstrip()
234 def doMediaCheck(self):
235 if self.ksdata.mediacheck == True:
236 return "mediacheck"
238 def doMethod(self):
239 if self.ksdata.method["method"] == "cdrom":
240 return "# Use CDROM installation media\ncdrom"
241 elif self.ksdata.method["method"] == "harddrive":
242 return "# Use hard drive installation media\nharddrive --partition=%s --dir=%s" % (self.ksdata.method["partition"], self.ksdata.method["dir"])
243 elif self.ksdata.method["method"] == "nfs":
244 return "# Use NFS installation media\nnfs --server=%s --dir=%s" % (self.ksdata.method["server"], self.ksdata.method["dir"])
245 elif self.ksdata.method["method"] == "url":
246 return "# Use network installation\nurl --url=%s" % self.ksdata.method["url"]
248 def doMonitor(self):
249 str = "monitor"
251 if self.ksdata.monitor["hsync"] != "":
252 str = str + " --hsync=%s" % self.ksdata.monitor["hsync"]
253 if self.ksdata.monitor["monitor"] != "":
254 str = str + " --monitor=%s" % self.ksdata.monitor["monitor"]
255 if self.ksdata.monitor["vsync"] != "":
256 str = str + " --vsync=%s" % self.ksdata.monitor["vsync"]
258 if str != "monitor":
259 return str
261 def doNetwork(self):
262 if self.ksdata.network == []:
263 return
265 str = "# Network information\n"
267 for nic in self.ksdata.network:
268 str = str + "network"
270 if nic.bootProto != "":
271 str = str + " --bootproto=%s" % nic.bootProto
272 if nic.dhcpclass != "":
273 str = str + " --dhcpclass=%s" % nic.dhcpclass
274 if nic.device != "":
275 str = str + " --device=%s" % nic.device
276 if nic.essid != "":
277 str = str + " --essid=\"%s\"" % nic.essid
278 if nic.ethtool != "":
279 str = str + " --ethtool=%s" % nic.ethtool
280 if nic.gateway != "":
281 str = str + " --gateway=%s" % nic.gateway
282 if nic.hostname != "":
283 str = str + " --hostname=%s" % nic.hostname
284 if nic.ip != "":
285 str = str + " --ip=%s" % nic.ip
286 if nic.nameserver != "":
287 str = str + " --nameserver=%s" % nic.nameserver
288 if nic.netmask != "":
289 str = str + " --netmask=%s" % nic.netmask
290 if nic.nodns == True:
291 str = str + " --nodns"
292 if nic.notksdevice == True:
293 str = str + " --notksdevice"
294 if nic.onboot == True:
295 str = str + " --onboot"
296 if nic.wepkey != "":
297 str = str + " --wepkey=%s" % nic.wepkey
299 str = str + "\n"
301 return str.rstrip()
303 def doPartition(self):
304 if self.ksdata.upgrade == True or self.ksdata.partitions == []:
305 return
307 str = "# Disk partitioning information\n"
309 for part in self.ksdata.partitions:
310 str = str + "part %s" % part.mountpoint
312 if part.active == True:
313 str = str + " --active"
314 if part.primOnly == True:
315 str = str + " --asprimary"
316 if part.bytesPerInode != 0:
317 str = str + " --bytes-per-inode=%d" % part.bytesPerInode
318 if part.end != 0:
319 str = str + " --end=%d" % part.end
320 if part.fsopts != "":
321 str = str + " --fsoptions=\"%s\"" % part.fsopts
322 if part.fstype != "":
323 str = str + " --fstype=\"%s\"" % part.fstype
324 if part.grow == True:
325 str = str + " --grow"
326 if part.label != "":
327 str = str + " --label=%s" % part.label
328 if part.maxSizeMB > 0:
329 str = str + " --maxsize=%d" % part.maxSizeMB
330 if part.format == False:
331 str = str + " --noformat"
332 if part.onbiosdisk != "":
333 str = str + " --onbiosdisk=%s" % part.onbiosdisk
334 if part.disk != "":
335 str = str + " --ondisk=%s" % part.disk
336 if part.onPart != "":
337 str = str + " --onpart=%s" % part.onPart
338 if part.recommended == True:
339 str = str + " --recommended"
340 if part.size != 0:
341 str = str + " --size=%d" % part.size
342 if part.start != 0:
343 str = str + " --start=%d" % part.start
345 str = str + "\n"
347 return str.rstrip()
349 def doReboot(self):
350 if self.ksdata.reboot == True:
351 return "# Reboot after installation\nreboot"
353 def doRaid(self):
354 if self.ksdata.upgrade == True:
355 return
357 str = ""
359 for raid in self.ksdata.raidList:
360 str = str + "raid %s" % raid.mountpoint
362 if raid.bytesPerInode != 0:
363 str = str + " --bytes-per-inode=%d" % raid.bytesPerInode
364 if raid.device != "":
365 str = str + " --device=%s" % raid.device
366 if raid.fsopts != "":
367 str = str + " --fsoptions=\"%s\"" % raid.fsopts
368 if raid.fstype != "":
369 str = str + " --fstype=\"%s\"" % raid.fstype
370 if raid.level != "":
371 str = str + " --level=%s" % raid.level
372 if raid.format == False:
373 str = str + " --noformat"
374 if raid.spares != 0:
375 str = str + " --spares=%d" % raid.spares
376 if raid.preexist == True:
377 str = str + " --useexisting"
379 str = str + " %s\n" % string.join(raid.members)
381 return str.rstrip()
383 def doRootPw(self):
384 if self.ksdata.rootpw["password"] != "":
385 if self.ksdata.rootpw["isCrypted"] == True:
386 crypted = "--iscrypted"
387 else:
388 crypted = ""
390 return "#Root password\nrootpw %s %s" % (crypted, self.ksdata.rootpw["password"])
392 def doSELinux(self):
393 str = "# SELinux configuration\n"
395 if self.ksdata.selinux == SELINUX_DISABLED:
396 return str + "selinux --disabled"
397 elif self.ksdata.selinux == SELINUX_ENFORCING:
398 return str + "selinux --enforcing"
399 elif self.ksdata.selinux == SELINUX_PERMISSIVE:
400 return str + "selinux --permissive"
402 def doSkipX(self):
403 if self.ksdata.skipx == True and self.ksdata.upgrade == False:
404 return "# Do not configure the X Window System\nskipx"
406 def doTimezone(self):
407 if self.ksdata.timezone["timezone"] != "":
408 if self.ksdata.timezone["isUtc"] == True:
409 utc = "--isUtc"
410 else:
411 utc = ""
413 return "# System timezone\ntimezone %s %s" %(utc, self.ksdata.timezone["timezone"])
415 def doUpgrade(self):
416 if self.ksdata.upgrade == True:
417 return "# Upgrade existing installation\nupgrade"
418 else:
419 return "# Install OS instead of upgrade\ninstall"
421 def doVnc(self):
422 if self.ksdata.vnc["enabled"] == True:
423 if self.ksdata.vnc["password"] != "":
424 password = "--password=%s" % self.ksdata.vnc["password"]
425 else:
426 password = ""
428 if self.ksdata.vnc["port"] != "":
429 port = ":%s" % self.ksdata.vnc["port"]
430 else:
431 port = ""
433 return "vnc --enabled %s %s%s" % (password, self.ksdata.vnc["host"],
434 port)
436 def doVolumeGroup(self):
437 if self.ksdata.upgrade == True:
438 return
440 str = ""
442 for vg in self.ksdata.vgList:
443 str = str + "volgroup %s" % vg.vgname
445 if vg.format == False:
446 str = str + " --noformat"
447 if vg.pesize != 0:
448 str = str + " --pesize=%d" % vg.pesize
449 if vg.preexist == True:
450 str = str + " --useexisting"
452 str = str + " %s\n" % string.join(vg.physvols, ",")
454 return str.rstrip()
456 def doXConfig(self):
457 if self.ksdata.upgrade == True or self.ksdata.skipx == True:
458 return
460 str = "# X Window System configuration information\nxconfig"
462 if self.ksdata.xconfig["driver"] != "":
463 str = str + " --driver=%s" % self.ksdata.xconfig["driver"]
464 if self.ksdata.xconfig["defaultdesktop"] != "":
465 str = str + " --defaultdesktop=%s" % self.ksdata.xconfig["defaultdesktop"]
466 if self.ksdata.xconfig["depth"] != 0:
467 str = str + " --depth=%d" % self.ksdata.xconfig["depth"]
468 if self.ksdata.xconfig["hsync"] != "":
469 str = str + " --hsync=%s" % self.ksdata.xconfig["hsync"]
470 if self.ksdata.xconfig["monitor"] != "":
471 str = str + " --monitor=\"%s\"" % self.ksdata.xconfig["monitor"]
472 if self.ksdata.xconfig["probe"] == False:
473 str = str + " --noprobe"
474 if self.ksdata.xconfig["resolution"] != "":
475 str = str + " --resolution=%s" % self.ksdata.xconfig["resolution"]
476 if self.ksdata.xconfig["startX"] == True:
477 str = str + " --startxonboot"
478 if self.ksdata.xconfig["videoRam"] != "":
479 str = str + " --videoram=%s" % self.ksdata.xconfig["videoRam"]
480 if self.ksdata.xconfig["vsync"] != "":
481 str = str + " --vsync=%s" % self.ksdata.xconfig["vsync"]
483 if str != "xconfig":
484 return str
486 def doZeroMbr(self):
487 if self.ksdata.zerombr == True:
488 return "# Clear the Master Boot Record\nzerombr"
490 def doZFCP(self):
491 if self.ksdata.zfcp["devnum"] != "":
492 return "zfcp --devnum=%(devnum)s --fcplun=%(fcplun)s --scsiid=%(scsiid)s --scsilun=%(scsilun)s --wwpn=%(wwpn)s" % self.ksdata.zfcp
494 def doScripts(self):
495 preStr = ""
496 postStr = ""
497 tracebackStr = ""
499 for script in self.ksdata.scripts:
500 if script.type == KS_SCRIPT_PRE:
501 preStr = preStr + "%%pre %s" % script.write()
502 elif script.type == KS_SCRIPT_POST:
503 postStr = postStr + "%%post %s" % script.write()
504 elif script.type == KS_SCRIPT_TRACEBACK:
505 tracebackStr = tracebackStr + "%%traceback %s" % script.write()
507 return preStr + postStr + tracebackStr.rstrip()
509 def doPackages(self):
510 if self.ksdata.upgrade == True:
511 return
513 str = "\n%packages\n"
515 for pkg in self.ksdata.packageList:
516 str = str + "%s\n" % pkg
518 for pkg in self.ksdata.excludedList:
519 str = str + "-%s\n" % pkg
521 for grp in self.ksdata.groupList:
522 str = str + "@%s\n" % grp
524 return str.rstrip()