freedombone-image-customise 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Based on bin/freedombox-customize from freedom-maker
  12. #
  13. # License
  14. # =======
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. set -e
  29. set -x
  30. PROJECT_NAME='freedombone'
  31. INSTALL_DIR=/root/build
  32. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  33. PROJECT_REPO="https://github.com/bashrc/${PROJECT_NAME}"
  34. VARIANT='full'
  35. # username created by default within a debian image
  36. GENERIC_IMAGE_USERNAME='fbone'
  37. export TEXTDOMAIN=${PROJECT_NAME}-image-customise
  38. export TEXTDOMAINDIR="/usr/share/locale"
  39. # Whether to minimise the number of decisions during interactive install
  40. MINIMAL_INSTALL="yes"
  41. MY_USERNAME='debian'
  42. MY_PASSWORD="${PROJECT_NAME}"
  43. # IP address of the router (gateway)
  44. ROUTER_IP_ADDRESS="192.168.1.254"
  45. # The fixed IP address of the Beaglebone Black on your local network
  46. BOX_IP_ADDRESS="192.168.1.55"
  47. # DNS
  48. NAMESERVER1='213.73.91.35'
  49. NAMESERVER2='85.214.20.141'
  50. # optional configuration file containing freedombone settings
  51. CONFIG_FILENAME=
  52. # Optional ssh public key to allow
  53. SSH_PUBKEY="no"
  54. # Whether this is a generic image for mass redistribution on the interwebs
  55. GENERIC_IMAGE="no"
  56. # default SSH port
  57. SSH_PORT=2222
  58. # for mesh installs
  59. TRACKER_PORT=6969
  60. # Whether sites are accessible only within a Tor browser
  61. ONION_ONLY="no"
  62. # Whether to only install debian and nothing else
  63. DEBIAN_INSTALL_ONLY="no"
  64. # wifi settings
  65. WIFI_INTERFACE='wlan0'
  66. WIFI_SSID=
  67. WIFI_TYPE='wpa2-psk'
  68. WIFI_PASSPHRASE=
  69. WIFI_HOTSPOT='no'
  70. WIFI_NETWORKS_FILE=~/${PROJECT_NAME}-wifi.cfg
  71. enable_eatmydata_override() {
  72. chroot $rootdir apt-get install --no-install-recommends -y eatmydata
  73. if [ -x $rootdir/usr/bin/eatmydata ] && \
  74. [ ! -f $rootdir/etc/apt/apt.conf.d/95debian-edu-install-dpkg-eatmydata ]; then
  75. echo $"info: Adding apt config to call dpkg via eatmydata"
  76. printf "#!/bin/sh\nexec eatmydata dpkg \"\$@\"\n" \
  77. > $rootdir/var/tmp/dpkg-eatmydata
  78. chmod 755 $rootdir/var/tmp/dpkg-eatmydata
  79. cat > $rootdir/etc/apt/apt.conf.d/95debian-edu-install-dpkg-eatmydata <<EOF
  80. Dir::Bin::dpkg "/var/tmp/dpkg-eatmydata";
  81. EOF
  82. else
  83. echo $"error: unable to find /usr/bin/eatmydata after installing the eatmydata package"
  84. fi
  85. }
  86. disable_eatmydata_override() {
  87. for override in \
  88. /etc/apt/apt.conf.d/95debian-edu-install-dpkg-eatmydata \
  89. /var/tmp/dpkg-eatmydata ; do
  90. echo $"info: Removing apt config to call dpkg via eatmydata"
  91. if [ -f $rootdir$override ] ; then
  92. rm -f $rootdir$override
  93. else
  94. echo $"warning: missing $rootdir$override"
  95. fi
  96. done
  97. sync # Flush file buffers before continuing
  98. }
  99. set_apt_sources() {
  100. NEW_MIRROR="$1"
  101. COMPONENTS="main"
  102. cat <<EOF > etc/apt/sources.list
  103. deb $NEW_MIRROR $SUITE $COMPONENTS
  104. deb-src $NEW_MIRROR $SUITE $COMPONENTS
  105. #deb http://security.debian.org/ $SUITE/updates main
  106. #deb-src http://security.debian.org/ $SUITE/updates main
  107. EOF
  108. }
  109. configure_networking() {
  110. if [[ $DEBIAN_INSTALL_ONLY != "no" ]]; then
  111. return
  112. fi
  113. if [[ $GENERIC_IMAGE == "no" ]]; then
  114. echo "# This file describes the network interfaces available on your system
  115. # and how to activate them. For more information, see interfaces(5).
  116. # The loopback network interface
  117. auto lo
  118. iface lo inet loopback
  119. # The primary network interface
  120. auto eth0
  121. iface eth0 inet static
  122. address $BOX_IP_ADDRESS
  123. netmask 255.255.255.0
  124. gateway $ROUTER_IP_ADDRESS
  125. dns-nameservers $NAMESERVER1 $NAMESERVER2
  126. # Example to keep MAC address between reboots
  127. #hwaddress ether B5:A2:BE:3F:1A:FE
  128. # The secondary network interface
  129. #auto eth1
  130. #iface eth1 inet dhcp
  131. # WiFi Example
  132. #auto wlan0
  133. #iface wlan0 inet dhcp
  134. # wpa-ssid \"essid\"
  135. # wpa-psk \"password\"
  136. # Ethernet/RNDIS gadget (g_ether)
  137. # ... or on host side, usbnet and random hwaddr
  138. # Note on some boards, usb0 is automaticly setup with an init script
  139. #iface usb0 inet static
  140. # address 192.168.7.2
  141. # netmask 255.255.255.0
  142. # network 192.168.7.0
  143. # gateway 192.168.7.1" > $rootdir/etc/network/interfaces
  144. hexarray=( 1 2 3 4 5 6 7 8 9 0 a b c d e f )
  145. a=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
  146. b=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
  147. c=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
  148. d=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
  149. e=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
  150. sed -i "s|#hwaddress ether.*|hwaddress ether de:$a:$b:$c:$d:$e|g" \
  151. $rootdir/etc/network/interfaces
  152. fi
  153. sed -i "s/nameserver.*/nameserver $NAMESERVER1/g" $rootdir/etc/resolv.conf
  154. sed -i "/nameserver $NAMESERVER1/a\nameserver $NAMESERVER2" $rootdir/etc/resolv.conf
  155. # change the motd to show further install instructions
  156. echo $"
  157. .---. . .
  158. | | |
  159. |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  160. | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  161. ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  162. Initial base install
  163. Your system is not yet installed. To complete the process run the
  164. following commands, then enter your details.
  165. sudo su
  166. ${PROJECT_NAME} menuconfig
  167. " > $rootdir/etc/motd
  168. }
  169. configure_ssh() {
  170. sed -i "s/Port .*/Port ${SSH_PORT}/g" $rootdir/etc/ssh/sshd_config
  171. if [[ "$SSH_PUBKEY" != "no" ]]; then
  172. if [ ! -d $rootdir/home/$MY_USERNAME/.ssh ]; then
  173. mkdir $rootdir/home/$MY_USERNAME/.ssh
  174. fi
  175. echo "$SSH_PUBKEY" > $rootdir/home/$MY_USERNAME/.ssh/authorized_keys
  176. chroot $rootdir chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.ssh
  177. sed -i 's|PasswordAuthentication.*|PasswordAuthentication no|g' $rootdir/etc/ssh/sshd_config
  178. echo $"Using ssh public key:"
  179. echo $SSH_PUBKEY
  180. echo $'Password ssh authentication turned off'
  181. fi
  182. }
  183. admin_user_sudo() {
  184. echo "$MY_USERNAME ALL=(ALL) ALL" >> $rootdir/etc/sudoers
  185. }
  186. create_generic_image() {
  187. if [[ $DEBIAN_INSTALL_ONLY != "no" ]]; then
  188. return
  189. fi
  190. if [[ $GENERIC_IMAGE == "no" ]]; then
  191. return
  192. fi
  193. if [ $CONFIG_FILENAME ]; then
  194. if [[ "$CONFIG_FILENAME" == *"mesh.cfg"* ]]; then
  195. VARIANT="mesh"
  196. fi
  197. fi
  198. # Don't install any configuration. This will be a base system
  199. if [[ $VARIANT != "mesh" ]]; then
  200. CONFIG_FILENAME=
  201. else
  202. touch $rootdir/root/.initial_mesh_setup
  203. fi
  204. # The presence of this file indicates that the initial
  205. # setup has not yet been completed
  206. touch $rootdir/home/$MY_USERNAME/.initial_setup
  207. chroot $rootdir chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.initial_setup
  208. touch $rootdir/root/.initial_setup
  209. cat >> $rootdir/home/$MY_USERNAME/.bashrc <<EOF
  210. # initial setup of the system
  211. if [ -f ~/.initial_setup ]; then
  212. clear
  213. echo "
  214. .---. . .
  215. | | |
  216. |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  217. | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  218. ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  219. Initial setup process
  220. Please enter your password a second time.
  221. "
  222. sudo su
  223. fi
  224. EOF
  225. echo '# initial setup of the system' >> $rootdir/root/.bashrc
  226. echo 'if [ -f ~/.initial_setup ]; then' >> $rootdir/root/.bashrc
  227. echo ' if [ -f ~/login.txt ]; then' >> $rootdir/root/.bashrc
  228. echo ' NEW_USER_PASSWORD=$(printf `cat ~/login.txt`)' >> $rootdir/root/.bashrc
  229. echo ' else' >> $rootdir/root/.bashrc
  230. echo ' ENTROPY=$(cat /proc/sys/kernel/random/entropy_avail)' >> $rootdir/root/.bashrc
  231. echo ' if [ $ENTROPY -lt 500 ]; then' >> $rootdir/root/.bashrc
  232. ENTROPY_MESSAGE1=$'Initial setup process'
  233. ENTROPY_MESSAGE2=$'Password Generation'
  234. ENTROPY_MESSAGE3=$'WARNING: The entropy available on this system is too low to generate a password.\n\nThe installation process cannot continue.'
  235. echo " dialog --backtitle \"${ENTROPY_MESSAGE1}\" --title \"${ENTROPY_MESSAGE2}\" --msgbox \"${ENTROPY_MESSAGE3}\" 8 50" >> $rootdir/root/.bashrc
  236. echo ' exit' >> $rootdir/root/.bashrc
  237. echo ' fi' >> $rootdir/root/.bashrc
  238. echo ' NEW_USER_PASSWORD="$(openssl rand -base64 12 | cut -c1-10)"' >> $rootdir/root/.bashrc
  239. echo ' fi' >> $rootdir/root/.bashrc
  240. echo ' echo "${NEW_USER_PASSWORD}" > ~/login.txt' >> $rootdir/root/.bashrc
  241. echo ' clear' >> $rootdir/root/.bashrc
  242. echo ' echo ""' >> $rootdir/root/.bashrc
  243. NEW_LOGIN_PASSWORD_MESSAGE1=$'Your new login password is:'
  244. NEW_LOGIN_PASSWORD_MESSAGE2=$'Use it whenever you wish to ssh into this system.'
  245. NEW_LOGIN_PASSWORD_MESSAGE3=$'IMPORTANT: Please take a moment to enter the above password into a\npassword manager or write it down somewhere.'
  246. PRESS_KEY_MESSAGE=$'Press any key to continue...'
  247. echo " echo \"${NEW_LOGIN_PASSWORD_MESSAGE1}\"" >> $rootdir/root/.bashrc
  248. echo ' echo ""' >> $rootdir/root/.bashrc
  249. echo ' toilet "${NEW_USER_PASSWORD}"' >> $rootdir/root/.bashrc
  250. echo ' echo ""' >> $rootdir/root/.bashrc
  251. echo ' echo " ${NEW_USER_PASSWORD}"' >> $rootdir/root/.bashrc
  252. echo ' echo ""' >> $rootdir/root/.bashrc
  253. echo " echo \"${NEW_LOGIN_PASSWORD_MESSAGE2}\"" >> $rootdir/root/.bashrc
  254. echo ' echo ""' >> $rootdir/root/.bashrc
  255. echo " echo \"${NEW_LOGIN_PASSWORD_MESSAGE3}\"" >> $rootdir/root/.bashrc
  256. echo ' echo ""' >> $rootdir/root/.bashrc
  257. echo " read -n1 -r -p \"${PRESS_KEY_MESSAGE}\" key" >> $rootdir/root/.bashrc
  258. # change the password for the admin user
  259. echo -n " echo \"${MY_USERNAME}:" >> $rootdir/root/.bashrc
  260. echo '$(printf `cat ~/login.txt`)"|chpasswd' >> $rootdir/root/.bashrc
  261. # update before continuing
  262. echo " cd /root/${PROJECT_NAME}" >> $rootdir/root/.bashrc
  263. echo " git stash" >> $rootdir/root/.bashrc
  264. echo " git pull" >> $rootdir/root/.bashrc
  265. echo " make install" >> $rootdir/root/.bashrc
  266. if [[ $VARIANT != "mesh" ]]; then
  267. if [[ $ONION_ONLY == "no" ]]; then
  268. if [[ $MINIMAL_INSTALL == "no" ]]; then
  269. echo " ${PROJECT_NAME} menuconfig-full" >> $rootdir/root/.bashrc
  270. else
  271. echo " ${PROJECT_NAME} menuconfig" >> $rootdir/root/.bashrc
  272. fi
  273. else
  274. echo " ${PROJECT_NAME} menuconfig-onion" >> $rootdir/root/.bashrc
  275. fi
  276. else
  277. echo " echo ''" >> $rootdir/root/.bashrc
  278. fi
  279. echo ' if [ "$?" = "0" ]; then' >> $rootdir/root/.bashrc
  280. echo " if [ -f ~/${PROJECT_NAME}-completed.txt ]; then" >> $rootdir/root/.bashrc
  281. # Remove the initial setup files
  282. echo ' rm /root/.initial_setup' >> $rootdir/root/.bashrc
  283. echo " rm /home/${MY_USERNAME}/.initial_setup" >> $rootdir/root/.bashrc
  284. echo " touch /root/.remove_${GENERIC_IMAGE_USERNAME}" >> $rootdir/root/.bashrc
  285. echo ' shred -zu ~/login.txt' >> $rootdir/root/.bashrc
  286. END_MESSAGE1=$'Congratulations!'
  287. if [[ $VARIANT != "mesh" ]]; then
  288. END_MESSAGE2=$'\nYour system has now installed\n\nThe onion ssh service is at $SSH_ONION_HOSTNAME\n\nPress any key to reboot and begin using it'
  289. echo ' SSH_ONION_HOSTNAME=$(cat /var/lib/tor/hidden_service_ssh/hostname)' >> $rootdir/root/.bashrc
  290. else
  291. END_MESSAGE2=$'\nYour system has now installed\n\nPress any key to reboot and begin using it'
  292. fi
  293. echo " dialog --title '$END_MESSAGE1' --msgbox \"$END_MESSAGE2\" 9 50" >> $rootdir/root/.bashrc
  294. echo ' reboot' >> $rootdir/root/.bashrc
  295. echo ' fi' >> $rootdir/root/.bashrc
  296. echo ' else' >> $rootdir/root/.bashrc
  297. echo ' key=' >> $rootdir/root/.bashrc
  298. echo ' while [[ $key != "x" ]]; do' >> $rootdir/root/.bashrc
  299. INSTALL_FAIL_MESSAGE=$"Install failed. Press x to continue..."
  300. echo " read -n1 -r -p \"${INSTALL_FAIL_MESSAGE}\" key" >> $rootdir/root/.bashrc
  301. echo ' done' >> $rootdir/root/.bashrc
  302. echo ' fi' >> $rootdir/root/.bashrc
  303. echo ' exit' >> $rootdir/root/.bashrc
  304. echo 'else' >> $rootdir/root/.bashrc
  305. echo ' # Remove default account after install' >> $rootdir/root/.bashrc
  306. echo " if [ -f /root/.remove_${GENERIC_IMAGE_USERNAME} ]; then" >> $rootdir/root/.bashrc
  307. echo " if [ -d /home/${GENERIC_IMAGE_USERNAME} ]; then" >> $rootdir/root/.bashrc
  308. echo " userdel -r ${GENERIC_IMAGE_USERNAME}" >> $rootdir/root/.bashrc
  309. echo " if [ -d /home/${GENERIC_IMAGE_USERNAME} ]; then" >> $rootdir/root/.bashrc
  310. echo " rm -rf /home/${GENERIC_IMAGE_USERNAME}" >> $rootdir/root/.bashrc
  311. echo " rm /root/.remove_${GENERIC_IMAGE_USERNAME}" >> $rootdir/root/.bashrc
  312. echo ' fi' >> $rootdir/root/.bashrc
  313. echo ' fi' >> $rootdir/root/.bashrc
  314. echo ' fi' >> $rootdir/root/.bashrc
  315. echo 'fi' >> $rootdir/root/.bashrc
  316. }
  317. continue_installation() {
  318. # If a configuration file exists then run with it
  319. # otherwise the interactive installer can be used
  320. # This is equivalent to installing freedombox-setup on freedombox
  321. if [ $CONFIG_FILENAME ]; then
  322. if [ ${#CONFIG_FILENAME} -gt 2 ]; then
  323. cp $CONFIG_FILENAME $rootdir/root/$PROJECT_NAME.cfg
  324. cat $rootdir/root/$PROJECT_NAME.cfg
  325. chroot "$rootdir" su -c "$PROJECT_NAME -c /root/$PROJECT_NAME.cfg" - root
  326. fi
  327. fi
  328. }
  329. atheros_wifi() {
  330. firmware_filename="open-ath9k-htc-firmware_1.3-1_all.deb"
  331. firmware_hash='5fea58ffefdf0ef15b504db7fbe3bc078c03e0d927bba64085e4b6f2546102f5'
  332. firmware_url="http://us.archive.trisquel.info/trisquel/pool/main/o/open-ath9k-htc-firmware/$firmware_filename"
  333. firmware_tempfile="/tmp/$firmware_filename"
  334. wget "$firmware_url" -O "$rootdir$firmware_tempfile"
  335. downloaded_firmware_hash=$(sha256sum "$rootdir$firmware_tempfile" | awk -F ' ' '{print $1}')
  336. if [[ "$downloaded_firmware_hash" == "$firmware_hash" ]]; then
  337. chroot "$rootdir" dpkg -i "$firmware_tempfile"
  338. else
  339. echo 'WARNING: Atheros Wifi firmware hash does not match. The driver has not been installed.'
  340. fi
  341. }
  342. configure_wifi() {
  343. if [[ $VARIANT == "mesh" ]]; then
  344. return
  345. fi
  346. if [ -f $WIFI_NETWORKS_FILE ]; then
  347. chroot "$rootdir" ${PROJECT_NAME}-wifi -i $WIFI_INTERFACE --networks $WIFI_NETWORKS_FILE
  348. return
  349. fi
  350. if [[ $WIFI_TYPE != 'none' ]]; then
  351. if [ ${#WIFI_PASSPHRASE} -lt 2 ]; then
  352. return
  353. fi
  354. chroot "$rootdir" ${PROJECT_NAME}-wifi -i $WIFI_INTERFACE -s $WIFI_SSID -t $WIFI_TYPE -p $WIFI_PASSPHRASE --hotspot $WIFI_HOTSPOT
  355. else
  356. chroot "$rootdir" ${PROJECT_NAME}-wifi -i $WIFI_INTERFACE -s $WIFI_SSID -t $WIFI_TYPE --hotspot $WIFI_HOTSPOT
  357. fi
  358. }
  359. ##############################################################################
  360. # Mesh networking
  361. ##############################################################################
  362. # for mesh installs
  363. TRACKER_PORT=6969
  364. WIFI_CHANNEL=2
  365. # B.A.T.M.A.N settings
  366. BATMAN_CELLID='02:BA:00:00:03:01'
  367. WIFI_SSID='mesh'
  368. # To avoid confusions these are obtained from the main project file
  369. TOXID_REPO=
  370. TOX_PORT=
  371. TOXCORE_REPO=
  372. TOXIC_REPO=
  373. TOXCORE_COMMIT=
  374. TOXIC_COMMIT=
  375. # These are some default nodes, but you can replace them with trusted nodes
  376. # as you prefer. See https://wiki.tox.im/Nodes
  377. TOX_NODES=
  378. #TOX_NODES=(
  379. # '192.254.75.102,2607:5600:284::2,33445,951C88B7E75C867418ACDB5D273821372BB5BD652740BCDF623A4FA293E75D2F,Tox RELENG,US'
  380. # '144.76.60.215,2a01:4f8:191:64d6::1,33445,04119E835DF3E78BACF0F84235B300546AF8B936F035185E2A8E9E0A67C8924F,sonOfRa,DE'
  381. #)
  382. # To avoid confusions these are obtained from the main project file
  383. ZERONET_REPO=
  384. ZERONET_COMMIT=
  385. ZERONET_PORT=
  386. # Directory where source code is downloaded and compiled
  387. INSTALL_DIR=$HOME/build
  388. function mesh_avahi {
  389. chroot "$rootdir" apt-get -y install avahi-utils avahi-autoipd avahi-dnsconfd
  390. decarray=( 1 2 3 4 5 6 7 8 9 0 )
  391. PEER_ID=${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}${decarray[$RANDOM%10]}
  392. sed -i "s|#host-name=.*|host-name=P$PEER_ID|g" $rootdir/etc/avahi/avahi-daemon.conf
  393. if [ ! -d $rootdir/etc/avahi/services ]; then
  394. mkdir -p $rootdir/etc/avahi/services
  395. fi
  396. # remove an avahi service which isn't used
  397. if [ -f $rootdir/etc/avahi/services/udisks.service ]; then
  398. rm $rootdir/etc/avahi/services/udisks.service
  399. fi
  400. # Add an ssh service
  401. echo '<?xml version="1.0" standalone="no"?><!--*-nxml-*-->' > $rootdir/etc/avahi/services/ssh.service
  402. echo '<!DOCTYPE service-group SYSTEM "avahi-service.dtd">' >> $rootdir/etc/avahi/services/ssh.service
  403. echo '<service-group>' >> $rootdir/etc/avahi/services/ssh.service
  404. echo ' <name replace-wildcards="yes">%h SSH</name>' >> $rootdir/etc/avahi/services/ssh.service
  405. echo ' <service>' >> $rootdir/etc/avahi/services/ssh.service
  406. echo ' <type>_ssh._tcp</type>' >> $rootdir/etc/avahi/services/ssh.service
  407. echo " <port>$SSH_PORT</port>" >> $rootdir/etc/avahi/services/ssh.service
  408. echo ' </service>' >> $rootdir/etc/avahi/services/ssh.service
  409. echo '</service-group>' >> $rootdir/etc/avahi/services/ssh.service
  410. # keep the daemon running
  411. WATCHDOG_SCRIPT_NAME="keepon"
  412. echo '' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  413. echo '# keep avahi daemon running' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  414. echo 'AVAHI_RUNNING=$(pgrep avahi-daemon > /dev/null && echo Running)' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  415. echo 'if [ ! $AVAHI_RUNNING ]; then' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  416. echo ' systemctl start avahi-daemon' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  417. echo ' echo -n $CURRENT_DATE >> $LOGFILE' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  418. echo ' echo " Avahi daemon restarted" >> $LOGFILE' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  419. echo 'fi' >> $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  420. chmod +x $rootdir/usr/bin/$WATCHDOG_SCRIPT_NAME
  421. }
  422. function mesh_batman {
  423. chroot "$rootdir" apt-get -y install iproute bridge-utils libnetfilter-conntrack3 batctl
  424. chroot "$rootdir" apt-get -y install python-dev libevent-dev ebtables python-pip git
  425. chroot "$rootdir" apt-get -y install wireless-tools rfkill
  426. if ! grep -q "batman_adv" $rootdir/etc/modules; then
  427. echo 'batman_adv' >> $rootdir/etc/modules
  428. fi
  429. BATMAN_SCRIPT=$rootdir/var/lib/batman
  430. if [ -f /usr/local/bin/${PROJECT_NAME}-mesh-batman ]; then
  431. cp /usr/local/bin/${PROJECT_NAME}-mesh-batman $BATMAN_SCRIPT
  432. else
  433. cp /usr/bin/${PROJECT_NAME}-mesh-batman $BATMAN_SCRIPT
  434. fi
  435. BATMAN_DAEMON=$rootdir/etc/systemd/system/batman.service
  436. echo '[Unit]' > $BATMAN_DAEMON
  437. echo 'Description=B.A.T.M.A.N. Advanced' >> $BATMAN_DAEMON
  438. echo '' >> $BATMAN_DAEMON
  439. echo '[Service]' >> $BATMAN_DAEMON
  440. echo 'Type=oneshot' >> $BATMAN_DAEMON
  441. echo "ExecStart=/var/lib/batman start" >> $BATMAN_DAEMON
  442. echo "ExecStop=/var/lib/batman stop" >> $BATMAN_DAEMON
  443. echo 'RemainAfterExit=yes' >> $BATMAN_DAEMON
  444. echo '' >> $BATMAN_DAEMON
  445. echo '# Allow time for the server to start/stop' >> $BATMAN_DAEMON
  446. echo 'TimeoutSec=300' >> $BATMAN_DAEMON
  447. echo '' >> $BATMAN_DAEMON
  448. echo '[Install]' >> $BATMAN_DAEMON
  449. echo 'WantedBy=multi-user.target' >> $BATMAN_DAEMON
  450. chroot "$rootdir" systemctl enable batman
  451. }
  452. function mesh_firewall {
  453. FIREWALL_FILENAME=${rootdir}/etc/systemd/system/meshfirewall.service
  454. MESH_FIREWALL_SCRIPT=${rootdir}/usr/bin/mesh-firewall
  455. echo '#!/bin/bash' > $MESH_FIREWALL_SCRIPT
  456. echo 'iptables -P INPUT ACCEPT' >> $MESH_FIREWALL_SCRIPT
  457. echo 'ip6tables -P INPUT ACCEPT' >> $MESH_FIREWALL_SCRIPT
  458. echo 'iptables -F' >> $MESH_FIREWALL_SCRIPT
  459. echo 'ip6tables -F' >> $MESH_FIREWALL_SCRIPT
  460. echo 'iptables -t nat -F' >> $MESH_FIREWALL_SCRIPT
  461. echo 'ip6tables -t nat -F' >> $MESH_FIREWALL_SCRIPT
  462. echo 'iptables -X' >> $MESH_FIREWALL_SCRIPT
  463. echo 'ip6tables -X' >> $MESH_FIREWALL_SCRIPT
  464. echo 'iptables -P INPUT DROP' >> $MESH_FIREWALL_SCRIPT
  465. echo 'ip6tables -P INPUT DROP' >> $MESH_FIREWALL_SCRIPT
  466. echo 'iptables -A INPUT -i lo -j ACCEPT' >> $MESH_FIREWALL_SCRIPT
  467. echo 'iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT' >> $MESH_FIREWALL_SCRIPT
  468. echo '' >> $MESH_FIREWALL_SCRIPT
  469. echo '# Make sure incoming tcp connections are SYN packets' >> $MESH_FIREWALL_SCRIPT
  470. echo 'iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP' >> $MESH_FIREWALL_SCRIPT
  471. echo '' >> $MESH_FIREWALL_SCRIPT
  472. echo '# Drop packets with incoming fragments' >> $MESH_FIREWALL_SCRIPT
  473. echo 'iptables -A INPUT -f -j DROP' >> $MESH_FIREWALL_SCRIPT
  474. echo '' >> $MESH_FIREWALL_SCRIPT
  475. echo '# Drop bogons' >> $MESH_FIREWALL_SCRIPT
  476. echo 'iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP' >> $MESH_FIREWALL_SCRIPT
  477. echo 'iptables -A INPUT -p tcp --tcp-flags ALL FIN,PSH,URG -j DROP' >> $MESH_FIREWALL_SCRIPT
  478. echo 'iptables -A INPUT -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP' >> $MESH_FIREWALL_SCRIPT
  479. echo '' >> $MESH_FIREWALL_SCRIPT
  480. echo '# Incoming malformed NULL packets:' >> $MESH_FIREWALL_SCRIPT
  481. echo 'iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP' >> $MESH_FIREWALL_SCRIPT
  482. echo '' >> $MESH_FIREWALL_SCRIPT
  483. echo "iptables -A INPUT -p tcp --dport $TOX_PORT -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  484. echo "iptables -A INPUT -i $WIFI_INTERFACE -p udp --dport $ZERONET_PORT -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  485. echo "iptables -A INPUT -i $WIFI_INTERFACE -p tcp --dport $ZERONET_PORT -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  486. echo "iptables -A INPUT -i $WIFI_INTERFACE -p udp --dport $TRACKER_PORT -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  487. echo "iptables -A INPUT -i $WIFI_INTERFACE -p tcp --dport $TRACKER_PORT -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  488. echo "iptables -A INPUT -i $WIFI_INTERFACE -p udp --dport 1900 -j ACCEPT" >> $MESH_FIREWALL_SCRIPT
  489. chmod +x $MESH_FIREWALL_SCRIPT
  490. echo '[Unit]' > $FIREWALL_FILENAME
  491. echo 'Description=Mesh Firewall' >> $FIREWALL_FILENAME
  492. echo '' >> $FIREWALL_FILENAME
  493. echo '[Service]' >> $FIREWALL_FILENAME
  494. echo 'Type=oneshot' >> $FIREWALL_FILENAME
  495. echo 'ExecStart=/usr/bin/mesh-firewall' >> $FIREWALL_FILENAME
  496. echo 'RemainAfterExit=no' >> $FIREWALL_FILENAME
  497. echo '' >> $FIREWALL_FILENAME
  498. echo 'TimeoutSec=30' >> $FIREWALL_FILENAME
  499. echo '' >> $FIREWALL_FILENAME
  500. echo '[Install]' >> $FIREWALL_FILENAME
  501. echo 'WantedBy=multi-user.target' >> $FIREWALL_FILENAME
  502. chroot "$rootdir" systemctl enable meshfirewall
  503. }
  504. function mesh_tox_node {
  505. # obtain commits from the main file
  506. if [ -f /usr/local/bin/${PROJECT_NAME} ]; then
  507. TOXCORE_COMMIT_MAIN=$(cat /usr/local/bin/${PROJECT_NAME} | grep "TOXCORE_COMMIT=" | head -n 1 | awk -F "'" '{print $2}')
  508. else
  509. TOXCORE_COMMIT_MAIN=$(cat /usr/bin/${PROJECT_NAME} | grep "TOXCORE_COMMIT=" | head -n 1 | awk -F "'" '{print $2}')
  510. fi
  511. if [ ${#TOXCORE_COMMIT_MAIN} -gt 10 ]; then
  512. TOXCORE_COMMIT=$TOXCORE_COMMIT_MAIN
  513. fi
  514. if [ ! $TOXCORE_COMMIT ]; then
  515. echo $'No Tox commit was specified'
  516. exit 76325
  517. fi
  518. if [ -f /usr/local/bin/${PROJECT_NAME} ]; then
  519. TOXID_REPO_MAIN=$(cat /usr/local/bin/${PROJECT_NAME} | grep "TOXID_REPO=" | head -n 1 | awk -F '"' '{print $2}')
  520. else
  521. TOXID_REPO_MAIN=$(cat /usr/bin/${PROJECT_NAME} | grep "TOXID_REPO=" | head -n 1 | awk -F '"' '{print $2}')
  522. fi
  523. if [ ${#TOXID_REPO_MAIN} -gt 5 ]; then
  524. TOXID_REPO=$TOXID_REPO_MAIN
  525. fi
  526. if [ ! $TOXID_REPO ]; then
  527. echo $'No ToxID repo was specified'
  528. exit 78252
  529. fi
  530. if [ -f /usr/local/bin/${PROJECT_NAME} ]; then
  531. TOX_PORT_MAIN=$(cat /usr/local/bin/${PROJECT_NAME} | grep "TOX_PORT=" | head -n 1 | awk -F '=' '{print $2}')
  532. else
  533. TOX_PORT_MAIN=$(cat /usr/bin/${PROJECT_NAME} | grep "TOX_PORT=" | head -n 1 | awk -F '=' '{print $2}')
  534. fi
  535. if [ ${#TOX_PORT_MAIN} -gt 2 ]; then
  536. TOX_PORT=$TOX_PORT_MAIN
  537. fi
  538. if [ ! $TOX_PORT ]; then
  539. echo $'No Tox port was specified'
  540. exit 32856
  541. fi
  542. if [ -f /usr/local/bin/${PROJECT_NAME} ]; then
  543. TOXCORE_REPO_MAIN=$(cat /usr/local/bin/${PROJECT_NAME} | grep "TOXCORE_REPO=" | head -n 1 | awk -F '"' '{print $2}')
  544. else
  545. TOXCORE_REPO_MAIN=$(cat /usr/bin/${PROJECT_NAME} | grep "TOXCORE_REPO=" | head -n 1 | awk -F '"' '{print $2}')
  546. fi
  547. if [ ${#TOXCORE_REPO_MAIN} -gt 10 ]; then
  548. TOXCORE_REPO=$TOXCORE_REPO_MAIN
  549. fi
  550. if [ ! $TOXCORE_REPO ]; then
  551. echo $'No Tox repo was specified'
  552. exit 16865
  553. fi
  554. chroot "$rootdir" apt-get -y install build-essential libtool autotools-dev
  555. chroot "$rootdir" apt-get -y install automake checkinstall check git yasm
  556. chroot "$rootdir" apt-get -y install libsodium13 libsodium-dev libcap2-bin
  557. chroot "$rootdir" apt-get -y install libconfig9 libconfig-dev
  558. if [ ! -d $rootdir$INSTALL_DIR ]; then
  559. mkdir -p $rootdir$INSTALL_DIR
  560. fi
  561. git clone $TOXCORE_REPO $rootdir$INSTALL_DIR/toxcore
  562. if [ ! -d $rootdir$INSTALL_DIR/toxcore ]; then
  563. echo $'Unable to clone toxcore'
  564. exit 543335
  565. fi
  566. cd $rootdir$INSTALL_DIR/toxcore
  567. git checkout $TOXCORE_COMMIT -b $TOXCORE_COMMIT
  568. if ! grep -q "Toxcore commit" $rootdir$COMPLETION_FILE; then
  569. echo "Toxcore commit:$TOXCORE_COMMIT" >> $rootdir$COMPLETION_FILE
  570. else
  571. sed -i "s|Toxcore commit.*|Toxcore commit:$TOXCORE_COMMIT|g" $rootdir$COMPLETION_FILE
  572. fi
  573. }
  574. function mesh_tox_client {
  575. if [ -f /usr/local/bin/${PROJECT_NAME} ]; then
  576. TOXIC_FILE=$(cat /usr/local/bin/${PROJECT_NAME} | grep "TOXIC_FILE=" | head -n 1 | awk -F '=' '{print $2}')
  577. else
  578. TOXIC_FILE=$(cat /usr/bin/${PROJECT_NAME} | grep "TOXIC_FILE=" | head -n 1 | awk -F '=' '{print $2}')
  579. fi
  580. # obtain commits from the main file
  581. if [ -f /usr/local/bin/${PROJECT_NAME} ]; then
  582. TOXIC_COMMIT_MAIN=$(cat /usr/local/bin/${PROJECT_NAME} | grep "TOXIC_COMMIT=" | head -n 1 | awk -F "'" '{print $2}')
  583. else
  584. TOXIC_COMMIT_MAIN=$(cat /usr/bin/${PROJECT_NAME} | grep "TOXIC_COMMIT=" | head -n 1 | awk -F "'" '{print $2}')
  585. fi
  586. if [ ${#TOXIC_COMMIT_MAIN} -gt 10 ]; then
  587. TOXIC_COMMIT=$TOXIC_COMMIT_MAIN
  588. fi
  589. if [ -f /usr/local/bin/${PROJECT_NAME} ]; then
  590. TOXIC_REPO_MAIN=$(cat /usr/local/bin/${PROJECT_NAME} | grep "TOXIC_REPO=" | head -n 1 | awk -F '"' '{print $2}')
  591. else
  592. TOXIC_REPO_MAIN=$(cat /usr/bin/${PROJECT_NAME} | grep "TOXIC_REPO=" | head -n 1 | awk -F '"' '{print $2}')
  593. fi
  594. if [ ${#TOXIC_REPO_MAIN} -gt 5 ]; then
  595. TOXIC_REPO=$TOXIC_REPO_MAIN
  596. fi
  597. chroot "$rootdir" apt-get -y install libncursesw5-dev libconfig-dev libqrencode-dev
  598. chroot "$rootdir" apt-get -y install libcurl4-openssl-dev libvpx-dev libopenal-dev
  599. if [ ! -d $rootdir$INSTALL_DIR ]; then
  600. mkdir -p $rootdir$INSTALL_DIR
  601. fi
  602. git clone $TOXIC_REPO $rootdir$INSTALL_DIR/toxic
  603. cd $rootdir$INSTALL_DIR/toxic
  604. git checkout $TOXIC_COMMIT -b $TOXIC_COMMIT
  605. if ! grep -q "Toxic commit" $rootdir$COMPLETION_FILE; then
  606. echo "Toxic commit:$TOXIC_COMMIT" >> $rootdir$COMPLETION_FILE
  607. else
  608. sed -i "s|Toxic commit.*|Toxic commit:$TOXIC_COMMIT|g" $rootdir$COMPLETION_FILE
  609. fi
  610. }
  611. function mesh_zeronet {
  612. # obtain commits from the main file
  613. if [ -f /usr/local/bin/${PROJECT_NAME} ]; then
  614. ZERONET_COMMIT_MAIN=$(cat /usr/local/bin/${PROJECT_NAME} | grep "ZERONET_COMMIT=" | head -n 1 | awk -F "'" '{print $2}')
  615. else
  616. ZERONET_COMMIT_MAIN=$(cat /usr/bin/${PROJECT_NAME} | grep "ZERONET_COMMIT=" | head -n 1 | awk -F "'" '{print $2}')
  617. fi
  618. if [ ${#ZERONET_COMMIT_MAIN} -gt 10 ]; then
  619. ZERONET_COMMIT=$ZERONET_COMMIT_MAIN
  620. fi
  621. if [ ! $ZERONET_COMMIT ]; then
  622. echo $'No Tox commit was specified'
  623. exit 37046
  624. fi
  625. if [ -f /usr/local/bin/${PROJECT_NAME} ]; then
  626. ZERONET_REPO_MAIN=$(cat /usr/local/bin/${PROJECT_NAME} | grep "ZERONET_REPO=" | head -n 1 | awk -F '"' '{print $2}')
  627. else
  628. ZERONET_REPO_MAIN=$(cat /usr/bin/${PROJECT_NAME} | grep "ZERONET_REPO=" | head -n 1 | awk -F '"' '{print $2}')
  629. fi
  630. if [ ${#ZERONET_REPO_MAIN} -gt 5 ]; then
  631. ZERONET_REPO=$ZERONET_REPO_MAIN
  632. fi
  633. if [ ! $ZERONET_REPO ]; then
  634. echo $'No Tox commit was specified'
  635. exit 37046
  636. fi
  637. if [ -f /usr/local/bin/${PROJECT_NAME} ]; then
  638. ZERONET_PORT_MAIN=$(cat /usr/local/bin/${PROJECT_NAME} | grep "ZERONET_PORT=" | head -n 1 | awk -F '=' '{print $2}')
  639. else
  640. ZERONET_PORT_MAIN=$(cat /usr/bin/${PROJECT_NAME} | grep "ZERONET_PORT=" | head -n 1 | awk -F '=' '{print $2}')
  641. fi
  642. if [ ${#ZERONET_PORT_MAIN} -gt 1 ]; then
  643. ZERONET_PORT=$ZERONET_PORT_MAIN
  644. fi
  645. if [ ! $ZERONET_PORT ]; then
  646. echo $'No zeronet port was specified'
  647. exit 67433
  648. fi
  649. chroot "$rootdir" apt-get -y install python python-msgpack python-gevent
  650. chroot "$rootdir" apt-get -y install python-pip bittornado
  651. chroot "$rootdir" pip install msgpack-python --upgrade
  652. chroot "$rootdir" useradd -d /opt/zeronet/ -s /bin/false zeronet
  653. git clone $ZERONET_REPO $rootdir/opt/zeronet
  654. if [ ! -d $rootdir/opt/zeronet ]; then
  655. echo 'WARNING: Unable to clone zeronet'
  656. return
  657. fi
  658. cd $rootdir/opt/zeronet
  659. git checkout $ZERONET_COMMIT -b $ZERONET_COMMIT
  660. if ! grep -q "ZeroNet commit" $COMPLETION_FILE; then
  661. echo "ZeroNet commit:$ZERONET_COMMIT" >> $rootdir$COMPLETION_FILE
  662. else
  663. sed -i "s/ZeroNet commit.*/ZeroNet commit:$ZERONET_COMMIT/g" $COMPLETION_FILE
  664. fi
  665. chroot "$rootdir" chown -R zeronet:zeronet /opt/zeronet
  666. # Hack to ensure that the file access port is opened
  667. # This is because zeronet normally relies on an internet site
  668. # to do this, but on a purely local mesh the internet isn't available
  669. sed -i 's|fileserver_port = 0|fileserver_port = config.fileserver_port\n sys.modules["main"].file_server.port_opened = True|g' $rootdir/opt/zeronet/src/Site/Site.py
  670. ZERONET_DAEMON=$rootdir/etc/systemd/system/zeronet.service
  671. echo '[Unit]' > $ZERONET_DAEMON
  672. echo 'Description=Zeronet Server' >> $ZERONET_DAEMON
  673. echo 'After=syslog.target' >> $ZERONET_DAEMON
  674. echo 'After=network.target' >> $ZERONET_DAEMON
  675. echo '[Service]' >> $ZERONET_DAEMON
  676. echo 'Type=simple' >> $ZERONET_DAEMON
  677. echo 'User=zeronet' >> $ZERONET_DAEMON
  678. echo 'Group=zeronet' >> $ZERONET_DAEMON
  679. echo 'WorkingDirectory=/opt/zeronet' >> $ZERONET_DAEMON
  680. echo 'ExecStart=/usr/bin/python zeronet.py --ip_external replace.local --trackers_file /opt/zeronet/bootstrap' >> $ZERONET_DAEMON
  681. echo '' >> $ZERONET_DAEMON
  682. echo 'TimeoutSec=300' >> $ZERONET_DAEMON
  683. echo '' >> $ZERONET_DAEMON
  684. echo '[Install]' >> $ZERONET_DAEMON
  685. echo 'WantedBy=multi-user.target' >> $ZERONET_DAEMON
  686. TRACKER_DAEMON=$rootdir/etc/systemd/system/tracker.service
  687. echo '[Unit]' > $TRACKER_DAEMON
  688. echo 'Description=Torrent Tracker' >> $TRACKER_DAEMON
  689. echo 'After=syslog.target' >> $TRACKER_DAEMON
  690. echo 'After=network.target' >> $TRACKER_DAEMON
  691. echo '[Service]' >> $TRACKER_DAEMON
  692. echo 'Type=simple' >> $TRACKER_DAEMON
  693. echo 'User=tracker' >> $TRACKER_DAEMON
  694. echo 'Group=tracker' >> $TRACKER_DAEMON
  695. echo 'WorkingDirectory=/opt/tracker' >> $TRACKER_DAEMON
  696. echo "ExecStart=/usr/bin/bttrack --port $TRACKER_PORT --dfile /opt/tracker/dstate --logfile /opt/tracker/tracker.log --nat_check 0 --scrape_allowed full --ipv6_enabled 0" >> $TRACKER_DAEMON
  697. echo '' >> $TRACKER_DAEMON
  698. echo 'TimeoutSec=300' >> $TRACKER_DAEMON
  699. echo '' >> $TRACKER_DAEMON
  700. echo '[Install]' >> $TRACKER_DAEMON
  701. echo 'WantedBy=multi-user.target' >> $TRACKER_DAEMON
  702. chroot "$rootdir" useradd -d /opt/tracker/ -s /bin/false tracker
  703. if [ ! -d $rootdir/opt/tracker ]; then
  704. mkdir $rootdir/opt/tracker
  705. fi
  706. chroot "$rootdir" chown -R tracker:tracker /opt/tracker
  707. # publish regularly
  708. echo "* * * * * root zeronetavahi > /dev/null" >> $rootdir/etc/crontab
  709. chroot "$rootdir" systemctl enable tracker.service
  710. chroot "$rootdir" systemctl enable zeronet.service
  711. }
  712. initialise_mesh() {
  713. if [[ $VARIANT != "mesh" || $DEBIAN_INSTALL_ONLY != "no" ]]; then
  714. return
  715. fi
  716. mesh_firewall
  717. mesh_avahi
  718. mesh_batman
  719. mesh_tox_node
  720. mesh_tox_client
  721. mesh_zeronet
  722. MESH_SERVICE='mesh-setup.service'
  723. MESH_SETUP_DAEMON=$rootdir/etc/systemd/system/$MESH_SERVICE
  724. echo '[Unit]' > $MESH_SETUP_DAEMON
  725. echo 'Description=Initial mesh router configuration' >> $MESH_SETUP_DAEMON
  726. echo 'After=syslog.target' >> $MESH_SETUP_DAEMON
  727. echo 'After=network.target' >> $MESH_SETUP_DAEMON
  728. echo '[Service]' >> $MESH_SETUP_DAEMON
  729. echo 'Type=simple' >> $MESH_SETUP_DAEMON
  730. echo 'User=root' >> $MESH_SETUP_DAEMON
  731. echo 'Group=root' >> $MESH_SETUP_DAEMON
  732. echo 'WorkingDirectory=/root' >> $MESH_SETUP_DAEMON
  733. echo "ExecStart=/usr/local/bin/${PROJECT_NAME}-image-mesh > /var/log/mesh-setup.log" >> $MESH_SETUP_DAEMON
  734. echo '' >> $MESH_SETUP_DAEMON
  735. echo 'TimeoutSec=99999' >> $MESH_SETUP_DAEMON
  736. echo '' >> $MESH_SETUP_DAEMON
  737. echo '[Install]' >> $MESH_SETUP_DAEMON
  738. echo 'WantedBy=multi-user.target' >> $MESH_SETUP_DAEMON
  739. chroot "$rootdir" systemctl enable $MESH_SERVICE
  740. }
  741. ##############################################################################
  742. # Set to true/false to control if eatmydata is used during build
  743. use_eatmydata=true
  744. rootdir="$1"
  745. fmdir="$(pwd)"
  746. image="$fmdir"/"$2"
  747. cd "$rootdir"
  748. echo info: building $MACHINE for $ARCHITECTURE
  749. export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true
  750. export LC_ALL=C LANGUAGE=C LANG=C
  751. # Override libpam-tmpdir setting during build, as the directories
  752. # are not created yet.
  753. export TMP=/tmp/ TMPDIR=/tmp/
  754. username=$MY_USERNAME
  755. echo $"warning: creating initial user $username with well known password!"
  756. password=$MY_PASSWORD
  757. chroot "$rootdir" adduser --gecos $username --disabled-password $username
  758. echo $username:$password | chroot $rootdir /usr/sbin/chpasswd
  759. chroot "$rootdir" adduser $username sudo
  760. case "$MACHINE" in
  761. virtualbox)
  762. # hide irrelevant console keyboard messages.
  763. echo "echo \"4 4 1 7\" > /proc/sys/kernel/printk" \
  764. >> /etc/init.d/rc.local
  765. ;;
  766. qemu)
  767. # hide irrelevant console keyboard messages.
  768. echo "echo \"4 4 1 7\" > /proc/sys/kernel/printk" \
  769. >> /etc/init.d/rc.local
  770. ;;
  771. esac
  772. set_apt_sources $BUILD_MIRROR
  773. chroot "$rootdir" apt-get clean
  774. chroot "$rootdir" rm -rf /var/lib/apt/lists/*
  775. chroot "$rootdir" apt-get clean
  776. chroot "$rootdir" apt-get update
  777. cat > $rootdir/usr/sbin/policy-rc.d <<EOF
  778. #!/bin/sh
  779. exit 101
  780. EOF
  781. chmod a+rx $rootdir/usr/sbin/policy-rc.d
  782. if $use_eatmydata ; then
  783. enable_eatmydata_override
  784. fi
  785. if [ -n "$CUSTOM_SETUP" ]; then
  786. cp "$CUSTOM_SETUP" "$rootdir"/tmp
  787. chroot "$rootdir" apt-get install -y gdebi-core
  788. chroot "$rootdir" gdebi -n /tmp/"$(basename $CUSTOM_SETUP)"
  789. fi
  790. chroot "$rootdir" apt-get install -y sudo git dialog toilet build-essential openssh-server
  791. chroot "$rootdir" apt-get install -y avahi-daemon avahi-utils avahi-discover avahi-autoipd
  792. chroot "$rootdir" apt-get install -y iptables dnsutils net-tools network-manager iputils-ping
  793. chroot "$rootdir" apt-get install -y libnss-mdns libnss-myhostname libnss-gw-name nano man ntp
  794. chroot "$rootdir" apt-get install -y locales locales-all debconf wireless-tools wpasupplicant
  795. sed -i "s|#host-name=.*|host-name=${PROJECT_NAME}|g" $rootdir/etc/avahi/avahi-daemon.conf
  796. chroot "$rootdir" /bin/bash -x <<EOF
  797. git clone $PROJECT_REPO /root/$PROJECT_NAME
  798. cd /root/$PROJECT_NAME
  799. make install
  800. EOF
  801. chroot "$rootdir" ${PROJECT_NAME}-image-hardware-setup 2>&1 | \
  802. tee $rootdir/var/log/${PROJECT_NAME}-image-hardware-setup.log
  803. rm $rootdir/usr/sbin/policy-rc.d
  804. # Set up HRNG for systems known to have one
  805. # Otherwise install haveged
  806. if [[ "$MACHINE" != "beaglebone" ]]; then
  807. chroot $rootdir apt-get -y install haveged
  808. else
  809. chroot $rootdir apt-get -y install rng-tools
  810. sed -i 's|#HRNGDEVICE=/dev/hwrng|HRNGDEVICE=/dev/hwrng|g' $rootdir/etc/default/rng-tools
  811. fi
  812. # copy u-boot to beginning of image
  813. case "$MACHINE" in
  814. beaglebone)
  815. dd if=$rootdir/usr/lib/u-boot/am335x_boneblack/MLO of="$image" \
  816. count=1 seek=1 conv=notrunc bs=128k
  817. dd if=$rootdir/usr/lib/u-boot/am335x_boneblack/u-boot.img of="$image" \
  818. count=2 seek=1 conv=notrunc bs=384k
  819. ;;
  820. cubieboard2)
  821. dd if=$rootdir/usr/lib/u-boot/Cubieboard2/u-boot-sunxi-with-spl.bin of="$image" \
  822. seek=8 conv=notrunc bs=1k
  823. ;;
  824. a20-olinuxino-lime)
  825. dd if=$rootdir/usr/lib/u-boot/A20-OLinuXino-Lime/u-boot-sunxi-with-spl.bin \
  826. of="$image" seek=8 conv=notrunc bs=1k
  827. ;;
  828. esac
  829. if $use_eatmydata ; then
  830. disable_eatmydata_override
  831. fi
  832. set_apt_sources $MIRROR
  833. chroot "$rootdir" apt-get update
  834. configure_ssh
  835. configure_networking
  836. admin_user_sudo
  837. create_generic_image
  838. atheros_wifi
  839. continue_installation
  840. initialise_mesh
  841. configure_wifi
  842. cd /
  843. echo $"info: killing leftover processes in chroot"
  844. fuser -mvk $rootdir/. || true
  845. exit 0