debootstrap.sh 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. #!/bin/bash
  2. #
  3. # Copyright (c) 2013-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. # Functions:
  9. # debootstrap_ng
  10. # create_rootfs_cache
  11. # prepare_partitions
  12. # update_initramfs
  13. # create_image
  14. # debootstrap_ng
  15. #
  16. debootstrap_ng()
  17. {
  18. display_alert "Starting rootfs and image building process for" "${BRANCH} ${BOARD} ${RELEASE} ${DESKTOP_APPGROUPS_SELECTED:-null} ${DESKTOP_ENVIRONMENT:-null} ${BUILD_MINIMAL}" "info"
  19. [[ $ROOTFS_TYPE != ext4 ]] && display_alert "Assuming $BOARD $BRANCH kernel supports $ROOTFS_TYPE" "" "wrn"
  20. # trap to unmount stuff in case of error/manual interruption
  21. trap unmount_on_exit INT TERM EXIT
  22. # stage: clean and create directories
  23. rm -rf $SDCARD $MOUNT
  24. mkdir -p $SDCARD $MOUNT $DEST/images $EXTER/cache/rootfs
  25. # stage: verify tmpfs configuration and mount
  26. # CLI needs ~1.5GiB, desktop - ~3.5GiB
  27. # calculate and set tmpfs mount to use 9/10 of available RAM+SWAP
  28. local phymem=$(( (($(awk '/MemTotal/ {print $2}' /proc/meminfo) + $(awk '/SwapTotal/ {print $2}' /proc/meminfo))) / 1024 * 9 / 10 )) # MiB
  29. if [[ $BUILD_DESKTOP == yes ]]; then local tmpfs_max_size=3500; else local tmpfs_max_size=1500; fi # MiB
  30. if [[ $FORCE_USE_RAMDISK == no ]]; then local use_tmpfs=no
  31. elif [[ $FORCE_USE_RAMDISK == yes || $phymem -gt $tmpfs_max_size ]]; then
  32. local use_tmpfs=yes
  33. fi
  34. [[ -n $FORCE_TMPFS_SIZE ]] && phymem=$FORCE_TMPFS_SIZE
  35. [[ $use_tmpfs == yes ]] && mount -t tmpfs -o size=${phymem}M tmpfs $SDCARD
  36. # stage: prepare basic rootfs: unpack cache or create from scratch
  37. create_rootfs_cache
  38. call_extension_method "pre_install_distribution_specific" "config_pre_install_distribution_specific" << 'PRE_INSTALL_DISTRIBUTION_SPECIFIC'
  39. *give config a chance to act before install_distribution_specific*
  40. Called after `create_rootfs_cache` (_prepare basic rootfs: unpack cache or create from scratch_) but before `install_distribution_specific` (_install distribution and board specific applications_).
  41. PRE_INSTALL_DISTRIBUTION_SPECIFIC
  42. # stage: install kernel and u-boot packages
  43. # install distribution and board specific applications
  44. if [[ ${RELEASE} == "raspi" ]]; then
  45. install_opi_specific
  46. else
  47. install_distribution_specific
  48. install_common
  49. # install locally built packages or install pre-built packages from orangepi
  50. [[ $EXTERNAL_NEW == compile || $EXTERNAL_NEW == prebuilt ]] && chroot_installpackages_local
  51. #[[ $EXTERNAL_NEW == prebuilt ]] && chroot_installpackages "yes"
  52. # stage: user customization script
  53. # NOTE: installing too many packages may fill tmpfs mount
  54. customize_image
  55. # remove packages that are no longer needed. Since we have intrudoced uninstall feature, we might want to clean things that are no longer needed
  56. display_alert "No longer needed packages" "purge" "info"
  57. chroot $SDCARD /bin/bash -c "apt-get autoremove -y" >/dev/null 2>&1
  58. # create list of installed packages for debug purposes
  59. chroot $SDCARD /bin/bash -c "dpkg --get-selections" | grep -v deinstall | awk '{print $1}' | cut -f1 -d':' > $DEST/${LOG_SUBPATH}/installed-packages-${RELEASE}$([[ ${BUILD_MINIMAL} == yes ]] && echo "-minimal")$([[ ${BUILD_DESKTOP} == yes ]] && echo "-desktop").list 2>&1
  60. fi
  61. # clean up / prepare for making the image
  62. umount_chroot "$SDCARD"
  63. post_debootstrap_tweaks
  64. if [[ $ROOTFS_TYPE == fel ]]; then
  65. FEL_ROOTFS=$SDCARD/
  66. display_alert "Starting FEL boot" "$BOARD" "info"
  67. source $SRC/scripts/fel-load.sh
  68. else
  69. prepare_partitions
  70. create_image
  71. fi
  72. # stage: unmount tmpfs
  73. umount $SDCARD 2>&1
  74. if [[ $use_tmpfs = yes ]]; then
  75. while grep -qs "$SDCARD" /proc/mounts
  76. do
  77. umount $SDCARD
  78. sleep 5
  79. done
  80. fi
  81. rm -rf $SDCARD
  82. # remove exit trap
  83. trap - INT TERM EXIT
  84. } #############################################################################
  85. bootstrap(){
  86. local BOOTSTRAP_CMD=debootstrap
  87. local BOOTSTRAP_ARGS=()
  88. export CAPSH_ARG="--drop=cap_setfcap"
  89. export http_proxy=${APT_PROXY}
  90. BOOTSTRAP_ARGS+=(--arch arm64)
  91. BOOTSTRAP_ARGS+=(--include gnupg)
  92. #BOOTSTRAP_ARGS+=(--components "main,contrib,non-free")
  93. BOOTSTRAP_ARGS+=(--components "main")
  94. BOOTSTRAP_ARGS+=(--exclude=info)
  95. BOOTSTRAP_ARGS+=(--include=ca-certificates)
  96. BOOTSTRAP_ARGS+=("$@")
  97. printf -v BOOTSTRAP_STR '%q ' "${BOOTSTRAP_ARGS[@]}"
  98. ${BOOTSTRAP_CMD} $BOOTSTRAP_STR || true
  99. }
  100. export -f bootstrap
  101. # create_rootfs_cache
  102. #
  103. # unpacks cached rootfs for $RELEASE or creates one
  104. #
  105. create_rootfs_cache()
  106. {
  107. local packages_hash=$(get_package_list_hash "$ROOTFSCACHE_VERSION")
  108. local cache_type="cli"
  109. [[ ${BUILD_DESKTOP} == yes ]] && local cache_type="xfce-desktop"
  110. [[ -n ${DESKTOP_ENVIRONMENT} ]] && local cache_type="${DESKTOP_ENVIRONMENT}"
  111. [[ ${BUILD_MINIMAL} == yes ]] && local cache_type="minimal"
  112. local cache_name=${RELEASE}-${cache_type}-${ARCH}.$packages_hash.tar.lz4
  113. local cache_fname=${EXTER}/cache/rootfs/${cache_name}
  114. local display_name=${RELEASE}-${cache_type}-${ARCH}.${packages_hash:0:3}...${packages_hash:29}.tar.lz4
  115. if [[ -f $cache_fname && "$ROOT_FS_CREATE_ONLY" != "force" ]]; then
  116. local date_diff=$(( ($(date +%s) - $(stat -c %Y $cache_fname)) / 86400 ))
  117. display_alert "Extracting $display_name" "$date_diff days old" "info"
  118. pv -p -b -r -c -N "[ .... ] $display_name" "$cache_fname" | lz4 -dc | tar xp --xattrs -C $SDCARD/
  119. [[ $? -ne 0 ]] && rm $cache_fname && exit_with_error "Cache $cache_fname is corrupted and was deleted. Restart."
  120. rm $SDCARD/etc/resolv.conf
  121. echo "nameserver $NAMESERVER" >> $SDCARD/etc/resolv.conf
  122. create_sources_list "$RELEASE" "$SDCARD/"
  123. elif [[ $RELEASE == "raspi" ]]; then
  124. display_alert "local not found" "Creating new rootfs cache for $RELEASE" "info"
  125. cd $SDCARD # this will prevent error sh: 0: getcwd() failed
  126. bootstrap bullseye "$SDCARD" "https://mirrors.ustc.edu.cn/debian/"
  127. mount_chroot "$SDCARD"
  128. display_alert "Diverting" "initctl/start-stop-daemon" "info"
  129. # policy-rc.d script prevents starting or reloading services during image creation
  130. printf '#!/bin/sh\nexit 101' > $SDCARD/usr/sbin/policy-rc.d
  131. LC_ALL=C LANG=C chroot $SDCARD /bin/bash -c "dpkg-divert --quiet --local --rename --add /sbin/initctl" &> /dev/null
  132. LC_ALL=C LANG=C chroot $SDCARD /bin/bash -c "dpkg-divert --quiet --local --rename --add /sbin/start-stop-daemon" &> /dev/null
  133. printf '#!/bin/sh\necho "Warning: Fake start-stop-daemon called, doing nothing"' > $SDCARD/sbin/start-stop-daemon
  134. printf '#!/bin/sh\necho "Warning: Fake initctl called, doing nothing"' > $SDCARD/sbin/initctl
  135. chmod 755 $SDCARD/usr/sbin/policy-rc.d
  136. chmod 755 $SDCARD/sbin/initctl
  137. chmod 755 $SDCARD/sbin/start-stop-daemon
  138. install_raspi_specific
  139. umount_chroot "$SDCARD"
  140. tar cp --xattrs --directory=$SDCARD/ --exclude='./dev/*' --exclude='./proc/*' --exclude='./run/*' --exclude='./tmp/*' \
  141. --exclude='./sys/*' . | pv -p -b -r -s $(du -sb $SDCARD/ | cut -f1) -N "$display_name" | lz4 -5 -c > $cache_fname
  142. else
  143. display_alert "local not found" "Creating new rootfs cache for $RELEASE" "info"
  144. # stage: debootstrap base system
  145. if [[ $NO_APT_CACHER != yes ]]; then
  146. # apt-cacher-ng apt-get proxy parameter
  147. local apt_extra="-o Acquire::http::Proxy=\"http://${APT_PROXY_ADDR:-localhost:3142}\""
  148. local apt_mirror="http://${APT_PROXY_ADDR:-localhost:3142}/$APT_MIRROR"
  149. else
  150. local apt_mirror="http://$APT_MIRROR"
  151. fi
  152. # fancy progress bars
  153. [[ -z $OUTPUT_DIALOG ]] && local apt_extra_progress="--show-progress -o DPKG::Progress-Fancy=1"
  154. # Ok so for eval+PIPESTATUS.
  155. # Try this on your bash shell:
  156. # ONEVAR="testing" eval 'bash -c "echo value once $ONEVAR && false && echo value twice $ONEVAR"' '| grep value' '| grep value' ; echo ${PIPESTATUS[*]}
  157. # Notice how PIPESTATUS has only one element. and it is always true, although we failed explicitly with false in the middle of the bash.
  158. # That is because eval itself is considered a single command, no matter how many pipes you put in there, you'll get a single value, the return code of the LAST pipe.
  159. # Lets export the value of the pipe inside eval so we know outside what happened:
  160. # ONEVAR="testing" eval 'bash -e -c "echo value once $ONEVAR && false && echo value twice $ONEVAR"' '| grep value' '| grep value' ';EVALPIPE=(${PIPESTATUS[@]})' ; echo ${EVALPIPE[*]}
  161. local release_version=${RELEASE}
  162. if [[ ${RELEASE} == "sid" ]]; then
  163. release_version=unstable
  164. apt_mirror="https://snapshot.debian.org/archive/debian-ports/20221225T084846Z"
  165. DEBOOTSTRAP_OPTION="--no-check-gpg --no-merged-usr"
  166. PACKAGE_LIST_EXCLUDE="usr-is-merged"
  167. fi
  168. display_alert "Installing base system" "Stage 1/2" "info"
  169. cd $SDCARD # this will prevent error sh: 0: getcwd() failed
  170. eval 'debootstrap --variant=minbase --include=${DEBOOTSTRAP_LIST// /,} ${PACKAGE_LIST_EXCLUDE:+ --exclude=${PACKAGE_LIST_EXCLUDE// /,}} \
  171. --arch=$ARCH --components=${DEBOOTSTRAP_COMPONENTS} $DEBOOTSTRAP_OPTION --foreign ${release_version} $SDCARD/ ${apt_mirror}' \
  172. ${PROGRESS_LOG_TO_FILE:+' | tee -a $DEST/${LOG_SUBPATH}/debootstrap.log'} \
  173. ${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" --progressbox "Debootstrap (stage 1/2)..." $TTY_Y $TTY_X'} \
  174. ${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'} ';EVALPIPE=(${PIPESTATUS[@]})'
  175. [[ ${EVALPIPE[0]} -ne 0 || ! -f $SDCARD/debootstrap/debootstrap ]] && exit_with_error "Debootstrap base system for ${BRANCH} ${BOARD} ${RELEASE} ${DESKTOP_APPGROUPS_SELECTED} ${DESKTOP_ENVIRONMENT} ${BUILD_MINIMAL} first stage failed"
  176. cp /usr/bin/$QEMU_BINARY $SDCARD/usr/bin/
  177. mkdir -p $SDCARD/usr/share/keyrings/
  178. cp /usr/share/keyrings/*-archive-keyring.gpg $SDCARD/usr/share/keyrings/
  179. display_alert "Installing base system" "Stage 2/2" "info"
  180. eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -e -c "/debootstrap/debootstrap --second-stage"' \
  181. ${PROGRESS_LOG_TO_FILE:+' | tee -a $DEST/${LOG_SUBPATH}/debootstrap.log'} \
  182. ${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" --progressbox "Debootstrap (stage 2/2)..." $TTY_Y $TTY_X'} \
  183. ${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'} ';EVALPIPE=(${PIPESTATUS[@]})'
  184. [[ ${EVALPIPE[0]} -ne 0 || ! -f $SDCARD/bin/bash ]] && exit_with_error "Debootstrap base system for ${BRANCH} ${BOARD} ${RELEASE} ${DESKTOP_APPGROUPS_SELECTED} ${DESKTOP_ENVIRONMENT} ${BUILD_MINIMAL} second stage failed"
  185. mount_chroot "$SDCARD"
  186. if [[ ${RELEASE} == "sid" ]]; then
  187. mkdir -p $SDCARD/etc/apt/apt.conf.d/
  188. echo "Acquire::Check-Valid-Until no;" > $SDCARD/etc/apt/apt.conf.d/99-no-check-valid-until
  189. wget -qnc -P ${EXTER}/cache/debs/ https://snapshot.debian.org/archive/debian-ports/20220616T194833Z/pool-riscv64/main/i/icu/libicu71_71.1-3_riscv64.deb
  190. cp -v ${EXTER}/cache/debs/libicu71_71.1-3_riscv64.deb $SDCARD/
  191. LC_ALL=C LANG=C chroot $SDCARD /bin/bash -c "dpkg -i /libicu71_71.1-3_riscv64.deb" &> /dev/null
  192. fi
  193. display_alert "Diverting" "initctl/start-stop-daemon" "info"
  194. # policy-rc.d script prevents starting or reloading services during image creation
  195. printf '#!/bin/sh\nexit 101' > $SDCARD/usr/sbin/policy-rc.d
  196. LC_ALL=C LANG=C chroot $SDCARD /bin/bash -c "dpkg-divert --quiet --local --rename --add /sbin/initctl" &> /dev/null
  197. LC_ALL=C LANG=C chroot $SDCARD /bin/bash -c "dpkg-divert --quiet --local --rename --add /sbin/start-stop-daemon" &> /dev/null
  198. printf '#!/bin/sh\necho "Warning: Fake start-stop-daemon called, doing nothing"' > $SDCARD/sbin/start-stop-daemon
  199. printf '#!/bin/sh\necho "Warning: Fake initctl called, doing nothing"' > $SDCARD/sbin/initctl
  200. chmod 755 $SDCARD/usr/sbin/policy-rc.d
  201. chmod 755 $SDCARD/sbin/initctl
  202. chmod 755 $SDCARD/sbin/start-stop-daemon
  203. # stage: configure language and locales
  204. display_alert "Configuring locales" "$DEST_LANG" "info"
  205. [[ -f $SDCARD/etc/locale.gen ]] && sed -i "s/^# $DEST_LANG/$DEST_LANG/" $SDCARD/etc/locale.gen
  206. eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -c "locale-gen $DEST_LANG"' ${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'}
  207. eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -c "update-locale LANG=$DEST_LANG LANGUAGE=$DEST_LANG LC_MESSAGES=$DEST_LANG"' \
  208. ${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'}
  209. if [[ -f $SDCARD/etc/default/console-setup ]]; then
  210. sed -e 's/CHARMAP=.*/CHARMAP="UTF-8"/' -e 's/FONTSIZE=.*/FONTSIZE="8x16"/' \
  211. -e 's/CODESET=.*/CODESET="guess"/' -i $SDCARD/etc/default/console-setup
  212. eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -c "setupcon --save --force"'
  213. fi
  214. # stage: create apt-get sources list
  215. create_sources_list "$RELEASE" "$SDCARD/"
  216. # add armhf arhitecture to arm64, unless configured not to do so.
  217. if [[ "a${ARMHF_ARCH}" != "askip" ]]; then
  218. [[ $ARCH == arm64 ]] && eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -c "dpkg --add-architecture armhf"'
  219. fi
  220. # this should fix resolvconf installation failure in some cases
  221. chroot $SDCARD /bin/bash -c 'echo "resolvconf resolvconf/linkify-resolvconf boolean false" | debconf-set-selections'
  222. # stage: update packages list
  223. display_alert "Updating package list" "$RELEASE" "info"
  224. eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -e -c "apt-get -q -y $apt_extra update"' \
  225. ${PROGRESS_LOG_TO_FILE:+' | tee -a $DEST/${LOG_SUBPATH}/debootstrap.log'} \
  226. ${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" --progressbox "Updating package lists..." $TTY_Y $TTY_X'} \
  227. ${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'} ';EVALPIPE=(${PIPESTATUS[@]})'
  228. [[ ${EVALPIPE[0]} -ne 0 ]] && display_alert "Updating package lists" "failed" "wrn"
  229. # stage: upgrade base packages from xxx-updates and xxx-backports repository branches
  230. display_alert "Upgrading base packages" "Orange Pi" "info"
  231. eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -e -c "DEBIAN_FRONTEND=noninteractive apt-get -y -q \
  232. $apt_extra $apt_extra_progress upgrade"' \
  233. ${PROGRESS_LOG_TO_FILE:+' | tee -a $DEST/${LOG_SUBPATH}/debootstrap.log'} \
  234. ${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" --progressbox "Upgrading base packages..." $TTY_Y $TTY_X'} \
  235. ${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'} ';EVALPIPE=(${PIPESTATUS[@]})'
  236. # Myy: Dividing the desktop packages installation steps into multiple
  237. # ones. We first install the "ADDITIONAL_PACKAGES" in order to get
  238. # access to software-common-properties installation.
  239. # THEN we add the APT sources and install the Desktop packages.
  240. # TODO : Find a way to add APT sources WITHOUT software-common-properties
  241. [[ ${EVALPIPE[0]} -ne 0 ]] && display_alert "Upgrading base packages" "failed" "wrn"
  242. # stage: install additional packages
  243. display_alert "Installing the main packages for" "Orange Pi" "info"
  244. eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -e -c "DEBIAN_FRONTEND=noninteractive apt-get -y -q \
  245. $apt_extra $apt_extra_progress --no-install-recommends install $PACKAGE_MAIN_LIST"' \
  246. ${PROGRESS_LOG_TO_FILE:+' | tee -a $DEST/${LOG_SUBPATH}/debootstrap.log'} \
  247. ${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" --progressbox "Installing Orange Pi main packages..." $TTY_Y $TTY_X'} \
  248. ${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'} ';EVALPIPE=(${PIPESTATUS[@]})'
  249. [[ ${PIPESTATUS[0]} -ne 0 ]] && exit_with_error "Installation of Orange Pi main packages for ${BRANCH} ${BOARD} ${RELEASE} ${DESKTOP_APPGROUPS_SELECTED} ${DESKTOP_ENVIRONMENT} ${BUILD_MINIMAL} failed"
  250. if [[ $BUILD_DESKTOP == "yes" ]]; then
  251. # FIXME Myy : Are we keeping this only for Desktop users,
  252. # or should we extend this to CLI users too ?
  253. # There might be some clunky boards that require Debian packages from
  254. # specific repos...
  255. display_alert "Adding apt sources for Desktop packages"
  256. add_desktop_package_sources
  257. local apt_desktop_install_flags=""
  258. if [[ ! -z ${DESKTOP_APT_FLAGS_SELECTED+x} ]]; then
  259. for flag in ${DESKTOP_APT_FLAGS_SELECTED}; do
  260. apt_desktop_install_flags+=" --install-${flag}"
  261. done
  262. else
  263. # Myy : Using the previous default option, if the variable isn't defined
  264. # And ONLY if it's not defined !
  265. apt_desktop_install_flags+=" --no-install-recommends"
  266. fi
  267. display_alert "Installing the desktop packages for" "Orange Pi" "info"
  268. eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -e -c "DEBIAN_FRONTEND=noninteractive apt-get -y -q \
  269. $apt_extra $apt_extra_progress install ${apt_desktop_install_flags} $PACKAGE_LIST_DESKTOP"' \
  270. ${PROGRESS_LOG_TO_FILE:+' | tee -a $DEST/${LOG_SUBPATH}/debootstrap.log'} \
  271. ${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" --progressbox "Installing Orange Pi desktop packages..." $TTY_Y $TTY_X'} \
  272. ${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'} ';EVALPIPE=(${PIPESTATUS[@]})'
  273. [[ ${PIPESTATUS[0]} -ne 0 ]] && exit_with_error "Installation of Orange Pi desktop packages for ${BRANCH} ${BOARD} ${RELEASE} ${DESKTOP_APPGROUPS_SELECTED} ${DESKTOP_ENVIRONMENT} ${BUILD_MINIMAL} failed"
  274. fi
  275. install_docker
  276. [[ ${BOARDFAMILY} == "starfive2" ]] && jh7110_install_libs
  277. # Remove packages from packages.uninstall
  278. display_alert "Uninstall packages" "$PACKAGE_LIST_UNINSTALL" "info"
  279. eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -e -c "DEBIAN_FRONTEND=noninteractive apt-get -y -qq \
  280. $apt_extra $apt_extra_progress purge $PACKAGE_LIST_UNINSTALL"' \
  281. ${PROGRESS_LOG_TO_FILE:+' >> $DEST/${LOG_SUBPATH}/debootstrap.log'} \
  282. ${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" --progressbox "Removing packages.uninstall packages..." $TTY_Y $TTY_X'} \
  283. ${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'} ';EVALPIPE=(${PIPESTATUS[@]})'
  284. [[ ${EVALPIPE[0]} -ne 0 ]] && exit_with_error "Installation of Orange Pi packages failed"
  285. # stage: purge residual packages
  286. display_alert "Purging residual packages for" "Orange Pi" "info"
  287. PURGINGPACKAGES=$(chroot $SDCARD /bin/bash -c "dpkg -l | grep \"^rc\" | awk '{print \$2}' | tr \"\n\" \" \"")
  288. eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -e -c "DEBIAN_FRONTEND=noninteractive apt-get -y -q \
  289. $apt_extra $apt_extra_progress remove --purge $PURGINGPACKAGES"' \
  290. ${PROGRESS_LOG_TO_FILE:+' | tee -a $DEST/${LOG_SUBPATH}/debootstrap.log'} \
  291. ${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" --progressbox "Purging residual Orange Pi packages..." $TTY_Y $TTY_X'} \
  292. ${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'} ';EVALPIPE=(${PIPESTATUS[@]})'
  293. [[ ${EVALPIPE[0]} -ne 0 ]] && exit_with_error "Purging of residual Orange Pi packages failed"
  294. # stage: remove downloaded packages
  295. chroot $SDCARD /bin/bash -c "apt-get -y autoremove; apt-get clean"
  296. # DEBUG: print free space
  297. local freespace=$(LC_ALL=C df -h)
  298. echo $freespace >> $DEST/${LOG_SUBPATH}/debootstrap.log
  299. display_alert "Free SD cache" "$(echo -e "$freespace" | grep $SDCARD | awk '{print $5}')" "info"
  300. display_alert "Mount point" "$(echo -e "$freespace" | grep $MOUNT | head -1 | awk '{print $5}')" "info"
  301. # create list of installed packages for debug purposes
  302. chroot $SDCARD /bin/bash -c "dpkg --get-selections" | grep -v deinstall | awk '{print $1}' | cut -f1 -d':' > ${cache_fname}.list 2>&1
  303. # creating xapian index that synaptic runs faster
  304. if [[ $BUILD_DESKTOP == yes ]]; then
  305. display_alert "Recreating Synaptic search index" "Please wait" "info"
  306. chroot $SDCARD /bin/bash -c "[[ -f /usr/sbin/update-apt-xapian-index ]] && /usr/sbin/update-apt-xapian-index -u"
  307. fi
  308. # this is needed for the build process later since resolvconf generated file in /run is not saved
  309. rm $SDCARD/etc/resolv.conf
  310. echo "nameserver $NAMESERVER" >> $SDCARD/etc/resolv.conf
  311. # stage: make rootfs cache archive
  312. display_alert "Ending debootstrap process and preparing cache" "$RELEASE" "info"
  313. sync
  314. # the only reason to unmount here is compression progress display
  315. # based on rootfs size calculation
  316. umount_chroot "$SDCARD"
  317. tar cp --xattrs --directory=$SDCARD/ --exclude='./dev/*' --exclude='./proc/*' --exclude='./run/*' --exclude='./tmp/*' \
  318. --exclude='./sys/*' --exclude='./home/*' --exclude='./root/*' . | pv -p -b -r -s $(du -sb $SDCARD/ | cut -f1) -N "$display_name" | lz4 -5 -c > $cache_fname
  319. # sign rootfs cache archive that it can be used for web cache once. Internal purposes
  320. if [[ -n "${GPG_PASS}" && "${SUDO_USER}" ]]; then
  321. [[ -n ${SUDO_USER} ]] && sudo chown -R ${SUDO_USER}:${SUDO_USER} "${DEST}"/images/
  322. echo "${GPG_PASS}" | sudo -H -u ${SUDO_USER} bash -c "gpg --passphrase-fd 0 --armor --detach-sign --pinentry-mode loopback --batch --yes ${cache_fname}" || exit 1
  323. fi
  324. # needed for backend to keep current only
  325. touch $cache_fname.current
  326. fi
  327. # used for internal purposes. Faster rootfs cache rebuilding
  328. if [[ -n "$ROOT_FS_CREATE_ONLY" ]]; then
  329. umount --lazy "$SDCARD"
  330. rm -rf $SDCARD
  331. display_alert "Rootfs build done" "@host" "info"
  332. display_alert "Target directory" "${EXTER}/cache/rootfs" "info"
  333. display_alert "File name" "${cache_name}" "info"
  334. # remove exit trap
  335. trap - INT TERM EXIT
  336. exit
  337. fi
  338. mount_chroot "$SDCARD"
  339. } #############################################################################
  340. # prepare_partitions
  341. #
  342. # creates image file, partitions and fs
  343. # and mounts it to local dir
  344. # FS-dependent stuff (boot and root fs partition types) happens here
  345. #
  346. prepare_partitions() {
  347. display_alert "Preparing image file for rootfs" "$BOARD $RELEASE" "info"
  348. # possible partition combinations
  349. # /boot: none, ext4, ext2, fat (BOOTFS_TYPE)
  350. # root: ext4, btrfs, f2fs, nfs (ROOTFS_TYPE)
  351. # declare makes local variables by default if used inside a function
  352. # NOTE: mountopts string should always start with comma if not empty
  353. # array copying in old bash versions is tricky, so having filesystems as arrays
  354. # with attributes as keys is not a good idea
  355. declare -A parttype mkopts mkopts_label mkfs mountopts
  356. parttype[ext4]=ext4
  357. parttype[ext2]=ext2
  358. parttype[fat]=fat16
  359. parttype[f2fs]=ext4 # not a copy-paste error
  360. parttype[btrfs]=btrfs
  361. parttype[xfs]=xfs
  362. # parttype[nfs] is empty
  363. # metadata_csum and 64bit may need to be disabled explicitly when migrating to newer supported host OS releases
  364. if [[ $HOSTRELEASE =~ buster|bullseye|bookworm|bionic|focal|jammy|noble|kinetic|sid ]]; then
  365. mkopts[ext4]="-q -m 2 -O ^64bit,^metadata_csum"
  366. fi
  367. # mkopts[fat] is empty
  368. mkopts[ext2]='-q'
  369. # mkopts[f2fs] is empty
  370. mkopts[btrfs]='-m dup'
  371. # mkopts[xfs] is empty
  372. # mkopts[nfs] is empty
  373. mkopts_label[ext4]='-L '
  374. mkopts_label[ext2]='-L '
  375. mkopts_label[fat]='-n '
  376. mkopts_label[f2fs]='-l '
  377. mkopts_label[btrfs]='-L '
  378. mkopts_label[xfs]='-L '
  379. # mkopts_label[nfs] is empty
  380. mkfs[ext4]=ext4
  381. mkfs[ext2]=ext2
  382. mkfs[fat]=vfat
  383. mkfs[f2fs]=f2fs
  384. mkfs[btrfs]=btrfs
  385. mkfs[xfs]=xfs
  386. # mkfs[nfs] is empty
  387. mountopts[ext4]=',commit=600,errors=remount-ro'
  388. # mountopts[ext2] is empty
  389. # mountopts[fat] is empty
  390. # mountopts[f2fs] is empty
  391. mountopts[btrfs]=',commit=600'
  392. # mountopts[xfs] is empty
  393. # mountopts[nfs] is empty
  394. # default BOOTSIZE to use if not specified
  395. DEFAULT_BOOTSIZE=1024 # MiB
  396. # size of UEFI partition. 0 for no UEFI. Don't mix UEFISIZE>0 and BOOTSIZE>0
  397. UEFISIZE=${UEFISIZE:-0}
  398. BIOSSIZE=${BIOSSIZE:-0}
  399. UEFI_MOUNT_POINT=${UEFI_MOUNT_POINT:-/boot/efi}
  400. UEFI_FS_LABEL="${UEFI_FS_LABEL:-opi_efi}"
  401. ROOT_FS_LABEL="${ROOT_FS_LABEL:-opi_root}"
  402. BOOT_FS_LABEL="${BOOT_FS_LABEL:-opi_boot}"
  403. call_extension_method "pre_prepare_partitions" "prepare_partitions_custom" << 'PRE_PREPARE_PARTITIONS'
  404. *allow custom options for mkfs*
  405. Good time to change stuff like mkfs opts, types etc.
  406. PRE_PREPARE_PARTITIONS
  407. # stage: determine partition configuration
  408. local next=1
  409. # Check if we need UEFI partition
  410. if [[ $UEFISIZE -gt 0 ]]; then
  411. # Check if we need BIOS partition
  412. [[ $BIOSSIZE -gt 0 ]] && local biospart=$((next++))
  413. local uefipart=$((next++))
  414. fi
  415. # Check if we need boot partition
  416. if [[ -n $BOOTFS_TYPE || $ROOTFS_TYPE != ext4 || $CRYPTROOT_ENABLE == yes ]]; then
  417. local bootpart=$((next++))
  418. local bootfs=${BOOTFS_TYPE:-ext4}
  419. [[ -z $BOOTSIZE || $BOOTSIZE -le 8 ]] && BOOTSIZE=${DEFAULT_BOOTSIZE}
  420. else
  421. BOOTSIZE=0
  422. fi
  423. # Check if we need root partition
  424. [[ $ROOTFS_TYPE != nfs ]] &&
  425. local rootpart=$((next++))
  426. # stage: calculate rootfs size
  427. export rootfs_size=$(du -sm $SDCARD/ | cut -f1) # MiB
  428. display_alert "Current rootfs size" "$rootfs_size MiB" "info"
  429. call_extension_method "prepare_image_size" "config_prepare_image_size" << 'PREPARE_IMAGE_SIZE'
  430. *allow dynamically determining the size based on the $rootfs_size*
  431. Called after `${rootfs_size}` is known, but before `${FIXED_IMAGE_SIZE}` is taken into account.
  432. A good spot to determine `FIXED_IMAGE_SIZE` based on `rootfs_size`.
  433. UEFISIZE can be set to 0 for no UEFI partition, or to a size in MiB to include one.
  434. Last chance to set `USE_HOOK_FOR_PARTITION`=yes and then implement create_partition_table hook_point.
  435. PREPARE_IMAGE_SIZE
  436. if [[ -n $FIXED_IMAGE_SIZE && $FIXED_IMAGE_SIZE =~ ^[0-9]+$ ]]; then
  437. display_alert "Using user-defined image size" "$FIXED_IMAGE_SIZE MiB" "info"
  438. local sdsize=$FIXED_IMAGE_SIZE
  439. # basic sanity check
  440. if [[ $ROOTFS_TYPE != nfs && $sdsize -lt $rootfs_size ]]; then
  441. exit_with_error "User defined image size is too small" "$sdsize <= $rootfs_size"
  442. fi
  443. else
  444. local imagesize=$(($rootfs_size + $OFFSET + $BOOTSIZE + $UEFISIZE + $EXTRA_ROOTFS_MIB_SIZE)) # MiB
  445. # Hardcoded overhead +25% is needed for desktop images,
  446. # for CLI it could be lower. Align the size up to 4MiB
  447. if [[ $BUILD_DESKTOP == yes ]]; then
  448. local sdsize=$(bc -l <<< "scale=0; ((($imagesize * 1.35) / 1 + 0) / 4 + 1) * 4")
  449. else
  450. local sdsize=$(bc -l <<< "scale=0; ((($imagesize * 1.30) / 1 + 0) / 4 + 1) * 4")
  451. fi
  452. fi
  453. # stage: create blank image
  454. display_alert "Creating blank image for rootfs" "$sdsize MiB" "info"
  455. if [[ $FAST_CREATE_IMAGE == yes ]]; then
  456. truncate --size=${sdsize}M ${SDCARD}.raw # sometimes results in fs corruption, revert to previous know to work solution
  457. sync
  458. else
  459. dd if=/dev/zero bs=1M status=none count=$sdsize | pv -p -b -r -s $(($sdsize * 1024 * 1024)) -N "[ .... ] dd" | dd status=none of=${SDCARD}.raw
  460. fi
  461. # stage: create partition table
  462. display_alert "Creating partitions" "${bootfs:+/boot: $bootfs }root: $ROOTFS_TYPE" "info"
  463. if [[ "${USE_HOOK_FOR_PARTITION}" == "yes" ]]; then
  464. {
  465. [[ "$IMAGE_PARTITION_TABLE" == "msdos" ]] &&
  466. echo "label: dos" ||
  467. echo "label: $IMAGE_PARTITION_TABLE"
  468. } | sfdisk ${SDCARD}.raw >> "${DEST}/${LOG_SUBPATH}/install.log" 2>&1 ||
  469. exit_with_error "Create partition table fail. Please check" "${DEST}/${LOG_SUBPATH}/install.log"
  470. call_extension_method "create_partition_table" <<- 'CREATE_PARTITION_TABLE'
  471. *only called when USE_HOOK_FOR_PARTITION=yes to create the complete partition table*
  472. Finally, we can get our own partition table. You have to partition ${SDCARD}.raw
  473. yourself. Good luck.
  474. CREATE_PARTITION_TABLE
  475. else
  476. {
  477. [[ "$IMAGE_PARTITION_TABLE" == "msdos" ]] &&
  478. echo "label: dos" ||
  479. echo "label: $IMAGE_PARTITION_TABLE"
  480. local next=$OFFSET
  481. if [[ -n "$biospart" ]]; then
  482. # gpt: BIOS boot
  483. local type="21686148-6449-6E6F-744E-656564454649"
  484. echo "$biospart : name=\"bios\", start=${next}MiB, size=${BIOSSIZE}MiB, type=${type}"
  485. local next=$(($next + $BIOSSIZE))
  486. fi
  487. if [[ -n "$uefipart" ]]; then
  488. # dos: EFI (FAT-12/16/32)
  489. # gpt: EFI System
  490. [[ "$IMAGE_PARTITION_TABLE" != "gpt" ]] &&
  491. local type="ef" ||
  492. local type="C12A7328-F81F-11D2-BA4B-00A0C93EC93B"
  493. echo "$uefipart : name=\"efi\", start=${next}MiB, size=${UEFISIZE}MiB, type=${type}"
  494. local next=$(($next + $UEFISIZE))
  495. fi
  496. if [[ -n "$bootpart" ]]; then
  497. # Linux extended boot
  498. [[ "$IMAGE_PARTITION_TABLE" != "gpt" ]] &&
  499. local type="ea" ||
  500. local type="BC13C2FF-59E6-4262-A352-B275FD6F7172"
  501. if [[ -n "$rootpart" ]]; then
  502. echo "$bootpart : name=\"bootfs\", start=${next}MiB, size=${BOOTSIZE}MiB, type=${type}"
  503. local next=$(($next + $BOOTSIZE))
  504. else
  505. # no `size` argument mean "as much as possible"
  506. echo "$bootpart : name=\"bootfs\", start=${next}MiB, type=${type}"
  507. fi
  508. fi
  509. if [[ -n "$rootpart" ]]; then
  510. # dos: Linux
  511. # gpt: Linux filesystem
  512. [[ "$IMAGE_PARTITION_TABLE" != "gpt" ]] &&
  513. local type="83" ||
  514. local type="0FC63DAF-8483-4772-8E79-3D69D8477DE4"
  515. # no `size` argument mean "as much as possible"
  516. echo "$rootpart : name=\"rootfs\", start=${next}MiB, type=${type}"
  517. fi
  518. } | sfdisk ${SDCARD}.raw >> "${DEST}/${LOG_SUBPATH}/install.log" 2>&1 ||
  519. exit_with_error "Partition fail. Please check" "${DEST}/${LOG_SUBPATH}/install.log"
  520. fi
  521. call_extension_method "post_create_partitions" <<- 'POST_CREATE_PARTITIONS'
  522. *called after all partitions are created, but not yet formatted*
  523. POST_CREATE_PARTITIONS
  524. # stage: mount image
  525. # lock access to loop devices
  526. exec {FD}> /var/lock/orangepi-debootstrap-losetup
  527. flock -x $FD
  528. LOOP=$(losetup -f)
  529. [[ -z $LOOP ]] && exit_with_error "Unable to find free loop device"
  530. check_loop_device "$LOOP"
  531. losetup $LOOP ${SDCARD}.raw
  532. # loop device was grabbed here, unlock
  533. flock -u $FD
  534. partprobe $LOOP
  535. # stage: create fs, mount partitions, create fstab
  536. rm -f $SDCARD/etc/fstab
  537. if [[ -n $rootpart ]]; then
  538. local rootdevice="${LOOP}p${rootpart}"
  539. if [[ $CRYPTROOT_ENABLE == yes ]]; then
  540. display_alert "Encrypting root partition with LUKS..." "cryptsetup luksFormat $rootdevice" ""
  541. echo -n $CRYPTROOT_PASSPHRASE | cryptsetup luksFormat $CRYPTROOT_PARAMETERS $rootdevice -
  542. echo -n $CRYPTROOT_PASSPHRASE | cryptsetup luksOpen $rootdevice $ROOT_MAPPER -
  543. display_alert "Root partition encryption complete." "" "ext"
  544. # TODO: pass /dev/mapper to Docker
  545. rootdevice=/dev/mapper/$ROOT_MAPPER # used by `mkfs` and `mount` commands
  546. fi
  547. check_loop_device "$rootdevice"
  548. display_alert "Creating rootfs" "$ROOTFS_TYPE on $rootdevice"
  549. mkfs.${mkfs[$ROOTFS_TYPE]} ${mkopts[$ROOTFS_TYPE]} ${mkopts_label[$ROOTFS_TYPE]:+${mkopts_label[$ROOTFS_TYPE]}"$ROOT_FS_LABEL"} $rootdevice >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1
  550. [[ $ROOTFS_TYPE == ext4 ]] && tune2fs -o journal_data_writeback $rootdevice > /dev/null
  551. if [[ $ROOTFS_TYPE == btrfs && $BTRFS_COMPRESSION != none ]]; then
  552. local fscreateopt="-o compress-force=${BTRFS_COMPRESSION}"
  553. fi
  554. mount ${fscreateopt} $rootdevice $MOUNT/
  555. # create fstab (and crypttab) entry
  556. if [[ $CRYPTROOT_ENABLE == yes ]]; then
  557. # map the LUKS container partition via its UUID to be the 'cryptroot' device
  558. echo "$ROOT_MAPPER UUID=$(blkid -s UUID -o value ${LOOP}p${rootpart}) none luks" >> $SDCARD/etc/crypttab
  559. local rootfs=$rootdevice # used in fstab
  560. else
  561. local rootfs="UUID=$(blkid -s UUID -o value $rootdevice)"
  562. fi
  563. echo "$rootfs / ${mkfs[$ROOTFS_TYPE]} defaults,noatime${mountopts[$ROOTFS_TYPE]} 0 1" >> $SDCARD/etc/fstab
  564. else
  565. # update_initramfs will fail if /lib/modules/ doesn't exist
  566. mount --bind --make-private $SDCARD $MOUNT/
  567. echo "/dev/nfs / nfs defaults 0 0" >> $SDCARD/etc/fstab
  568. fi
  569. if [[ -n $bootpart ]]; then
  570. display_alert "Creating /boot" "$bootfs on ${LOOP}p${bootpart}"
  571. check_loop_device "${LOOP}p${bootpart}"
  572. mkfs.${mkfs[$bootfs]} ${mkopts[$bootfs]} ${mkopts_label[$bootfs]:+${mkopts_label[$bootfs]}"$BOOT_FS_LABEL"} ${LOOP}p${bootpart} >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1
  573. mkdir -p $MOUNT/boot/
  574. mount ${LOOP}p${bootpart} $MOUNT/boot/
  575. echo "UUID=$(blkid -s UUID -o value ${LOOP}p${bootpart}) /boot ${mkfs[$bootfs]} defaults${mountopts[$bootfs]} 0 2" >> $SDCARD/etc/fstab
  576. fi
  577. if [[ -n $uefipart ]]; then
  578. display_alert "Creating EFI partition" "FAT32 ${UEFI_MOUNT_POINT} on ${LOOP}p${uefipart} label ${UEFI_FS_LABEL}"
  579. check_loop_device "${LOOP}p${uefipart}"
  580. mkfs.fat -F32 -n "${UEFI_FS_LABEL}" ${LOOP}p${uefipart} >> "${DEST}"/debug/install.log 2>&1
  581. mkdir -p "${MOUNT}${UEFI_MOUNT_POINT}"
  582. mount ${LOOP}p${uefipart} "${MOUNT}${UEFI_MOUNT_POINT}"
  583. echo "UUID=$(blkid -s UUID -o value ${LOOP}p${uefipart}) ${UEFI_MOUNT_POINT} vfat defaults 0 2" >> $SDCARD/etc/fstab
  584. fi
  585. echo "tmpfs /tmp tmpfs defaults,nosuid 0 0" >> $SDCARD/etc/fstab
  586. call_extension_method "format_partitions" <<- 'FORMAT_PARTITIONS'
  587. *if you created your own partitions, this would be a good time to format them*
  588. The loop device is mounted, so ${LOOP}p1 is it's first partition etc.
  589. FORMAT_PARTITIONS
  590. # stage: adjust boot script or boot environment
  591. if [[ -f $SDCARD/boot/orangepiEnv.txt ]]; then
  592. if [[ $CRYPTROOT_ENABLE == yes ]]; then
  593. echo "rootdev=$rootdevice cryptdevice=UUID=$(blkid -s UUID -o value ${LOOP}p${rootpart}):$ROOT_MAPPER" >> $SDCARD/boot/orangepiEnv.txt
  594. else
  595. echo "rootdev=$rootfs" >> $SDCARD/boot/orangepiEnv.txt
  596. fi
  597. echo "rootfstype=$ROOTFS_TYPE" >> $SDCARD/boot/orangepiEnv.txt
  598. elif [[ $rootpart != 1 ]] && [[ $SRC_EXTLINUX != yes ]]; then
  599. local bootscript_dst=${BOOTSCRIPT##*:}
  600. sed -i 's/mmcblk0p1/mmcblk0p2/' $SDCARD/boot/$bootscript_dst
  601. sed -i -e "s/rootfstype=ext4/rootfstype=$ROOTFS_TYPE/" \
  602. -e "s/rootfstype \"ext4\"/rootfstype \"$ROOTFS_TYPE\"/" $SDCARD/boot/$bootscript_dst
  603. fi
  604. # if we have boot.ini = remove orangepiEnv.txt and add UUID there if enabled
  605. if [[ -f $SDCARD/boot/boot.ini ]]; then
  606. sed -i -e "s/rootfstype \"ext4\"/rootfstype \"$ROOTFS_TYPE\"/" $SDCARD/boot/boot.ini
  607. if [[ $CRYPTROOT_ENABLE == yes ]]; then
  608. local rootpart="UUID=$(blkid -s UUID -o value ${LOOP}p${rootpart})"
  609. sed -i 's/^setenv rootdev .*/setenv rootdev "\/dev\/mapper\/'$ROOT_MAPPER' cryptdevice='$rootpart':'$ROOT_MAPPER'"/' $SDCARD/boot/boot.ini
  610. else
  611. sed -i 's/^setenv rootdev .*/setenv rootdev "'$rootfs'"/' $SDCARD/boot/boot.ini
  612. fi
  613. if [[ $LINUXFAMILY != meson64 ]]; then
  614. [[ -f $SDCARD/boot/orangepiEnv.txt ]] && rm $SDCARD/boot/orangepiEnv.txt
  615. fi
  616. fi
  617. # if we have a headless device, set console to DEFAULT_CONSOLE
  618. if [[ -n $DEFAULT_CONSOLE && -f $SDCARD/boot/orangepiEnv.txt ]]; then
  619. if grep -lq "^console=" $SDCARD/boot/orangepiEnv.txt; then
  620. sed -i "s/^console=.*/console=$DEFAULT_CONSOLE/" $SDCARD/boot/orangepiEnv.txt
  621. else
  622. echo "console=$DEFAULT_CONSOLE" >> $SDCARD/boot/orangepiEnv.txt
  623. fi
  624. fi
  625. # recompile .cmd to .scr if boot.cmd exists
  626. if [[ -f $SDCARD/boot/boot.cmd ]]; then
  627. if [ -z $BOOTSCRIPT_OUTPUT ]; then BOOTSCRIPT_OUTPUT=boot.scr; fi
  628. mkimage -C none -A arm -T script -d $SDCARD/boot/boot.cmd $SDCARD/boot/$BOOTSCRIPT_OUTPUT > /dev/null 2>&1
  629. fi
  630. # create extlinux config
  631. if [[ -f $SDCARD/boot/extlinux/extlinux.conf ]]; then
  632. echo " append root=$rootfs $SRC_CMDLINE $MAIN_CMDLINE" >> $SDCARD/boot/extlinux/extlinux.conf
  633. [[ -f $SDCARD/boot/orangepiEnv.txt ]] && rm $SDCARD/boot/orangepiEnv.txt
  634. fi
  635. }
  636. # update_initramfs
  637. #
  638. # this should be invoked as late as possible for any modifications by
  639. # customize_image (userpatches) and prepare_partitions to be reflected in the
  640. # final initramfs
  641. #
  642. # especially, this needs to be invoked after /etc/crypttab has been created
  643. # for cryptroot-unlock to work:
  644. # https://serverfault.com/questions/907254/cryproot-unlock-with-dropbear-timeout-while-waiting-for-askpass
  645. #
  646. # since Debian buster, it has to be called within create_image() on the $MOUNT
  647. # path instead of $SDCARD (which can be a tmpfs and breaks cryptsetup-initramfs).
  648. #
  649. update_initramfs()
  650. {
  651. local chroot_target=$1
  652. local target_dir=$(
  653. find ${chroot_target}/lib/modules/ -maxdepth 1 -type d -name "*${VER}*"
  654. )
  655. if [ "$target_dir" != "" ]; then
  656. update_initramfs_cmd="update-initramfs -uv -k $(basename $target_dir)"
  657. else
  658. exit_with_error "No kernel installed for the version" "${VER}"
  659. fi
  660. display_alert "Updating initramfs..." "$update_initramfs_cmd" ""
  661. cp /usr/bin/$QEMU_BINARY $chroot_target/usr/bin/
  662. mount_chroot "$chroot_target/"
  663. chroot $chroot_target /bin/bash -c "$update_initramfs_cmd" >> $DEST/${LOG_SUBPATH}/install.log 2>&1 || {
  664. display_alert "Updating initramfs FAILED, see:" "$DEST/${LOG_SUBPATH}/install.log" "err"
  665. exit 23
  666. }
  667. display_alert "Updated initramfs." "for details see: $DEST/${LOG_SUBPATH}/install.log" "info"
  668. display_alert "Re-enabling" "initramfs-tools hook for kernel"
  669. chroot $chroot_target /bin/bash -c "chmod -v +x /etc/kernel/postinst.d/initramfs-tools" >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1
  670. umount_chroot "$chroot_target/"
  671. rm $chroot_target/usr/bin/$QEMU_BINARY
  672. } #############################################################################
  673. # create_image
  674. #
  675. # finishes creation of image from cached rootfs
  676. #
  677. create_image()
  678. {
  679. # stage: create file name
  680. if [[ $SELECTED_CONFIGURATION == "cli_standard" ]]; then
  681. IMAGE_TYPE=server
  682. elif [[ $SELECTED_CONFIGURATION == "cli_minimal" ]]; then
  683. IMAGE_TYPE=minimal
  684. else
  685. IMAGE_TYPE=desktop
  686. fi
  687. local version="${BOARD^}_${REVISION}_${DISTRIBUTION,}_${RELEASE}_${IMAGE_TYPE}"${DESKTOP_ENVIRONMENT:+_$DESKTOP_ENVIRONMENT}"_linux$(grab_version "$LINUXSOURCEDIR")"
  688. if [[ ${RELEASE} == "raspi" ]]; then
  689. local version="${BOARD^}_${REVISION}_raspios_bullseye_${IMAGE_TYPE}"${DESKTOP_ENVIRONMENT:+_$DESKTOP_ENVIRONMENT}"_linux$(grab_version "$LINUXSOURCEDIR")"
  690. fi
  691. [[ $ROOTFS_TYPE == nfs ]] && version=${version}_nfsboot
  692. destimg=$DEST/images/${version}
  693. rm -rf $destimg
  694. mkdir -p $destimg
  695. if [[ $ROOTFS_TYPE != nfs ]]; then
  696. display_alert "Copying files to" "/"
  697. echo -e "\nCopying files to [/]" >>"${DEST}"/${LOG_SUBPATH}/install.log
  698. rsync -aHWXh \
  699. --exclude="/boot/*" \
  700. --exclude="/dev/*" \
  701. --exclude="/proc/*" \
  702. --exclude="/run/*" \
  703. --exclude="/tmp/*" \
  704. --exclude="/sys/*" \
  705. --info=progress0,stats1 $SDCARD/ $MOUNT/ >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1
  706. else
  707. display_alert "Creating rootfs archive" "rootfs.tgz" "info"
  708. tar cp --xattrs --directory=$SDCARD/ --exclude='./boot/*' --exclude='./dev/*' --exclude='./proc/*' --exclude='./run/*' --exclude='./tmp/*' \
  709. --exclude='./sys/*' . | pv -p -b -r -s $(du -sb $SDCARD/ | cut -f1) -N "rootfs.tgz" | gzip -c > $destimg/${version}-rootfs.tgz
  710. fi
  711. # stage: rsync /boot
  712. display_alert "Copying files to" "/boot"
  713. echo -e "\nCopying files to [/boot]" >>"${DEST}"/${LOG_SUBPATH}/install.log
  714. if [[ $(findmnt --target $MOUNT/boot -o FSTYPE -n) == vfat ]]; then
  715. # fat32
  716. rsync -rLtWh \
  717. --info=progress0,stats1 \
  718. --log-file="${DEST}"/${LOG_SUBPATH}/install.log $SDCARD/boot $MOUNT >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1
  719. else
  720. # ext4
  721. rsync -aHWXh \
  722. --info=progress0,stats1 \
  723. --log-file="${DEST}"/${LOG_SUBPATH}/install.log $SDCARD/boot $MOUNT >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1
  724. fi
  725. call_extension_method "pre_update_initramfs" "config_pre_update_initramfs" << 'PRE_UPDATE_INITRAMFS'
  726. *allow config to hack into the initramfs create process*
  727. Called after rsync has synced both `/root` and `/root` on the target, but before calling `update_initramfs`.
  728. PRE_UPDATE_INITRAMFS
  729. # stage: create final initramfs
  730. [[ -n $KERNELSOURCE ]] && {
  731. update_initramfs $MOUNT
  732. }
  733. # DEBUG: print free space
  734. local freespace=$(LC_ALL=C df -h)
  735. echo $freespace >> $DEST/${LOG_SUBPATH}/debootstrap.log
  736. display_alert "Free SD cache" "$(echo -e "$freespace" | grep $SDCARD | awk '{print $5}')" "info"
  737. display_alert "Mount point" "$(echo -e "$freespace" | grep $MOUNT | head -1 | awk '{print $5}')" "info"
  738. # stage: write u-boot
  739. write_uboot $LOOP
  740. # fix wrong / permissions
  741. chmod 755 $MOUNT
  742. call_extension_method "pre_umount_final_image" "config_pre_umount_final_image" << 'PRE_UMOUNT_FINAL_IMAGE'
  743. *allow config to hack into the image before the unmount*
  744. Called before unmounting both `/root` and `/boot`.
  745. PRE_UMOUNT_FINAL_IMAGE
  746. # unmount /boot/efi first, then /boot, rootfs third, image file last
  747. sync
  748. [[ $UEFISIZE != 0 ]] && umount -l "${MOUNT}${UEFI_MOUNT_POINT}"
  749. [[ $BOOTSIZE != 0 ]] && umount -l $MOUNT/boot
  750. [[ $ROOTFS_TYPE != nfs ]] && umount -l $MOUNT
  751. [[ $CRYPTROOT_ENABLE == yes ]] && cryptsetup luksClose $ROOT_MAPPER
  752. call_extension_method "post_umount_final_image" "config_post_umount_final_image" << 'POST_UMOUNT_FINAL_IMAGE'
  753. *allow config to hack into the image after the unmount*
  754. Called after unmounting both `/root` and `/boot`.
  755. POST_UMOUNT_FINAL_IMAGE
  756. # to make sure its unmounted
  757. while grep -Eq '(${MOUNT}|${DESTIMG})' /proc/mounts
  758. do
  759. display_alert "Wait for unmount" "${MOUNT}" "info"
  760. sleep 5
  761. done
  762. losetup -d $LOOP
  763. rm -rf --one-file-system $DESTIMG $MOUNT
  764. mkdir -p $DESTIMG
  765. mv ${SDCARD}.raw $DESTIMG/${version}.img
  766. FINALDEST=${destimg}
  767. # custom post_build_image_modify hook to run before fingerprinting and compression
  768. [[ $(type -t post_build_image_modify) == function ]] && display_alert "Custom Hook Detected" "post_build_image_modify" "info" && post_build_image_modify "${DESTIMG}/${version}.img"
  769. if [[ $BUILD_ALL != yes ]]; then
  770. if [[ $COMPRESS_OUTPUTIMAGE == "" || $COMPRESS_OUTPUTIMAGE == no ]]; then
  771. COMPRESS_OUTPUTIMAGE="sha,gpg,img"
  772. elif [[ $COMPRESS_OUTPUTIMAGE == yes ]]; then
  773. COMPRESS_OUTPUTIMAGE="sha,gpg,7z"
  774. fi
  775. if [[ $COMPRESS_OUTPUTIMAGE == *gz* ]]; then
  776. display_alert "Compressing" "${DESTIMG}/${version}.img.gz" "info"
  777. pigz -3 < $DESTIMG/${version}.img > $DESTIMG/${version}.img.gz
  778. compression_type=".gz"
  779. fi
  780. if [[ $COMPRESS_OUTPUTIMAGE == *xz* ]]; then
  781. display_alert "Compressing" "${DESTIMG}/${version}.img.xz" "info"
  782. # compressing consumes a lot of memory we don't have. Waiting for previous packing job to finish helps to run a lot more builds in parallel
  783. available_cpu=$(grep -c 'processor' /proc/cpuinfo)
  784. [[ ${BUILD_ALL} == yes ]] && available_cpu=$(( $available_cpu * 30 / 100 )) # lets use 20% of resources in case of build-all
  785. [[ ${available_cpu} -gt 8 ]] && available_cpu=8 # using more cpu cores for compressing is pointless
  786. available_mem=$(LC_ALL=c free | grep Mem | awk '{print $4/$2 * 100.0}' | awk '{print int($1)}') # in percentage
  787. # build optimisations when memory drops below 5%
  788. if [[ ${BUILD_ALL} == yes && ( ${available_mem} -lt 15 || $(ps -uax | grep "pixz" | wc -l) -gt 4 )]]; then
  789. while [[ $(ps -uax | grep "pixz" | wc -l) -gt 2 ]]
  790. do echo -en "#"
  791. sleep 20
  792. done
  793. fi
  794. pixz -7 -p ${available_cpu} -f $(expr ${available_cpu} + 2) < $DESTIMG/${version}.img > ${DESTIMG}/${version}.img.xz
  795. compression_type=".xz"
  796. fi
  797. if [[ $COMPRESS_OUTPUTIMAGE == *img* || $COMPRESS_OUTPUTIMAGE == *7z* ]]; then
  798. # mv $DESTIMG/${version}.img ${FINALDEST}/${version}.img || exit 1
  799. compression_type=""
  800. fi
  801. if [[ $COMPRESS_OUTPUTIMAGE == *sha* ]]; then
  802. cd ${DESTIMG}
  803. display_alert "SHA256 calculating" "${version}.img${compression_type}" "info"
  804. sha256sum -b ${version}.img${compression_type} > ${version}.img${compression_type}.sha
  805. fi
  806. if [[ $COMPRESS_OUTPUTIMAGE == *gpg* ]]; then
  807. cd ${DESTIMG}
  808. if [[ -n $GPG_PASS ]]; then
  809. display_alert "GPG signing" "${version}.img${compression_type}" "info"
  810. [[ -n ${SUDO_USER} ]] && sudo chown -R ${SUDO_USER}:${SUDO_USER} "${DESTIMG}"/
  811. echo "${GPG_PASS}" | sudo -H -u ${SUDO_USER} bash -c "gpg --passphrase-fd 0 --armor --detach-sign --pinentry-mode loopback --batch --yes ${DESTIMG}/${version}.img${compression_type}" || exit 1
  812. #else
  813. # display_alert "GPG signing skipped - no GPG_PASS" "${version}.img" "wrn"
  814. fi
  815. fi
  816. #fingerprint_image "${DESTIMG}/${version}.img${compression_type}.txt" "${version}"
  817. if [[ $COMPRESS_OUTPUTIMAGE == *7z* ]]; then
  818. display_alert "Compressing" "${DESTIMG}/${version}.7z" "info"
  819. 7za a -t7z -bd -m0=lzma2 -mx=3 -mfb=64 -md=32m -ms=on \
  820. ${DESTIMG}/${version}.7z ${version}.key ${version}.img* >/dev/null 2>&1
  821. find ${DESTIMG}/ -type \
  822. f \( -name "${version}.img" -o -name "${version}.img.asc" -o -name "${version}.img.txt" -o -name "${version}.img.sha" \) -print0 \
  823. >/dev/null 2>&1
  824. fi
  825. fi
  826. #display_alert "Done building" "${DESTIMG}/${version}.img" "info"
  827. display_alert "Done building" "${FINALDEST}/${version}.img" "info"
  828. # call custom post build hook
  829. [[ $(type -t post_build_image) == function ]] && post_build_image "${DESTIMG}/${version}.img"
  830. # move artefacts from temporally directory to its final destination
  831. [[ -n $compression_type ]] && rm $DESTIMG/${version}.img
  832. mv $DESTIMG/${version}* ${FINALDEST}
  833. rm -rf $DESTIMG
  834. # write image to SD card
  835. if [[ $(lsblk "$CARD_DEVICE" 2>/dev/null) && -f ${FINALDEST}/${version}.img ]]; then
  836. # make sha256sum if it does not exists. we need it for comparisson
  837. if [[ -f "${FINALDEST}/${version}".img.sha ]]; then
  838. local ifsha=$(cat ${FINALDEST}/${version}.img.sha | awk '{print $1}')
  839. else
  840. local ifsha=$(sha256sum -b "${FINALDEST}/${version}".img | awk '{print $1}')
  841. fi
  842. display_alert "Writing image" "$CARD_DEVICE ${readsha}" "info"
  843. # write to SD card
  844. pv -p -b -r -c -N "[ .... ] dd" ${FINALDEST}/${version}.img | dd of=$CARD_DEVICE bs=1M iflag=fullblock oflag=direct status=none
  845. call_extension_method "post_write_sdcard" <<- 'POST_BUILD_IMAGE'
  846. *run after writing img to sdcard*
  847. After the image is written to `$CARD_DEVICE`, but before verifying it.
  848. You can still set SKIP_VERIFY=yes to skip verification.
  849. POST_BUILD_IMAGE
  850. if [[ "${SKIP_VERIFY}" != "yes" ]]; then
  851. # read and compare
  852. display_alert "Verifying. Please wait!"
  853. local ofsha=$(dd if=$CARD_DEVICE count=$(du -b ${FINALDEST}/${version}.img | cut -f1) status=none iflag=count_bytes oflag=direct | sha256sum | awk '{print $1}')
  854. if [[ $ifsha == $ofsha ]]; then
  855. display_alert "Writing verified" "${version}.img" "info"
  856. else
  857. display_alert "Writing failed" "${version}.img" "err"
  858. fi
  859. fi
  860. elif [[ `systemd-detect-virt` == 'docker' && -n $CARD_DEVICE ]]; then
  861. # display warning when we want to write sd card under Docker
  862. display_alert "Can't write to $CARD_DEVICE" "Enable docker privileged mode in config-docker.conf" "wrn"
  863. fi
  864. } #############################################################################