freedombone-image-customise 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Based on bin/freedombox-customize from freedom-maker
  12. #
  13. # License
  14. # =======
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. set -e
  29. set -x
  30. PROJECT_NAME='freedombone'
  31. MY_USERNAME='debian'
  32. MY_PASSWORD='freedombone'
  33. # IP address of the router (gateway)
  34. ROUTER_IP_ADDRESS="192.168.1.254"
  35. # The fixed IP address of the Beaglebone Black on your local network
  36. BOX_IP_ADDRESS="192.168.1.55"
  37. # DNS
  38. NAMESERVER1='213.73.91.35'
  39. NAMESERVER2='85.214.20.141'
  40. # optional configuration file containing freedombone settings
  41. CONFIG_FILENAME=
  42. # Optional ssh public key to allow
  43. SSH_PUBKEY="no"
  44. # Whether this is a generic image for mass redistribution on the interwebs
  45. GENERIC_IMAGE="no"
  46. enable_eatmydata_override() {
  47. chroot $rootdir apt-get install --no-install-recommends -y eatmydata
  48. if [ -x $rootdir/usr/bin/eatmydata ] && \
  49. [ ! -f $rootdir/etc/apt/apt.conf.d/95debian-edu-install-dpkg-eatmydata ]; then
  50. echo "info: Adding apt config to call dpkg via eatmydata"
  51. printf "#!/bin/sh\nexec eatmydata dpkg \"\$@\"\n" \
  52. > $rootdir/var/tmp/dpkg-eatmydata
  53. chmod 755 $rootdir/var/tmp/dpkg-eatmydata
  54. cat > $rootdir/etc/apt/apt.conf.d/95debian-edu-install-dpkg-eatmydata <<EOF
  55. Dir::Bin::dpkg "/var/tmp/dpkg-eatmydata";
  56. EOF
  57. else
  58. echo "error: unable to find /usr/bin/eatmydata after installing the eatmydata package"
  59. fi
  60. }
  61. disable_eatmydata_override() {
  62. for override in \
  63. /etc/apt/apt.conf.d/95debian-edu-install-dpkg-eatmydata \
  64. /var/tmp/dpkg-eatmydata ; do
  65. echo "info: Removing apt config to call dpkg via eatmydata"
  66. if [ -f $rootdir$override ] ; then
  67. rm -f $rootdir$override
  68. else
  69. echo "warning: missing $rootdir$override"
  70. fi
  71. done
  72. sync # Flush file buffers before continuing
  73. }
  74. set_apt_sources() {
  75. NEW_MIRROR="$1"
  76. COMPONENTS="main"
  77. cat <<EOF > etc/apt/sources.list
  78. deb $NEW_MIRROR $SUITE $COMPONENTS
  79. deb-src $NEW_MIRROR $SUITE $COMPONENTS
  80. #deb http://security.debian.org/ $SUITE/updates main
  81. #deb-src http://security.debian.org/ $SUITE/updates main
  82. EOF
  83. }
  84. configure_networking() {
  85. if [[ $GENERIC_IMAGE == "no" ]]; then
  86. echo "# This file describes the network interfaces available on your system
  87. # and how to activate them. For more information, see interfaces(5).
  88. # The loopback network interface
  89. auto lo
  90. iface lo inet loopback
  91. # The primary network interface
  92. auto eth0
  93. iface eth0 inet static
  94. address $BOX_IP_ADDRESS
  95. netmask 255.255.255.0
  96. gateway $ROUTER_IP_ADDRESS
  97. dns-nameservers $NAMESERVER1 $NAMESERVER2
  98. # Example to keep MAC address between reboots
  99. #hwaddress ether B5:A2:BE:3F:1A:FE
  100. # The secondary network interface
  101. #auto eth1
  102. #iface eth1 inet dhcp
  103. # WiFi Example
  104. #auto wlan0
  105. #iface wlan0 inet dhcp
  106. # wpa-ssid \"essid\"
  107. # wpa-psk \"password\"
  108. # Ethernet/RNDIS gadget (g_ether)
  109. # ... or on host side, usbnet and random hwaddr
  110. # Note on some boards, usb0 is automaticly setup with an init script
  111. #iface usb0 inet static
  112. # address 192.168.7.2
  113. # netmask 255.255.255.0
  114. # network 192.168.7.0
  115. # gateway 192.168.7.1" > $rootdir/etc/network/interfaces
  116. hexarray=( 1 2 3 4 5 6 7 8 9 0 a b c d e f )
  117. a=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
  118. b=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
  119. c=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
  120. d=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
  121. e=${hexarray[$RANDOM%16]}${hexarray[$RANDOM%16]}
  122. sed -i "s|#hwaddress ether.*|hwaddress ether de:$a:$b:$c:$d:$e|g" \
  123. $rootdir/etc/network/interfaces
  124. fi
  125. sed -i "s/nameserver.*/nameserver $NAMESERVER1/g" $rootdir/etc/resolv.conf
  126. sed -i "/nameserver $NAMESERVER1/a\nameserver $NAMESERVER2" $rootdir/etc/resolv.conf
  127. # change the motd to show further install instructions
  128. echo "
  129. .---. . .
  130. | | |
  131. |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  132. | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  133. ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  134. Initial base install
  135. Your system is not yet installed. To complete the process run the
  136. following commands, then enter your details.
  137. sudo su
  138. freedombone menuconfig
  139. " > $rootdir/etc/motd
  140. }
  141. configure_ssh() {
  142. sed -i "s/Port .*/Port 2222/g" $rootdir/etc/ssh/sshd_config
  143. if [[ "$SSH_PUBKEY" != "no" ]]; then
  144. if [ ! -d $rootdir/home/$MY_USERNAME/.ssh ]; then
  145. mkdir $rootdir/home/$MY_USERNAME/.ssh
  146. fi
  147. echo "$SSH_PUBKEY" > $rootdir/home/$MY_USERNAME/.ssh/authorized_keys
  148. chroot $rootdir chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.ssh
  149. sed -i 's|PasswordAuthentication.*|PasswordAuthentication no|g' $rootdir/etc/ssh/sshd_config
  150. echo "Using ssh public key:"
  151. echo $SSH_PUBKEY
  152. echo 'Password ssh authentication turned off'
  153. fi
  154. }
  155. admin_user_sudo() {
  156. echo "$MY_USERNAME ALL=(ALL) ALL" >> $rootdir/etc/sudoers
  157. }
  158. create_generic_image() {
  159. if [[ $GENERIC_IMAGE == "no" ]]; then
  160. return
  161. fi
  162. # Don't install any configuration. This will be a base system
  163. CONFIG_FILENAME=
  164. # The presence of this file indicates that the initial
  165. # setup has not yet been completed
  166. touch $rootdir/home/$MY_USERNAME/.initial_setup
  167. chroot $rootdir chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.initial_setup
  168. touch $rootdir/root/.initial_setup
  169. cat >> $rootdir/home/$MY_USERNAME/.bashrc <<EOF
  170. # initial setup of the system
  171. if [ -f ~/.initial_setup ]; then
  172. echo "
  173. .---. . .
  174. | | |
  175. |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  176. | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  177. ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  178. Initial setup process
  179. Please enter your password a second time.
  180. "
  181. sudo su
  182. fi
  183. EOF
  184. cat >> $rootdir/root/.bashrc <<EOF
  185. # initial setup of the system
  186. if [ -f ~/.initial_setup ]; then
  187. clear
  188. echo "
  189. .---. . .
  190. | | |
  191. |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  192. | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  193. ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  194. Initial setup process
  195. "
  196. echo 'This is your new password. Use it whenever you wish to ssh into'
  197. echo 'this system.'
  198. echo ''
  199. EOF
  200. echo ' if [ -f ~/login.txt ]; then' >> $rootdir/root/.bashrc
  201. echo ' NEW_USER_PASSWORD=$(cat ~/login.txt)' >> $rootdir/root/.bashrc
  202. echo ' else' >> $rootdir/root/.bashrc
  203. echo ' NEW_USER_PASSWORD="$(openssl rand -base64 12 | cut -c1-10)"' >> $rootdir/root/.bashrc
  204. echo ' fi' >> $rootdir/root/.bashrc
  205. echo ' echo -n "${NEW_USER_PASSWORD}" > ~/login.txt' >> $rootdir/root/.bashrc
  206. echo ' echo " $NEW_USER_PASSWORD"' >> $rootdir/root/.bashrc
  207. cat >> $rootdir/root/.bashrc <<EOF
  208. echo ''
  209. echo 'Copy it into a password manager or write it down somewhere.'
  210. echo ''
  211. read -n1 -r -p "Press any key to continue..." key
  212. freedombone menuconfig
  213. if [ "$?" = "0" ]; then
  214. EOF
  215. # change the password for the admin user
  216. echo -n " echo \"${MY_USERNAME}:" >> $rootdir/root/.bashrc
  217. echo -n '$(cat /home/' >> $rootdir/root/.bashrc
  218. echo "${MY_USERNAME}/login.txt)\"|chpasswd" >> $rootdir/root/.bashrc
  219. # Remove the initial setup files
  220. echo " rm /root/.initial_setup" >> $rootdir/root/.bashrc
  221. echo " rm /home/${MY_USERNAME}/.initial_setup" >> $rootdir/root/.bashrc
  222. echo " shred -zu /home/${MY_USERNAME}/login.txt" >> $rootdir/root/.bashrc
  223. cat >> $rootdir/root/.bashrc <<EOF
  224. fi
  225. exit
  226. fi
  227. EOF
  228. }
  229. continue_installation() {
  230. # If a configuration file exists then run with it
  231. # otherwise the interactive installer can be used
  232. # This is equivalent to installing freedombox-setup on freedombox
  233. if [ $CONFIG_FILENAME ]; then
  234. if [ ${#CONFIG_FILENAME} -gt 2 ]; then
  235. cp $CONFIG_FILENAME $rootdir/root/$PROJECT_NAME.cfg
  236. chroot $rootdir $PROJECT_NAME -c /root/$PROJECT_NAME.cfg
  237. fi
  238. fi
  239. }
  240. # Set to true/false to control if eatmydata is used during build
  241. use_eatmydata=true
  242. rootdir="$1"
  243. fmdir="$(pwd)"
  244. image="$fmdir"/"$2"
  245. cd "$rootdir"
  246. echo info: building $MACHINE for $ARCHITECTURE
  247. export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true
  248. export LC_ALL=C LANGUAGE=C LANG=C
  249. # Override libpam-tmpdir setting during build, as the directories
  250. # are not created yet.
  251. export TMP=/tmp/ TMPDIR=/tmp/
  252. username=$MY_USERNAME
  253. echo "warning: creating initial user $username with well known password!"
  254. password=$MY_PASSWORD
  255. chroot "$rootdir" adduser --gecos $username --disabled-password $username
  256. echo $username:$password | chroot $rootdir /usr/sbin/chpasswd
  257. chroot "$rootdir" adduser $username sudo
  258. case "$MACHINE" in
  259. virtualbox)
  260. # hide irrelevant console keyboard messages.
  261. echo "echo \"4 4 1 7\" > /proc/sys/kernel/printk" \
  262. >> /etc/init.d/rc.local
  263. ;;
  264. qemu)
  265. # hide irrelevant console keyboard messages.
  266. echo "echo \"4 4 1 7\" > /proc/sys/kernel/printk" \
  267. >> /etc/init.d/rc.local
  268. ;;
  269. esac
  270. set_apt_sources $BUILD_MIRROR
  271. chroot "$rootdir" apt-get update
  272. cat > $rootdir/usr/sbin/policy-rc.d <<EOF
  273. #!/bin/sh
  274. exit 101
  275. EOF
  276. chmod a+rx $rootdir/usr/sbin/policy-rc.d
  277. if $use_eatmydata ; then
  278. enable_eatmydata_override
  279. fi
  280. if [ -n "$CUSTOM_SETUP" ]; then
  281. cp "$CUSTOM_SETUP" "$rootdir"/tmp
  282. chroot "$rootdir" apt-get install -y gdebi-core
  283. chroot "$rootdir" gdebi -n /tmp/"$(basename $CUSTOM_SETUP)"
  284. fi
  285. chroot "$rootdir" apt-get install -y sudo git dialog build-essential openssh-server
  286. chroot "$rootdir" apt-get install -y avahi-daemon avahi-utils avahi-discover
  287. chroot "$rootdir" /bin/bash -x <<EOF
  288. git clone https://github.com/bashrc/freedombone /root/freedombone
  289. cd /root/freedombone
  290. make install
  291. EOF
  292. chroot "$rootdir" freedombone-image-hardware-setup 2>&1 | \
  293. tee $rootdir/var/log/freedombone-image-hardware-setup.log
  294. rm $rootdir/usr/sbin/policy-rc.d
  295. chroot "$rootdir" /usr/lib/freedombone/setup 2>&1 | \
  296. tee $rootdir/var/log/freedombone-setup.log
  297. # Remove SSH keys from the image
  298. rm $rootdir/etc/ssh/ssh_host_* || true
  299. if [[ "$MACHINE" != "beaglebone" ]]; then
  300. chroot $rootdir apt-get -y install haveged
  301. else
  302. chroot $rootdir apt-get -y install rng-tools
  303. sed -i 's|#HRNGDEVICE=/dev/hwrng|HRNGDEVICE=/dev/hwrng|g' $rootdir/etc/default/rng-tools
  304. fi
  305. # copy u-boot to beginning of image
  306. case "$MACHINE" in
  307. beaglebone)
  308. dd if=$rootdir/usr/lib/u-boot/am335x_boneblack/MLO of="$image" \
  309. count=1 seek=1 conv=notrunc bs=128k
  310. dd if=$rootdir/usr/lib/u-boot/am335x_boneblack/u-boot.img of="$image" \
  311. count=2 seek=1 conv=notrunc bs=384k
  312. ;;
  313. cubieboard2)
  314. dd if=$rootdir/usr/lib/u-boot/Cubieboard2/u-boot-sunxi-with-spl.bin of="$image" \
  315. seek=8 conv=notrunc bs=1k
  316. ;;
  317. esac
  318. if $use_eatmydata ; then
  319. disable_eatmydata_override
  320. fi
  321. set_apt_sources $MIRROR
  322. chroot "$rootdir" apt-get update
  323. configure_ssh
  324. configure_networking
  325. admin_user_sudo
  326. create_generic_image
  327. continue_installation
  328. cd /
  329. echo "info: killing leftover processes in chroot"
  330. # 2014-11-04 this killed /usr/lib/erlang/erts-6.2/bin/epmd, see
  331. # <URL: https://www.ejabberd.im/epmd?q=epmd > to learn more.
  332. fuser -mvk $rootdir/. || true