blob: c9b0e51f3c054fb49fd9780cd002f6fb20124868 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/usr/bin/env python -tt
2#
3# Copyright (c) 2007 Red Hat, Inc.
4# Copyright (c) 2009, 2010, 2011 Intel, Inc.
5#
6# This program is free software; you can redistribute it and/or modify it
7# under the terms of the GNU General Public License as published by the Free
8# Software Foundation; version 2 of the License
9#
10# This program is distributed in the hope that it will be useful, but
11# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13# for more details.
14#
15# You should have received a copy of the GNU General Public License along
16# with this program; if not, write to the Free Software Foundation, Inc., 59
17# Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19import os, sys, re
20import shutil
21import subprocess
22import string
23
24import pykickstart.sections as kssections
25import pykickstart.commands as kscommands
26import pykickstart.constants as ksconstants
27import pykickstart.errors as kserrors
28import pykickstart.parser as ksparser
29import pykickstart.version as ksversion
30from pykickstart.handlers.control import commandMap
31from pykickstart.handlers.control import dataMap
32
33from wic import msger
34from wic.utils import errors, misc, runner, fs_related as fs
35from custom_commands import wicboot, partition
36
37def read_kickstart(path):
38 """Parse a kickstart file and return a KickstartParser instance.
39
40 This is a simple utility function which takes a path to a kickstart file,
41 parses it and returns a pykickstart KickstartParser instance which can
42 be then passed to an ImageCreator constructor.
43
44 If an error occurs, a CreatorError exception is thrown.
45 """
46
47 #version = ksversion.makeVersion()
48 #ks = ksparser.KickstartParser(version)
49
50 using_version = ksversion.DEVEL
51 commandMap[using_version]["bootloader"] = wicboot.Wic_Bootloader
52 commandMap[using_version]["part"] = partition.Wic_Partition
53 commandMap[using_version]["partition"] = partition.Wic_Partition
54 dataMap[using_version]["PartData"] = partition.Wic_PartData
55 superclass = ksversion.returnClassForVersion(version=using_version)
56
57 class KSHandlers(superclass):
58 def __init__(self):
59 superclass.__init__(self, mapping=commandMap[using_version])
60
61 kickstart = ksparser.KickstartParser(KSHandlers(), errorsAreFatal=True)
62
63 try:
64 kickstart.readKickstart(path)
65 except (kserrors.KickstartParseError, kserrors.KickstartError), err:
66 msger.warning("Errors occurred when parsing kickstart file: %s\n" % path)
67 msger.error("%s" % err)
68
69 return kickstart
70
71def get_image_size(kickstart, default=None):
72 __size = 0
73 for part in kickstart.handler.partition.partitions:
74 if part.mountpoint == "/" and part.size:
75 __size = part.size
76 if __size > 0:
77 return int(__size) * 1024L
78 else:
79 return default
80
81def get_image_fstype(kickstart, default=None):
82 for part in kickstart.handler.partition.partitions:
83 if part.mountpoint == "/" and part.fstype:
84 return part.fstype
85 return default
86
87def get_image_fsopts(kickstart, default=None):
88 for part in kickstart.handler.partition.partitions:
89 if part.mountpoint == "/" and part.fsopts:
90 return part.fsopts
91 return default
92
93def get_timeout(kickstart, default=None):
94 if not hasattr(kickstart.handler.bootloader, "timeout"):
95 return default
96 if kickstart.handler.bootloader.timeout is None:
97 return default
98 return int(kickstart.handler.bootloader.timeout)
99
100def get_kernel_args(kickstart, default="ro rd.live.image"):
101 if not hasattr(kickstart.handler.bootloader, "appendLine"):
102 return default
103 if kickstart.handler.bootloader.appendLine is None:
104 return default
105 return "%s %s" %(default, kickstart.handler.bootloader.appendLine)
106
107def get_menu_args(kickstart, default=""):
108 if not hasattr(kickstart.handler.bootloader, "menus"):
109 return default
110 if kickstart.handler.bootloader.menus in (None, ""):
111 return default
112 return "%s" % kickstart.handler.bootloader.menus
113
114def get_default_kernel(kickstart, default=None):
115 if not hasattr(kickstart.handler.bootloader, "default"):
116 return default
117 if not kickstart.handler.bootloader.default:
118 return default
119 return kickstart.handler.bootloader.default
120
121def get_partitions(kickstart):
122 return kickstart.handler.partition.partitions