bsp-cli.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 create_board_package() {
  10. display_alert "Creating board support package for CLI" "$CHOSEN_ROOTFS" "info"
  11. declare cleanup_id="" bsptempdir=""
  12. prepare_temp_dir_in_workdir_and_schedule_cleanup "deb-bsp-cli" cleanup_id bsptempdir # namerefs
  13. # "destination" is used a lot in hooks already. keep this name, even if only for compatibility.
  14. local destination=${bsptempdir}/${BSP_CLI_PACKAGE_FULLNAME}
  15. mkdir -p "${destination}"/DEBIAN
  16. cd "${destination}" || exit_with_error "Failed to cd to ${destination}"
  17. # copy general overlay from packages/bsp-cli
  18. # in practice: packages/bsp-cli and variations of config/optional/...
  19. copy_all_packages_files_for "bsp-cli"
  20. # install copy of boot script & environment file
  21. if [[ -n "${BOOTSCRIPT}" ]] && [[ $SRC_EXTLINUX != yes ]]; then
  22. # @TODO: add extension method bsp_prepare_bootloader(), refactor into u-boot extension
  23. local bootscript_src=${BOOTSCRIPT%%:*}
  24. local bootscript_dst=${BOOTSCRIPT##*:}
  25. mkdir -p "${destination}"/usr/share/armbian/
  26. display_alert "BOOTSCRIPT" "${BOOTSCRIPT}" "debug"
  27. display_alert "bootscript_src" "${bootscript_src}" "debug"
  28. display_alert "bootscript_dst" "${bootscript_dst}" "debug"
  29. # if not using extlinux, copy armbianEnv from template; prefer userpatches source
  30. if [[ $SRC_EXTLINUX != yes ]]; then
  31. if [ -f "${USERPATCHES_PATH}/bootscripts/${bootscript_src}" ]; then
  32. run_host_command_logged cp -pv "${USERPATCHES_PATH}/bootscripts/${bootscript_src}" "${destination}/usr/share/armbian/${bootscript_dst}"
  33. else
  34. run_host_command_logged cp -pv "${SRC}/config/bootscripts/${bootscript_src}" "${destination}/usr/share/armbian/${bootscript_dst}"
  35. fi
  36. if [[ -n $BOOTENV_FILE && -f $SRC/config/bootenv/$BOOTENV_FILE ]]; then
  37. run_host_command_logged cp -pv "${SRC}/config/bootenv/${BOOTENV_FILE}" "${destination}"/usr/share/armbian/armbianEnv.txt
  38. fi
  39. else
  40. display_alert "Using extlinux, regular bootscripts ignored" "SRC_EXTLINUX=${SRC_EXTLINUX}" "warn"
  41. fi
  42. # add configuration for setting uboot environment from userspace with: fw_setenv fw_printenv
  43. if [[ -n $UBOOT_FW_ENV ]]; then
  44. UBOOT_FW_ENV=($(tr ',' ' ' <<< "$UBOOT_FW_ENV"))
  45. mkdir -p "${destination}"/etc
  46. echo "# Device to access offset env size" > "${destination}"/etc/fw_env.config
  47. echo "/dev/mmcblk0 ${UBOOT_FW_ENV[0]} ${UBOOT_FW_ENV[1]}" >> "${destination}"/etc/fw_env.config
  48. fi
  49. fi
  50. # Replaces: base-files is needed to replace /etc/update-motd.d/ files on Xenial
  51. # Replaces: unattended-upgrades may be needed to replace /etc/apt/apt.conf.d/50unattended-upgrades
  52. # (distributions provide good defaults, so this is not needed currently)
  53. # Depends: linux-base is needed for "linux-version" command in initrd cleanup script
  54. # Depends: fping is needed for armbianmonitor to upload armbian-hardware-monitor.log
  55. cat <<- EOF > "${destination}"/DEBIAN/control
  56. Package: ${BSP_CLI_PACKAGE_NAME}
  57. Version: $REVISION
  58. Architecture: $ARCH
  59. Maintainer: $MAINTAINER <$MAINTAINERMAIL>
  60. Installed-Size: 1
  61. Section: kernel
  62. Priority: optional
  63. Depends: bash, linux-base, u-boot-tools, initramfs-tools, lsb-release, fping
  64. Provides: linux-${RELEASE}-root-legacy-$BOARD, linux-${RELEASE}-root-current-$BOARD, linux-${RELEASE}-root-edge-$BOARD
  65. Suggests: armbian-config
  66. Replaces: zram-config, base-files, armbian-tools-$RELEASE, linux-${RELEASE}-root-legacy-$BOARD (<< $REVISION~), linux-${RELEASE}-root-current-$BOARD (<< $REVISION~), linux-${RELEASE}-root-edge-$BOARD (<< $REVISION~)
  67. Breaks: linux-${RELEASE}-root-legacy-$BOARD (<< $REVISION~), linux-${RELEASE}-root-current-$BOARD (<< $REVISION~), linux-${RELEASE}-root-edge-$BOARD (<< $REVISION~)
  68. Recommends: bsdutils, parted, util-linux, toilet
  69. Description: Armbian board support files for $BOARD
  70. EOF
  71. # set up pre install script
  72. cat <<- EOF > "${destination}"/DEBIAN/preinst
  73. #!/bin/sh
  74. # tell people to reboot at next login
  75. [ "\$1" = "upgrade" ] && touch /var/run/.reboot_required
  76. # convert link to file
  77. if [ -L "/etc/network/interfaces" ]; then
  78. cp /etc/network/interfaces /etc/network/interfaces.tmp
  79. rm /etc/network/interfaces
  80. mv /etc/network/interfaces.tmp /etc/network/interfaces
  81. fi
  82. # fixing ramdisk corruption when using lz4 compression method
  83. sed -i "s/^COMPRESS=.*/COMPRESS=gzip/" /etc/initramfs-tools/initramfs.conf
  84. # swap
  85. grep -q vm.swappiness /etc/sysctl.conf
  86. case \$? in
  87. 0)
  88. sed -i 's/vm\.swappiness.*/vm.swappiness=100/' /etc/sysctl.conf
  89. ;;
  90. *)
  91. echo vm.swappiness=100 >>/etc/sysctl.conf
  92. ;;
  93. esac
  94. sysctl -p >/dev/null 2>&1
  95. # replace canonical advertisement
  96. if [ -d "/var/lib/ubuntu-advantage/messages/" ]; then
  97. echo "\nSupport Armbian! \nLearn more at https://armbian.com/donate" > /var/lib/ubuntu-advantage/messages/apt-pre-invoke-esm-service-status
  98. cp /var/lib/ubuntu-advantage/messages/apt-pre-invoke-esm-service-status /var/lib/ubuntu-advantage/messages/apt-pre-invoke-no-packages-apps.tmpl
  99. cp /var/lib/ubuntu-advantage/messages/apt-pre-invoke-esm-service-status /var/lib/ubuntu-advantage/messages/apt-pre-invoke-packages-apps
  100. cp /var/lib/ubuntu-advantage/messages/apt-pre-invoke-esm-service-status /var/lib/ubuntu-advantage/messages/apt-pre-invoke-packages-apps.tmpl
  101. fi
  102. # disable deprecated services
  103. [ -f "/etc/profile.d/activate_psd_user.sh" ] && rm /etc/profile.d/activate_psd_user.sh
  104. [ -f "/etc/profile.d/check_first_login.sh" ] && rm /etc/profile.d/check_first_login.sh
  105. [ -f "/etc/profile.d/check_first_login_reboot.sh" ] && rm /etc/profile.d/check_first_login_reboot.sh
  106. [ -f "/etc/profile.d/ssh-title.sh" ] && rm /etc/profile.d/ssh-title.sh
  107. [ -f "/etc/update-motd.d/10-header" ] && rm /etc/update-motd.d/10-header
  108. [ -f "/etc/update-motd.d/30-sysinfo" ] && rm /etc/update-motd.d/30-sysinfo
  109. [ -f "/etc/update-motd.d/35-tips" ] && rm /etc/update-motd.d/35-tips
  110. [ -f "/etc/update-motd.d/40-updates" ] && rm /etc/update-motd.d/40-updates
  111. [ -f "/etc/update-motd.d/98-autoreboot-warn" ] && rm /etc/update-motd.d/98-autoreboot-warn
  112. [ -f "/etc/update-motd.d/99-point-to-faq" ] && rm /etc/update-motd.d/99-point-to-faq
  113. [ -f "/etc/update-motd.d/80-esm" ] && rm /etc/update-motd.d/80-esm
  114. [ -f "/etc/update-motd.d/80-livepatch" ] && rm /etc/update-motd.d/80-livepatch
  115. [ -f "/etc/apt/apt.conf.d/02compress-indexes" ] && rm /etc/apt/apt.conf.d/02compress-indexes
  116. [ -f "/etc/apt/apt.conf.d/02periodic" ] && rm /etc/apt/apt.conf.d/02periodic
  117. [ -f "/etc/apt/apt.conf.d/no-languages" ] && rm /etc/apt/apt.conf.d/no-languages
  118. [ -f "/etc/init.d/armhwinfo" ] && rm /etc/init.d/armhwinfo
  119. [ -f "/etc/logrotate.d/armhwinfo" ] && rm /etc/logrotate.d/armhwinfo
  120. [ -f "/etc/init.d/firstrun" ] && rm /etc/init.d/firstrun
  121. [ -f "/etc/init.d/resize2fs" ] && rm /etc/init.d/resize2fs
  122. [ -f "/lib/systemd/system/firstrun-config.service" ] && rm /lib/systemd/system/firstrun-config.service
  123. [ -f "/lib/systemd/system/firstrun.service" ] && rm /lib/systemd/system/firstrun.service
  124. [ -f "/lib/systemd/system/resize2fs.service" ] && rm /lib/systemd/system/resize2fs.service
  125. [ -f "/usr/lib/armbian/apt-updates" ] && rm /usr/lib/armbian/apt-updates
  126. [ -f "/usr/lib/armbian/firstrun-config.sh" ] && rm /usr/lib/armbian/firstrun-config.sh
  127. # fix for https://bugs.launchpad.net/ubuntu/+source/lightdm-gtk-greeter/+bug/1897491
  128. [ -d "/var/lib/lightdm" ] && (chown -R lightdm:lightdm /var/lib/lightdm ; chmod 0750 /var/lib/lightdm)
  129. exit 0
  130. EOF
  131. chmod 755 "${destination}"/DEBIAN/preinst
  132. # postrm script
  133. cat <<- EOF > "${destination}"/DEBIAN/postrm
  134. #!/bin/sh
  135. if [ remove = "\$1" ] || [ abort-install = "\$1" ]; then
  136. systemctl disable armbian-hardware-monitor.service armbian-hardware-optimize.service >/dev/null 2>&1
  137. systemctl disable armbian-zram-config.service armbian-ramlog.service >/dev/null 2>&1
  138. fi
  139. exit 0
  140. EOF
  141. chmod 755 "${destination}"/DEBIAN/postrm
  142. # set up post install script
  143. cat <<- EOF > "${destination}"/DEBIAN/postinst
  144. #!/bin/sh
  145. #
  146. # ${BOARD} BSP post installation script
  147. #
  148. [ -f /etc/lib/systemd/system/armbian-ramlog.service ] && systemctl --no-reload enable armbian-ramlog.service
  149. # check if it was disabled in config and disable in new service
  150. if [ -n "\$(grep -w '^ENABLED=false' /etc/default/log2ram 2> /dev/null)" ]; then
  151. sed -i "s/^ENABLED=.*/ENABLED=false/" /etc/default/armbian-ramlog
  152. fi
  153. # fix boot delay "waiting for suspend/resume device"
  154. if [ -f "/etc/initramfs-tools/initramfs.conf" ]; then
  155. if ! grep --quiet "RESUME=none" /etc/initramfs-tools/initramfs.conf; then
  156. echo "RESUME=none" >> /etc/initramfs-tools/initramfs.conf
  157. fi
  158. fi
  159. EOF
  160. # install bootscripts if they are not present. Fix upgrades from old images
  161. if [[ $FORCE_BOOTSCRIPT_UPDATE == yes ]]; then
  162. cat <<- EOF >> "${destination}"/DEBIAN/postinst
  163. if [ true ]; then
  164. # this package recreate boot scripts
  165. EOF
  166. else
  167. cat <<- EOF >> "${destination}"/DEBIAN/postinst
  168. if [ ! -f /boot/$bootscript_dst ]; then
  169. # if boot script does not exits its recreated
  170. EOF
  171. fi
  172. cat <<- EOF >> "${destination}"/DEBIAN/postinst
  173. # move bootscript to /usr/share/armbian
  174. # create a backup
  175. [ -f /etc/armbian-release ] && . /etc/armbian-release
  176. [ -z \${VERSION} ] && VERSION=$(echo \`date +%s\`)
  177. if [ -f /boot/$bootscript_dst ]; then
  178. cp /boot/$bootscript_dst /usr/share/armbian/${bootscript_dst}-\${VERSION} >/dev/null 2>&1
  179. echo "NOTE: You can find previous bootscript versions in /usr/share/armbian !"
  180. fi
  181. # cleanup old bootscript backup
  182. ls /usr/share/armbian/boot.cmd-* >/dev/null 2>&1 | head -n -5 | xargs rm -f --
  183. ls /usr/share/armbian/boot.ini-* >/dev/null 2>&1 | head -n -5 | xargs rm -f --
  184. echo "Recreating boot script"
  185. cp /usr/share/armbian/$bootscript_dst /boot >/dev/null 2>&1
  186. rootdev=\$(sed -e 's/^.*root=//' -e 's/ .*\$//' < /proc/cmdline)
  187. rootfstype=\$(sed -e 's/^.*rootfstype=//' -e 's/ .*$//' < /proc/cmdline)
  188. # recreate armbianEnv.txt if it and extlinux does not exists
  189. if [ ! -f /boot/armbianEnv.txt ] && [ ! -f /boot/extlinux/extlinux.conf ]; then
  190. cp /usr/share/armbian/armbianEnv.txt /boot >/dev/null 2>&1
  191. echo "rootdev="\$rootdev >> /boot/armbianEnv.txt
  192. echo "rootfstype="\$rootfstype >> /boot/armbianEnv.txt
  193. fi
  194. [ -f /boot/boot.ini ] && sed -i "s/setenv rootdev.*/setenv rootdev \\"\$rootdev\\"/" /boot/boot.ini
  195. [ -f /boot/boot.ini ] && sed -i "s/setenv rootfstype.*/setenv rootfstype \\"\$rootfstype\\"/" /boot/boot.ini
  196. [ -f /boot/boot.cmd ] && mkimage -C none -A arm -T script -d /boot/boot.cmd /boot/boot.scr >/dev/null 2>&1
  197. fi
  198. [ ! -f "/etc/network/interfaces" ] && [ -f "/etc/network/interfaces.default" ] && cp /etc/network/interfaces.default /etc/network/interfaces
  199. ln -sf /var/run/motd /etc/motd
  200. rm -f /etc/update-motd.d/00-header /etc/update-motd.d/10-help-text
  201. if [ ! -f "/etc/default/armbian-motd" ]; then
  202. mv /etc/default/armbian-motd.dpkg-dist /etc/default/armbian-motd
  203. fi
  204. if [ ! -f "/etc/default/armbian-ramlog" ] && [ -f /etc/default/armbian-ramlog.dpkg-dist ]; then
  205. mv /etc/default/armbian-ramlog.dpkg-dist /etc/default/armbian-ramlog
  206. fi
  207. if [ ! -f "/etc/default/armbian-zram-config" ] && [ -f /etc/default/armbian-zram-config.dpkg-dist ]; then
  208. mv /etc/default/armbian-zram-config.dpkg-dist /etc/default/armbian-zram-config
  209. fi
  210. if [ -L "/usr/lib/chromium-browser/master_preferences.dpkg-dist" ]; then
  211. mv /usr/lib/chromium-browser/master_preferences.dpkg-dist /usr/lib/chromium-browser/master_preferences
  212. fi
  213. # Read release value
  214. if [ -f /etc/lsb-release ]; then
  215. RELEASE=\$(cat /etc/lsb-release | grep CODENAME | cut -d"=" -f2 | sed 's/.*/\u&/')
  216. sed -i "s/^PRETTY_NAME=.*/PRETTY_NAME=\"${VENDOR} $REVISION ${RELEASE}\"/" /etc/os-release
  217. echo "${VENDOR} ${REVISION} \${RELEASE} \\l \n" > /etc/issue
  218. echo "${VENDOR} ${REVISION} \${RELEASE}" > /etc/issue.net
  219. fi
  220. # Reload services
  221. systemctl --no-reload enable armbian-hardware-monitor.service armbian-hardware-optimize.service armbian-zram-config.service armbian-led-state.service >/dev/null 2>&1
  222. exit 0
  223. EOF
  224. chmod 755 "${destination}"/DEBIAN/postinst
  225. # won't recreate files if they were removed by user
  226. # TODO: Add proper handling for updated conffiles
  227. # We are runing this script each time apt runs. If this package is removed, file is removed and error is triggered.
  228. # Keeping armbian-apt-updates as a configuration, solve the problem
  229. cat <<- EOF > "${destination}"/DEBIAN/conffiles
  230. /usr/lib/armbian/armbian-apt-updates
  231. EOF
  232. # copy common files from a premade directory structure
  233. # @TODO this includes systemd config, assumes things about serial console, etc, that need dynamism or just to not exist with modern systemd
  234. run_host_command_logged rsync -a "${SRC}"/packages/bsp/common/* "${destination}"
  235. # trigger uInitrd creation after installation, to apply
  236. # /etc/initramfs/post-update.d/99-uboot
  237. cat <<- EOF > "${destination}"/DEBIAN/triggers
  238. activate update-initramfs
  239. EOF
  240. # copy distribution support status # @TODO: why? this changes over time and will be out of date
  241. local releases=($(find ${SRC}/config/distributions -mindepth 1 -maxdepth 1 -type d))
  242. for i in "${releases[@]}"; do
  243. echo "$(echo $i | sed 's/.*\///')=$(cat $i/support)" >> "${destination}"/etc/armbian-distribution-status
  244. done
  245. # armhwinfo, firstrun, armbianmonitor, etc. config file
  246. cat <<- EOF > "${destination}"/etc/armbian-release
  247. # PLEASE DO NOT EDIT THIS FILE
  248. BOARD=$BOARD
  249. BOARD_NAME="$BOARD_NAME"
  250. BOARDFAMILY=${BOARDFAMILY}
  251. BUILD_REPOSITORY_URL=${BUILD_REPOSITORY_URL}
  252. BUILD_REPOSITORY_COMMIT=${BUILD_REPOSITORY_COMMIT}
  253. VERSION=$REVISION
  254. LINUXFAMILY=$LINUXFAMILY
  255. ARCH=$ARCHITECTURE
  256. IMAGE_TYPE=$IMAGE_TYPE
  257. BOARD_TYPE=$BOARD_TYPE
  258. INITRD_ARCH=$INITRD_ARCH
  259. KERNEL_IMAGE_TYPE=$KERNEL_IMAGE_TYPE
  260. EOF
  261. # this is required for NFS boot to prevent deconfiguring the network on shutdown
  262. sed -i 's/#no-auto-down/no-auto-down/g' "${destination}"/etc/network/interfaces.default
  263. # execute $LINUXFAMILY-specific tweaks
  264. if [[ $(type -t family_tweaks_bsp) == function ]]; then
  265. display_alert "Running family_tweaks_bsp" "${LINUXFAMILY} - ${BOARDFAMILY}" "debug"
  266. family_tweaks_bsp
  267. display_alert "Done with family_tweaks_bsp" "${LINUXFAMILY} - ${BOARDFAMILY}" "debug"
  268. fi
  269. call_extension_method "post_family_tweaks_bsp" <<- 'POST_FAMILY_TWEAKS_BSP'
  270. *family_tweaks_bsp overrrides what is in the config, so give it a chance to override the family tweaks*
  271. This should be implemented by the config to tweak the BSP, after the board or family has had the chance to.
  272. POST_FAMILY_TWEAKS_BSP
  273. # add some summary to the image # @TODO: another?
  274. fingerprint_image "${destination}/etc/armbian.txt"
  275. # fixing permissions (basic), reference: dh_fixperms
  276. find "${destination}" -print0 2> /dev/null | xargs -0r chown --no-dereference 0:0
  277. find "${destination}" ! -type l -print0 2> /dev/null | xargs -0r chmod 'go=rX,u+rw,a-s'
  278. # create board DEB file
  279. fakeroot_dpkg_deb_build "${destination}" "${destination}.deb"
  280. mkdir -p "${DEB_STORAGE}/"
  281. run_host_command_logged rsync --remove-source-files -r "${destination}.deb" "${DEB_STORAGE}/"
  282. done_with_temp_dir "${cleanup_id}" # changes cwd to "${SRC}" and fires the cleanup function early
  283. display_alert "Done building BSP CLI package" "${destination}" "debug"
  284. }