partition.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. #
  2. # Copyright (c) 2013-2016 Intel Corporation.
  3. #
  4. # SPDX-License-Identifier: GPL-2.0-only
  5. #
  6. # DESCRIPTION
  7. # This module provides the OpenEmbedded partition object definitions.
  8. #
  9. # AUTHORS
  10. # Tom Zanussi <tom.zanussi (at] linux.intel.com>
  11. # Ed Bartosh <ed.bartosh> (at] linux.intel.com>
  12. import logging
  13. import os
  14. import uuid
  15. from wic import WicError
  16. from wic.misc import exec_cmd, exec_native_cmd, get_bitbake_var
  17. from wic.pluginbase import PluginMgr
  18. logger = logging.getLogger('wic')
  19. class Partition():
  20. def __init__(self, args, lineno):
  21. self.args = args
  22. self.active = args.active
  23. self.align = args.align
  24. self.disk = args.disk
  25. self.device = None
  26. self.extra_space = args.extra_space
  27. self.exclude_path = args.exclude_path
  28. self.include_path = args.include_path
  29. self.change_directory = args.change_directory
  30. self.fsopts = args.fsopts
  31. self.fstype = args.fstype
  32. self.label = args.label
  33. self.use_label = args.use_label
  34. self.mkfs_extraopts = args.mkfs_extraopts
  35. self.mountpoint = args.mountpoint
  36. self.no_table = args.no_table
  37. self.num = None
  38. self.offset = args.offset
  39. self.overhead_factor = args.overhead_factor
  40. self.part_name = args.part_name
  41. self.part_type = args.part_type
  42. self.rootfs_dir = args.rootfs_dir
  43. self.size = args.size
  44. self.fixed_size = args.fixed_size
  45. self.source = args.source
  46. self.sourceparams = args.sourceparams
  47. self.system_id = args.system_id
  48. self.use_uuid = args.use_uuid
  49. self.uuid = args.uuid
  50. self.fsuuid = args.fsuuid
  51. self.type = args.type
  52. self.lineno = lineno
  53. self.source_file = ""
  54. def get_extra_block_count(self, current_blocks):
  55. """
  56. The --size param is reflected in self.size (in kB), and we already
  57. have current_blocks (1k) blocks, calculate and return the
  58. number of (1k) blocks we need to add to get to --size, 0 if
  59. we're already there or beyond.
  60. """
  61. logger.debug("Requested partition size for %s: %d",
  62. self.mountpoint, self.size)
  63. if not self.size:
  64. return 0
  65. requested_blocks = self.size
  66. logger.debug("Requested blocks %d, current_blocks %d",
  67. requested_blocks, current_blocks)
  68. if requested_blocks > current_blocks:
  69. return requested_blocks - current_blocks
  70. else:
  71. return 0
  72. def get_rootfs_size(self, actual_rootfs_size=0):
  73. """
  74. Calculate the required size of rootfs taking into consideration
  75. --size/--fixed-size flags as well as overhead and extra space, as
  76. specified in kickstart file. Raises an error if the
  77. `actual_rootfs_size` is larger than fixed-size rootfs.
  78. """
  79. if self.fixed_size:
  80. rootfs_size = self.fixed_size
  81. if actual_rootfs_size > rootfs_size:
  82. raise WicError("Actual rootfs size (%d kB) is larger than "
  83. "allowed size %d kB" %
  84. (actual_rootfs_size, rootfs_size))
  85. else:
  86. extra_blocks = self.get_extra_block_count(actual_rootfs_size)
  87. if extra_blocks < self.extra_space:
  88. extra_blocks = self.extra_space
  89. rootfs_size = actual_rootfs_size + extra_blocks
  90. rootfs_size *= self.overhead_factor
  91. logger.debug("Added %d extra blocks to %s to get to %d total blocks",
  92. extra_blocks, self.mountpoint, rootfs_size)
  93. return rootfs_size
  94. @property
  95. def disk_size(self):
  96. """
  97. Obtain on-disk size of partition taking into consideration
  98. --size/--fixed-size options.
  99. """
  100. return self.fixed_size if self.fixed_size else self.size
  101. def prepare(self, creator, cr_workdir, oe_builddir, rootfs_dir,
  102. bootimg_dir, kernel_dir, native_sysroot):
  103. """
  104. Prepare content for individual partitions, depending on
  105. partition command parameters.
  106. """
  107. if not self.source:
  108. if not self.size and not self.fixed_size:
  109. raise WicError("The %s partition has a size of zero. Please "
  110. "specify a non-zero --size/--fixed-size for that "
  111. "partition." % self.mountpoint)
  112. if self.fstype == "swap":
  113. self.prepare_swap_partition(cr_workdir, oe_builddir,
  114. native_sysroot)
  115. self.source_file = "%s/fs.%s" % (cr_workdir, self.fstype)
  116. else:
  117. if self.fstype == 'squashfs':
  118. raise WicError("It's not possible to create empty squashfs "
  119. "partition '%s'" % (self.mountpoint))
  120. rootfs = "%s/fs_%s.%s.%s" % (cr_workdir, self.label,
  121. self.lineno, self.fstype)
  122. if os.path.isfile(rootfs):
  123. os.remove(rootfs)
  124. prefix = "ext" if self.fstype.startswith("ext") else self.fstype
  125. method = getattr(self, "prepare_empty_partition_" + prefix)
  126. method(rootfs, oe_builddir, native_sysroot)
  127. self.source_file = rootfs
  128. return
  129. plugins = PluginMgr.get_plugins('source')
  130. if self.source not in plugins:
  131. raise WicError("The '%s' --source specified for %s doesn't exist.\n\t"
  132. "See 'wic list source-plugins' for a list of available"
  133. " --sources.\n\tSee 'wic help source-plugins' for "
  134. "details on adding a new source plugin." %
  135. (self.source, self.mountpoint))
  136. srcparams_dict = {}
  137. if self.sourceparams:
  138. # Split sourceparams string of the form key1=val1[,key2=val2,...]
  139. # into a dict. Also accepts valueless keys i.e. without =
  140. splitted = self.sourceparams.split(',')
  141. srcparams_dict = dict(par.split('=', 1) for par in splitted if par)
  142. plugin = PluginMgr.get_plugins('source')[self.source]
  143. plugin.do_configure_partition(self, srcparams_dict, creator,
  144. cr_workdir, oe_builddir, bootimg_dir,
  145. kernel_dir, native_sysroot)
  146. plugin.do_stage_partition(self, srcparams_dict, creator,
  147. cr_workdir, oe_builddir, bootimg_dir,
  148. kernel_dir, native_sysroot)
  149. plugin.do_prepare_partition(self, srcparams_dict, creator,
  150. cr_workdir, oe_builddir, bootimg_dir,
  151. kernel_dir, rootfs_dir, native_sysroot)
  152. plugin.do_post_partition(self, srcparams_dict, creator,
  153. cr_workdir, oe_builddir, bootimg_dir,
  154. kernel_dir, rootfs_dir, native_sysroot)
  155. # further processing required Partition.size to be an integer, make
  156. # sure that it is one
  157. if not isinstance(self.size, int):
  158. raise WicError("Partition %s internal size is not an integer. "
  159. "This a bug in source plugin %s and needs to be fixed." %
  160. (self.mountpoint, self.source))
  161. if self.fixed_size and self.size > self.fixed_size:
  162. raise WicError("File system image of partition %s is "
  163. "larger (%d kB) than its allowed size %d kB" %
  164. (self.mountpoint, self.size, self.fixed_size))
  165. def prepare_rootfs(self, cr_workdir, oe_builddir, rootfs_dir,
  166. native_sysroot, real_rootfs = True, pseudo_dir = None):
  167. """
  168. Prepare content for a rootfs partition i.e. create a partition
  169. and fill it from a /rootfs dir.
  170. Currently handles ext2/3/4, btrfs, vfat and squashfs.
  171. """
  172. rootfs = "%s/rootfs_%s.%s.%s" % (cr_workdir, self.label,
  173. self.lineno, self.fstype)
  174. if os.path.isfile(rootfs):
  175. os.remove(rootfs)
  176. p_prefix = os.environ.get("PSEUDO_PREFIX", "%s/usr" % native_sysroot)
  177. if (pseudo_dir):
  178. pseudo = "export PSEUDO_PREFIX=%s;" % p_prefix
  179. pseudo += "export PSEUDO_LOCALSTATEDIR=%s;" % pseudo_dir
  180. pseudo += "export PSEUDO_PASSWD=%s;" % rootfs_dir
  181. pseudo += "export PSEUDO_NOSYMLINKEXP=1;"
  182. pseudo += "export PSEUDO_IGNORE_PATHS=%s;" % (rootfs + "," + (get_bitbake_var("PSEUDO_IGNORE_PATHS") or ""))
  183. pseudo += "%s " % get_bitbake_var("FAKEROOTCMD")
  184. else:
  185. pseudo = None
  186. if not self.size and real_rootfs:
  187. # The rootfs size is not set in .ks file so try to get it
  188. # from bitbake variable
  189. rsize_bb = get_bitbake_var('ROOTFS_SIZE')
  190. rdir = get_bitbake_var('IMAGE_ROOTFS')
  191. if rsize_bb and rdir == rootfs_dir:
  192. # Bitbake variable ROOTFS_SIZE is calculated in
  193. # Image._get_rootfs_size method from meta/lib/oe/image.py
  194. # using IMAGE_ROOTFS_SIZE, IMAGE_ROOTFS_ALIGNMENT,
  195. # IMAGE_OVERHEAD_FACTOR and IMAGE_ROOTFS_EXTRA_SPACE
  196. self.size = int(round(float(rsize_bb)))
  197. else:
  198. # Bitbake variable ROOTFS_SIZE is not defined so compute it
  199. # from the rootfs_dir size using the same logic found in
  200. # get_rootfs_size() from meta/classes/image.bbclass
  201. du_cmd = "du -ks %s" % rootfs_dir
  202. out = exec_cmd(du_cmd)
  203. self.size = int(out.split()[0])
  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 -U %s -d %s" % \
  228. (self.fstype, extraopts, rootfs, label_str, self.fsuuid, 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. """
  237. du_cmd = "du -ks %s" % rootfs_dir
  238. out = exec_cmd(du_cmd)
  239. actual_rootfs_size = int(out.split()[0])
  240. rootfs_size = self.get_rootfs_size(actual_rootfs_size)
  241. with open(rootfs, 'w') as sparse:
  242. os.ftruncate(sparse.fileno(), rootfs_size * 1024)
  243. label_str = ""
  244. if self.label:
  245. label_str = "-L %s" % self.label
  246. mkfs_cmd = "mkfs.%s -b %d -r %s %s %s -U %s %s" % \
  247. (self.fstype, rootfs_size * 1024, rootfs_dir, label_str,
  248. self.mkfs_extraopts, self.fsuuid, rootfs)
  249. exec_native_cmd(mkfs_cmd, native_sysroot, pseudo=pseudo)
  250. def prepare_rootfs_msdos(self, rootfs, oe_builddir, rootfs_dir,
  251. native_sysroot, pseudo):
  252. """
  253. Prepare content for a msdos/vfat rootfs partition.
  254. """
  255. du_cmd = "du -bks %s" % rootfs_dir
  256. out = exec_cmd(du_cmd)
  257. blocks = int(out.split()[0])
  258. rootfs_size = self.get_rootfs_size(blocks)
  259. label_str = "-n boot"
  260. if self.label:
  261. label_str = "-n %s" % self.label
  262. size_str = ""
  263. if self.fstype == 'msdos':
  264. size_str = "-F 16" # FAT 16
  265. extraopts = self.mkfs_extraopts or '-S 512'
  266. dosfs_cmd = "mkdosfs %s -i %s %s %s -C %s %d" % \
  267. (label_str, self.fsuuid, size_str, extraopts, rootfs,
  268. 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 -U %s %s" % \
  297. (self.fstype, extraopts, label_str, self.fsuuid, 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 -U %s %s %s" % \
  311. (self.fstype, self.size * 1024, label_str, self.fsuuid,
  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 -i %s %s %s -C %s %d" % \
  328. (label_str, self.fsuuid, extraopts, size_str, rootfs,
  329. blocks)
  330. exec_native_cmd(dosfs_cmd, native_sysroot)
  331. chmod_cmd = "chmod 644 %s" % rootfs
  332. exec_cmd(chmod_cmd)
  333. prepare_empty_partition_vfat = prepare_empty_partition_msdos
  334. def prepare_swap_partition(self, cr_workdir, oe_builddir, native_sysroot):
  335. """
  336. Prepare a swap partition.
  337. """
  338. path = "%s/fs.%s" % (cr_workdir, self.fstype)
  339. with open(path, 'w') as sparse:
  340. os.ftruncate(sparse.fileno(), self.size * 1024)
  341. label_str = ""
  342. if self.label:
  343. label_str = "-L %s" % self.label
  344. mkswap_cmd = "mkswap %s -U %s %s" % (label_str, self.fsuuid, path)
  345. exec_native_cmd(mkswap_cmd, native_sysroot)