freedombone-prep 11KB

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