freedombone-prep 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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.00"
  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="debian-jessie-console-armhf-2014-08-13"
  46. # Downloads for the Debian installer
  47. DOWNLOAD_LINK1="https://rcn-ee.net/deb/rootfs/jessie/$DEBIAN_FILE_NAME.tar.xz"
  48. DOWNLOAD_LINK2="http://ynezz.ibawizard.net/beagleboard/jessie/$DEBIAN_FILE_NAME.tar.xz"
  49. ROOTFS='rootfs'
  50. BOOT='BOOT'
  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/usb0 ]; then
  70. MICROSD_MOUNT_POINT=/media
  71. BOOT=usb0
  72. fi
  73. fi
  74. if [ ! -d $MICROSD_MOUNT_POINT ]; then
  75. echo "The mount directory $MICROSD_MOUNT_POINT does not exist."
  76. echo 'Use the --mount option to specify where the microSD gets mounted to.'
  77. exit 67563
  78. fi
  79. echo "MICROSD_MOUNT_POINT=$MICROSD_MOUNT_POINT"
  80. echo "BOOT=$BOOT"
  81. while [[ $# > 1 ]]
  82. do
  83. key="$1"
  84. case $key in
  85. -h|--help)
  86. show_help
  87. ;;
  88. # Drive path for the microSD
  89. -d|--drive)
  90. shift
  91. MICROSD_DRIVE="$1"
  92. ;;
  93. # BBB static IP address on the LAN
  94. --ip)
  95. shift
  96. BBB_FIXED_IP_ADDRESS="$1"
  97. ;;
  98. # Router IP address on the LAN
  99. --iprouter)
  100. shift
  101. ROUTER_IP_ADDRESS="$1"
  102. ;;
  103. # mount point
  104. --mount)
  105. shift
  106. MICROSD_MOUNT_POINT="$1"
  107. ;;
  108. # nameserver 1
  109. --ns1)
  110. shift
  111. NAMESERVER1="$1"
  112. ;;
  113. # nameserver 2
  114. --ns2)
  115. shift
  116. NAMESERVER2="$1"
  117. ;;
  118. *)
  119. # unknown option
  120. ;;
  121. esac
  122. shift
  123. done
  124. if [ ! $MICROSD_DRIVE ]; then
  125. echo 'You need to specify a drive for the connected microSD.'
  126. echo 'This can most easily be found by removing the microSD, then'
  127. echo 'running:'
  128. echo ''
  129. echo ' ls /dev/sd*'
  130. echo ''
  131. echo 'Then plugging the microSD back in and entering the same command again'
  132. exit 1
  133. fi
  134. if [ ! -b ${MICROSD_DRIVE}1 ]; then
  135. echo "The microSD drive could not be found at ${MICROSD_DRIVE}1"
  136. exit 2
  137. fi
  138. if [ ! -d ~/freedombone ]; then
  139. mkdir ~/freedombone
  140. fi
  141. cd ~/freedombone
  142. if [ ! -f ~/freedombone/$DEBIAN_FILE_NAME.tar.xz ]; then
  143. wget $DOWNLOAD_LINK1
  144. fi
  145. if [ ! -f ~/freedombone/$DEBIAN_FILE_NAME.tar.xz ]; then
  146. # try another site
  147. wget $DOWNLOAD_LINK2
  148. if [ ! -f ~/freedombone/$DEBIAN_FILE_NAME.tar.xz ]; then
  149. echo 'The Debian installer could not be downloaded'
  150. exit 3
  151. fi
  152. fi
  153. echo 'Extracting files...'
  154. tar xJf $DEBIAN_FILE_NAME.tar.xz
  155. if [ ! -d ~/freedombone/$DEBIAN_FILE_NAME ]; then
  156. echo "Couldn't extract files"
  157. exit 4
  158. fi
  159. cd $DEBIAN_FILE_NAME
  160. SUDO=
  161. if [ -f /usr/bin/sudo ]; then
  162. SUDO='sudo'
  163. fi
  164. $SUDO apt-get install u-boot-tools dosfstools git-core kpartx wget parted
  165. $SUDO ./setup_sdcard.sh --mmc $MICROSD_DRIVE --dtb beaglebone
  166. echo ''
  167. echo ''
  168. $SUDO umount $MICROSD_MOUNT_POINT/$ROOTFS
  169. $SUDO umount $MICROSD_MOUNT_POINT/$BOOT
  170. sync
  171. sleep 5
  172. $SUDO mkdir -p $MICROSD_MOUNT_POINT/$BOOT
  173. $SUDO mkdir -p $MICROSD_MOUNT_POINT/$ROOTFS
  174. $SUDO mount ${MICROSD_DRIVE}1 $MICROSD_MOUNT_POINT/$BOOT
  175. $SUDO mount ${MICROSD_DRIVE}2 $MICROSD_MOUNT_POINT/$ROOTFS
  176. sync
  177. if [ ! -b ${MICROSD_DRIVE}1 ]; then
  178. echo ''
  179. echo "The microSD drive could not be found at ${MICROSD_DRIVE}1"
  180. read -p "Wait for the drive to mount then press any key... " -n1 -s
  181. if [ ! -b ${MICROSD_DRIVE}1 ]; then
  182. echo "microSD drive not found at ${MICROSD_DRIVE}1"
  183. exit 5
  184. fi
  185. fi
  186. if [ ! -d $MICROSD_MOUNT_POINT/$BOOT ]; then
  187. echo ''
  188. echo "The boot partition $MICROSD_MOUNT_POINT/$BOOT was not found."
  189. ls $MICROSD_MOUNT_POINT
  190. exit 67857
  191. fi
  192. if [ ! -d $MICROSD_MOUNT_POINT/$ROOTFS ]; then
  193. echo ''
  194. echo "The rootfs partition $MICROSD_MOUNT_POINT/$ROOTFS was not found."
  195. ls $MICROSD_MOUNT_POINT
  196. exit 65688
  197. fi
  198. if [ ! -d $MICROSD_MOUNT_POINT/$ROOTFS/home ]; then
  199. echo ''
  200. echo "The rootfs partition was not written correctly."
  201. ls $MICROSD_MOUNT_POINT/$ROOTFS
  202. exit 65688
  203. fi
  204. $SUDO cp $MICROSD_MOUNT_POINT/$BOOT/bbb-uEnv.txt $MICROSD_MOUNT_POINT/$BOOT/uEnv.txt
  205. $SUDO sed -i 's/iface eth0 inet dhcp/iface eth0 inet static/g' $MICROSD_MOUNT_POINT/$ROOTFS/etc/network/interfaces
  206. $SUDO sed -i "/iface eth0 inet static/a\ dns-nameservers $NAMESERVER1 $NAMESERVER2" $MICROSD_MOUNT_POINT/$ROOTFS/etc/network/interfaces
  207. $SUDO sed -i "/iface eth0 inet static/a\ gateway $ROUTER_IP_ADDRESS" $MICROSD_MOUNT_POINT/$ROOTFS/etc/network/interfaces
  208. $SUDO sed -i '/iface eth0 inet static/a\ netmask 255.255.255.0' $MICROSD_MOUNT_POINT/$ROOTFS/etc/network/interfaces
  209. $SUDO sed -i "/iface eth0 inet static/a\ address $BBB_FIXED_IP_ADDRESS" $MICROSD_MOUNT_POINT/$ROOTFS/etc/network/interfaces
  210. $SUDO sed -i '/iface usb0 inet static/,/ gateway 192.168.7.1/ s/^/#/' $MICROSD_MOUNT_POINT/$ROOTFS/etc/network/interfaces
  211. $SUDO sed -i "s/nameserver.*/nameserver $NAMESERVER1/g" $MICROSD_MOUNT_POINT/$ROOTFS/etc/resolv.conf
  212. $SUDO sed -i "/nameserver $NAMESERVER1/a\nameserver $NAMESERVER2" $MICROSD_MOUNT_POINT/$ROOTFS/etc/resolv.conf
  213. # copy the commands to the card
  214. $SUDO cp -f $(which freedombone)* $MICROSD_MOUNT_POINT/$ROOTFS/usr/local/bin/
  215. if [ ! -f $MICROSD_MOUNT_POINT/$ROOTFS/usr/local/bin/freedombone ]; then
  216. echo 'There was a problem with writing freedombone commands to the SD card'
  217. exit 8736
  218. fi
  219. # change the motd to show further install instructions
  220. echo 'Become the root user by typing:' > /tmp/freedombone_motd
  221. echo '' >> /tmp/freedombone_motd
  222. echo ' su' >> /tmp/freedombone_motd
  223. echo '' >> /tmp/freedombone_motd
  224. echo 'Using the password "root". Change the root user password by typing:' >> /tmp/freedombone_motd
  225. echo '' >> /tmp/freedombone_motd
  226. echo ' passwd' >> /tmp/freedombone_motd
  227. echo '' >> /tmp/freedombone_motd
  228. echo 'Then create a user for the system with:' >> /tmp/freedombone_motd
  229. echo '' >> /tmp/freedombone_motd
  230. echo ' adduser [username]' >> /tmp/freedombone_motd
  231. echo '' >> /tmp/freedombone_motd
  232. echo 'Enter the command "exit" a couple of times to get back to your main system' >> /tmp/freedombone_motd
  233. echo 'then log back in as the user you just created with:' >> /tmp/freedombone_motd
  234. echo '' >> /tmp/freedombone_motd
  235. echo " ssh [username]@$BBB_FIXED_IP_ADDRESS" >> /tmp/freedombone_motd
  236. echo '' >> /tmp/freedombone_motd
  237. echo 'and use the "su" command to become the root user again.' >> /tmp/freedombone_motd
  238. echo '' >> /tmp/freedombone_motd
  239. echo 'Finally you can use the freedombone command to install a server configuration:' >> /tmp/freedombone_motd
  240. echo '' >> /tmp/freedombone_motd
  241. echo ' apt-get update' >> /tmp/freedombone_motd
  242. echo ' apt-get -y install git dialog build-essential' >> /tmp/freedombone_motd
  243. echo ' freedombone menuconfig' >> /tmp/freedombone_motd
  244. $SUDO cp -f /tmp/freedombone_motd $MICROSD_MOUNT_POINT/$ROOTFS/etc/motd
  245. clear
  246. echo '*** Initial microSD card setup is complete ***'
  247. echo ''
  248. echo 'The microSD card can now be removed and inserted into the Beaglebone Black.'
  249. echo 'Once the Beaglebone has booted then you can log in with:'
  250. echo ''
  251. echo " ssh debian@$BBB_FIXED_IP_ADDRESS"
  252. echo ''
  253. echo 'The password is "temppwd".'
  254. cat /tmp/freedombone_motd
  255. rm /tmp/freedombone_motd
  256. $SUDO umount $MICROSD_MOUNT_POINT/$ROOTFS
  257. $SUDO umount $MICROSD_MOUNT_POINT/$BOOT
  258. sync
  259. if [ -d $MICROSD_MOUNT_POINT/$ROOTFS ]; then
  260. $SUDO rm -rf $MICROSD_MOUNT_POINT/$ROOTFS
  261. fi
  262. if [ -d $MICROSD_MOUNT_POINT/$BOOT ]; then
  263. $SUDO rm -rf $MICROSD_MOUNT_POINT/$BOOT
  264. fi
  265. exit 0