freedombone-prep 11KB

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