freedombone-image-customise 13KB

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