freedombone-prep 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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_IMAGE_FILENAME=
  46. DEBIAN_FILE_NAME="bone-debian-8.1-console-armhf-2015-07-12-2gb.img"
  47. # Downloads for the Debian installer
  48. DOWNLOAD_LINK1="https://rcn-ee.com/rootfs/bb.org/testing/2015-07-12/console/$DEBIAN_FILE_NAME.xz"
  49. ROOTFS='rootfs'
  50. PARTITION_NUMBER=1
  51. function show_help {
  52. echo ''
  53. echo 'freedombone-prep -d [microSD device] --ip [BBB LAN IP address] --iprouter [Router LAN IP address] --mount [mount directory]'
  54. echo ''
  55. echo 'See the manpage for more details'
  56. echo ''
  57. }
  58. # if no arguments are given
  59. if [[ $NO_OF_ARGS == 0 ]]; then
  60. show_help
  61. exit 0
  62. fi
  63. if [ -d /media ]; then
  64. # different directories for Debian
  65. if [ -d /media/usb1 ]; then
  66. MICROSD_MOUNT_POINT=/media
  67. ROOTFS=usb1
  68. fi
  69. if [ -d /media/usb1/@ ]; then
  70. MICROSD_MOUNT_POINT=/media
  71. ROOTFS='usb1/@'
  72. fi
  73. if [ -d /media/usb0 ]; then
  74. MICROSD_MOUNT_POINT=/media
  75. fi
  76. fi
  77. if [ ! -d $MICROSD_MOUNT_POINT ]; then
  78. echo "The mount directory $MICROSD_MOUNT_POINT does not exist."
  79. echo 'Use the --mount option to specify where the microSD gets mounted to.'
  80. exit 67563
  81. fi
  82. echo "MICROSD_MOUNT_POINT=$MICROSD_MOUNT_POINT"
  83. while [[ $# > 1 ]]
  84. do
  85. key="$1"
  86. case $key in
  87. -h|--help)
  88. show_help
  89. ;;
  90. # Drive path for the microSD
  91. -d|--drive)
  92. shift
  93. MICROSD_DRIVE="$1"
  94. ;;
  95. # debian disk image created with freedombone-image
  96. -i|--image)
  97. shift
  98. DEBIAN_IMAGE_FILENAME="$1"
  99. ROOTFS='usb1/@'
  100. ;;
  101. # BBB static IP address on the LAN
  102. --ip)
  103. shift
  104. BBB_FIXED_IP_ADDRESS="$1"
  105. ;;
  106. # Router IP address on the LAN
  107. --iprouter)
  108. shift
  109. ROUTER_IP_ADDRESS="$1"
  110. ;;
  111. # mount point
  112. --mount)
  113. shift
  114. MICROSD_MOUNT_POINT="$1"
  115. ;;
  116. # nameserver 1
  117. --ns1)
  118. shift
  119. NAMESERVER1="$1"
  120. ;;
  121. # nameserver 2
  122. --ns2)
  123. shift
  124. NAMESERVER2="$1"
  125. ;;
  126. *)
  127. # unknown option
  128. ;;
  129. esac
  130. shift
  131. done
  132. if [ ! $MICROSD_DRIVE ]; then
  133. echo 'You need to specify a drive for the connected microSD.'
  134. echo 'This can most easily be found by removing the microSD, then'
  135. echo 'running:'
  136. echo ''
  137. echo ' ls /dev/sd*'
  138. echo ''
  139. echo 'or'
  140. echo ''
  141. echo ' ls /dev/mmcblk*'
  142. echo ''
  143. echo 'Then plugging the microSD back in and entering the same command again'
  144. exit 1
  145. fi
  146. if [ ! -b ${MICROSD_DRIVE}${PARTITION_NUMBER} ]; then
  147. if [ -b ${MICROSD_DRIVE}p${PARTITION_NUMBER} ]; then
  148. PARTITION_NUMBER=p${PARTITION_NUMBER}
  149. else
  150. echo "The microSD drive could not be found at ${MICROSD_DRIVE}1"
  151. exit 2
  152. fi
  153. fi
  154. SUDO=
  155. if [ -f /usr/bin/sudo ]; then
  156. SUDO='sudo'
  157. fi
  158. $SUDO apt-get install p7zip dd wget
  159. if [ ! -d ~/freedombone ]; then
  160. mkdir ~/freedombone
  161. fi
  162. cd ~/freedombone
  163. # extract the image
  164. if [ $DEBIAN_IMAGE_FILENAME ]; then
  165. # debian image filename specified as an option
  166. if [[ $DEBIAN_IMAGE_FILENAME == *".bz2" ]]; then
  167. tar -xjvf $DEBIAN_IMAGE_FILENAME
  168. pattern="*.img"
  169. files=( $pattern )
  170. DEBIAN_IMAGE_FILENAME=${files[0]}
  171. fi
  172. if [[ $DEBIAN_IMAGE_FILENAME != *".img" ]]; then
  173. echo 'Debian image (.img) file expected'
  174. exit 62394
  175. fi
  176. DEBIAN_FILE_NAME=$DEBIAN_IMAGE_FILENAME
  177. else
  178. # default debian image downloaded from elsewhere
  179. if [ ! -f ~/freedombone/$DEBIAN_FILE_NAME.xz ]; then
  180. if [ ! -f ~/freedombone/$DEBIAN_FILE_NAME ]; then
  181. wget $DOWNLOAD_LINK1
  182. fi
  183. fi
  184. xz -d $DEBIAN_FILE_NAME.xz
  185. fi
  186. if [ ! -f ~/freedombone/$DEBIAN_FILE_NAME ]; then
  187. echo "Couldn't extract image $DEBIAN_FILE_NAME"
  188. exit 4
  189. fi
  190. cd ~/freedombone
  191. echo 'Flashing image. This may take a while.'
  192. $SUDO dd if=$DEBIAN_FILE_NAME of=$MICROSD_DRIVE
  193. sync
  194. sleep 5
  195. if [ -d $MICROSD_MOUNT_POINT/$ROOTFS ]; then
  196. umount $MICROSD_MOUNT_POINT/$ROOTFS
  197. $SUDO rm $MICROSD_MOUNT_POINT/$ROOTFS
  198. fi
  199. $SUDO mkdir -p $MICROSD_MOUNT_POINT/$ROOTFS
  200. $SUDO mount ${MICROSD_DRIVE}${PARTITION_NUMBER} $MICROSD_MOUNT_POINT/$ROOTFS
  201. sync
  202. if [ ! -b ${MICROSD_DRIVE}${PARTITION_NUMBER} ]; then
  203. echo ''
  204. echo "The microSD drive could not be found at ${MICROSD_DRIVE}${PARTITION_NUMBER}"
  205. read -p "Wait for the drive to mount then press any key... " -n1 -s
  206. if [ ! -b ${MICROSD_DRIVE}${PARTITION_NUMBER} ]; then
  207. echo "microSD drive not found at ${MICROSD_DRIVE}${PARTITION_NUMBER}"
  208. exit 5
  209. fi
  210. fi
  211. if [ ! -d $MICROSD_MOUNT_POINT/$ROOTFS ]; then
  212. echo ''
  213. echo "The rootfs partition $MICROSD_MOUNT_POINT/$ROOTFS was not found."
  214. ls $MICROSD_MOUNT_POINT
  215. exit 65688
  216. fi
  217. if [ ! -d $MICROSD_MOUNT_POINT/$ROOTFS/home ]; then
  218. echo ''
  219. echo "The rootfs partition was not written correctly."
  220. ls $MICROSD_MOUNT_POINT/$ROOTFS
  221. exit 65688
  222. fi
  223. $SUDO sed -i 's/iface eth0 inet dhcp/iface eth0 inet static/g' $MICROSD_MOUNT_POINT/$ROOTFS/etc/network/interfaces
  224. $SUDO sed -i "/iface eth0 inet static/a\ dns-nameservers $NAMESERVER1 $NAMESERVER2" $MICROSD_MOUNT_POINT/$ROOTFS/etc/network/interfaces
  225. $SUDO sed -i "/iface eth0 inet static/a\ gateway $ROUTER_IP_ADDRESS" $MICROSD_MOUNT_POINT/$ROOTFS/etc/network/interfaces
  226. $SUDO sed -i '/iface eth0 inet static/a\ netmask 255.255.255.0' $MICROSD_MOUNT_POINT/$ROOTFS/etc/network/interfaces
  227. $SUDO sed -i "/iface eth0 inet static/a\ address $BBB_FIXED_IP_ADDRESS" $MICROSD_MOUNT_POINT/$ROOTFS/etc/network/interfaces
  228. $SUDO sed -i '/iface usb0 inet static/,/ gateway 192.168.7.1/ s/^/#/' $MICROSD_MOUNT_POINT/$ROOTFS/etc/network/interfaces
  229. hexarray=( 1 2 3 4 5 6 7 8 9 0 a b c d e f )
  230. a=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
  231. b=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
  232. c=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
  233. d=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
  234. e=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
  235. $SUDO sed -i "s|#hwaddress ether.*|hwaddress ether de:$a:$b:$c:$d:$e|g" $MICROSD_MOUNT_POINT/$ROOTFS/etc/network/interfaces
  236. $SUDO sed -i "s/nameserver.*/nameserver $NAMESERVER1/g" $MICROSD_MOUNT_POINT/$ROOTFS/etc/resolv.conf
  237. $SUDO sed -i "/nameserver $NAMESERVER1/a\nameserver $NAMESERVER2" $MICROSD_MOUNT_POINT/$ROOTFS/etc/resolv.conf
  238. # copy the commands to the card
  239. $SUDO cp -f $(which freedombone)* $MICROSD_MOUNT_POINT/$ROOTFS/usr/local/bin/
  240. $SUDO cp -f $(which zeronetavahi)* $MICROSD_MOUNT_POINT/$ROOTFS/usr/local/bin/
  241. $SUDO cp -f $MICROSD_MOUNT_POINT/$ROOTFS/usr/local/bin/freedombone-mesh $MICROSD_MOUNT_POINT/$ROOTFS/usr/local/bin/mesh
  242. $SUDO cp -f $MICROSD_MOUNT_POINT/$ROOTFS/usr/local/bin/freedombone-meshweb $MICROSD_MOUNT_POINT/$ROOTFS/usr/local/bin/meshweb
  243. if [ ! -f $MICROSD_MOUNT_POINT/$ROOTFS/usr/local/bin/freedombone ]; then
  244. echo 'There was a problem with writing freedombone commands to the SD card'
  245. exit 8736
  246. fi
  247. # remove automatic motd creator if it exists
  248. if [ -f $MICROSD_MOUNT_POINT/$ROOTFS/etc/init.d/motd ]; then
  249. $SUDO rm -f $MICROSD_MOUNT_POINT/$ROOTFS/etc/init.d/motd
  250. fi
  251. # change the motd to show further install instructions
  252. echo '' >> /tmp/freedombone_motd
  253. echo 'Create a user for the system with:' >> /tmp/freedombone_motd
  254. echo '' >> /tmp/freedombone_motd
  255. echo ' adduser [username]' >> /tmp/freedombone_motd
  256. echo '' >> /tmp/freedombone_motd
  257. echo 'Enter the command "exit" a couple of times to get back to your main system' >> /tmp/freedombone_motd
  258. echo 'then log back in as the user you just created with:' >> /tmp/freedombone_motd
  259. echo '' >> /tmp/freedombone_motd
  260. echo " ssh [username]@$BBB_FIXED_IP_ADDRESS" >> /tmp/freedombone_motd
  261. echo '' >> /tmp/freedombone_motd
  262. echo 'and use the "su" command to become the root user again.' >> /tmp/freedombone_motd
  263. echo '' >> /tmp/freedombone_motd
  264. echo 'Finally you can use the freedombone command to install a server configuration:' >> /tmp/freedombone_motd
  265. echo '' >> /tmp/freedombone_motd
  266. echo ' apt-get update' >> /tmp/freedombone_motd
  267. echo ' apt-get -y install git dialog build-essential' >> /tmp/freedombone_motd
  268. echo ' freedombone menuconfig' >> /tmp/freedombone_motd
  269. $SUDO cp -f /tmp/freedombone_motd $MICROSD_MOUNT_POINT/$ROOTFS/etc/motd
  270. clear
  271. echo '*** Initial microSD card setup is complete ***'
  272. echo ''
  273. echo 'To avoid running out of disk space you may first wish to resize the'
  274. echo 'partition to the size of your microSD card, using something like gparted.'
  275. echo ''
  276. echo 'The microSD card can now be removed and inserted into the Beaglebone Black.'
  277. echo 'Once the Beaglebone has booted then you can log in with:'
  278. echo ''
  279. echo " ssh root@$BBB_FIXED_IP_ADDRESS"
  280. echo ''
  281. echo 'The root password should be changed with the command "passwd".'
  282. cat /tmp/freedombone_motd
  283. rm /tmp/freedombone_motd
  284. $SUDO umount $MICROSD_MOUNT_POINT/$ROOTFS
  285. sync
  286. if [ -d $MICROSD_MOUNT_POINT/$ROOTFS ]; then
  287. $SUDO rm -rf $MICROSD_MOUNT_POINT/$ROOTFS
  288. fi
  289. exit 0