makeboarddeb.sh 14 KB

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