ksparser.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #!/usr/bin/env python -tt
  2. # ex:ts=4:sw=4:sts=4:et
  3. # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  4. #
  5. # Copyright (c) 2016 Intel, Inc.
  6. #
  7. # This program is free software; you can redistribute it and/or modify it
  8. # under the terms of the GNU General Public License as published by the Free
  9. # Software Foundation; version 2 of the License
  10. #
  11. # This program is distributed in the hope that it will be useful, but
  12. # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  13. # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. # for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License along
  17. # with this program; if not, write to the Free Software Foundation, Inc., 59
  18. # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. #
  20. # DESCRIPTION
  21. # This module provides parser for kickstart format
  22. #
  23. # AUTHORS
  24. # Ed Bartosh <ed.bartosh> (at] linux.intel.com>
  25. """Kickstart parser module."""
  26. import os
  27. import shlex
  28. import logging
  29. from argparse import ArgumentParser, ArgumentError, ArgumentTypeError
  30. from wic.engine import find_canned
  31. from wic.partition import Partition
  32. logger = logging.getLogger('wic')
  33. class KickStartError(Exception):
  34. """Custom exception."""
  35. pass
  36. class KickStartParser(ArgumentParser):
  37. """
  38. This class overwrites error method to throw exception
  39. instead of producing usage message(default argparse behavior).
  40. """
  41. def error(self, message):
  42. raise ArgumentError(None, message)
  43. def sizetype(arg):
  44. """
  45. Custom type for ArgumentParser
  46. Converts size string in <num>[K|k|M|G] format into the integer value
  47. """
  48. if arg.isdigit():
  49. return int(arg) * 1024
  50. if not arg[:-1].isdigit():
  51. raise ArgumentTypeError("Invalid size: %r" % arg)
  52. size = int(arg[:-1])
  53. if arg.endswith("k") or arg.endswith("K"):
  54. return size
  55. if arg.endswith("M"):
  56. return size * 1024
  57. if arg.endswith("G"):
  58. return size * 1024 * 1024
  59. raise ArgumentTypeError("Invalid size: %r" % arg)
  60. def overheadtype(arg):
  61. """
  62. Custom type for ArgumentParser
  63. Converts overhead string to float and checks if it's bigger than 1.0
  64. """
  65. try:
  66. result = float(arg)
  67. except ValueError:
  68. raise ArgumentTypeError("Invalid value: %r" % arg)
  69. if result < 1.0:
  70. raise ArgumentTypeError("Overhead factor should be > 1.0" % arg)
  71. return result
  72. def cannedpathtype(arg):
  73. """
  74. Custom type for ArgumentParser
  75. Tries to find file in the list of canned wks paths
  76. """
  77. scripts_path = os.path.abspath(os.path.dirname(__file__) + '../../..')
  78. result = find_canned(scripts_path, arg)
  79. if not result:
  80. raise ArgumentTypeError("file not found: %s" % arg)
  81. return result
  82. def systemidtype(arg):
  83. """
  84. Custom type for ArgumentParser
  85. Checks if the argument sutisfies system id requirements,
  86. i.e. if it's one byte long integer > 0
  87. """
  88. error = "Invalid system type: %s. must be hex "\
  89. "between 0x1 and 0xFF" % arg
  90. try:
  91. result = int(arg, 16)
  92. except ValueError:
  93. raise ArgumentTypeError(error)
  94. if result <= 0 or result > 0xff:
  95. raise ArgumentTypeError(error)
  96. return arg
  97. class KickStart():
  98. """Kickstart parser implementation."""
  99. DEFAULT_EXTRA_SPACE = 10*1024
  100. DEFAULT_OVERHEAD_FACTOR = 1.3
  101. def __init__(self, confpath):
  102. self.partitions = []
  103. self.bootloader = None
  104. self.lineno = 0
  105. self.partnum = 0
  106. parser = KickStartParser()
  107. subparsers = parser.add_subparsers()
  108. part = subparsers.add_parser('part')
  109. part.add_argument('mountpoint', nargs='?')
  110. part.add_argument('--active', action='store_true')
  111. part.add_argument('--align', type=int)
  112. part.add_argument('--exclude-path', nargs='+')
  113. part.add_argument("--extra-space", type=sizetype)
  114. part.add_argument('--fsoptions', dest='fsopts')
  115. part.add_argument('--fstype', default='vfat',
  116. choices=('ext2', 'ext3', 'ext4', 'btrfs',
  117. 'squashfs', 'vfat', 'msdos', 'swap'))
  118. part.add_argument('--mkfs-extraopts', default='')
  119. part.add_argument('--label')
  120. part.add_argument('--no-table', action='store_true')
  121. part.add_argument('--ondisk', '--ondrive', dest='disk', default='sda')
  122. part.add_argument("--overhead-factor", type=overheadtype)
  123. part.add_argument('--part-name')
  124. part.add_argument('--part-type')
  125. part.add_argument('--rootfs-dir')
  126. # --size and --fixed-size cannot be specified together; options
  127. # ----extra-space and --overhead-factor should also raise a parser
  128. # --error, but since nesting mutually exclusive groups does not work,
  129. # ----extra-space/--overhead-factor are handled later
  130. sizeexcl = part.add_mutually_exclusive_group()
  131. sizeexcl.add_argument('--size', type=sizetype, default=0)
  132. sizeexcl.add_argument('--fixed-size', type=sizetype, default=0)
  133. part.add_argument('--source')
  134. part.add_argument('--sourceparams')
  135. part.add_argument('--system-id', type=systemidtype)
  136. part.add_argument('--use-uuid', action='store_true')
  137. part.add_argument('--uuid')
  138. bootloader = subparsers.add_parser('bootloader')
  139. bootloader.add_argument('--append')
  140. bootloader.add_argument('--configfile')
  141. bootloader.add_argument('--ptable', choices=('msdos', 'gpt'),
  142. default='msdos')
  143. bootloader.add_argument('--timeout', type=int)
  144. bootloader.add_argument('--source')
  145. include = subparsers.add_parser('include')
  146. include.add_argument('path', type=cannedpathtype)
  147. self._parse(parser, confpath)
  148. if not self.bootloader:
  149. logger.warning('bootloader config not specified, using defaults\n')
  150. self.bootloader = bootloader.parse_args([])
  151. def _parse(self, parser, confpath):
  152. """
  153. Parse file in .wks format using provided parser.
  154. """
  155. with open(confpath) as conf:
  156. lineno = 0
  157. for line in conf:
  158. line = line.strip()
  159. lineno += 1
  160. if line and line[0] != '#':
  161. try:
  162. line_args = shlex.split(line)
  163. parsed = parser.parse_args(line_args)
  164. except ArgumentError as err:
  165. raise KickStartError('%s:%d: %s' % \
  166. (confpath, lineno, err))
  167. if line.startswith('part'):
  168. # using ArgumentParser one cannot easily tell if option
  169. # was passed as argument, if said option has a default
  170. # value; --overhead-factor/--extra-space cannot be used
  171. # with --fixed-size, so at least detect when these were
  172. # passed with non-0 values ...
  173. if parsed.fixed_size:
  174. if parsed.overhead_factor or parsed.extra_space:
  175. err = "%s:%d: arguments --overhead-factor and --extra-space not "\
  176. "allowed with argument --fixed-size" \
  177. % (confpath, lineno)
  178. raise KickStartError(err)
  179. else:
  180. # ... and provide defaults if not using
  181. # --fixed-size iff given option was not used
  182. # (again, one cannot tell if option was passed but
  183. # with value equal to 0)
  184. if '--overhead-factor' not in line_args:
  185. parsed.overhead_factor = self.DEFAULT_OVERHEAD_FACTOR
  186. if '--extra-space' not in line_args:
  187. parsed.extra_space = self.DEFAULT_EXTRA_SPACE
  188. self.partnum += 1
  189. self.partitions.append(Partition(parsed, self.partnum))
  190. elif line.startswith('include'):
  191. self._parse(parser, parsed.path)
  192. elif line.startswith('bootloader'):
  193. if not self.bootloader:
  194. self.bootloader = parsed
  195. else:
  196. err = "%s:%d: more than one bootloader specified" \
  197. % (confpath, lineno)
  198. raise KickStartError(err)