freedombone-image-make 8.8KB

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