partition.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. # ex:ts=4:sw=4:sts=4:et
  2. # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  3. #
  4. # Copyright (c) 2013-2016 Intel Corporation.
  5. # All rights reserved.
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License version 2 as
  9. # published by the Free Software Foundation.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License 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.,
  18. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. #
  20. # DESCRIPTION
  21. # This module provides the OpenEmbedded partition object definitions.
  22. #
  23. # AUTHORS
  24. # Tom Zanussi <tom.zanussi (at] linux.intel.com>
  25. # Ed Bartosh <ed.bartosh> (at] linux.intel.com>
  26. import logging
  27. import os
  28. from wic import WicError
  29. from wic.misc import exec_cmd, exec_native_cmd, get_bitbake_var
  30. from wic.pluginbase import PluginMgr
  31. logger = logging.getLogger('wic')
  32. class Partition():
  33. def __init__(self, args, lineno):
  34. self.args = args
  35. self.active = args.active
  36. self.align = args.align
  37. self.disk = args.disk
  38. self.device = None
  39. self.extra_space = args.extra_space
  40. self.exclude_path = args.exclude_path
  41. self.fsopts = args.fsopts
  42. self.fstype = args.fstype
  43. self.label = args.label
  44. self.mkfs_extraopts = args.mkfs_extraopts
  45. self.mountpoint = args.mountpoint
  46. self.no_table = args.no_table
  47. self.num = None
  48. self.overhead_factor = args.overhead_factor
  49. self.part_name = args.part_name
  50. self.part_type = args.part_type
  51. self.rootfs_dir = args.rootfs_dir
  52. self.size = args.size
  53. self.fixed_size = args.fixed_size
  54. self.source = args.source
  55. self.sourceparams = args.sourceparams
  56. self.system_id = args.system_id
  57. self.use_uuid = args.use_uuid
  58. self.uuid = args.uuid
  59. self.lineno = lineno
  60. self.source_file = ""
  61. self.sourceparams_dict = {}
  62. def get_extra_block_count(self, current_blocks):
  63. """
  64. The --size param is reflected in self.size (in kB), and we already
  65. have current_blocks (1k) blocks, calculate and return the
  66. number of (1k) blocks we need to add to get to --size, 0 if
  67. we're already there or beyond.
  68. """
  69. logger.debug("Requested partition size for %s: %d",
  70. self.mountpoint, self.size)
  71. if not self.size:
  72. return 0
  73. requested_blocks = self.size
  74. logger.debug("Requested blocks %d, current_blocks %d",
  75. requested_blocks, current_blocks)
  76. if requested_blocks > current_blocks:
  77. return requested_blocks - current_blocks
  78. else:
  79. return 0
  80. def get_rootfs_size(self, actual_rootfs_size=0):
  81. """
  82. Calculate the required size of rootfs taking into consideration
  83. --size/--fixed-size flags as well as overhead and extra space, as
  84. specified in kickstart file. Raises an error if the
  85. `actual_rootfs_size` is larger than fixed-size rootfs.
  86. """
  87. if self.fixed_size:
  88. rootfs_size = self.fixed_size
  89. if actual_rootfs_size > rootfs_size:
  90. raise WicError("Actual rootfs size (%d kB) is larger than "
  91. "allowed size %d kB" %
  92. (actual_rootfs_size, rootfs_size))
  93. else:
  94. extra_blocks = self.get_extra_block_count(actual_rootfs_size)
  95. if extra_blocks < self.extra_space:
  96. extra_blocks = self.extra_space
  97. rootfs_size = actual_rootfs_size + extra_blocks
  98. rootfs_size *= self.overhead_factor
  99. logger.debug("Added %d extra blocks to %s to get to %d total blocks",
  100. extra_blocks, self.mountpoint, rootfs_size)
  101. return rootfs_size
  102. @property
  103. def disk_size(self):
  104. """
  105. Obtain on-disk size of partition taking into consideration
  106. --size/--fixed-size options.
  107. """
  108. return self.fixed_size if self.fixed_size else self.size
  109. def prepare(self, creator, cr_workdir, oe_builddir, rootfs_dir,
  110. bootimg_dir, kernel_dir, native_sysroot):
  111. """
  112. Prepare content for individual partitions, depending on
  113. partition command parameters.
  114. """
  115. if not self.source:
  116. if not self.size and not self.fixed_size:
  117. raise WicError("The %s partition has a size of zero. Please "
  118. "specify a non-zero --size/--fixed-size for that "
  119. "partition." % self.mountpoint)
  120. if self.fstype == "swap":
  121. self.prepare_swap_partition(cr_workdir, oe_builddir,
  122. native_sysroot)
  123. self.source_file = "%s/fs.%s" % (cr_workdir, self.fstype)
  124. else:
  125. if self.fstype == 'squashfs':
  126. raise WicError("It's not possible to create empty squashfs "
  127. "partition '%s'" % (self.mountpoint))
  128. rootfs = "%s/fs_%s.%s.%s" % (cr_workdir, self.label,
  129. self.lineno, self.fstype)
  130. if os.path.isfile(rootfs):
  131. os.remove(rootfs)
  132. prefix = "ext" if self.fstype.startswith("ext") else self.fstype
  133. method = getattr(self, "prepare_empty_partition_" + prefix)
  134. method(rootfs, oe_builddir, native_sysroot)
  135. self.source_file = rootfs
  136. return
  137. plugins = PluginMgr.get_plugins('source')
  138. if self.source not in plugins:
  139. raise WicError("The '%s' --source specified for %s doesn't exist.\n\t"
  140. "See 'wic list source-plugins' for a list of available"
  141. " --sources.\n\tSee 'wic help source-plugins' for "
  142. "details on adding a new source plugin." %
  143. (self.source, self.mountpoint))
  144. srcparams_dict = {}
  145. if self.sourceparams:
  146. # Split sourceparams string of the form key1=val1[,key2=val2,...]
  147. # into a dict. Also accepts valueless keys i.e. without =
  148. splitted = self.sourceparams.split(',')
  149. srcparams_dict = dict(par.split('=') for par in splitted if par)
  150. plugin = PluginMgr.get_plugins('source')[self.source]
  151. plugin.do_configure_partition(self, srcparams_dict, creator,
  152. cr_workdir, oe_builddir, bootimg_dir,
  153. kernel_dir, native_sysroot)
  154. plugin.do_stage_partition(self, srcparams_dict, creator,
  155. cr_workdir, oe_builddir, bootimg_dir,
  156. kernel_dir, native_sysroot)
  157. plugin.do_prepare_partition(self, srcparams_dict, creator,
  158. cr_workdir, oe_builddir, bootimg_dir,
  159. kernel_dir, rootfs_dir, native_sysroot)
  160. # further processing required Partition.size to be an integer, make
  161. # sure that it is one
  162. if not isinstance(self.size, int):
  163. raise WicError("Partition %s internal size is not an integer. "
  164. "This a bug in source plugin %s and needs to be fixed." %
  165. (self.mountpoint, self.source))
  166. if self.fixed_size and self.size > self.fixed_size:
  167. raise WicError("File system image of partition %s is "
  168. "larger (%d kB) than its allowed size %d kB" %
  169. (self.mountpoint, self.size, self.fixed_size))
  170. def prepare_rootfs(self, cr_workdir, oe_builddir, rootfs_dir,
  171. native_sysroot):
  172. """
  173. Prepare content for a rootfs partition i.e. create a partition
  174. and fill it from a /rootfs dir.
  175. Currently handles ext2/3/4, btrfs and vfat.
  176. """
  177. p_prefix = os.environ.get("PSEUDO_PREFIX", "%s/usr" % native_sysroot)
  178. p_localstatedir = os.environ.get("PSEUDO_LOCALSTATEDIR",
  179. "%s/../pseudo" % rootfs_dir)
  180. p_passwd = os.environ.get("PSEUDO_PASSWD", rootfs_dir)
  181. p_nosymlinkexp = os.environ.get("PSEUDO_NOSYMLINKEXP", "1")
  182. pseudo = "export PSEUDO_PREFIX=%s;" % p_prefix
  183. pseudo += "export PSEUDO_LOCALSTATEDIR=%s;" % p_localstatedir
  184. pseudo += "export PSEUDO_PASSWD=%s;" % p_passwd
  185. pseudo += "export PSEUDO_NOSYMLINKEXP=%s;" % p_nosymlinkexp
  186. pseudo += "%s " % get_bitbake_var("FAKEROOTCMD")
  187. rootfs = "%s/rootfs_%s.%s.%s" % (cr_workdir, self.label,
  188. self.lineno, self.fstype)
  189. if os.path.isfile(rootfs):
  190. os.remove(rootfs)
  191. # Get rootfs size from bitbake variable if it's not set in .ks file
  192. if not self.size:
  193. # Bitbake variable ROOTFS_SIZE is calculated in
  194. # Image._get_rootfs_size method from meta/lib/oe/image.py
  195. # using IMAGE_ROOTFS_SIZE, IMAGE_ROOTFS_ALIGNMENT,
  196. # IMAGE_OVERHEAD_FACTOR and IMAGE_ROOTFS_EXTRA_SPACE
  197. rsize_bb = get_bitbake_var('ROOTFS_SIZE')
  198. if rsize_bb:
  199. logger.warning('overhead-factor was specified, but size was not,'
  200. ' so bitbake variables will be used for the size.'
  201. ' In this case both IMAGE_OVERHEAD_FACTOR and '
  202. '--overhead-factor will be applied')
  203. self.size = int(round(float(rsize_bb)))
  204. prefix = "ext" if self.fstype.startswith("ext") else self.fstype
  205. method = getattr(self, "prepare_rootfs_" + prefix)
  206. method(rootfs, oe_builddir, rootfs_dir, native_sysroot, pseudo)
  207. self.source_file = rootfs
  208. # get the rootfs size in the right units for kickstart (kB)
  209. du_cmd = "du -Lbks %s" % rootfs
  210. out = exec_cmd(du_cmd)
  211. self.size = int(out.split()[0])
  212. def prepare_rootfs_ext(self, rootfs, oe_builddir, rootfs_dir,
  213. native_sysroot, pseudo):
  214. """
  215. Prepare content for an ext2/3/4 rootfs partition.
  216. """
  217. du_cmd = "du -ks %s" % rootfs_dir
  218. out = exec_cmd(du_cmd)
  219. actual_rootfs_size = int(out.split()[0])
  220. rootfs_size = self.get_rootfs_size(actual_rootfs_size)
  221. with open(rootfs, 'w') as sparse:
  222. os.ftruncate(sparse.fileno(), rootfs_size * 1024)
  223. extraopts = self.mkfs_extraopts or "-F -i 8192"
  224. label_str = ""
  225. if self.label:
  226. label_str = "-L %s" % self.label
  227. mkfs_cmd = "mkfs.%s %s %s %s -d %s" % \
  228. (self.fstype, extraopts, rootfs, label_str, rootfs_dir)
  229. exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
  230. mkfs_cmd = "fsck.%s -pvfD %s" % (self.fstype, rootfs)
  231. exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
  232. def prepare_rootfs_btrfs(self, rootfs, oe_builddir, rootfs_dir,
  233. native_sysroot, pseudo):
  234. """
  235. Prepare content for a btrfs rootfs partition.
  236. Currently handles ext2/3/4 and btrfs.
  237. """
  238. du_cmd = "du -ks %s" % rootfs_dir
  239. out = exec_cmd(du_cmd)
  240. actual_rootfs_size = int(out.split()[0])
  241. rootfs_size = self.get_rootfs_size(actual_rootfs_size)
  242. with open(rootfs, 'w') as sparse:
  243. os.ftruncate(sparse.fileno(), rootfs_size * 1024)
  244. label_str = ""
  245. if self.label:
  246. label_str = "-L %s" % self.label
  247. mkfs_cmd = "mkfs.%s -b %d -r %s %s %s %s" % \
  248. (self.fstype, rootfs_size * 1024, rootfs_dir, label_str,
  249. self.mkfs_extraopts, rootfs)
  250. exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
  251. def prepare_rootfs_msdos(self, rootfs, oe_builddir, rootfs_dir,
  252. native_sysroot, pseudo):
  253. """
  254. Prepare content for a msdos/vfat rootfs partition.
  255. """
  256. du_cmd = "du -bks %s" % rootfs_dir
  257. out = exec_cmd(du_cmd)
  258. blocks = int(out.split()[0])
  259. rootfs_size = self.get_rootfs_size(blocks)
  260. label_str = "-n boot"
  261. if self.label:
  262. label_str = "-n %s" % self.label
  263. size_str = ""
  264. if self.fstype == 'msdos':
  265. size_str = "-F 16" # FAT 16
  266. extraopts = self.mkfs_extraopts or '-S 512'
  267. dosfs_cmd = "mkdosfs %s %s %s -C %s %d" % \
  268. (label_str, size_str, extraopts, rootfs, rootfs_size)
  269. exec_native_cmd(dosfs_cmd, native_sysroot)
  270. mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (rootfs, rootfs_dir)
  271. exec_native_cmd(mcopy_cmd, native_sysroot)
  272. chmod_cmd = "chmod 644 %s" % rootfs
  273. exec_cmd(chmod_cmd)
  274. prepare_rootfs_vfat = prepare_rootfs_msdos
  275. def prepare_rootfs_squashfs(self, rootfs, oe_builddir, rootfs_dir,
  276. native_sysroot, pseudo):
  277. """
  278. Prepare content for a squashfs rootfs partition.
  279. """
  280. extraopts = self.mkfs_extraopts or '-noappend'
  281. squashfs_cmd = "mksquashfs %s %s %s" % \
  282. (rootfs_dir, rootfs, extraopts)
  283. exec_native_cmd(squashfs_cmd, native_sysroot, pseudo=pseudo)
  284. def prepare_empty_partition_ext(self, rootfs, oe_builddir,
  285. native_sysroot):
  286. """
  287. Prepare an empty ext2/3/4 partition.
  288. """
  289. size = self.disk_size
  290. with open(rootfs, 'w') as sparse:
  291. os.ftruncate(sparse.fileno(), size * 1024)
  292. extraopts = self.mkfs_extraopts or "-i 8192"
  293. label_str = ""
  294. if self.label:
  295. label_str = "-L %s" % self.label
  296. mkfs_cmd = "mkfs.%s -F %s %s %s" % \
  297. (self.fstype, extraopts, label_str, rootfs)
  298. exec_native_cmd(mkfs_cmd, native_sysroot)
  299. def prepare_empty_partition_btrfs(self, rootfs, oe_builddir,
  300. native_sysroot):
  301. """
  302. Prepare an empty btrfs partition.
  303. """
  304. size = self.disk_size
  305. with open(rootfs, 'w') as sparse:
  306. os.ftruncate(sparse.fileno(), size * 1024)
  307. label_str = ""
  308. if self.label:
  309. label_str = "-L %s" % self.label
  310. mkfs_cmd = "mkfs.%s -b %d %s %s %s" % \
  311. (self.fstype, self.size * 1024, label_str,
  312. self.mkfs_extraopts, rootfs)
  313. exec_native_cmd(mkfs_cmd, native_sysroot)
  314. def prepare_empty_partition_msdos(self, rootfs, oe_builddir,
  315. native_sysroot):
  316. """
  317. Prepare an empty vfat partition.
  318. """
  319. blocks = self.disk_size
  320. label_str = "-n boot"
  321. if self.label:
  322. label_str = "-n %s" % self.label
  323. size_str = ""
  324. if self.fstype == 'msdos':
  325. size_str = "-F 16" # FAT 16
  326. extraopts = self.mkfs_extraopts or '-S 512'
  327. dosfs_cmd = "mkdosfs %s %s %s -C %s %d" % \
  328. (label_str, extraopts, size_str, rootfs, blocks)
  329. exec_native_cmd(dosfs_cmd, native_sysroot)
  330. chmod_cmd = "chmod 644 %s" % rootfs
  331. exec_cmd(chmod_cmd)
  332. prepare_empty_partition_vfat = prepare_empty_partition_msdos
  333. def prepare_swap_partition(self, cr_workdir, oe_builddir, native_sysroot):
  334. """
  335. Prepare a swap partition.
  336. """
  337. path = "%s/fs.%s" % (cr_workdir, self.fstype)
  338. with open(path, 'w') as sparse:
  339. os.ftruncate(sparse.fileno(), self.size * 1024)
  340. import uuid
  341. label_str = ""
  342. if self.label:
  343. label_str = "-L %s" % self.label
  344. mkswap_cmd = "mkswap %s -U %s %s" % (label_str, str(uuid.uuid1()), path)
  345. exec_native_cmd(mkswap_cmd, native_sysroot)