buildFilesystem.sh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. KVER=$1
  49. DEBIAN_SUITE=$2
  50. BASE=$3
  51. PRAWNOS_ROOT=$4
  52. PRAWNOS_SHARED_SCRIPTS=$5
  53. outmnt=$(mktemp -d -p "$(pwd)")
  54. outdev=/dev/loop5
  55. install_resources=resources/InstallResources
  56. build_resources=resources/BuildResources
  57. # Import the package lists, shared scripts
  58. source $PRAWNOS_SHARED_SCRIPTS/*
  59. #A hacky way to ensure the loops are properly unmounted and the temp files are properly deleted.
  60. #Without this, a reboot is sometimes required to properly clean the loop devices and ensure a clean build
  61. cleanup() {
  62. set +e
  63. umount -l $outmnt > /dev/null 2>&1
  64. rmdir $outmnt > /dev/null 2>&1
  65. losetup -d $outdev > /dev/null 2>&1
  66. umount -l $outmnt > /dev/null 2>&1
  67. rmdir $outmnt > /dev/null 2>&1
  68. losetup -d $outdev > /dev/null 2>&1
  69. #delete the base file, we didn't complete our work
  70. rm -rf $BASE
  71. echo "FILESYSTEM BUILD FAILED"
  72. exit 1
  73. }
  74. trap cleanup INT TERM EXIT
  75. #layout the partitons and write filesystem information
  76. create_image() {
  77. dd if=/dev/zero of=$1 bs=$3 count=$4 conv=sparse
  78. parted --script $1 mklabel gpt
  79. cgpt create $1
  80. kernel_start=8192
  81. kernel_size=65536
  82. cgpt add -i 1 -t kernel -b $kernel_start -s $kernel_size -l Kernel -S 1 -T 5 -P 10 $1
  83. #Now the main filesystem
  84. root_start=$(($kernel_start + $kernel_size))
  85. end=`cgpt show $1 | grep 'Sec GPT table' | awk '{print $1}'`
  86. root_size=$(($end - $root_start))
  87. cgpt add -i 2 -t data -b $root_start -s $root_size -l Root $1
  88. # $root_size is in 512 byte blocks while ext4 uses a block size of 1024 bytes
  89. losetup -P $2 $1
  90. mkfs.ext4 -F -b 1024 ${2}p2 $(($root_size / 2))
  91. # mount the / partition
  92. mount -o noatime ${2}p2 $5
  93. }
  94. # create a 2GB image with the Chrome OS partition layout
  95. create_image $BASE $outdev 50M 40 $outmnt
  96. # use default debootstrap mirror if none is specified
  97. if [ "$PRAWNOS_DEBOOTSTRAP_MIRROR" = "" ]
  98. then
  99. PRAWNOS_DEBOOTSTRAP_MIRROR=http://ftp.us.debian.org/debian
  100. fi
  101. # install Debian on it
  102. export DEBIAN_FRONTEND=noninteractive
  103. # need ca-certs, gnupg, openssl to handle https apt links and key adding for deb.prawnos.com
  104. printf -v debootstrap_debs_install_joined '%s,' "${debootstrap_debs_install[@]}"
  105. qemu-debootstrap --arch armhf $DEBIAN_SUITE \
  106. --include ${debootstrap_debs_install_joined%,} \
  107. --keyring=$build_resources/debian-archive-keyring.gpg \
  108. $outmnt \
  109. $PRAWNOS_DEBOOTSTRAP_MIRROR \
  110. --cache-dir=$PRAWNOS_ROOT/build/apt-cache/
  111. chroot $outmnt passwd -d root
  112. #Place the config files and installer script and give them the proper permissions
  113. echo -n PrawnOS > $outmnt/etc/hostname
  114. cp -R $install_resources/ $outmnt/InstallResources/
  115. # and the icons for the lockscreen and app menu
  116. mkdir $outmnt/InstallResources/icons/
  117. cp $build_resources/logo/icons/icon-small.png $outmnt/InstallResources/icons/
  118. cp $build_resources/logo/icons/ascii/* $outmnt/InstallResources/icons/
  119. cp scripts/InstallScripts/* $outmnt/InstallResources/
  120. cp $PRAWNOS_SHARED_SCRIPTS/package_lists.sh $outmnt/InstallResources/
  121. cp scripts/InstallScripts/InstallPrawnOS.sh $outmnt/
  122. chmod +x $outmnt/*.sh
  123. #Setup the chroot for apt
  124. #This is what https://wiki.debian.org/EmDebian/CrossDebootstrap suggests
  125. cp /etc/hosts $outmnt/etc/
  126. cp $build_resources/sources.list $outmnt/etc/apt/sources.list
  127. sed -i -e "s/suite/$DEBIAN_SUITE/g" $outmnt/etc/apt/sources.list
  128. if [ "$DEBIAN_SUITE" != "sid" ]
  129. then
  130. # sid doesn't have updates or security; they're present for all other suites
  131. cat $build_resources/updates.list >> $outmnt/etc/apt/sources.list
  132. sed -i -e "s/suite/$DEBIAN_SUITE/g" $outmnt/etc/apt/sources.list
  133. # sid doesn't have backports; it's present for all other suites
  134. cp $build_resources/backports.list $outmnt/etc/apt/sources.list.d/
  135. sed -i -e "s/suite/$DEBIAN_SUITE/g" $outmnt/etc/apt/sources.list.d/backports.list
  136. #setup apt pinning
  137. cp $build_resources/backports.pref $outmnt/etc/apt/preferences.d/
  138. sed -i -e "s/suite/$DEBIAN_SUITE/g" $outmnt/etc/apt/preferences.d/backports.pref
  139. # Install sid (unstable) as an additional source for bleeding edge packages.
  140. cp $build_resources/sid.list $outmnt/etc/apt/sources.list.d/
  141. #setup apt pinning
  142. cp $build_resources/sid.pref $outmnt/etc/apt/preferences.d/
  143. fi
  144. if [ "$DEBIAN_SUITE" = "buster" ]
  145. then
  146. # Install bullseye (testing) as an additional source
  147. cp $build_resources/bullseye.list $outmnt/etc/apt/sources.list.d/
  148. #setup apt pinning
  149. cp $build_resources/bullseye.pref $outmnt/etc/apt/preferences.d/
  150. fi
  151. #Bring in the deb.prawnos.com gpg keyring
  152. cp $build_resources/deb.prawnos.com.gpg.key $outmnt/InstallResources/
  153. chroot $outmnt apt-key add /InstallResources/deb.prawnos.com.gpg.key
  154. chroot $outmnt apt update
  155. #Setup the locale
  156. cp $build_resources/locale.gen $outmnt/etc/locale.gen
  157. chroot $outmnt locale-gen
  158. #Install the base packages
  159. chroot $outmnt apt update
  160. chroot $outmnt apt install -y ${base_debs_install[@]}
  161. #add the live-boot fstab
  162. cp -f $build_resources/external_fstab $outmnt/etc/fstab
  163. chmod 644 $outmnt/etc/fstab
  164. #Cleanup to reduce install size
  165. chroot $outmnt apt-get autoremove --purge
  166. chroot $outmnt apt-get clean
  167. #Download the shared packages to be installed by Install.sh:
  168. chroot $outmnt apt-get install -y -d ${base_debs_download[@]}
  169. ## DEs
  170. #Download the xfce packages to be installed by Install.sh:
  171. chroot $outmnt apt-get install -y -d ${xfce_debs_download[@]}
  172. #Download the lxqt packages to be installed by Install.sh:
  173. chroot $outmnt apt-get install -y -d ${lxqt_debs_download[@]}
  174. #Download the gnome packages to be installed by Install.sh:
  175. chroot $outmnt apt-get install -y -d ${gnome_debs_download[@]}
  176. # we want to include all of our built packages in the apt cache for installation later, but we want to let apt download dependencies
  177. # if required
  178. # this gets tricky when we build some of the dependencies. To avoid issues
  179. # first, manually cache the deb
  180. # apt install ./local-package.deb alone doesn't work because apt will resort to downloading it from deb.prawnos.com, which we dont want
  181. # copy into /var/cache/apt/archives to place it in the cache
  182. #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
  183. #Copy the built prawnos debs over to the image, and update apts cache
  184. cd $PRAWNOS_ROOT && make packages_install INSTALL_TARGET=$outmnt/var/cache/apt/archives/
  185. chroot $outmnt apt install -y ${prawnos_base_debs_prebuilt_install[@]}
  186. chroot $outmnt apt install -y -d ${prawnos_base_debs_prebuilt_download[@]}
  187. chroot $outmnt apt install -y -d ${prawnos_xfce_debs_prebuilt_download[@]}
  188. ## GPU support
  189. #download mesa packages
  190. chroot $outmnt apt-get install -y -d ${mesa_debs_download[@]}
  191. #Cleanup hosts
  192. rm -rf $outmnt/etc/hosts #This is what https://wiki.debian.org/EmDebian/CrossDebootstrap suggests
  193. echo -n "127.0.0.1 PrawnOS" > $outmnt/etc/hosts
  194. # do a non-error cleanup
  195. umount -l $outmnt > /dev/null 2>&1
  196. rmdir $outmnt > /dev/null 2>&1
  197. losetup -d $outdev > /dev/null 2>&1
  198. echo "DONE!"
  199. trap - INT TERM EXIT