buildFilesystem.sh 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #!/bin/sh -xe
  2. # Build fs, image
  3. # This file is part of PrawnOS (http://www.prawnos.com)
  4. # Copyright (c) 2018 Hal Emmerich <hal@halemmerich.com>
  5. # PrawnOS is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License version 2
  7. # as published by the Free Software Foundation.
  8. # PrawnOS is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with PrawnOS. If not, see <https://www.gnu.org/licenses/>.
  14. #Ensure Sudo
  15. if [ ! $UID = "0" ]
  16. then
  17. echo "Please run this script with sudo, or as root:"
  18. echo "sudo $0 $*"
  19. exit 1
  20. fi
  21. if [ -z "$1" ]
  22. then
  23. echo "No kernel version supplied"
  24. exit 1
  25. fi
  26. KVER=$1
  27. [ ! -d build ] && echo "No build folder found, is the kernel built?" && exit
  28. outmnt=$(mktemp -d -p `pwd`)
  29. outdev=/dev/loop5
  30. install_resources=resources/InstallResources
  31. build_resources=resources/BuildResources
  32. #A hacky way to ensure the loops are properly unmounted and the temp files are properly deleted.
  33. #Without this, a reboot is sometimes required to properly clean the loop devices and ensure a clean build
  34. cleanup() {
  35. set +e
  36. umount -l $outmnt > /dev/null 2>&1
  37. rmdir $outmnt > /dev/null 2>&1
  38. losetup -d $outdev > /dev/null 2>&1
  39. set +e
  40. umount -l $outmnt > /dev/null 2>&1
  41. rmdir $outmnt > /dev/null 2>&1
  42. losetup -d $outdev > /dev/null 2>&1
  43. }
  44. trap cleanup INT TERM EXIT
  45. #layout the partitons and write filesystem information
  46. create_image() {
  47. dd if=/dev/zero of=$1 bs=$3 count=$4 conv=sparse
  48. parted --script $1 mklabel gpt
  49. cgpt create $1
  50. kernel_start=8192
  51. kernel_size=65536
  52. boot_size=409600 # 200 MB
  53. cgpt add -i 1 -t kernel -b $kernel_start -s $kernel_size -l Kernel -S 1 -T 5 -P 10 $1
  54. #create the initramfs partiton, aka /boot
  55. boot_start=$(($kernel_start + $kernel_size))
  56. cgpt add -i 2 -t data -b $boot_start -s $boot_size -l Boot $1
  57. #Now the main filesystem
  58. root_start=$(($boot_start + $boot_size))
  59. end=`cgpt show $1 | grep 'Sec GPT table' | awk '{print $1}'`
  60. root_size=$(($end - $root_start))
  61. cgpt add -i 3 -t data -b $root_start -s $root_size -l Root $1
  62. # $size is in 512 byte blocks while ext4 uses a block size of 1024 bytes
  63. losetup -P $2 $1
  64. mkfs.ext4 -F -b 1024 -m 0 ${2}p2 $(($boot_size / 2))
  65. mkfs.ext4 -F -b 1024 -m 0 ${2}p3 $(($root_size / 2))
  66. # mount the / partition
  67. mount -o noatime ${2}p3 $5
  68. # mount the /boot partiton
  69. mkdir -p $5/boot
  70. mount -o noatime ${2}p2 $5/boot
  71. }
  72. # use buster if no suite is specified
  73. if [ "$PRAWNOS_SUITE" = "" ]
  74. then
  75. PRAWNOS_SUITE=buster
  76. fi
  77. # create a 2GB image with the Chrome OS partition layout
  78. create_image PrawnOS-${PRAWNOS_SUITE}-Alpha-c201-libre-2GB.img-BASE $outdev 50M 40 $outmnt
  79. # use default debootstrap mirror if none is specified
  80. if [ "$PRAWNOS_DEBOOTSTRAP_MIRROR" = "" ]
  81. then
  82. PRAWNOS_DEBOOTSTRAP_MIRROR=http://ftp.us.debian.org/debian
  83. fi
  84. # install Debian on it
  85. export DEBIAN_FRONTEND=noninteractive
  86. qemu-debootstrap --arch armhf $PRAWNOS_SUITE --include locales,init --keyring=$build_resources/debian-archive-keyring.gpg $outmnt $PRAWNOS_DEBOOTSTRAP_MIRROR
  87. chroot $outmnt passwd -d root
  88. #Place the config files and installer script and give them the proper permissions
  89. echo -n PrawnOS-Alpha > $outmnt/etc/hostname
  90. cp -R $install_resources/ $outmnt/InstallResources/
  91. # and the icons for the lockscreen and app menu
  92. mkdir $outmnt/InstallResources/icons/
  93. cp $build_resources/logo/icons/icon-small.png $outmnt/InstallResources/icons/
  94. cp $build_resources/logo/icons/ascii/* $outmnt/InstallResources/icons/
  95. cp scripts/InstallScripts/* $outmnt/InstallResources/
  96. cp scripts/InstallScripts/InstallToInternal.sh $outmnt/
  97. chmod +x $outmnt/*.sh
  98. #Setup the chroot for apt
  99. #This is what https://wiki.debian.org/EmDebian/CrossDebootstrap suggests
  100. cp /etc/hosts $outmnt/etc/
  101. cp $build_resources/sources.list $outmnt/etc/apt/sources.list
  102. sed -i -e "s/stretch/$PRAWNOS_SUITE/g" $outmnt/etc/apt/sources.list
  103. if [ "$PRAWNOS_SUITE" != "sid" ]
  104. then
  105. # sid doesn't have updates or security; they're present for all other suites
  106. cat $build_resources/updates.list >> $outmnt/etc/apt/sources.list
  107. sed -i -e "s/stretch/$PRAWNOS_SUITE/g" $outmnt/etc/apt/sources.list
  108. # sid doesn't have backports; it's present for all other suites
  109. cp $build_resources/backports.list $outmnt/etc/apt/sources.list.d/
  110. sed -i -e "s/stretch/$PRAWNOS_SUITE/g" $outmnt/etc/apt/sources.list.d/backports.list
  111. #setup apt pinning
  112. cp $build_resources/backports.pref $outmnt/etc/apt/preferences.d/
  113. sed -i -e "s/stretch/$PRAWNOS_SUITE/g" $outmnt/etc/apt/preferences.d/backports.pref
  114. fi
  115. if [ "$PRAWNOS_SUITE" = "stretch" ]
  116. then
  117. # Install buster as an additional source if the suite is less than buster
  118. cp $build_resources/buster.list $outmnt/etc/apt/sources.list.d/
  119. #setup apt pinning
  120. cp $build_resources/buster.pref $outmnt/etc/apt/preferences.d/
  121. fi
  122. if [ "$PRAWNOS_SUITE" = "stretch" ] || [ "$PRAWNOS_SUITE" = "buster" ]
  123. then
  124. # Install sid as an additional source if the suite is less than bullseye.
  125. # This should be replaced with bullseye after bullseye branches from sid.
  126. cp $build_resources/sid.list $outmnt/etc/apt/sources.list.d/
  127. #setup apt pinning
  128. cp $build_resources/sid.pref $outmnt/etc/apt/preferences.d/
  129. fi
  130. #Setup the locale
  131. cp $build_resources/locale.gen $outmnt/etc/locale.gen
  132. chroot $outmnt locale-gen
  133. #Install the base packages
  134. chroot $outmnt apt update
  135. chroot $outmnt apt install -y initscripts udev kmod net-tools inetutils-ping traceroute iproute2 isc-dhcp-client wpasupplicant iw alsa-utils cgpt vim-tiny less psmisc netcat-openbsd ca-certificates bzip2 xz-utils ifupdown nano apt-utils git kpartx gdisk parted rsync busybox-static cryptsetup
  136. #make the initramfs image that gets copied to partiton 2
  137. #this is not yet fully funtional, needs the kernel parts which are
  138. #added in "injectKernelIntoFS.sh"
  139. #make a skeleton filesystem
  140. initramfs_src=$outmnt/InstallResources/initramfs_src
  141. mkdir -p $initramfs_src
  142. mkdir $initramfs_src/bin
  143. mkdir $initramfs_src/dev
  144. mkdir $initramfs_src/etc
  145. mkdir $initramfs_src/newroot
  146. mkdir $initramfs_src/proc
  147. mkdir $initramfs_src/sys
  148. mkdir $initramfs_src/sbin
  149. mkdir $initramfs_src/run
  150. mkdir $initramfs_src/lib
  151. mkdir $initramfs_src/lib/arm-linux-gnueabihf
  152. #install the few tools we need, and the supporting libs
  153. cp $outmnt/bin/busybox $outmnt/sbin/cryptsetup $initramfs_src/bin/
  154. cp $outmnt/lib/arm-linux-gnueabihf/libblkid.so.1 $initramfs_src/lib/arm-linux-gnueabihf/
  155. cp $outmnt/lib/arm-linux-gnueabihf/libuuid.so.1 $initramfs_src/lib/arm-linux-gnueabihf/
  156. cp $outmnt/lib/arm-linux-gnueabihf/libc.so.6 $initramfs_src/lib/arm-linux-gnueabihf/
  157. cp $outmnt/lib/ld-linux-armhf.so.3 $initramfs_src/lib/
  158. cp $outmnt/sbin/blkid $initramfs_src/bin/
  159. #add the init script
  160. cp $build_resources/initramfs-init $initramfs_src/init
  161. chmod +x $initramfs_src/init
  162. #compress and install
  163. find $initramfs_src -print0 | cpio --null --create --verbose --format=newc | gzip --best > $outmnt/boot/PrawnOS-initramfs.cpio.gz
  164. #add the live-boot fstab
  165. cp -f $build_resources/external_fstab $outmnt/etc/fstab
  166. chmod 644 /etc/fstab
  167. #Cleanup to reduce install size
  168. chroot $outmnt apt-get autoremove --purge
  169. chroot $outmnt apt-get clean
  170. #Download support for libinput-gestures
  171. chroot $outmnt apt install -y libinput-tools xdotool build-essential
  172. #Package is copied into /InstallResources/packages
  173. #Download the packages to be installed by Install.sh:
  174. 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 emacs25 accountsservice sudo pavucontrol-qt xsecurelock
  175. if [ "$PRAWNOS_SUITE" = "stretch" ]
  176. then
  177. CHROMIUM_SUITE=buster
  178. else
  179. CHROMIUM_SUITE=$PRAWNOS_SUITE
  180. fi
  181. # grab chromium as well, since sound is still broken in firefox for some media
  182. chroot $outmnt apt-get -t $CHROMIUM_SUITE install -d -y chromium
  183. if [ "$PRAWNOS_SUITE" = "stretch" ]
  184. then
  185. FIREFOX_SUITE=buster
  186. else
  187. FIREFOX_SUITE=$PRAWNOS_SUITE
  188. fi
  189. # #grab firefox from buster or higher, since stretch is broken
  190. chroot $outmnt apt-get -t $FIREFOX_SUITE install -d -y firefox-esr
  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-Alpha" > $outmnt/etc/hosts
  194. umount -l $outmnt > /dev/null 2>&1
  195. rmdir $outmnt > /dev/null 2>&1
  196. losetup -d $outdev > /dev/null 2>&1
  197. echo "DONE!"
  198. trap - INT TERM EXIT