freedombone-prep 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # This script installs the Debian image to the microSD card, and should
  12. # be run on your laptop/desktop with the microSD card plugged in.
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2015 Bob Mottram <bob@robotics.uk.to>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. # The number of arguments
  31. NO_OF_ARGS=$#
  32. # Version number of this script
  33. VERSION="1.01"
  34. # typically /dev/sdb or /dev/sdc, depending upon how
  35. # many drives there are on your system
  36. MICROSD_DRIVE=
  37. # IP address of the router (gateway)
  38. ROUTER_IP_ADDRESS="192.168.1.254"
  39. # The fixed IP address of the Beaglebone Black on your local network
  40. BBB_FIXED_IP_ADDRESS="192.168.1.55"
  41. # DNS
  42. NAMESERVER1='213.73.91.35'
  43. NAMESERVER2='85.214.20.141'
  44. MICROSD_MOUNT_POINT="/media/$USER"
  45. DEBIAN_FILE_NAME="bone-debian-8.1-console-armhf-2015-07-12-2gb"
  46. # Downloads for the Debian installer
  47. DOWNLOAD_LINK1="https://rcn-ee.com/rootfs/bb.org/testing/2015-07-12/console/$DEBIAN_FILE_NAME.img.xz"
  48. ROOTFS='bbb'
  49. function show_help {
  50. echo ''
  51. echo 'freedombone-prep -d [microSD device] --ip [BBB LAN IP address] --iprouter [Router LAN IP address] --mount [mount directory]'
  52. echo ''
  53. echo 'See the manpage for more details'
  54. echo ''
  55. }
  56. # if no arguments are given
  57. if [[ $NO_OF_ARGS == 0 ]]; then
  58. show_help
  59. exit 0
  60. fi
  61. if [ -d /media ]; then
  62. # different directories for Debian
  63. if [ -d /media/usb1 ]; then
  64. MICROSD_MOUNT_POINT=/media
  65. ROOTFS=usb1
  66. fi
  67. if [ -d /media/usb0 ]; then
  68. MICROSD_MOUNT_POINT=/media
  69. fi
  70. fi
  71. if [ ! -d $MICROSD_MOUNT_POINT ]; then
  72. echo "The mount directory $MICROSD_MOUNT_POINT does not exist."
  73. echo 'Use the --mount option to specify where the microSD gets mounted to.'
  74. exit 67563
  75. fi
  76. echo "MICROSD_MOUNT_POINT=$MICROSD_MOUNT_POINT"
  77. while [[ $# > 1 ]]
  78. do
  79. key="$1"
  80. case $key in
  81. -h|--help)
  82. show_help
  83. ;;
  84. # Drive path for the microSD
  85. -d|--drive)
  86. shift
  87. MICROSD_DRIVE="$1"
  88. ;;
  89. # BBB static IP address on the LAN
  90. --ip)
  91. shift
  92. BBB_FIXED_IP_ADDRESS="$1"
  93. ;;
  94. # Router IP address on the LAN
  95. --iprouter)
  96. shift
  97. ROUTER_IP_ADDRESS="$1"
  98. ;;
  99. # mount point
  100. --mount)
  101. shift
  102. MICROSD_MOUNT_POINT="$1"
  103. ;;
  104. # nameserver 1
  105. --ns1)
  106. shift
  107. NAMESERVER1="$1"
  108. ;;
  109. # nameserver 2
  110. --ns2)
  111. shift
  112. NAMESERVER2="$1"
  113. ;;
  114. *)
  115. # unknown option
  116. ;;
  117. esac
  118. shift
  119. done
  120. if [ ! $MICROSD_DRIVE ]; then
  121. echo 'You need to specify a drive for the connected microSD.'
  122. echo 'This can most easily be found by removing the microSD, then'
  123. echo 'running:'
  124. echo ''
  125. echo ' ls /dev/sd*'
  126. echo ''
  127. echo 'Then plugging the microSD back in and entering the same command again'
  128. exit 1
  129. fi
  130. if [ ! -b ${MICROSD_DRIVE}1 ]; then
  131. echo "The microSD drive could not be found at ${MICROSD_DRIVE}1"
  132. exit 2
  133. fi
  134. SUDO=
  135. if [ -f /usr/bin/sudo ]; then
  136. SUDO='sudo'
  137. fi
  138. $SUDO apt-get install p7zip dd wget
  139. if [ ! -d ~/freedombone ]; then
  140. mkdir ~/freedombone
  141. fi
  142. cd ~/freedombone
  143. if [ ! -f ~/freedombone/$DEBIAN_FILE_NAME.img.xz ]; then
  144. if [ ! -f ~/freedombone/$DEBIAN_FILE_NAME.img ]; then
  145. wget $DOWNLOAD_LINK1
  146. fi
  147. fi
  148. echo 'Extracting image...'
  149. xz -d $DEBIAN_FILE_NAME.img.xz
  150. if [ ! -f ~/freedombone/$DEBIAN_FILE_NAME.img ]; then
  151. echo "Couldn't extract image"
  152. exit 4
  153. fi
  154. cd ~/freedombone
  155. echo 'Flashing image. This may take a while.'
  156. $SUDO dd if=$DEBIAN_FILE_NAME.img of=$MICROSD_DRIVE
  157. sync
  158. sleep 5
  159. if [ ! -d $MICROSD_MOUNT_POINT/$ROOTFS ]; then
  160. $SUDO mkdir -p $MICROSD_MOUNT_POINT/$ROOTFS
  161. fi
  162. $SUDO mount ${MICROSD_DRIVE}1 $MICROSD_MOUNT_POINT/$ROOTFS
  163. sync
  164. if [ ! -b ${MICROSD_DRIVE}1 ]; then
  165. echo ''
  166. echo "The microSD drive could not be found at ${MICROSD_DRIVE}1"
  167. read -p "Wait for the drive to mount then press any key... " -n1 -s
  168. if [ ! -b ${MICROSD_DRIVE}1 ]; then
  169. echo "microSD drive not found at ${MICROSD_DRIVE}1"
  170. exit 5
  171. fi
  172. fi
  173. if [ ! -d $MICROSD_MOUNT_POINT/$ROOTFS ]; then
  174. echo ''
  175. echo "The rootfs partition $MICROSD_MOUNT_POINT/$ROOTFS was not found."
  176. ls $MICROSD_MOUNT_POINT
  177. exit 65688
  178. fi
  179. if [ ! -d $MICROSD_MOUNT_POINT/$ROOTFS/home ]; then
  180. echo ''
  181. echo "The rootfs partition was not written correctly."
  182. ls $MICROSD_MOUNT_POINT/$ROOTFS
  183. exit 65688
  184. fi
  185. $SUDO sed -i 's/iface eth0 inet dhcp/iface eth0 inet static/g' $MICROSD_MOUNT_POINT/$ROOTFS/etc/network/interfaces
  186. $SUDO sed -i "/iface eth0 inet static/a\ dns-nameservers $NAMESERVER1 $NAMESERVER2" $MICROSD_MOUNT_POINT/$ROOTFS/etc/network/interfaces
  187. $SUDO sed -i "/iface eth0 inet static/a\ gateway $ROUTER_IP_ADDRESS" $MICROSD_MOUNT_POINT/$ROOTFS/etc/network/interfaces
  188. $SUDO sed -i '/iface eth0 inet static/a\ netmask 255.255.255.0' $MICROSD_MOUNT_POINT/$ROOTFS/etc/network/interfaces
  189. $SUDO sed -i "/iface eth0 inet static/a\ address $BBB_FIXED_IP_ADDRESS" $MICROSD_MOUNT_POINT/$ROOTFS/etc/network/interfaces
  190. $SUDO sed -i '/iface usb0 inet static/,/ gateway 192.168.7.1/ s/^/#/' $MICROSD_MOUNT_POINT/$ROOTFS/etc/network/interfaces
  191. $SUDO sed -i "s/nameserver.*/nameserver $NAMESERVER1/g" $MICROSD_MOUNT_POINT/$ROOTFS/etc/resolv.conf
  192. $SUDO sed -i "/nameserver $NAMESERVER1/a\nameserver $NAMESERVER2" $MICROSD_MOUNT_POINT/$ROOTFS/etc/resolv.conf
  193. # copy the commands to the card
  194. $SUDO cp -f $(which freedombone)* $MICROSD_MOUNT_POINT/$ROOTFS/usr/local/bin/
  195. if [ ! -f $MICROSD_MOUNT_POINT/$ROOTFS/usr/local/bin/freedombone ]; then
  196. echo 'There was a problem with writing freedombone commands to the SD card'
  197. exit 8736
  198. fi
  199. # change the motd to show further install instructions
  200. echo '' >> /tmp/freedombone_motd
  201. echo 'Create a user for the system with:' >> /tmp/freedombone_motd
  202. echo '' >> /tmp/freedombone_motd
  203. echo ' adduser [username]' >> /tmp/freedombone_motd
  204. echo '' >> /tmp/freedombone_motd
  205. echo 'Enter the command "exit" a couple of times to get back to your main system' >> /tmp/freedombone_motd
  206. echo 'then log back in as the user you just created with:' >> /tmp/freedombone_motd
  207. echo '' >> /tmp/freedombone_motd
  208. echo " ssh [username]@$BBB_FIXED_IP_ADDRESS" >> /tmp/freedombone_motd
  209. echo '' >> /tmp/freedombone_motd
  210. echo 'and use the "su" command to become the root user again.' >> /tmp/freedombone_motd
  211. echo '' >> /tmp/freedombone_motd
  212. echo 'Finally you can use the freedombone command to install a server configuration:' >> /tmp/freedombone_motd
  213. echo '' >> /tmp/freedombone_motd
  214. echo ' apt-get update' >> /tmp/freedombone_motd
  215. echo ' apt-get -y install git dialog build-essential' >> /tmp/freedombone_motd
  216. echo ' freedombone menuconfig' >> /tmp/freedombone_motd
  217. $SUDO cp -f /tmp/freedombone_motd $MICROSD_MOUNT_POINT/$ROOTFS/etc/motd
  218. clear
  219. echo '*** Initial microSD card setup is complete ***'
  220. echo ''
  221. echo 'The microSD card can now be removed and inserted into the Beaglebone Black.'
  222. echo 'Once the Beaglebone has booted then you can log in with:'
  223. echo ''
  224. echo " ssh root@$BBB_FIXED_IP_ADDRESS"
  225. echo ''
  226. echo 'The root password should be changed with the command "passwd".'
  227. cat /tmp/freedombone_motd
  228. rm /tmp/freedombone_motd
  229. $SUDO umount $MICROSD_MOUNT_POINT/$ROOTFS
  230. sync
  231. if [ -d $MICROSD_MOUNT_POINT/$ROOTFS ]; then
  232. $SUDO rm -rf $MICROSD_MOUNT_POINT/$ROOTFS
  233. fi
  234. exit 0