artifact-rootfs.sh 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/usr/bin/env bash
  2. #
  3. # SPDX-License-Identifier: GPL-2.0
  4. #
  5. # Copyright (c) 2013-2023 Igor Pecovnik, igor@armbian.com
  6. #
  7. # This file is a part of the Armbian Build Framework
  8. # https://github.com/armbian/build/
  9. function artifact_rootfs_config_dump() {
  10. artifact_input_variables[ARCH]="${ARCH}"
  11. artifact_input_variables[RELEASE]="${RELEASE}"
  12. artifact_input_variables[SELECTED_CONFIGURATION]="${SELECTED_CONFIGURATION}" # should be represented below anyway
  13. artifact_input_variables[BUILD_MINIMAL]="${BUILD_MINIMAL}"
  14. artifact_input_variables[DESKTOP_ENVIRONMENT]="${DESKTOP_ENVIRONMENT:-"no_DESKTOP_ENVIRONMENT_set"}"
  15. artifact_input_variables[DESKTOP_ENVIRONMENT_CONFIG_NAME]="${DESKTOP_ENVIRONMENT_CONFIG_NAME:-"no_DESKTOP_ENVIRONMENT_CONFIG_NAME_set"}"
  16. artifact_input_variables[DESKTOP_APPGROUPS_SELECTED]="${DESKTOP_APPGROUPS_SELECTED:-"no_DESKTOP_APPGROUPS_SELECTED_set"}"
  17. # Hash of the packages added/removed by extensions
  18. declare pkgs_hash="undetermined"
  19. pkgs_hash="$(echo "${REMOVE_PACKAGES[*]} ${EXTRA_PACKAGES_ROOTFS[*]}" | sha256sum | cut -d' ' -f1)"
  20. artifact_input_variables[EXTRA_PKG_ADD_REMOVE_HASH]="${pkgs_hash}"
  21. }
  22. function artifact_rootfs_prepare_version() {
  23. artifact_version="undetermined" # outer scope
  24. artifact_version_reason="undetermined" # outer scope
  25. [[ -z "${artifact_prefix_version}" ]] && exit_with_error "artifact_prefix_version is not set"
  26. assert_requires_aggregation # Bombs if aggregation has not run
  27. declare -g rootfs_cache_id="none_yet"
  28. calculate_rootfs_cache_id # sets rootfs_cache_id
  29. display_alert "Going to build rootfs" "packages_hash: '${packages_hash:-}' cache_type: '${cache_type:-}' rootfs_cache_id: '${rootfs_cache_id}'" "info"
  30. declare -a reasons=(
  31. "arch \"${ARCH}\""
  32. "release \"${RELEASE}\""
  33. "type \"${cache_type}\""
  34. "cache_id \"${rootfs_cache_id}\""
  35. )
  36. # rootfs does NOT include ${artifact_prefix_version} -- there's no reason to, since rootfs is not in an apt repo
  37. # instead, we use YYYYMM to make a new rootfs cache version per-month, even if nothing else changes.
  38. declare yyyymm="undetermined"
  39. yyyymm="$(date +%Y%m)"
  40. # outer scope
  41. artifact_version="${yyyymm}-${rootfs_cache_id}"
  42. artifact_version_reason="${reasons[*]}"
  43. artifact_name="rootfs-${ARCH}-${RELEASE}-${cache_type}"
  44. artifact_type="tar.zst"
  45. artifact_base_dir="${SRC}/cache/rootfs"
  46. artifact_final_file="${SRC}/cache/rootfs/${artifact_name}_${artifact_version}.tar.zst"
  47. return 0
  48. }
  49. function artifact_rootfs_build_from_sources() {
  50. debug_var artifact_final_file
  51. debug_var artifact_final_file_basename
  52. # Creates a cleanup handler 'trap_handler_cleanup_rootfs_and_image'
  53. LOG_SECTION="prepare_rootfs_build_params_and_trap" do_with_logging prepare_rootfs_build_params_and_trap
  54. debug_var artifact_final_file
  55. debug_var artifact_final_file_basename
  56. # validate that tmpfs_estimated_size is set and higher than zero, or exit_with_error
  57. [[ -z ${tmpfs_estimated_size} ]] && exit_with_error "tmpfs_estimated_size is not set"
  58. [[ ${tmpfs_estimated_size} -le 0 ]] && exit_with_error "tmpfs_estimated_size is not higher than zero"
  59. # "rootfs" CLI skips over a lot goes straight to create the rootfs. It doesn't check cache etc.
  60. LOG_SECTION="create_new_rootfs_cache" do_with_logging create_new_rootfs_cache
  61. debug_var artifact_final_file
  62. debug_var artifact_final_file_basename
  63. debug_var cache_name
  64. debug_var cache_fname
  65. if [[ ! -f "${artifact_final_file}" ]]; then
  66. exit_with_error "Rootfs cache file '${artifact_final_file}' does not exist after create_new_rootfs_cache()."
  67. else
  68. display_alert "Rootfs cache file '${artifact_final_file}' exists after create_new_rootfs_cache()." "YESSS" "debug"
  69. fi
  70. # obtain the size, in MiB, of "${SDCARD}" at this point.
  71. declare -i rootfs_size_mib
  72. rootfs_size_mib=$(du -sm "${SDCARD}" | awk '{print $1}')
  73. display_alert "Actual rootfs size" "${rootfs_size_mib}MiB after basic/cache" ""
  74. # warn if rootfs_size_mib is higher than the tmpfs_estimated_size
  75. if [[ ${rootfs_size_mib} -gt ${tmpfs_estimated_size} ]]; then
  76. display_alert "Rootfs actual size is larger than estimated tmpfs size after basic/cache" "${rootfs_size_mib}MiB > ${tmpfs_estimated_size}MiB" "wrn"
  77. fi
  78. # Run the cleanup handler.
  79. execute_and_remove_cleanup_handler trap_handler_cleanup_rootfs_and_image
  80. return 0
  81. }
  82. function artifact_rootfs_cli_adapter_pre_run() {
  83. declare -g ARMBIAN_COMMAND_REQUIRE_BASIC_DEPS="yes" # Require prepare_host_basic to run before the command.
  84. # "gimme root on a Linux machine"
  85. cli_standard_relaunch_docker_or_sudo
  86. }
  87. function artifact_rootfs_cli_adapter_config_prep() {
  88. declare -g artifact_version_requires_aggregation="yes"
  89. declare -g ROOTFS_COMPRESSION_RATIO="${ROOTFS_COMPRESSION_RATIO:-"15"}" # default to Compress stronger when we make rootfs cache
  90. # If BOARD is set, use it to convert to an ARCH.
  91. if [[ -n ${BOARD} ]]; then
  92. display_alert "BOARD is set, converting to ARCH for rootfs building" "'BOARD=${BOARD}'" "warn"
  93. # Convert BOARD to ARCH; source the BOARD and FAMILY stuff
  94. LOG_SECTION="config_source_board_file" do_with_conditional_logging config_source_board_file
  95. LOG_SECTION="source_family_config_and_arch" do_with_conditional_logging source_family_config_and_arch
  96. display_alert "Done sourcing board file" "'${BOARD}' - arch: '${ARCH}'" "warn"
  97. fi
  98. declare -a vars_need_to_be_set=("RELEASE" "ARCH")
  99. # loop through all vars and check if they are not set and bomb out if so
  100. for var in "${vars_need_to_be_set[@]}"; do
  101. if [[ -z ${!var} ]]; then
  102. exit_with_error "Param '${var}' is not set but needs to be set for rootfs CLI."
  103. fi
  104. done
  105. declare -r __wanted_rootfs_arch="${ARCH}"
  106. declare -g -r RELEASE="${RELEASE}" # make readonly for finding who tries to change it
  107. declare -g -r NEEDS_BINFMT="yes" # make sure binfmts are installed during prepare_host_interactive
  108. # prep_conf_main_only_rootfs_ni is prep_conf_main_only_rootfs_ni() + mark_aggregation_required_in_default_build_start()
  109. prep_conf_main_only_rootfs_ni < /dev/null # no stdin for this, so it bombs if tries to be interactive.
  110. declare -g -r ARCH="${ARCH}" # make readonly for finding who tries to change it
  111. if [[ "${ARCH}" != "${__wanted_rootfs_arch}" ]]; then
  112. exit_with_error "Param 'ARCH' is set to '${ARCH}' after config, but different from wanted '${__wanted_rootfs_arch}'"
  113. fi
  114. }
  115. function artifact_rootfs_get_default_oci_target() {
  116. artifact_oci_target_base="${GHCR_SOURCE}/armbian/os/"
  117. }
  118. function artifact_rootfs_is_available_in_local_cache() {
  119. is_artifact_available_in_local_cache
  120. }
  121. function artifact_rootfs_is_available_in_remote_cache() {
  122. is_artifact_available_in_remote_cache
  123. }
  124. function artifact_rootfs_obtain_from_remote_cache() {
  125. obtain_artifact_from_remote_cache
  126. }
  127. function artifact_rootfs_deploy_to_remote_cache() {
  128. upload_artifact_to_oci
  129. }