Add support for --eject on reboot/shutdown.
[pykickstart.git] / pykickstart / writer.py
blob9d1401a451e504e0e0bf2b8a12417d647054404f
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["probe"] == False:
256 str = str + " --noprobe"
257 if self.ksdata.monitor["vsync"] != "":
258 str = str + " --vsync=%s" % self.ksdata.monitor["vsync"]
260 if str != "monitor":
261 return str
263 def doNetwork(self):
264 if self.ksdata.network == []:
265 return
267 str = "# Network information\n"
269 for nic in self.ksdata.network:
270 str = str + "network"
272 if nic.bootProto != "":
273 str = str + " --bootproto=%s" % nic.bootProto
274 if nic.dhcpclass != "":
275 str = str + " --dhcpclass=%s" % nic.dhcpclass
276 if nic.device != "":
277 str = str + " --device=%s" % nic.device
278 if nic.essid != "":
279 str = str + " --essid=\"%s\"" % nic.essid
280 if nic.ethtool != "":
281 str = str + " --ethtool=\"%s\"" % nic.ethtool
282 if nic.gateway != "":
283 str = str + " --gateway=%s" % nic.gateway
284 if nic.hostname != "":
285 str = str + " --hostname=%s" % nic.hostname
286 if nic.ip != "":
287 str = str + " --ip=%s" % nic.ip
288 if nic.nameserver != "":
289 str = str + " --nameserver=%s" % nic.nameserver
290 if nic.netmask != "":
291 str = str + " --netmask=%s" % nic.netmask
292 if nic.nodns == True:
293 str = str + " --nodns"
294 if nic.notksdevice == True:
295 str = str + " --notksdevice"
296 if nic.onboot == True:
297 str = str + " --onboot"
298 if nic.wepkey != "":
299 str = str + " --wepkey=%s" % nic.wepkey
301 str = str + "\n"
303 return str.rstrip()
305 def doPartition(self):
306 if self.ksdata.upgrade == True or self.ksdata.partitions == []:
307 return
309 str = "# Disk partitioning information\n"
311 for part in self.ksdata.partitions:
312 str = str + "part %s" % part.mountpoint
314 if part.active == True:
315 str = str + " --active"
316 if part.primOnly == True:
317 str = str + " --asprimary"
318 if part.bytesPerInode != 0:
319 str = str + " --bytes-per-inode=%d" % part.bytesPerInode
320 if part.end != 0:
321 str = str + " --end=%d" % part.end
322 if part.fsopts != "":
323 str = str + " --fsoptions=\"%s\"" % part.fsopts
324 if part.fstype != "":
325 str = str + " --fstype=\"%s\"" % part.fstype
326 if part.grow == True:
327 str = str + " --grow"
328 if part.label != "":
329 str = str + " --label=%s" % part.label
330 if part.maxSizeMB > 0:
331 str = str + " --maxsize=%d" % part.maxSizeMB
332 if part.format == False:
333 str = str + " --noformat"
334 if part.onbiosdisk != "":
335 str = str + " --onbiosdisk=%s" % part.onbiosdisk
336 if part.disk != "":
337 str = str + " --ondisk=%s" % part.disk
338 if part.onPart != "":
339 str = str + " --onpart=%s" % part.onPart
340 if part.recommended == True:
341 str = str + " --recommended"
342 if part.size != 0:
343 str = str + " --size=%d" % part.size
344 if part.start != 0:
345 str = str + " --start=%d" % part.start
347 str = str + "\n"
349 return str.rstrip()
351 def doReboot(self):
352 str = ""
354 if self.ksdata.reboot["action"] == KS_REBOOT:
355 str = "# Reboot after installation\nreboot"
356 elif self.ksdata.reboot["action"] == KS_SHUTDOWN:
357 str = "# Shutdown after installation\nshutdown"
359 if self.ksdata.reboot["eject"] == True:
360 str = str + " --eject"
362 return "%s\n" % str
364 def doRaid(self):
365 if self.ksdata.upgrade == True:
366 return
368 str = ""
370 for raid in self.ksdata.raidList:
371 str = str + "raid %s" % raid.mountpoint
373 if raid.bytesPerInode != 0:
374 str = str + " --bytes-per-inode=%d" % raid.bytesPerInode
375 if raid.device != "":
376 str = str + " --device=%s" % raid.device
377 if raid.fsopts != "":
378 str = str + " --fsoptions=\"%s\"" % raid.fsopts
379 if raid.fstype != "":
380 str = str + " --fstype=\"%s\"" % raid.fstype
381 if raid.level != "":
382 str = str + " --level=%s" % raid.level
383 if raid.format == False:
384 str = str + " --noformat"
385 if raid.spares != 0:
386 str = str + " --spares=%d" % raid.spares
387 if raid.preexist == True:
388 str = str + " --useexisting"
390 str = str + " %s\n" % string.join(raid.members)
392 return str.rstrip()
394 def doRootPw(self):
395 if self.ksdata.rootpw["password"] != "":
396 if self.ksdata.rootpw["isCrypted"] == True:
397 crypted = "--iscrypted"
398 else:
399 crypted = ""
401 return "#Root password\nrootpw %s %s" % (crypted, self.ksdata.rootpw["password"])
403 def doSELinux(self):
404 str = "# SELinux configuration\n"
406 if self.ksdata.selinux == SELINUX_DISABLED:
407 return str + "selinux --disabled"
408 elif self.ksdata.selinux == SELINUX_ENFORCING:
409 return str + "selinux --enforcing"
410 elif self.ksdata.selinux == SELINUX_PERMISSIVE:
411 return str + "selinux --permissive"
413 def doSkipX(self):
414 if self.ksdata.skipx == True and self.ksdata.upgrade == False:
415 return "# Do not configure the X Window System\nskipx"
417 def doTimezone(self):
418 if self.ksdata.timezone["timezone"] != "":
419 if self.ksdata.timezone["isUtc"] == True:
420 utc = "--isUtc"
421 else:
422 utc = ""
424 return "# System timezone\ntimezone %s %s" %(utc, self.ksdata.timezone["timezone"])
426 def doUpgrade(self):
427 if self.ksdata.upgrade == True:
428 return "# Upgrade existing installation\nupgrade"
429 else:
430 return "# Install OS instead of upgrade\ninstall"
432 def doVnc(self):
433 if self.ksdata.vnc["enabled"] == True:
434 if self.ksdata.vnc["password"] != "":
435 password = "--password=%s" % self.ksdata.vnc["password"]
436 else:
437 password = ""
439 if self.ksdata.vnc["port"] != "":
440 port = ":%s" % self.ksdata.vnc["port"]
441 else:
442 port = ""
444 return "vnc --enabled %s %s%s" % (password, self.ksdata.vnc["host"],
445 port)
447 def doVolumeGroup(self):
448 if self.ksdata.upgrade == True:
449 return
451 str = ""
453 for vg in self.ksdata.vgList:
454 str = str + "volgroup %s" % vg.vgname
456 if vg.format == False:
457 str = str + " --noformat"
458 if vg.pesize != 0:
459 str = str + " --pesize=%d" % vg.pesize
460 if vg.preexist == True:
461 str = str + " --useexisting"
463 str = str + " %s\n" % string.join(vg.physvols, ",")
465 return str.rstrip()
467 def doXConfig(self):
468 if self.ksdata.upgrade == True or self.ksdata.skipx == True:
469 return
471 str = "# X Window System configuration information\nxconfig"
473 if self.ksdata.xconfig["driver"] != "":
474 str = str + " --driver=%s" % self.ksdata.xconfig["driver"]
475 if self.ksdata.xconfig["defaultdesktop"] != "":
476 str = str + " --defaultdesktop=%s" % self.ksdata.xconfig["defaultdesktop"]
477 if self.ksdata.xconfig["depth"] != 0:
478 str = str + " --depth=%d" % self.ksdata.xconfig["depth"]
479 if self.ksdata.xconfig["resolution"] != "":
480 str = str + " --resolution=%s" % self.ksdata.xconfig["resolution"]
481 if self.ksdata.xconfig["startX"] == True:
482 str = str + " --startxonboot"
483 if self.ksdata.xconfig["videoRam"] != "":
484 str = str + " --videoram=%s" % self.ksdata.xconfig["videoRam"]
486 if str != "xconfig":
487 return str
489 def doZeroMbr(self):
490 if self.ksdata.zerombr == True:
491 return "# Clear the Master Boot Record\nzerombr"
493 def doZFCP(self):
494 if self.ksdata.zfcp["devnum"] != "":
495 return "zfcp --devnum=%(devnum)s --fcplun=%(fcplun)s --scsiid=%(scsiid)s --scsilun=%(scsilun)s --wwpn=%(wwpn)s" % self.ksdata.zfcp
497 def doScripts(self):
498 preStr = ""
499 postStr = ""
500 tracebackStr = ""
502 for script in self.ksdata.scripts:
503 if script.type == KS_SCRIPT_PRE:
504 preStr = preStr + "%%pre %s" % script.write()
505 elif script.type == KS_SCRIPT_POST:
506 postStr = postStr + "%%post %s" % script.write()
507 elif script.type == KS_SCRIPT_TRACEBACK:
508 tracebackStr = tracebackStr + "%%traceback %s" % script.write()
510 return preStr + postStr + tracebackStr.rstrip()
512 def doPackages(self):
513 if self.ksdata.upgrade == True:
514 return
516 str = "\n%packages\n"
518 for pkg in self.ksdata.packageList:
519 str = str + "%s\n" % pkg
521 for pkg in self.ksdata.excludedList:
522 str = str + "-%s\n" % pkg
524 for grp in self.ksdata.groupList:
525 str = str + "@%s\n" % grp
527 return str.rstrip()