buildFilesystem.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #!/bin/bash
  2. set -x
  3. set -e
  4. # Build fs, image
  5. # This file is part of PrawnOS (http://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. KVER=$1
  39. DEBIAN_SUITE=$2
  40. BASE=$3
  41. outmnt=$(mktemp -d -p `pwd`)
  42. outdev=/dev/loop5
  43. install_resources=resources/InstallResources
  44. build_resources=resources/BuildResources
  45. #HACK XSECURELOCK our usage of stable and unstable packages has caught up to us. We end up carrying conflicting files if
  46. # we grab build-essential from stable and xsecurelock from unstable. This was fixed by grabbing build-essential from
  47. # unstable as well, but that conflicts with some of the gnome packages it seems. Luckily, we can now build xsecurelock
  48. # for buster instead of grabbing it from unstable.
  49. # I'm rethinking the build system to make (heh) this more elegant, but for now to get the build fixed I'll implement this
  50. XSECURELOCK_PATH=packages/filesystem/xsecurelock
  51. #A hacky way to ensure the loops are properly unmounted and the temp files are properly deleted.
  52. #Without this, a reboot is sometimes required to properly clean the loop devices and ensure a clean build
  53. cleanup() {
  54. set +e
  55. umount -l $outmnt > /dev/null 2>&1
  56. rmdir $outmnt > /dev/null 2>&1
  57. losetup -d $outdev > /dev/null 2>&1
  58. umount -l $outmnt > /dev/null 2>&1
  59. rmdir $outmnt > /dev/null 2>&1
  60. losetup -d $outdev > /dev/null 2>&1
  61. #delete the base file, we didn't complete our work
  62. rm -rf $BASE
  63. echo "FILESYSTEM BUILD FAILED"
  64. exit 1
  65. }
  66. trap cleanup INT TERM EXIT
  67. #layout the partitons and write filesystem information
  68. create_image() {
  69. dd if=/dev/zero of=$1 bs=$3 count=$4 conv=sparse
  70. parted --script $1 mklabel gpt
  71. cgpt create $1
  72. kernel_start=8192
  73. kernel_size=65536
  74. cgpt add -i 1 -t kernel -b $kernel_start -s $kernel_size -l Kernel -S 1 -T 5 -P 10 $1
  75. #Now the main filesystem
  76. root_start=$(($kernel_start + $kernel_size))
  77. end=`cgpt show $1 | grep 'Sec GPT table' | awk '{print $1}'`
  78. root_size=$(($end - $root_start))
  79. cgpt add -i 2 -t data -b $root_start -s $root_size -l Root $1
  80. # $root_size is in 512 byte blocks while ext4 uses a block size of 1024 bytes
  81. losetup -P $2 $1
  82. mkfs.ext4 -F -b 1024 ${2}p2 $(($root_size / 2))
  83. # mount the / partition
  84. mount -o noatime ${2}p2 $5
  85. }
  86. build_install_crossystem() {
  87. # install crossystem
  88. apt install -y vboot-utils
  89. #install clang and pre-reqs
  90. apt install -y clang uuid-dev meson pkg-config cmake libcmocka-dev cargo
  91. flashmap_src=/root/flashmap
  92. mosys_src=/root/mosys
  93. mkdir $flashmap_src
  94. mkdir $mosys_src
  95. #clone flashmap, need to build libfmap
  96. git clone https://github.com/dhendrix/flashmap.git /root/flashmap
  97. cd $flashmap_src && make all
  98. cd $flashmap_src && make install
  99. ldconfig
  100. #clone mosys. Later releases start depending on the minijail library which we would have to build, and that we don't care about anyway on linux
  101. git clone https://chromium.googlesource.com/chromiumos/platform/mosys /root/mosys
  102. cd $mosys_src && git checkout release-R69-10895.B
  103. mkdir $mosys_src/build
  104. # compile the c parts
  105. cd $mosys_src && CFLAGS="-Wno-error" CC=clang meson -Darch=arm $mosys_src/build
  106. cd $mosys_src && ninja -C $mosys_src/build
  107. # install mosys so crossystem can access it. It EXPECTS it to be right here and fails otherwise...
  108. mkdir -p /usr/sbin/
  109. cp --verbose $mosys_src/build/mosys /usr/sbin/
  110. # cleanup the source
  111. rm -rf $flashmap_src
  112. rm -rf $mosys_src
  113. # cleanup the unnecessary build packages, need the noninteractive flag as -y is not enough to avoid prompting users on remove for some reason
  114. DEBIAN_FRONTEND=noninteractive apt-get purge -y --auto-remove clang meson libcmocka-dev cargo cmake pkg-config
  115. }
  116. # create a 2GB image with the Chrome OS partition layout
  117. create_image $BASE $outdev 50M 40 $outmnt
  118. # use default debootstrap mirror if none is specified
  119. if [ "$PRAWNOS_DEBOOTSTRAP_MIRROR" = "" ]
  120. then
  121. PRAWNOS_DEBOOTSTRAP_MIRROR=http://ftp.us.debian.org/debian
  122. fi
  123. # install Debian on it
  124. export DEBIAN_FRONTEND=noninteractive
  125. # need ca-certs, gnupg, openssl to handle https apt links and key adding for deb.prawnos.com
  126. qemu-debootstrap --arch armhf $DEBIAN_SUITE --include openssl,ca-certificates,gnupg,locales,init --keyring=$build_resources/debian-archive-keyring.gpg $outmnt $PRAWNOS_DEBOOTSTRAP_MIRROR
  127. chroot $outmnt passwd -d root
  128. #Place the config files and installer script and give them the proper permissions
  129. echo -n PrawnOS > $outmnt/etc/hostname
  130. cp -R $install_resources/ $outmnt/InstallResources/
  131. # and the icons for the lockscreen and app menu
  132. mkdir $outmnt/InstallResources/icons/
  133. cp $build_resources/logo/icons/icon-small.png $outmnt/InstallResources/icons/
  134. cp $build_resources/logo/icons/ascii/* $outmnt/InstallResources/icons/
  135. cp scripts/InstallScripts/* $outmnt/InstallResources/
  136. cp scripts/InstallScripts/InstallPrawnOS.sh $outmnt/
  137. chmod +x $outmnt/*.sh
  138. #Setup the chroot for apt
  139. #This is what https://wiki.debian.org/EmDebian/CrossDebootstrap suggests
  140. cp /etc/hosts $outmnt/etc/
  141. cp $build_resources/sources.list $outmnt/etc/apt/sources.list
  142. sed -i -e "s/suite/$DEBIAN_SUITE/g" $outmnt/etc/apt/sources.list
  143. if [ "$DEBIAN_SUITE" != "sid" ]
  144. then
  145. # sid doesn't have updates or security; they're present for all other suites
  146. cat $build_resources/updates.list >> $outmnt/etc/apt/sources.list
  147. sed -i -e "s/suite/$DEBIAN_SUITE/g" $outmnt/etc/apt/sources.list
  148. # sid doesn't have backports; it's present for all other suites
  149. cp $build_resources/backports.list $outmnt/etc/apt/sources.list.d/
  150. sed -i -e "s/suite/$DEBIAN_SUITE/g" $outmnt/etc/apt/sources.list.d/backports.list
  151. #setup apt pinning
  152. cp $build_resources/backports.pref $outmnt/etc/apt/preferences.d/
  153. sed -i -e "s/suite/$DEBIAN_SUITE/g" $outmnt/etc/apt/preferences.d/backports.pref
  154. # Install sid (unstable) as an additional source for bleeding edge packages.
  155. cp $build_resources/sid.list $outmnt/etc/apt/sources.list.d/
  156. #setup apt pinning
  157. cp $build_resources/sid.pref $outmnt/etc/apt/preferences.d/
  158. fi
  159. if [ "$DEBIAN_SUITE" = "buster" ]
  160. then
  161. # Install bullseye (testing) as an additional source
  162. cp $build_resources/bullseye.list $outmnt/etc/apt/sources.list.d/
  163. #setup apt pinning
  164. cp $build_resources/bullseye.pref $outmnt/etc/apt/preferences.d/
  165. fi
  166. #Bring in the deb.prawnos.com gpg keyring
  167. cp $build_resources/deb.prawnos.com.gpg.key $outmnt/InstallResources/
  168. chroot $outmnt apt-key add /InstallResources/deb.prawnos.com.gpg.key
  169. chroot $outmnt apt update
  170. #Setup the locale
  171. cp $build_resources/locale.gen $outmnt/etc/locale.gen
  172. chroot $outmnt locale-gen
  173. #Install the base packages
  174. chroot $outmnt apt update
  175. chroot $outmnt apt install -y udev kmod net-tools inetutils-ping traceroute iproute2 isc-dhcp-client wpasupplicant iw alsa-utils cgpt less psmisc netcat-openbsd ca-certificates bzip2 xz-utils ifupdown nano apt-utils git kpartx gdisk parted rsync busybox-static cryptsetup bash-completion libnss-systemd libpam-cap nftables uuid-runtime libgpg-error-l10n libatm1 laptop-detect e2fsprogs-l10n vim
  176. #build and install crossystem/mosys, funky way to call the bash function inside the chroot
  177. export -f build_install_crossystem
  178. chroot $outmnt /bin/bash -ec "build_install_crossystem"
  179. #add the live-boot fstab
  180. cp -f $build_resources/external_fstab $outmnt/etc/fstab
  181. chmod 644 $outmnt/etc/fstab
  182. #Cleanup to reduce install size
  183. chroot $outmnt apt-get autoremove --purge
  184. chroot $outmnt apt-get clean
  185. #Download support for libinput-gestures
  186. #Package is copied into /InstallResources/packages
  187. chroot $outmnt apt install -y libinput-tools xdotool
  188. chroot $outmnt apt install -y build-essential
  189. # we want to include all of our built packages in the apt cache for installation later, but we want to let apt download dependencies
  190. # if required
  191. # this gets tricky when we build some of the dependencies. To avoid issues
  192. # first, manually cache the deb
  193. # apt install ./local-package.deb alone doesn't work because apt will resort to downloading it from deb.prawnos.com, which we dont want
  194. # copy into /var/cache/apt/archives to place it in the cache
  195. #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
  196. #HACK XSECURELOCK
  197. PRAWN_ROOT=$(pwd)
  198. cd $XSECURELOCK_PATH && make
  199. cd $PRAWN_ROOT
  200. cp $XSECURELOCK_PATH/xsecurelock_*_armhf.deb $outmnt/var/cache/apt/archives/
  201. chroot $outmnt apt install -y -d xsecurelock
  202. #Download the packages to be installed by Install.sh:
  203. chroot $outmnt apt-get install -y -d xorg acpi-support lightdm tasksel dpkg librsvg2-common xorg xserver-xorg-input-libinput alsa-utils anacron avahi-daemon eject iw libnss-mdns xdg-utils lxqt crda xfce4 dbus-user-session system-config-printer tango-icon-theme xfce4-power-manager xfce4-terminal xfce4-goodies mousepad vlc libutempter0 xterm numix-gtk-theme dconf-cli dconf-editor plank network-manager-gnome network-manager-openvpn network-manager-openvpn-gnome dtrx emacs accountsservice sudo pavucontrol-qt papirus-icon-theme sysfsutils bluetooth
  204. #Download the gnome packages
  205. chroot $outmnt apt-get install -y -d gdm3 gnome-session dbus-user-session gnome-shell-extensions nautilus nautilus-admin file-roller gnome-software gnome-software-plugin-flatpak gedit gnome-system-monitor gnome-clocks evince gnome-logs gnome-disk-utility gnome-terminal epiphany-browser fonts-cantarell gnome-tweaks seahorse materia-gtk-theme eog libpeas-1.0-0 gir1.2-peas-1.0 libgtk3-perl
  206. #download mesa packages
  207. chroot $outmnt apt-get install -y -d libegl-mesa0 libegl1-mesa libgl1-mesa-dri libglapi-mesa libglu1-mesa libglx-mesa0
  208. chroot $outmnt apt-get install -d -y firefox-esr
  209. # grab chromium as well, since sound is still broken in firefox for some media
  210. chroot $outmnt apt-get install -d -y chromium
  211. #Cleanup hosts
  212. rm -rf $outmnt/etc/hosts #This is what https://wiki.debian.org/EmDebian/CrossDebootstrap suggests
  213. echo -n "127.0.0.1 PrawnOS" > $outmnt/etc/hosts
  214. # do a non-error cleanup
  215. umount -l $outmnt > /dev/null 2>&1
  216. rmdir $outmnt > /dev/null 2>&1
  217. losetup -d $outdev > /dev/null 2>&1
  218. echo "DONE!"
  219. trap - INT TERM EXIT