freedombone-image-make 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Based upon bin/mk-freedombox-image from freedom-maker
  10. # With non-free stuff removed
  11. #
  12. # License
  13. # =======
  14. #
  15. # This program is free software: you can redistribute it and/or modify
  16. # it under the terms of the GNU Affero General Public License as published by
  17. # the Free Software Foundation, either version 3 of the License, or
  18. # (at your option) any later version.
  19. #
  20. # This program is distributed in the hope that it will be useful,
  21. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. # GNU Affero General Public License for more details.
  24. #
  25. # You should have received a copy of the GNU Affero General Public License
  26. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. set -e # Exit on first error
  28. PROJECT_NAME='freedombone'
  29. export TEXTDOMAIN=${PROJECT_NAME}-image-make
  30. export TEXTDOMAINDIR="/usr/share/locale"
  31. PROJECT_INSTALL_DIR=/usr/local/bin
  32. if [ -f "/usr/bin/${PROJECT_NAME}" ]; then
  33. PROJECT_INSTALL_DIR=/usr/bin
  34. fi
  35. source "/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-setup"
  36. #set -x # Enable debugging
  37. IMAGE=$1
  38. export ARCHITECTURE
  39. export MACHINE
  40. export SOURCE
  41. export SUITE
  42. export MYUSERNAME
  43. export MYPASSWORD
  44. export ROUTER_IP_ADDRESS
  45. export BOX_IP_ADDRESS
  46. export NAMESERVER1
  47. export NAMESERVER2
  48. export NAMESERVER3
  49. export NAMESERVER4
  50. export NAMESERVER5
  51. export NAMESERVER6
  52. export PROJECT_NAME
  53. export CONFIG_FILENAME
  54. export SSH_PUBKEY
  55. export GENERIC_IMAGE
  56. export MINIMAL_INSTALL
  57. export SSH_PORT
  58. export ONION_ONLY
  59. export PROJECT_REPO
  60. export DEBIAN_INSTALL_ONLY
  61. export WIFI_INTERFACE
  62. export WIFI_SSID
  63. export WIFI_TYPE
  64. export WIFI_PASSPHRASE
  65. export WIFI_HOTSPOT
  66. export WIFI_NETWORKS_FILE
  67. export VARIANT
  68. export MINIMUM_PASSWORD_LENGTH
  69. export INSECURE
  70. export AMNESIC
  71. export SOCIALINSTANCE
  72. export LOCAL_NAME
  73. export EXTERNAL_DRIVE
  74. export CONTINUOUS_INTEGRATION
  75. # Locate vmdebootstrap program fetched in Makefile
  76. basedir=`pwd`
  77. vendor_dir="${basedir}/vendor"
  78. vmdebootstrap_dir="${vendor_dir}/vmdebootstrap"
  79. if [ -z "$MIRROR" ] || [ -z "$SUITE" ] ; then
  80. echo $"error: Missing MIRROR and SUITE settings inherited from Makefile."
  81. exit 1
  82. fi
  83. # Packages to install in all Freedombone environments
  84. base_pkgs="apt base-files ifupdown initramfs-tools \
  85. logrotate kmod netbase rsyslog udev debian-archive-keyring"
  86. # Packages needed on the beaglebone
  87. beaglebone_pkgs="linux-image-armmp u-boot-tools u-boot"
  88. # Packages needed on the Allwinner A20 devices:
  89. a20_pkgs="linux-image-armmp-lpae u-boot-tools u-boot u-boot-sunxi"
  90. # Packages needed for self-hosted development
  91. dev_pkgs="build-essential devscripts make man-db emacs org-mode git mercurial"
  92. echo Building "$MACHINE" "$PROJECT_NAME" for "$ARCHITECTURE" "$EXTERNAL_DRIVE"
  93. case "$MACHINE" in
  94. beaglebone)
  95. extra_pkgs="$beaglebone_pkgs"
  96. extra_opts="\
  97. --variant minbase \
  98. --bootoffset=2mib \
  99. --bootsize 128M \
  100. --boottype ext2 \
  101. --no-kernel \
  102. --no-extlinux \
  103. --foreign /usr/bin/qemu-arm-static \
  104. --roottype btrfs \
  105. "
  106. ;;
  107. cubietruck | a20-olinuxino-lime | a20-olinuxino-lime2 | a20-olinuxino-micro | cubieboard2 | pcduino3 | banana-pro)
  108. extra_pkgs="$a20_pkgs"
  109. extra_opts="\
  110. --variant minbase \
  111. --bootoffset=1mib \
  112. --bootsize 128M \
  113. --boottype vfat \
  114. --no-kernel \
  115. --no-extlinux \
  116. --foreign /usr/bin/qemu-arm-static \
  117. --roottype btrfs \
  118. "
  119. ;;
  120. qemu)
  121. extra_opts="\
  122. --grub \
  123. --roottype btrfs \
  124. " ;;
  125. usb)
  126. extra_opts="\
  127. --grub \
  128. --roottype btrfs \
  129. " ;;
  130. all)
  131. extra_opts="\
  132. --grub \
  133. --roottype ext4 \
  134. " ;;
  135. esac
  136. # allow for lots of extra fun customization options.
  137. for customization in $CUSTOMIZATIONS
  138. do
  139. case "$customization" in
  140. development)
  141. extra_pkgs="$extra_pkgs $dev_pkgs"
  142. ;;
  143. esac
  144. done
  145. for p in $base_pkgs $extra_pkgs; do
  146. pkgopts="$pkgopts --package $p"
  147. done
  148. # Make sure file is owned by current user, not root
  149. touch "$(dirname "$IMAGE")/${PROJECT_NAME}.log"
  150. if [ -x vendor/vmdebootstrap/vmdebootstrap ] ; then
  151. VMDEBOOTSTRAP=vendor/vmdebootstrap/vmdebootstrap
  152. else
  153. VMDEBOOTSTRAP=vmdebootstrap
  154. fi
  155. PROJECT_INSTALL_DIR=/usr/local/bin
  156. if [ -f "/usr/bin/${PROJECT_NAME}" ]; then
  157. PROJECT_INSTALL_DIR=/usr/bin
  158. fi
  159. echo $'Making customised customisation script'
  160. TEMP_CUSTOMISE="/etc/${PROJECT_NAME}/image-customise"
  161. TEMP_CUSTOMISE2="/tmp/${PROJECT_NAME}-image-customise2"
  162. TEMP_CUSTOMISE3="/tmp/${PROJECT_NAME}-image-customise3"
  163. TEMP_CUSTOMISE4="/tmp/${PROJECT_NAME}-image-customise4"
  164. # cat all the things together
  165. combine_all_scripts $TEMP_CUSTOMISE2
  166. if [ ! -f $TEMP_CUSTOMISE2 ]; then
  167. echo $'Could not combine scripts'
  168. exit 627219
  169. fi
  170. echo $'Changing values within customised customisation script'
  171. cp "$PROJECT_INSTALL_DIR/${PROJECT_NAME}-image-customise" "$TEMP_CUSTOMISE3"
  172. if [ "$MYUSERNAME" ]; then
  173. sed -i "0,/MY_USERNAME=.*/s//MY_USERNAME=${MYUSERNAME}/" "$TEMP_CUSTOMISE3"
  174. fi
  175. if [ "$MYPASSWORD" ]; then
  176. sed -i "s|MY_PASSWORD=.*|MY_PASSWORD=${MYPASSWORD}|g" "$TEMP_CUSTOMISE3"
  177. fi
  178. sed -i "s|ROUTER_IP_ADDRESS=.*|ROUTER_IP_ADDRESS=${ROUTER_IP_ADDRESS}|g" "$TEMP_CUSTOMISE3"
  179. sed -i "s|BOX_IP_ADDRESS=.*|BOX_IP_ADDRESS=${BOX_IP_ADDRESS}|g" "$TEMP_CUSTOMISE3"
  180. sed -i "s|NAMESERVER1=.*|NAMESERVER1=${NAMESERVER1}|g" "$TEMP_CUSTOMISE3"
  181. sed -i "s|NAMESERVER2=.*|NAMESERVER2=${NAMESERVER2}|g" "$TEMP_CUSTOMISE3"
  182. sed -i "s|NAMESERVER3=.*|NAMESERVER3=${NAMESERVER3}|g" "$TEMP_CUSTOMISE3"
  183. sed -i "s|NAMESERVER4=.*|NAMESERVER4=${NAMESERVER4}|g" "$TEMP_CUSTOMISE3"
  184. sed -i "s|NAMESERVER5=.*|NAMESERVER5=${NAMESERVER5}|g" "$TEMP_CUSTOMISE3"
  185. sed -i "s|NAMESERVER6=.*|NAMESERVER6=${NAMESERVER6}|g" "$TEMP_CUSTOMISE3"
  186. sed -i "s|PROJECT_NAME=.*|PROJECT_NAME=${PROJECT_NAME}|g" "$TEMP_CUSTOMISE3"
  187. sed -i "s|CONFIG_FILENAME=.*|CONFIG_FILENAME=${CONFIG_FILENAME}|g" "$TEMP_CUSTOMISE3"
  188. sed -i "s|SSH_PUBKEY=.*|SSH_PUBKEY=${SSH_PUBKEY}|g" "$TEMP_CUSTOMISE3"
  189. sed -i "s|GENERIC_IMAGE=.*|GENERIC_IMAGE=${GENERIC_IMAGE}|g" "$TEMP_CUSTOMISE3"
  190. sed -i "s|MINIMAL_INSTALL=.*|MINIMAL_INSTALL=\"${MINIMAL_INSTALL}\"|g" "$TEMP_CUSTOMISE3"
  191. sed -i "0,/SSH_PORT=.*/s//SSH_PORT=\"${SSH_PORT}\"/" "$TEMP_CUSTOMISE3"
  192. sed -i "s|ONION_ONLY=.*|ONION_ONLY=\"${ONION_ONLY}\"|g" "$TEMP_CUSTOMISE3"
  193. sed -i "s|PROJECT_REPO=.*|PROJECT_REPO=\"${PROJECT_REPO}\"|g" "$TEMP_CUSTOMISE3"
  194. sed -i "s|DEBIAN_INSTALL_ONLY=.*|DEBIAN_INSTALL_ONLY=\"${DEBIAN_INSTALL_ONLY}\"|g" "$TEMP_CUSTOMISE3"
  195. sed -i "s|WIFI_INTERFACE=.*|WIFI_INTERFACE=\"${WIFI_INTERFACE}\"|g" "$TEMP_CUSTOMISE3"
  196. sed -i "s|WIFI_SSID=.*|WIFI_SSID=\"${WIFI_SSID}\"|g" "$TEMP_CUSTOMISE3"
  197. sed -i "s|WIFI_TYPE=.*|WIFI_TYPE=\"${WIFI_TYPE}\"|g" "$TEMP_CUSTOMISE3"
  198. sed -i "s|WIFI_PASSPHRASE=.*|WIFI_PASSPHRASE=\"${WIFI_PASSPHRASE}\"|g" "$TEMP_CUSTOMISE3"
  199. sed -i "s|WIFI_HOTSPOT=.*|WIFI_HOTSPOT=\"${WIFI_HOTSPOT}\"|g" "$TEMP_CUSTOMISE3"
  200. sed -i "s|WIFI_NETWORKS_FILE=.*|WIFI_NETWORKS_FILE=\"${WIFI_NETWORKS_FILE}\"|g" "$TEMP_CUSTOMISE3"
  201. sed -i "s|VARIANT=.*|VARIANT=\"${VARIANT}\"|g" "$TEMP_CUSTOMISE3"
  202. sed -i "s|MINIMUM_PASSWORD_LENGTH=.*|MINIMUM_PASSWORD_LENGTH=\"${MINIMUM_PASSWORD_LENGTH}\"|g" "$TEMP_CUSTOMISE3"
  203. sed -i "s|INSECURE=.*|INSECURE=\"${INSECURE}\"|g" "$TEMP_CUSTOMISE3"
  204. sed -i "s|AMNESIC=.*|AMNESIC=\"${AMNESIC}\"|g" "$TEMP_CUSTOMISE3"
  205. sed -i "s|SOCIALINSTANCE=.*|SOCIALINSTANCE=\"${SOCIALINSTANCE}\"|g" "$TEMP_CUSTOMISE3"
  206. sed -i "s|LOCAL_NAME=.*|LOCAL_NAME=\"${LOCAL_NAME}\"|g" "$TEMP_CUSTOMISE3"
  207. sed -i "s|EXTERNAL_DRIVE=.*|EXTERNAL_DRIVE=\"${EXTERNAL_DRIVE}\"|g" "$TEMP_CUSTOMISE3"
  208. sed -i 's|#!/bin/bash||g' "$TEMP_CUSTOMISE3"
  209. sed -i "s|CONTINUOUS_INTEGRATION=.*|CONTINUOUS_INTEGRATION=${CONTINUOUS_INTEGRATION}|g" "$TEMP_CUSTOMISE3"
  210. cat $TEMP_CUSTOMISE2 $TEMP_CUSTOMISE3 > $TEMP_CUSTOMISE4
  211. if [ -f $TEMP_CUSTOMISE ]; then
  212. sudo rm $TEMP_CUSTOMISE
  213. fi
  214. sudo mv $TEMP_CUSTOMISE4 $TEMP_CUSTOMISE
  215. rm $TEMP_CUSTOMISE2 $TEMP_CUSTOMISE3
  216. if [ ! -f $TEMP_CUSTOMISE ]; then
  217. echo $'Customised customisation script could not be created'
  218. exit 735892
  219. fi
  220. sudo chmod +x $TEMP_CUSTOMISE
  221. echo $'Customised customisation script created'
  222. echo $"starting $VMDEBOOTSTRAP"
  223. # Run vmdebootstrap script to create image
  224. vmdebootstrap_failed=
  225. # shellcheck disable=SC2086
  226. sudo -H \
  227. SUITE="$SUITE" \
  228. MIRROR="$MIRROR" \
  229. BUILD_MIRROR="$BUILD_MIRROR"\
  230. MACHINE="$MACHINE" \
  231. ARCHITECTURE="$ARCHITECTURE" \
  232. SOURCE="$SOURCE" \
  233. CUSTOM_SETUP="$CUSTOM_SETUP" \
  234. EXTERNAL_DRIVE="$EXTERNAL_DRIVE" \
  235. CONTINUOUS_INTEGRATION="$CONTINUOUS_INTEGRATION" \
  236. $VMDEBOOTSTRAP \
  237. --log "$(dirname "$IMAGE")/${PROJECT_NAME}.log" \
  238. --log-level debug \
  239. --size "$IMAGE_SIZE" \
  240. --image "$IMAGE.img" \
  241. --hostname ${PROJECT_NAME} \
  242. --verbose \
  243. --mirror "$BUILD_MIRROR" \
  244. --customize "$TEMP_CUSTOMISE" \
  245. --lock-root-password \
  246. --arch "$ARCHITECTURE" \
  247. --distribution "$SUITE" \
  248. $extra_opts \
  249. $pkgopts
  250. echo $'Removing customised customisation script'
  251. sudo shred -zu $TEMP_CUSTOMISE