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.
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
]
41 # Return a string representation of the ksdata suitable for writing to
44 return string
.join(filter (lambda str: str != None and str != "",
45 map (lambda fn
: fn(), self
.handlers
)), "\n")
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
56 if self
.ksdata
.autopart
:
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":
85 def doClearPart(self
):
86 if self
.ksdata
.clearpart
["type"] == CLEARPART_TYPE_NONE
:
88 elif self
.ksdata
.clearpart
["type"] == CLEARPART_TYPE_LINUX
:
90 elif self
.ksdata
.clearpart
["type"] == CLEARPART_TYPE_LINUX
:
93 if self
.ksdata
.clearpart
["initAll"] == True:
94 initstr
= "--initlabel"
98 if len(self
.ksdata
.clearpart
["drives"]) > 0:
99 drivestr
= "--drives=" + \
100 string
.join (self
.ksdata
.clearpart
["drives"], ",")
104 return "# Partition clearing information\nclearpart %s %s %s" % (clearstr
, initstr
, drivestr
)
107 if self
.ksdata
.device
!= "":
108 return "device %s" % self
.ksdata
.device
110 def doDeviceProbe(self
):
111 if self
.ksdata
.deviceprobe
!= "":
112 return "deviceprobe %s" % self
.ksdata
.deviceprobe
114 def doDisplayMode(self
):
115 if self
.ksdata
.displayMode
== DISPLAY_MODE_CMDLINE
:
117 elif self
.ksdata
.displayMode
== DISPLAY_MODE_GRAPHICAL
:
118 return "# Use graphical install\ngraphical"
119 elif self
.ksdata
.displayMode
== DISPLAY_MODE_TEXT
:
120 return "# Use text mode install\ntext"
122 def doDriverDisk(self
):
123 if self
.ksdata
.driverdisk
!= "":
124 return "driverdisk %s" % self
.ksdata
.driverdisk
126 def doFirewall(self
):
127 if self
.ksdata
.upgrade
== True:
130 if self
.ksdata
.firewall
["enabled"]:
131 portstr
= string
.join (self
.ksdata
.firewall
["ports"], ",")
133 portstr
= "--ports= " + portstr
137 truststr
= string
.join (self
.ksdata
.firewall
["trusts"], ",")
138 if len(truststr
) > 0:
139 truststr
= "--trust= " + truststr
141 return "# Firewall configuration\nfirewall --enabled %s %s" % (portstr
, truststr
)
143 return "# Firewall configuration\nfirewall --disabled"
145 def doFirstboot(self
):
146 str = "# Run the Setup Agent on first boot\n"
148 if self
.ksdata
.firstboot
== FIRSTBOOT_SKIP
:
149 return str + "firstboot --disable"
150 elif self
.ksdata
.firstboot
== FIRSTBOOT_DEFAULT
:
151 return str + "firstboot --enable"
152 elif self
.ksdata
.firstboot
== FIRSTBOOT_RECONFIG
:
153 return str + "firstboot --reconfig"
155 def doIgnoreDisk(self
):
156 if len(self
.ksdata
.ignoredisk
) > 0:
157 str = string
.join (self
.ksdata
.ignoredisk
, ",")
158 return "ignoredisk --drives= %s" % str
160 def doInteractive(self
):
161 if self
.ksdata
.interactive
== True:
162 return "# Use interactive kickstart installation method\ninteractive"
164 def doKeyboard(self
):
165 if self
.ksdata
.keyboard
!= "":
166 return "# System keyboard\nkeyboard %s" % self
.ksdata
.keyboard
169 if self
.ksdata
.lang
!= "":
170 return "# System language\nlang %s" % self
.ksdata
.lang
172 def doLogicalVolume(self
):
173 if self
.ksdata
.upgrade
== True:
178 for part
in self
.ksdata
.lvList
:
179 str = str + "logvol %s" % part
.mountpoint
181 if part
.bytesPerInode
> 0:
182 str = str + " --bytes-per-inode= %d" % part
.bytesPerInode
183 if part
.fsopts
!= "":
184 str = str + " --fsoptions=\"%s\"" % part
.fsopts
185 if part
.fstype
!= "":
186 str = str + " --fstype=\"%s\"" % part
.fstype
187 if part
.grow
== True:
188 str = str + " --grow"
189 if part
.maxSizeMB
> 0:
190 str = str + " --maxsize=%d" % part
.maxSizeMB
191 if part
.format
== False:
192 str = str + " --noformat"
194 str = str + " --percent=%d" % part
.percent
195 if part
.recommended
== True:
196 str = str + " --recommended"
198 str = str + " --size=%d" % part
.size
199 if part
.preexist
== True:
200 str = str + " --useexisting"
202 str = str + " --name=%s --vgname=%s\n" % (part
.name
, part
.vgname
)
206 def doMediaCheck(self
):
207 if self
.ksdata
.mediacheck
== True:
211 if self
.ksdata
.method
["method"] == "cdrom":
212 return "# Use CDROM installation media\ncdrom"
213 elif self
.ksdata
.method
["method"] == "harddrive":
214 return "# Use hard drive installation media\nharddrive --partition=%s --dir=%s" % (self
.ksdata
.method
["partition"], self
.ksdata
.method
["dir"])
215 elif self
.ksdata
.method
["method"] == "nfs":
216 return "# Use NFS installation media\nnfs --server=%s --dir=%s" % (self
.ksdata
.method
["server"], self
.ksdata
.method
["dir"])
217 elif self
.ksdata
.method
["method"] == "url":
218 return "# Use network installation\nurl --url=%s" % self
.ksdata
.method
["url"]
223 if self
.ksdata
.monitor
["hsync"] != "":
224 str = str + " --hsync=%s" % self
.ksdata
.monitor
["hsync"]
225 if self
.ksdata
.monitor
["monitor"] != "":
226 str = str + " --monitor=%s" % self
.ksdata
.monitor
["monitor"]
227 if self
.ksdata
.monitor
["vsync"] != "":
228 str = str + " --vsync=%s" % self
.ksdata
.monitor
["vsync"]
234 if self
.ksdata
.network
== []:
237 str = "# Network information\n"
239 for nic
in self
.ksdata
.network
:
240 str = str + "network"
242 if nic
.bootProto
!= "":
243 str = str + " --bootproto=%s" % nic
.bootProto
244 if nic
.dhcpclass
!= "":
245 str = str + " --dhcpclass=%s" % nic
.dhcpclass
247 str = str + " --device=%s" % nic
.device
249 str = str + " --essid=\"%s\"" % nic
.essid
250 if nic
.ethtool
!= "":
251 str = str + " --ethtool=%s" % nic
.ethtool
252 if nic
.gateway
!= "":
253 str = str + " --gateway=%s" % nic
.gateway
254 if nic
.hostname
!= "":
255 str = str + " --hostname=%s" % nic
.hostname
257 str = str + " --ip=%s" % nic
.ip
258 if nic
.nameserver
!= "":
259 str = str + " --nameserver=%s" % nic
.nameserver
260 if nic
.netmask
!= "":
261 str = str + " --netmask=%s" % nic
.netmask
262 if nic
.nodns
== True:
263 str = str + " --nodns"
264 if nic
.notksdevice
== True:
265 str = str + " --notksdevice"
266 if nic
.onboot
== True:
267 str = str + " --onboot"
269 str = str + " --wepkey=%s" % nic
.wepkey
275 def doPartition(self
):
276 if self
.ksdata
.upgrade
== True or self
.ksdata
.partitions
== []:
279 str = "# Disk partitioning information\n"
281 for part
in self
.ksdata
.partitions
:
282 str = str + "part %s" % part
.mountpoint
284 if part
.active
== True:
285 str = str + " --active"
286 if part
.primOnly
== True:
287 str = str + " --asprimary"
288 if part
.bytesPerInode
!= 0:
289 str = str + " --bytes-per-inode=%d" % part
.bytesPerInode
291 str = str + " --end=%d" % part
.end
292 if part
.fsopts
!= "":
293 str = str + " --fsoptions=\"%s\"" % part
.fsopts
294 if part
.fstype
!= "":
295 str = str + " --fstype=\"%s\"" % part
.fstype
296 if part
.grow
== True:
297 str = str + " --grow"
299 str = str + " --label=%s" % part
.label
300 if part
.maxSizeMB
> 0:
301 str = str + " --maxsize=%d" % part
.maxSizeMB
302 if part
.format
== False:
303 str = str + " --noformat"
304 if part
.onbiosdisk
!= "":
305 str = str + " --onbiosdisk=%s" % part
.onbiosdisk
307 str = str + " --ondisk=%s" % part
.disk
308 if part
.onPart
!= "":
309 str = str + " --onpart=%s" % part
.onPart
310 if part
.recommended
== True:
311 str = str + " --recommended"
313 str = str + " --size=%d" % part
.size
315 str = str + " --start=%d" % part
.start
322 if self
.ksdata
.reboot
== True:
323 return "# Reboot after installation\nreboot"
326 if self
.ksdata
.upgrade
== True:
331 for raid
in self
.ksdata
.raidList
:
332 str = str + "raid %s" % raid
.mountpoint
334 if raid
.device
!= "":
335 str = str + " --device=%s" % raid
.device
336 if raid
.fsopts
!= "":
337 str = str + " --fsoptions=\"%s\"" % raid
.fsopts
338 if raid
.fstype
!= "":
339 str = str + " --fstype=\"%s\"" % raid
.fstype
341 str = str + " --level=%s" % raid
.level
342 if raid
.format
== False:
343 str = str + " --noformat"
345 str = str + " --spares=%d" % raid
.spares
346 if raid
.preexist
== True:
347 str = str + " --useexisting"
349 str = str + " %s\n" % string
.join(raid
.members
)
354 if self
.ksdata
.rootpw
["password"] != "":
355 if self
.ksdata
.rootpw
["isCrypted"] == True:
356 crypted
= "--iscrypted"
360 return "#Root password\nrootpw %s %s" % (crypted
, self
.ksdata
.rootpw
["password"])
363 str = "# SELinux configuration\n"
365 if self
.ksdata
.selinux
== SELINUX_DISABLED
:
366 return str + "selinux --disabled"
367 elif self
.ksdata
.selinux
== SELINUX_ENFORCING
:
368 return str + "selinux --enforcing"
369 elif self
.ksdata
.selinux
== SELINUX_PERMISSIVE
:
370 return str + "selinux --permissive"
373 if self
.ksdata
.skipx
== True and self
.ksdata
.upgrade
== False:
374 return "# Do not configure the X Window System\nskipx"
376 def doTimezone(self
):
377 if self
.ksdata
.timezone
["timezone"] != "":
378 if self
.ksdata
.timezone
["isUtc"] == True:
383 return "# System timezone\ntimezone %s %s" %(utc
, self
.ksdata
.timezone
["timezone"])
386 if self
.ksdata
.upgrade
== True:
387 return "# Upgrade existing installation\nupgrade"
389 return "# Install OS instead of upgrade\ninstall"
392 if self
.ksdata
.vnc
["enabled"] == True:
393 if self
.ksdata
.vnc
["password"] != "":
394 password
= "--password=%s" % self
.ksdata
.vnc
["password"]
398 if self
.ksdata
.vnc
["port"] != "":
399 port
= ":%s" % self
.ksdata
.vnc
["port"]
403 return "vnc --enabled %s %s%s" % (password
, self
.ksdata
.vnc
["host"],
406 def doVolumeGroup(self
):
407 if self
.ksdata
.upgrade
== True:
412 for vg
in self
.ksdata
.vgList
:
413 str = str + "volgroup %s" % vg
.vgname
415 if vg
.format
== False:
416 str = str + " --noformat"
418 str = str + " --pesize=%d" % vg
.pesize
419 if vg
.preexist
== True:
420 str = str + " --useexisting"
422 str = str + " %s\n" % string
.join(vg
.physvols
, ",")
427 if self
.ksdata
.upgrade
== True or self
.ksdata
.skipx
== True:
430 str = "# X Window System configuration information\nxconfig"
432 if self
.ksdata
.xconfig
["driver"] != "":
433 str = str + " --driver=%s" % self
.ksdata
.xconfig
["driver"]
434 if self
.ksdata
.xconfig
["defaultdesktop"] != "":
435 str = str + " --defaultdesktop=%s" % self
.ksdata
.xconfig
["defaultdesktop"]
436 if self
.ksdata
.xconfig
["depth"] != 0:
437 str = str + " --depth=%d" % self
.ksdata
.xconfig
["depth"]
438 if self
.ksdata
.xconfig
["hsync"] != "":
439 str = str + " --hsync=%s" % self
.ksdata
.xconfig
["hsync"]
440 if self
.ksdata
.xconfig
["monitor"] != "":
441 str = str + " --monitor=\"%s\"" % self
.ksdata
.xconfig
["monitor"]
442 if self
.ksdata
.xconfig
["probe"] == False:
443 str = str + " --noprobe"
444 if self
.ksdata
.xconfig
["resolution"] != "":
445 str = str + " --resolution=%s" % self
.ksdata
.xconfig
["resolution"]
446 if self
.ksdata
.xconfig
["startX"] == True:
447 str = str + " --startxonboot"
448 if self
.ksdata
.xconfig
["videoRam"] != "":
449 str = str + " --videoram=%s" % self
.ksdata
.xconfig
["videoRam"]
450 if self
.ksdata
.xconfig
["vsync"] != "":
451 str = str + " --vsync=%s" % self
.ksdata
.xconfig
["vsync"]
457 if self
.ksdata
.zerombr
== True:
458 return "# Clear the Master Boot Record\nzerombr"
461 if self
.ksdata
.zfcp
["devnum"] != "":
462 return "zfcp --devnum=%(devnum)s --fcplun=%(fcplun)s --scsiid=%(scsiid)s --scsilun=%(scsilun)s --wwpn=%(wwpn)s" % self
.ksdata
.zfcp
469 for script
in self
.ksdata
.scripts
:
470 if script
.type == KS_SCRIPT_PRE
:
471 preStr
= preStr
+ "%%pre %s" % script
.write()
472 elif script
.type == KS_SCRIPT_POST
:
473 postStr
= postStr
+ "%%post %s" % script
.write()
474 elif script
.type == KS_SCRIPT_TRACEBACK
:
475 tracebackStr
= tracebackStr
+ "%%traceback %s" % script
.write()
477 return preStr
+ postStr
+ tracebackStr
.rstrip()
479 def doPackages(self
):
480 if self
.ksdata
.upgrade
== True:
483 str = "\n%packages\n"
485 for pkg
in self
.ksdata
.packageList
:
486 str = str + "%s\n" % pkg
488 for pkg
in self
.ksdata
.excludedList
:
489 str = str + "-%s\n" % pkg
491 for grp
in self
.ksdata
.groupList
:
492 str = str + "@%s\n" % grp