freedombone-image-customise 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. # username created by default within a debian image
  35. GENERIC_IMAGE_USERNAME='fbone'
  36. export TEXTDOMAIN=${PROJECT_NAME}-image-customise
  37. export TEXTDOMAINDIR="/usr/share/locale"
  38. # Whether to minimise the number of decisions during interactive install
  39. MINIMAL_INSTALL="yes"
  40. MY_USERNAME='debian'
  41. MY_PASSWORD="${PROJECT_NAME}"
  42. # IP address of the router (gateway)
  43. ROUTER_IP_ADDRESS="192.168.1.254"
  44. # The fixed IP address of the Beaglebone Black on your local network
  45. BOX_IP_ADDRESS="192.168.1.55"
  46. # DNS
  47. NAMESERVER1='213.73.91.35'
  48. NAMESERVER2='85.214.20.141'
  49. # optional configuration file containing freedombone settings
  50. CONFIG_FILENAME=
  51. # Optional ssh public key to allow
  52. SSH_PUBKEY="no"
  53. # Whether this is a generic image for mass redistribution on the interwebs
  54. GENERIC_IMAGE="no"
  55. # default SSH port
  56. SSH_PORT=2222
  57. # for mesh installs
  58. TRACKER_PORT=6969
  59. # Whether sites are accessible only within a Tor browser
  60. ONION_ONLY="no"
  61. # Whether to only install debian and nothing else
  62. DEBIAN_INSTALL_ONLY="no"
  63. # wifi settings
  64. WIFI_INTERFACE='wlan0'
  65. WIFI_SSID=
  66. WIFI_TYPE='wpa-psk'
  67. WIFI_PASSPHRASE=
  68. WIFI_HOTSPOT='no'
  69. WIFI_NETWORKS_FILE=~/${PROJECT_NAME}-wifi.cfg
  70. enable_eatmydata_override() {
  71. chroot $rootdir apt-get install --no-install-recommends -y eatmydata
  72. if [ -x $rootdir/usr/bin/eatmydata ] && \
  73. [ ! -f $rootdir/etc/apt/apt.conf.d/95debian-edu-install-dpkg-eatmydata ]; then
  74. echo $"info: Adding apt config to call dpkg via eatmydata"
  75. printf "#!/bin/sh\nexec eatmydata dpkg \"\$@\"\n" \
  76. > $rootdir/var/tmp/dpkg-eatmydata
  77. chmod 755 $rootdir/var/tmp/dpkg-eatmydata
  78. cat > $rootdir/etc/apt/apt.conf.d/95debian-edu-install-dpkg-eatmydata <<EOF
  79. Dir::Bin::dpkg "/var/tmp/dpkg-eatmydata";
  80. EOF
  81. else
  82. echo $"error: unable to find /usr/bin/eatmydata after installing the eatmydata package"
  83. fi
  84. }
  85. disable_eatmydata_override() {
  86. for override in \
  87. /etc/apt/apt.conf.d/95debian-edu-install-dpkg-eatmydata \
  88. /var/tmp/dpkg-eatmydata ; do
  89. echo $"info: Removing apt config to call dpkg via eatmydata"
  90. if [ -f $rootdir$override ] ; then
  91. rm -f $rootdir$override
  92. else
  93. echo $"warning: missing $rootdir$override"
  94. fi
  95. done
  96. sync # Flush file buffers before continuing
  97. }
  98. set_apt_sources() {
  99. NEW_MIRROR="$1"
  100. COMPONENTS="main"
  101. cat <<EOF > etc/apt/sources.list
  102. deb $NEW_MIRROR $SUITE $COMPONENTS
  103. deb-src $NEW_MIRROR $SUITE $COMPONENTS
  104. #deb http://security.debian.org/ $SUITE/updates main
  105. #deb-src http://security.debian.org/ $SUITE/updates main
  106. EOF
  107. }
  108. configure_networking() {
  109. if [[ $DEBIAN_INSTALL_ONLY != "no" ]]; then
  110. return
  111. fi
  112. if [[ $GENERIC_IMAGE == "no" ]]; then
  113. echo "# This file describes the network interfaces available on your system
  114. # and how to activate them. For more information, see interfaces(5).
  115. # The loopback network interface
  116. auto lo
  117. iface lo inet loopback
  118. # The primary network interface
  119. auto eth0
  120. iface eth0 inet static
  121. address $BOX_IP_ADDRESS
  122. netmask 255.255.255.0
  123. gateway $ROUTER_IP_ADDRESS
  124. dns-nameservers $NAMESERVER1 $NAMESERVER2
  125. # Example to keep MAC address between reboots
  126. #hwaddress ether B5:A2:BE:3F:1A:FE
  127. # The secondary network interface
  128. #auto eth1
  129. #iface eth1 inet dhcp
  130. # WiFi Example
  131. #auto wlan0
  132. #iface wlan0 inet dhcp
  133. # wpa-ssid \"essid\"
  134. # wpa-psk \"password\"
  135. # Ethernet/RNDIS gadget (g_ether)
  136. # ... or on host side, usbnet and random hwaddr
  137. # Note on some boards, usb0 is automaticly setup with an init script
  138. #iface usb0 inet static
  139. # address 192.168.7.2
  140. # netmask 255.255.255.0
  141. # network 192.168.7.0
  142. # gateway 192.168.7.1" > $rootdir/etc/network/interfaces
  143. hexarray=( 1 2 3 4 5 6 7 8 9 0 a b c d e f )
  144. a=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
  145. b=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
  146. c=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
  147. d=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
  148. e=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
  149. sed -i "s|#hwaddress ether.*|hwaddress ether de:$a:$b:$c:$d:$e|g" \
  150. $rootdir/etc/network/interfaces
  151. fi
  152. sed -i "s/nameserver.*/nameserver $NAMESERVER1/g" $rootdir/etc/resolv.conf
  153. sed -i "/nameserver $NAMESERVER1/a\nameserver $NAMESERVER2" $rootdir/etc/resolv.conf
  154. # change the motd to show further install instructions
  155. echo $"
  156. .---. . .
  157. | | |
  158. |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  159. | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  160. ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  161. Initial base install
  162. Your system is not yet installed. To complete the process run the
  163. following commands, then enter your details.
  164. sudo su
  165. ${PROJECT_NAME} menuconfig
  166. " > $rootdir/etc/motd
  167. }
  168. configure_ssh() {
  169. sed -i "s/Port .*/Port ${SSH_PORT}/g" $rootdir/etc/ssh/sshd_config
  170. if [[ "$SSH_PUBKEY" != "no" ]]; then
  171. if [ ! -d $rootdir/home/$MY_USERNAME/.ssh ]; then
  172. mkdir $rootdir/home/$MY_USERNAME/.ssh
  173. fi
  174. echo "$SSH_PUBKEY" > $rootdir/home/$MY_USERNAME/.ssh/authorized_keys
  175. chroot $rootdir chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.ssh
  176. sed -i 's|PasswordAuthentication.*|PasswordAuthentication no|g' $rootdir/etc/ssh/sshd_config
  177. echo $"Using ssh public key:"
  178. echo $SSH_PUBKEY
  179. echo $'Password ssh authentication turned off'
  180. fi
  181. }
  182. admin_user_sudo() {
  183. echo "$MY_USERNAME ALL=(ALL) ALL" >> $rootdir/etc/sudoers
  184. }
  185. create_generic_image() {
  186. if [[ $DEBIAN_INSTALL_ONLY != "no" ]]; then
  187. return
  188. fi
  189. if [[ $GENERIC_IMAGE == "no" ]]; then
  190. return
  191. fi
  192. VARIANT="full"
  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. initialise_mesh() {
  343. if [[ $VARIANT != "mesh" || $DEBIAN_INSTALL_ONLY != "no" ]]; then
  344. return
  345. fi
  346. freedombone-mesh-install -f firewall -r "${rootdir}"
  347. freedombone-mesh-install -f avahi -r "${rootdir}"
  348. freedombone-mesh-install -f batman -r "${rootdir}"
  349. freedombone-mesh-install -f tox_node -r "${rootdir}"
  350. freedombone-mesh-install -f tox_client -r "${rootdir}"
  351. freedombone-mesh-install -f zeronet -r "${rootdir}"
  352. MESH_SERVICE='mesh-setup.service'
  353. MESH_SETUP_DAEMON=$rootdir/etc/systemd/system/$MESH_SERVICE
  354. echo '[Unit]' > $MESH_SETUP_DAEMON
  355. echo 'Description=Initial mesh router configuration' >> $MESH_SETUP_DAEMON
  356. echo 'After=syslog.target' >> $MESH_SETUP_DAEMON
  357. echo 'After=network.target' >> $MESH_SETUP_DAEMON
  358. echo '[Service]' >> $MESH_SETUP_DAEMON
  359. echo 'Type=simple' >> $MESH_SETUP_DAEMON
  360. echo 'User=root' >> $MESH_SETUP_DAEMON
  361. echo 'Group=root' >> $MESH_SETUP_DAEMON
  362. echo 'WorkingDirectory=/root' >> $MESH_SETUP_DAEMON
  363. echo "ExecStart=/usr/local/bin/${PROJECT_NAME}-image-mesh > /var/log/mesh-setup.log" >> $MESH_SETUP_DAEMON
  364. echo '' >> $MESH_SETUP_DAEMON
  365. echo 'TimeoutSec=99999' >> $MESH_SETUP_DAEMON
  366. echo '' >> $MESH_SETUP_DAEMON
  367. echo '[Install]' >> $MESH_SETUP_DAEMON
  368. echo 'WantedBy=multi-user.target' >> $MESH_SETUP_DAEMON
  369. chroot "$rootdir" systemctl enable $MESH_SERVICE
  370. }
  371. configure_wifi() {
  372. if [[ $VARIANT == "mesh" ]]; then
  373. return
  374. fi
  375. if [ -f $WIFI_NETWORKS_FILE ]; then
  376. chroot "$rootdir" ${PROJECT_NAME}-wifi -i $WIFI_INTERFACE --networks $WIFI_NETWORKS_FILE
  377. return
  378. fi
  379. if [[ $WIFI_TYPE != 'none' ]]; then
  380. if [ ${#WIFI_PASSPHRASE} -lt 2 ]; then
  381. return
  382. fi
  383. chroot "$rootdir" ${PROJECT_NAME}-wifi -i $WIFI_INTERFACE -s $WIFI_SSID -t $WIFI_TYPE -p $WIFI_PASSPHRASE --hotspot $WIFI_HOTSPOT
  384. else
  385. chroot "$rootdir" ${PROJECT_NAME}-wifi -i $WIFI_INTERFACE -s $WIFI_SSID -t $WIFI_TYPE --hotspot $WIFI_HOTSPOT
  386. fi
  387. }
  388. # Set to true/false to control if eatmydata is used during build
  389. use_eatmydata=true
  390. rootdir="$1"
  391. fmdir="$(pwd)"
  392. image="$fmdir"/"$2"
  393. cd "$rootdir"
  394. echo info: building $MACHINE for $ARCHITECTURE
  395. export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true
  396. export LC_ALL=C LANGUAGE=C LANG=C
  397. # Override libpam-tmpdir setting during build, as the directories
  398. # are not created yet.
  399. export TMP=/tmp/ TMPDIR=/tmp/
  400. username=$MY_USERNAME
  401. echo $"warning: creating initial user $username with well known password!"
  402. password=$MY_PASSWORD
  403. chroot "$rootdir" adduser --gecos $username --disabled-password $username
  404. echo $username:$password | chroot $rootdir /usr/sbin/chpasswd
  405. chroot "$rootdir" adduser $username sudo
  406. case "$MACHINE" in
  407. virtualbox)
  408. # hide irrelevant console keyboard messages.
  409. echo "echo \"4 4 1 7\" > /proc/sys/kernel/printk" \
  410. >> /etc/init.d/rc.local
  411. ;;
  412. qemu)
  413. # hide irrelevant console keyboard messages.
  414. echo "echo \"4 4 1 7\" > /proc/sys/kernel/printk" \
  415. >> /etc/init.d/rc.local
  416. ;;
  417. esac
  418. set_apt_sources $BUILD_MIRROR
  419. chroot "$rootdir" apt-get clean
  420. chroot "$rootdir" rm -rf /var/lib/apt/lists/*
  421. chroot "$rootdir" apt-get clean
  422. chroot "$rootdir" apt-get update
  423. cat > $rootdir/usr/sbin/policy-rc.d <<EOF
  424. #!/bin/sh
  425. exit 101
  426. EOF
  427. chmod a+rx $rootdir/usr/sbin/policy-rc.d
  428. if $use_eatmydata ; then
  429. enable_eatmydata_override
  430. fi
  431. if [ -n "$CUSTOM_SETUP" ]; then
  432. cp "$CUSTOM_SETUP" "$rootdir"/tmp
  433. chroot "$rootdir" apt-get install -y gdebi-core
  434. chroot "$rootdir" gdebi -n /tmp/"$(basename $CUSTOM_SETUP)"
  435. fi
  436. chroot "$rootdir" apt-get install -y sudo git dialog toilet build-essential openssh-server
  437. chroot "$rootdir" apt-get install -y avahi-daemon avahi-utils avahi-discover avahi-autoipd
  438. chroot "$rootdir" apt-get install -y iptables dnsutils net-tools network-manager iputils-ping
  439. chroot "$rootdir" apt-get install -y libnss-mdns libnss-myhostname libnss-gw-name nano man ntp
  440. chroot "$rootdir" apt-get install -y locales locales-all debconf wireless-tools wpasupplicant
  441. sed -i "s|#host-name=.*|host-name=${PROJECT_NAME}|g" $rootdir/etc/avahi/avahi-daemon.conf
  442. chroot "$rootdir" /bin/bash -x <<EOF
  443. git clone $PROJECT_REPO /root/$PROJECT_NAME
  444. cd /root/$PROJECT_NAME
  445. make install
  446. EOF
  447. chroot "$rootdir" ${PROJECT_NAME}-image-hardware-setup 2>&1 | \
  448. tee $rootdir/var/log/${PROJECT_NAME}-image-hardware-setup.log
  449. rm $rootdir/usr/sbin/policy-rc.d
  450. # Set up HRNG for systems known to have one
  451. # Otherwise install haveged
  452. if [[ "$MACHINE" != "beaglebone" ]]; then
  453. chroot $rootdir apt-get -y install haveged
  454. else
  455. chroot $rootdir apt-get -y install rng-tools
  456. sed -i 's|#HRNGDEVICE=/dev/hwrng|HRNGDEVICE=/dev/hwrng|g' $rootdir/etc/default/rng-tools
  457. fi
  458. # copy u-boot to beginning of image
  459. case "$MACHINE" in
  460. beaglebone)
  461. dd if=$rootdir/usr/lib/u-boot/am335x_boneblack/MLO of="$image" \
  462. count=1 seek=1 conv=notrunc bs=128k
  463. dd if=$rootdir/usr/lib/u-boot/am335x_boneblack/u-boot.img of="$image" \
  464. count=2 seek=1 conv=notrunc bs=384k
  465. ;;
  466. cubieboard2)
  467. dd if=$rootdir/usr/lib/u-boot/Cubieboard2/u-boot-sunxi-with-spl.bin of="$image" \
  468. seek=8 conv=notrunc bs=1k
  469. ;;
  470. a20-olinuxino-lime)
  471. dd if=$rootdir/usr/lib/u-boot/A20-OLinuXino-Lime/u-boot-sunxi-with-spl.bin \
  472. of="$image" seek=8 conv=notrunc bs=1k
  473. ;;
  474. esac
  475. if $use_eatmydata ; then
  476. disable_eatmydata_override
  477. fi
  478. set_apt_sources $MIRROR
  479. chroot "$rootdir" apt-get update
  480. configure_ssh
  481. configure_networking
  482. admin_user_sudo
  483. create_generic_image
  484. atheros_wifi
  485. continue_installation
  486. initialise_mesh
  487. configure_wifi
  488. cd /
  489. echo $"info: killing leftover processes in chroot"
  490. fuser -mvk $rootdir/. || true
  491. exit 0