freedombone-image-customise 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. clear
  173. echo "
  174. .---. . .
  175. | | |
  176. |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  177. | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  178. ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  179. Initial setup process
  180. Please enter your password a second time.
  181. "
  182. sudo su
  183. fi
  184. EOF
  185. cat >> $rootdir/root/.bashrc <<EOF
  186. # initial setup of the system
  187. if [ -f ~/.initial_setup ]; then
  188. EOF
  189. echo ' if [ -f ~/login.txt ]; then' >> $rootdir/root/.bashrc
  190. echo ' NEW_USER_PASSWORD=$(printf `cat ~/login.txt`)' >> $rootdir/root/.bashrc
  191. echo ' else' >> $rootdir/root/.bashrc
  192. echo ' ENTROPY=$(cat /proc/sys/kernel/random/entropy_avail)' >> $rootdir/root/.bashrc
  193. echo ' if [ $ENTROPY -lt 500 ]; then' >> $rootdir/root/.bashrc
  194. echo ' dialog --backtitle "Freedombone initial setup process" --title "Password Generation" --msgbox "WARNING: The entropy available on this system is too low to generate a password.\n\nThe installation process cannot continue." 8 50' >> $rootdir/root/.bashrc
  195. echo ' exit' >> $rootdir/root/.bashrc
  196. echo ' fi' >> $rootdir/root/.bashrc
  197. echo ' NEW_USER_PASSWORD="$(openssl rand -base64 12 | cut -c1-10)"' >> $rootdir/root/.bashrc
  198. echo ' fi' >> $rootdir/root/.bashrc
  199. echo ' echo "${NEW_USER_PASSWORD}" > ~/login.txt' >> $rootdir/root/.bashrc
  200. echo ' clear' >> $rootdir/root/.bashrc
  201. echo ' echo ""' >> $rootdir/root/.bashrc
  202. echo ' echo "Your new login password is:"' >> $rootdir/root/.bashrc
  203. echo ' echo ""' >> $rootdir/root/.bashrc
  204. echo ' toilet "${NEW_USER_PASSWORD}"' >> $rootdir/root/.bashrc
  205. echo ' echo ""' >> $rootdir/root/.bashrc
  206. echo ' echo " ${NEW_USER_PASSWORD}"' >> $rootdir/root/.bashrc
  207. echo ' echo ""' >> $rootdir/root/.bashrc
  208. echo ' echo "Use it whenever you wish to ssh into this system."' >> $rootdir/root/.bashrc
  209. echo ' echo ""' >> $rootdir/root/.bashrc
  210. echo ' echo "IMPORTANT: Please take a moment to enter the above password into a password manager or write it down somewhere."' >> $rootdir/root/.bashrc
  211. echo ' echo ""' >> $rootdir/root/.bashrc
  212. echo ' read -n1 -r -p "Press any key to continue..." key' >> $rootdir/root/.bashrc
  213. # change the password for the admin user
  214. echo -n " echo \"${MY_USERNAME}:" >> $rootdir/root/.bashrc
  215. echo '$(printf `cat ~/login.txt`)"|chpasswd' >> $rootdir/root/.bashrc
  216. echo ' freedombone menuconfig' >> $rootdir/root/.bashrc
  217. echo ' if [ "$?" = "0" ]; then' >> $rootdir/root/.bashrc
  218. echo ' if [ -f ~/freedombone-completed.txt ]; then' >> $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 ~/login.txt" >> $rootdir/root/.bashrc
  223. cat >> $rootdir/root/.bashrc <<EOF
  224. fi
  225. fi
  226. exit
  227. fi
  228. EOF
  229. }
  230. continue_installation() {
  231. # If a configuration file exists then run with it
  232. # otherwise the interactive installer can be used
  233. # This is equivalent to installing freedombox-setup on freedombox
  234. if [ $CONFIG_FILENAME ]; then
  235. if [ ${#CONFIG_FILENAME} -gt 2 ]; then
  236. cp $CONFIG_FILENAME $rootdir/root/$PROJECT_NAME.cfg
  237. chroot $rootdir $PROJECT_NAME -c /root/$PROJECT_NAME.cfg
  238. fi
  239. fi
  240. }
  241. # Set to true/false to control if eatmydata is used during build
  242. use_eatmydata=true
  243. rootdir="$1"
  244. fmdir="$(pwd)"
  245. image="$fmdir"/"$2"
  246. cd "$rootdir"
  247. echo info: building $MACHINE for $ARCHITECTURE
  248. export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true
  249. export LC_ALL=C LANGUAGE=C LANG=C
  250. # Override libpam-tmpdir setting during build, as the directories
  251. # are not created yet.
  252. export TMP=/tmp/ TMPDIR=/tmp/
  253. username=$MY_USERNAME
  254. echo "warning: creating initial user $username with well known password!"
  255. password=$MY_PASSWORD
  256. chroot "$rootdir" adduser --gecos $username --disabled-password $username
  257. echo $username:$password | chroot $rootdir /usr/sbin/chpasswd
  258. chroot "$rootdir" adduser $username sudo
  259. case "$MACHINE" in
  260. virtualbox)
  261. # hide irrelevant console keyboard messages.
  262. echo "echo \"4 4 1 7\" > /proc/sys/kernel/printk" \
  263. >> /etc/init.d/rc.local
  264. ;;
  265. qemu)
  266. # hide irrelevant console keyboard messages.
  267. echo "echo \"4 4 1 7\" > /proc/sys/kernel/printk" \
  268. >> /etc/init.d/rc.local
  269. ;;
  270. esac
  271. set_apt_sources $BUILD_MIRROR
  272. chroot "$rootdir" apt-get update
  273. cat > $rootdir/usr/sbin/policy-rc.d <<EOF
  274. #!/bin/sh
  275. exit 101
  276. EOF
  277. chmod a+rx $rootdir/usr/sbin/policy-rc.d
  278. if $use_eatmydata ; then
  279. enable_eatmydata_override
  280. fi
  281. if [ -n "$CUSTOM_SETUP" ]; then
  282. cp "$CUSTOM_SETUP" "$rootdir"/tmp
  283. chroot "$rootdir" apt-get install -y gdebi-core
  284. chroot "$rootdir" gdebi -n /tmp/"$(basename $CUSTOM_SETUP)"
  285. fi
  286. chroot "$rootdir" apt-get install -y sudo git dialog toilet build-essential openssh-server
  287. chroot "$rootdir" apt-get install -y avahi-daemon avahi-utils avahi-discover avahi-autoipd
  288. chroot "$rootdir" apt-get install -y iptables dnsutils net-tools network-manager
  289. chroot "$rootdir" apt-get install -y libnss-mdns libnss-myhostname libnss-gw-name
  290. chroot "$rootdir" /bin/bash -x <<EOF
  291. git clone https://github.com/bashrc/freedombone /root/freedombone
  292. cd /root/freedombone
  293. make install
  294. EOF
  295. chroot "$rootdir" freedombone-image-hardware-setup 2>&1 | \
  296. tee $rootdir/var/log/freedombone-image-hardware-setup.log
  297. rm $rootdir/usr/sbin/policy-rc.d
  298. chroot "$rootdir" /usr/lib/freedombone/setup 2>&1 | \
  299. tee $rootdir/var/log/freedombone-setup.log
  300. # Remove SSH keys from the image
  301. rm $rootdir/etc/ssh/ssh_host_* || true
  302. if [[ "$MACHINE" != "beaglebone" ]]; then
  303. chroot $rootdir apt-get -y install haveged
  304. else
  305. chroot $rootdir apt-get -y install rng-tools
  306. sed -i 's|#HRNGDEVICE=/dev/hwrng|HRNGDEVICE=/dev/hwrng|g' $rootdir/etc/default/rng-tools
  307. fi
  308. # copy u-boot to beginning of image
  309. case "$MACHINE" in
  310. beaglebone)
  311. dd if=$rootdir/usr/lib/u-boot/am335x_boneblack/MLO of="$image" \
  312. count=1 seek=1 conv=notrunc bs=128k
  313. dd if=$rootdir/usr/lib/u-boot/am335x_boneblack/u-boot.img of="$image" \
  314. count=2 seek=1 conv=notrunc bs=384k
  315. ;;
  316. cubieboard2)
  317. dd if=$rootdir/usr/lib/u-boot/Cubieboard2/u-boot-sunxi-with-spl.bin of="$image" \
  318. seek=8 conv=notrunc bs=1k
  319. ;;
  320. esac
  321. if $use_eatmydata ; then
  322. disable_eatmydata_override
  323. fi
  324. set_apt_sources $MIRROR
  325. chroot "$rootdir" apt-get update
  326. configure_ssh
  327. configure_networking
  328. admin_user_sudo
  329. create_generic_image
  330. continue_installation
  331. cd /
  332. echo "info: killing leftover processes in chroot"
  333. # 2014-11-04 this killed /usr/lib/erlang/erts-6.2/bin/epmd, see
  334. # <URL: https://www.ejabberd.im/epmd?q=epmd > to learn more.
  335. fuser -mvk $rootdir/. || true