buildFilesystem.sh 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #!/bin/bash
  2. set -x
  3. set -e
  4. # Build fs, image
  5. # This file is part of PrawnOS (https://www.prawnos.com)
  6. # Copyright (c) 2018 Hal Emmerich <hal@halemmerich.com>
  7. # PrawnOS is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License version 2
  9. # as published by the Free Software Foundation.
  10. # PrawnOS is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. # You should have received a copy of the GNU General Public License
  15. # along with PrawnOS. If not, see <https://www.gnu.org/licenses/>.
  16. #Ensure Sudo
  17. if [ ! $UID = "0" ]
  18. then
  19. echo "Please run this script with sudo, or as root:"
  20. echo "sudo $0 $*"
  21. exit 1
  22. fi
  23. if [ -z "$1" ]
  24. then
  25. echo "No kernel version supplied"
  26. exit 1
  27. fi
  28. if [ -z "$2" ]
  29. then
  30. echo "No debian suite supplied"
  31. exit 1
  32. fi
  33. if [ -z "$3" ]
  34. then
  35. echo "No base file system image filename supplied"
  36. exit 1
  37. fi
  38. if [ -z "$4" ]
  39. then
  40. echo "No prawnos_root path supplied"
  41. exit 1
  42. fi
  43. if [ -z "$5" ]
  44. then
  45. echo "No shared scripts path supplied"
  46. exit 1
  47. fi
  48. if [ -z "$6" ]
  49. then
  50. echo "No Filesystem resources path supplied"
  51. exit 1
  52. fi
  53. KVER=$1
  54. DEBIAN_SUITE=$2
  55. BASE=$3
  56. PRAWNOS_ROOT=$4
  57. PRAWNOS_SHARED_SCRIPTS=$5
  58. PRAWNOS_FILESYSTEM_RESOURCES=$6
  59. outmnt=$(mktemp -d -p "$(pwd)")
  60. outdev=/dev/loop5
  61. install_resources=$PRAWNOS_FILESYSTEM_RESOURCES/InstallResources
  62. build_resources=$PRAWNOS_FILESYSTEM_RESOURCES
  63. build_resources_apt=$build_resources/apt
  64. # Import the package lists, shared scripts
  65. source $PRAWNOS_SHARED_SCRIPTS/*
  66. #A hacky way to ensure the loops are properly unmounted and the temp files are properly deleted.
  67. #Without this, a reboot is sometimes required to properly clean the loop devices and ensure a clean build
  68. cleanup() {
  69. set +e
  70. umount -l $outmnt > /dev/null 2>&1
  71. rmdir $outmnt > /dev/null 2>&1
  72. losetup -d $outdev > /dev/null 2>&1
  73. umount -l $outmnt > /dev/null 2>&1
  74. rmdir $outmnt > /dev/null 2>&1
  75. losetup -d $outdev > /dev/null 2>&1
  76. #delete the base file, we didn't complete our work
  77. rm -rf $BASE
  78. echo "FILESYSTEM BUILD FAILED"
  79. exit 1
  80. }
  81. trap cleanup INT TERM EXIT
  82. # Download, cache externally, and optionally install the specified packages
  83. # 2: mount of the chroot
  84. # 3: list of packages in install
  85. # 4: if true, download, cache, and install. If false just download and cache
  86. apt_install() {
  87. PRAWNOS_ROOT=$1
  88. shift
  89. outmnt=$1
  90. shift
  91. install=$1
  92. shift
  93. package_list=("$@")
  94. chroot $outmnt apt install -y -d ${package_list[@]}
  95. cp "$outmnt/var/cache/apt/archives/"* "$PRAWNOS_ROOT/build/chroot-apt-cache/" || true
  96. if [ "$install" = true ]; then
  97. chroot $outmnt apt install -y ${package_list[@]}
  98. fi
  99. }
  100. #layout the partitons and write filesystem information
  101. create_image() {
  102. dd if=/dev/zero of=$1 bs=$3 count=$4 conv=sparse
  103. parted --script $1 mklabel gpt
  104. cgpt create $1
  105. kernel_start=8192
  106. kernel_size=65536
  107. cgpt add -i 1 -t kernel -b $kernel_start -s $kernel_size -l Kernel -S 1 -T 5 -P 10 $1
  108. #Now the main filesystem
  109. root_start=$(($kernel_start + $kernel_size))
  110. end=`cgpt show $1 | grep 'Sec GPT table' | awk '{print $1}'`
  111. root_size=$(($end - $root_start))
  112. cgpt add -i 2 -t data -b $root_start -s $root_size -l Root $1
  113. # $root_size is in 512 byte blocks while ext4 uses a block size of 1024 bytes
  114. losetup -P $2 $1
  115. mkfs.ext4 -F -b 1024 ${2}p2 $(($root_size / 2))
  116. # mount the / partition
  117. mount -o noatime ${2}p2 $5
  118. }
  119. # create a 2GB image with the Chrome OS partition layout
  120. create_image $BASE $outdev 50M 40 $outmnt
  121. # use default debootstrap mirror if none is specified
  122. if [ "$PRAWNOS_DEBOOTSTRAP_MIRROR" = "" ]
  123. then
  124. PRAWNOS_DEBOOTSTRAP_MIRROR=http://ftp.us.debian.org/debian
  125. fi
  126. # install Debian on it
  127. export DEBIAN_FRONTEND=noninteractive
  128. # need ca-certs, gnupg, openssl to handle https apt links and key adding for deb.prawnos.com
  129. printf -v debootstrap_debs_install_joined '%s,' "${debootstrap_debs_install[@]}"
  130. qemu-debootstrap --arch armhf $DEBIAN_SUITE \
  131. --include ${debootstrap_debs_install_joined%,} \
  132. --keyring=$build_resources_apt/debian-archive-keyring.gpg \
  133. $outmnt \
  134. $PRAWNOS_DEBOOTSTRAP_MIRROR \
  135. --cache-dir=$PRAWNOS_ROOT/build/debootstrap-apt-cache/
  136. chroot $outmnt passwd -d root
  137. #Place the config files and installer script and give them the proper permissions
  138. echo -n PrawnOS > $outmnt/etc/hostname
  139. cp -R $install_resources/ $outmnt/InstallResources/
  140. # and the icons for the lockscreen and app menu
  141. mkdir $outmnt/InstallResources/icons/
  142. cp $build_resources/logo/icons/icon-small.png $outmnt/InstallResources/icons/
  143. cp $build_resources/logo/icons/ascii/* $outmnt/InstallResources/icons/
  144. cp scripts/InstallScripts/* $outmnt/InstallResources/
  145. cp $PRAWNOS_SHARED_SCRIPTS/package_lists.sh $outmnt/InstallResources/
  146. cp scripts/InstallScripts/InstallPrawnOS.sh $outmnt/
  147. chmod +x $outmnt/*.sh
  148. #Setup the chroot for apt
  149. #This is what https://wiki.debian.org/EmDebian/CrossDebootstrap suggests
  150. cp /etc/hosts $outmnt/etc/
  151. cp $build_resources_apt/sources.list $outmnt/etc/apt/sources.list
  152. cp $build_resources_apt/prawnos.list $outmnt/etc/apt/sources.list.d/
  153. sed -i -e "s/suite/$DEBIAN_SUITE/g" $outmnt/etc/apt/sources.list
  154. sed -i -e "s/suite/$DEBIAN_SUITE/g" $outmnt/etc/apt/sources.list.d/prawnos.list
  155. if [ "$DEBIAN_SUITE" != "sid" ]
  156. then
  157. # sid doesn't have updates or security; they're present for all other suites
  158. cat $build_resources_apt/updates.list >> $outmnt/etc/apt/sources.list
  159. sed -i -e "s/suite/$DEBIAN_SUITE/g" $outmnt/etc/apt/sources.list
  160. # sid doesn't have backports; it's present for all other suites
  161. cp $build_resources_apt/backports.list $outmnt/etc/apt/sources.list.d/
  162. sed -i -e "s/suite/$DEBIAN_SUITE/g" $outmnt/etc/apt/sources.list.d/backports.list
  163. #setup apt pinning
  164. cp $build_resources_apt/backports.pref $outmnt/etc/apt/preferences.d/
  165. sed -i -e "s/suite/$DEBIAN_SUITE/g" $outmnt/etc/apt/preferences.d/backports.pref
  166. # Install sid (unstable) as an additional source for bleeding edge packages.
  167. cp $build_resources_apt/sid.list $outmnt/etc/apt/sources.list.d/
  168. #setup apt pinning
  169. cp $build_resources_apt/sid.pref $outmnt/etc/apt/preferences.d/
  170. fi
  171. if [ "$DEBIAN_SUITE" = "buster" ]
  172. then
  173. # Install bullseye (testing) as an additional source
  174. cp $build_resources_apt/bullseye.list $outmnt/etc/apt/sources.list.d/
  175. #setup apt pinning
  176. cp $build_resources_apt/bullseye.pref $outmnt/etc/apt/preferences.d/
  177. fi
  178. #Bring in the deb.prawnos.com gpg keyring
  179. cp $build_resources_apt/deb.prawnos.com.gpg.key $outmnt/InstallResources/
  180. chroot $outmnt apt-key add /InstallResources/deb.prawnos.com.gpg.key
  181. chroot $outmnt apt update
  182. #Setup the locale
  183. cp $build_resources/locale.gen $outmnt/etc/locale.gen
  184. chroot $outmnt locale-gen
  185. #Copy in the apt cache
  186. cp "$PRAWNOS_ROOT/build/chroot-apt-cache/"* "$outmnt/var/cache/apt/archives/" || true
  187. echo IMAGE SIZE
  188. df -h
  189. #Make apt retry on download failure
  190. chroot $outmnt echo "APT::Acquire::Retries \"3\";" > /etc/apt/apt.conf.d/80-retries
  191. #Install the base packages
  192. chroot $outmnt apt update
  193. apt_install $PRAWNOS_ROOT $outmnt true ${base_debs_install[@]}
  194. #add the live-boot fstab
  195. cp -f $build_resources/external_fstab $outmnt/etc/fstab
  196. chmod 644 $outmnt/etc/fstab
  197. #Cleanup to reduce install size
  198. chroot $outmnt apt-get autoremove --purge
  199. chroot $outmnt apt-get clean
  200. #Download the shared packages to be installed by Install.sh:
  201. apt_install $PRAWNOS_ROOT $outmnt false ${base_debs_download[@]}
  202. ## DEs
  203. #Download the xfce packages to be installed by Install.sh:
  204. apt_install $PRAWNOS_ROOT $outmnt false ${xfce_debs_download[@]}
  205. #Download the lxqt packages to be installed by Install.sh:
  206. apt_install $PRAWNOS_ROOT $outmnt false ${lxqt_debs_download[@]}
  207. #Download the gnome packages to be installed by Install.sh:
  208. apt_install $PRAWNOS_ROOT $outmnt false ${gnome_debs_download[@]}
  209. # we want to include all of our built packages in the apt cache for installation later, but we want to let apt download dependencies
  210. # if required
  211. # this gets tricky when we build some of the dependencies. To avoid issues
  212. # first, manually cache the deb
  213. # apt install ./local-package.deb alone doesn't work because apt will resort to downloading it from deb.prawnos.com, which we dont want
  214. # copy into /var/cache/apt/archives to place it in the cache
  215. #next call apt install -d on the ./filename or on the package name and apt will recognize it already has the package cached, so will only cache the dependencies
  216. #Copy the built prawnos debs over to the image, and update apts cache
  217. cd $PRAWNOS_ROOT && make filesystem_packages_install INSTALL_TARGET=$outmnt/var/cache/apt/archives/
  218. chroot $outmnt apt install -y ${prawnos_base_debs_prebuilt_install[@]}
  219. chroot $outmnt apt install -y -d ${prawnos_base_debs_prebuilt_download[@]}
  220. chroot $outmnt apt install -y -d ${prawnos_xfce_debs_prebuilt_download[@]}
  221. ## GPU support
  222. #download mesa packages
  223. apt_install $PRAWNOS_ROOT $outmnt false ${mesa_debs_download[@]}
  224. #Cleanup hosts
  225. rm -rf $outmnt/etc/hosts #This is what https://wiki.debian.org/EmDebian/CrossDebootstrap suggests
  226. echo -n "127.0.0.1 PrawnOS" > $outmnt/etc/hosts
  227. #Cleanup apt retry
  228. chroot $outmnt rm -f /etc/apt/apt.conf.d/80-retries
  229. # do a non-error cleanup
  230. umount -l $outmnt > /dev/null 2>&1
  231. rmdir $outmnt > /dev/null 2>&1
  232. losetup -d $outdev > /dev/null 2>&1
  233. echo "DONE!"
  234. trap - INT TERM EXIT