debootstrap.sh 44 KB

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