freedombone-prep 8.9KB

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