image.bbclass 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. # This software is a part of ISAR.
  2. # Copyright (C) 2015-2017 ilbers GmbH
  3. # Replace possible multiple spaces with single underscores
  4. IMAGE_SUFFIX = "${@'_'.join(d.getVar("IMAGE_FSTYPES", True).split())}"
  5. # Make workdir and stamps machine-specific without changing common PN target
  6. WORKDIR = "${TMPDIR}/work/${DISTRO}-${DISTRO_ARCH}/${PN}-${MACHINE}-${IMAGE_SUFFIX}/${PV}-${PR}"
  7. STAMP = "${STAMPS_DIR}/${DISTRO}-${DISTRO_ARCH}/${PN}-${MACHINE}-${IMAGE_SUFFIX}/${PV}-${PR}"
  8. STAMPCLEAN = "${STAMPS_DIR}/${DISTRO}-${DISTRO_ARCH}/${PN}-${MACHINE}-${IMAGE_SUFFIX}/*-*"
  9. # Sstate also needs to be machine-specific
  10. SSTATE_MANIFESTS = "${TMPDIR}/sstate-control/${MACHINE}-${DISTRO}-${DISTRO_ARCH}-${IMAGE_SUFFIX}"
  11. IMAGE_INSTALL ?= ""
  12. IMAGE_FSTYPES ?= "${@ d.getVar("IMAGE_TYPE", True) if d.getVar("IMAGE_TYPE", True) else "ext4-img"}"
  13. IMAGE_ROOTFS ?= "${WORKDIR}/rootfs"
  14. IMAGE_INSTALL += "${@ ("linux-image-" + d.getVar("KERNEL_NAME", True)) if d.getVar("KERNEL_NAME", True) else ""}"
  15. # Name of the image including distro&machine names
  16. IMAGE_FULLNAME = "${PN}-${DISTRO}-${MACHINE}"
  17. # These variables are used by wic and start_vm
  18. KERNEL_IMAGE ?= "${IMAGE_FULLNAME}-${KERNEL_FILE}"
  19. INITRD_IMAGE ?= "${IMAGE_FULLNAME}-initrd.img"
  20. # This defines the deployed dtbs for reuse by imagers
  21. DTB_FILES ?= ""
  22. # Useful variables for imager implementations:
  23. PP = "/home/builder/${PN}-${IMAGE_SUFFIX}"
  24. PP_DEPLOY = "${PP}/deploy"
  25. PP_ROOTFS = "${PP}/rootfs"
  26. PP_WORK = "${PP}/work"
  27. BUILDROOT = "${BUILDCHROOT_DIR}${PP}"
  28. BUILDROOT_DEPLOY = "${BUILDCHROOT_DIR}${PP_DEPLOY}"
  29. BUILDROOT_ROOTFS = "${BUILDCHROOT_DIR}${PP_ROOTFS}"
  30. BUILDROOT_WORK = "${BUILDCHROOT_DIR}${PP_WORK}"
  31. python(){
  32. if (d.getVar('IMAGE_TRANSIENT_PACKAGES')):
  33. bb.warn("IMAGE_TRANSIENT_PACKAGES is set and no longer supported")
  34. if (d.getVar('IMAGE_TYPE')):
  35. bb.warn("IMAGE_TYPE is deprecated, please switch to IMAGE_FSTYPES")
  36. }
  37. def cfg_script(d):
  38. cf = d.getVar('DISTRO_CONFIG_SCRIPT', True) or ''
  39. if cf:
  40. return 'file://' + cf
  41. return ''
  42. FILESPATH =. "${LAYERDIR_core}/conf/distro:"
  43. SRC_URI += "${@ cfg_script(d) }"
  44. DEPENDS += "${IMAGE_INSTALL}"
  45. ISAR_RELEASE_CMD_DEFAULT = "git -C ${LAYERDIR_core} describe --tags --dirty --match 'v[0-9].[0-9]*'"
  46. ISAR_RELEASE_CMD ?= "${ISAR_RELEASE_CMD_DEFAULT}"
  47. image_do_mounts() {
  48. sudo flock ${MOUNT_LOCKFILE} -c ' \
  49. mkdir -p "${BUILDROOT_DEPLOY}" "${BUILDROOT_ROOTFS}" "${BUILDROOT_WORK}"
  50. mount --bind "${DEPLOY_DIR_IMAGE}" "${BUILDROOT_DEPLOY}"
  51. mount --bind "${IMAGE_ROOTFS}" "${BUILDROOT_ROOTFS}"
  52. mount --bind "${WORKDIR}" "${BUILDROOT_WORK}"
  53. '
  54. buildchroot_do_mounts
  55. }
  56. ROOTFSDIR = "${IMAGE_ROOTFS}"
  57. ROOTFS_FEATURES += "clean-package-cache generate-manifest export-dpkg-status clean-log-files"
  58. ROOTFS_PACKAGES += "${IMAGE_PREINSTALL} ${IMAGE_INSTALL}"
  59. ROOTFS_MANIFEST_DEPLOY_DIR ?= "${DEPLOY_DIR_IMAGE}"
  60. ROOTFS_DPKGSTATUS_DEPLOY_DIR ?= "${DEPLOY_DIR_IMAGE}"
  61. ROOTFS_PACKAGE_SUFFIX ?= "${PN}-${DISTRO}-${MACHINE}"
  62. ROOTFS_POSTPROCESS_COMMAND_prepend = "${@bb.utils.contains('BASE_REPO_FEATURES', 'cache-deb-src', 'cache_deb_src', '', d)} "
  63. inherit rootfs
  64. inherit image-sdk-extension
  65. inherit image-tools-extension
  66. inherit image-postproc-extension
  67. inherit image-locales-extension
  68. inherit image-account-extension
  69. inherit image-container-extension
  70. # Extra space for rootfs in MB
  71. ROOTFS_EXTRA ?= "64"
  72. def get_rootfs_size(d):
  73. import subprocess
  74. rootfs_extra = int(d.getVar("ROOTFS_EXTRA", True))
  75. output = subprocess.check_output(
  76. ["sudo", "du", "-xs", "--block-size=1k", d.getVar("IMAGE_ROOTFS", True)]
  77. )
  78. base_size = int(output.split()[0])
  79. return base_size + rootfs_extra * 1024
  80. # here we call a command that should describe your whole build system,
  81. # this could be "git describe" or something similar.
  82. # set ISAR_RELEASE_CMD to customize, or override do_mark_rootfs to do something
  83. # completely different
  84. get_build_id() {
  85. if [ $(echo ${BBLAYERS} | wc -w) -ne 2 ] &&
  86. [ "${ISAR_RELEASE_CMD}" = "${ISAR_RELEASE_CMD_DEFAULT}" ]; then
  87. bbwarn "You are using external layers that will not be" \
  88. "considered in the build_id. Consider changing" \
  89. "ISAR_RELEASE_CMD."
  90. fi
  91. if ! ( ${ISAR_RELEASE_CMD} ) 2>/dev/null; then
  92. bbwarn "\"${ISAR_RELEASE_CMD}\" failed, returning empty build_id."
  93. echo ""
  94. fi
  95. }
  96. python set_image_size () {
  97. rootfs_size = get_rootfs_size(d)
  98. d.setVar('ROOTFS_SIZE', str(rootfs_size))
  99. d.setVarFlag('ROOTFS_SIZE', 'export', '1')
  100. }
  101. ROOTFS_CONFIGURE_COMMAND += "image_configure_fstab"
  102. image_configure_fstab[weight] = "2"
  103. image_configure_fstab() {
  104. sudo tee '${IMAGE_ROOTFS}/etc/fstab' << EOF
  105. # Begin /etc/fstab
  106. /dev/root / auto defaults 0 0
  107. proc /proc proc nosuid,noexec,nodev 0 0
  108. sysfs /sys sysfs nosuid,noexec,nodev 0 0
  109. devpts /dev/pts devpts gid=5,mode=620 0 0
  110. tmpfs /run tmpfs defaults 0 0
  111. devtmpfs /dev devtmpfs mode=0755,nosuid 0 0
  112. # End /etc/fstab
  113. EOF
  114. }
  115. do_copy_boot_files[dirs] = "${DEPLOY_DIR_IMAGE}"
  116. do_copy_boot_files[lockfiles] += "${DEPLOY_DIR_IMAGE}/isar.lock"
  117. do_copy_boot_files() {
  118. kernel="$(realpath -q '${IMAGE_ROOTFS}'/vmlinu[xz])"
  119. if [ ! -f "$kernel" ]; then
  120. kernel="$(realpath -q '${IMAGE_ROOTFS}'/boot/vmlinu[xz])"
  121. fi
  122. if [ -f "$kernel" ]; then
  123. sudo cat "$kernel" > "${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGE}"
  124. fi
  125. initrd="$(realpath -q '${IMAGE_ROOTFS}/initrd.img')"
  126. if [ ! -f "$initrd" ]; then
  127. initrd="$(realpath -q '${IMAGE_ROOTFS}/boot/initrd.img')"
  128. fi
  129. if [ -f "$initrd" ]; then
  130. cp -f "$initrd" '${DEPLOY_DIR_IMAGE}/${INITRD_IMAGE}'
  131. fi
  132. for file in ${DTB_FILES}; do
  133. dtb="$(find '${IMAGE_ROOTFS}/usr/lib' -type f \
  134. -iwholename '*linux-image-*/'${file} | head -1)"
  135. if [ -z "$dtb" -o ! -e "$dtb" ]; then
  136. die "${file} not found"
  137. fi
  138. cp -f "$dtb" "${DEPLOY_DIR_IMAGE}/"
  139. done
  140. }
  141. addtask copy_boot_files before do_rootfs_postprocess after do_rootfs_install
  142. python do_image_tools() {
  143. """Virtual task"""
  144. pass
  145. }
  146. addtask image_tools before do_build after do_rootfs
  147. python do_image() {
  148. """Virtual task"""
  149. pass
  150. }
  151. addtask image before do_build after do_image_tools
  152. python do_deploy() {
  153. """Virtual task"""
  154. pass
  155. }
  156. addtask deploy before do_build after do_image
  157. do_rootfs_finalize() {
  158. sudo -s <<'EOSUDO'
  159. set -e
  160. test -e "${ROOTFSDIR}/chroot-setup.sh" && \
  161. "${ROOTFSDIR}/chroot-setup.sh" "cleanup" "${ROOTFSDIR}"
  162. rm -f "${ROOTFSDIR}/chroot-setup.sh"
  163. test ! -e "${ROOTFSDIR}/usr/share/doc/qemu-user-static" && \
  164. find "${ROOTFSDIR}/usr/bin" \
  165. -maxdepth 1 -name 'qemu-*-static' -type f -delete
  166. mountpoint -q '${ROOTFSDIR}/isar-apt' && \
  167. umount -l ${ROOTFSDIR}/isar-apt && \
  168. rmdir --ignore-fail-on-non-empty ${ROOTFSDIR}/isar-apt
  169. mountpoint -q '${ROOTFSDIR}/base-apt' && \
  170. umount -l ${ROOTFSDIR}/base-apt && \
  171. rmdir --ignore-fail-on-non-empty ${ROOTFSDIR}/base-apt
  172. mountpoint -q '${ROOTFSDIR}/dev' && \
  173. umount -l ${ROOTFSDIR}/dev
  174. mountpoint -q '${ROOTFSDIR}/proc' && \
  175. umount -l ${ROOTFSDIR}/proc
  176. mountpoint -q '${ROOTFSDIR}/sys' && \
  177. umount -l ${ROOTFSDIR}/sys
  178. rm -f "${ROOTFSDIR}/etc/apt/apt.conf.d/55isar-fallback.conf"
  179. rm -f "${ROOTFSDIR}/etc/apt/sources.list.d/isar-apt.list"
  180. rm -f "${ROOTFSDIR}/etc/apt/preferences.d/isar-apt"
  181. rm -f "${ROOTFSDIR}/etc/apt/sources.list.d/base-apt.list"
  182. mv "${ROOTFSDIR}/etc/apt/sources-list" \
  183. "${ROOTFSDIR}/etc/apt/sources.list.d/bootstrap.list"
  184. rm -f "${ROOTFSDIR}/etc/apt/sources-list"
  185. EOSUDO
  186. }
  187. addtask rootfs_finalize before do_rootfs after do_rootfs_postprocess
  188. # Last so that the image type can overwrite tasks if needed
  189. inherit ${IMAGE_FSTYPES}