create_arm_debootstrap.sh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #!/bin/bash
  2. ######################
  3. # Debootstrap #
  4. ######################
  5. set -e
  6. set -x
  7. show_usage() {
  8. cat <<EOF
  9. # NAME
  10. $(basename $0) -- Script to create a minimal deboostrap
  11. # OPTIONS
  12. -d debian release (wheezy, jessie) (default: jessie)
  13. -b olinux board (see config_board.sh) (default: a20lime)
  14. -a add packages to deboostrap
  15. -n hostname (default: olinux)
  16. -t target directory for debootstrap (default: /olinux/debootstrap)
  17. -i install sunxi kernel files; you should have build them before.
  18. -y install yunohost (doesn't work with cross debootstrap)
  19. -c cross debootstrap
  20. -p use aptcacher proxy
  21. EOF
  22. exit 1
  23. }
  24. DEBIAN_RELEASE=jessie
  25. TARGET_DIR=/olinux/debootstrap
  26. DEB_HOSTNAME=olinux
  27. REP=$(dirname $0)
  28. while getopts ":a:b:d:n:t:i:ycp" opt; do
  29. case $opt in
  30. d)
  31. DEBIAN_RELEASE=$OPTARG
  32. ;;
  33. b)
  34. BOARD=$OPTARG
  35. ;;
  36. a)
  37. PACKAGES=$OPTARG
  38. ;;
  39. n)
  40. DEB_HOSTNAME=$OPTARG
  41. ;;
  42. t)
  43. TARGET_DIR=$OPTARG
  44. ;;
  45. i)
  46. INSTALL_KERNEL=$OPTARG
  47. ;;
  48. y)
  49. INSTALL_YUNOHOST=yes
  50. ;;
  51. c)
  52. CROSS=yes
  53. ;;
  54. p)
  55. APTCACHER=yes
  56. ;;
  57. \?)
  58. show_usage
  59. ;;
  60. esac
  61. done
  62. . ${REP}/config_board.sh
  63. rm -rf $TARGET_DIR && mkdir -p $TARGET_DIR
  64. chroot_deb (){
  65. LC_ALL=C LANGUAGE=C LANG=C chroot $1 /bin/bash -c "$2"
  66. }
  67. if [ ${CROSS} ] ; then
  68. # Debootstrap
  69. mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc
  70. bash ${REP}/script/binfmt-misc-arm.sh unregister
  71. bash ${REP}/script/binfmt-misc-arm.sh
  72. debootstrap --arch=armhf --foreign $DEBIAN_RELEASE $TARGET_DIR
  73. cp /usr/bin/qemu-arm-static $TARGET_DIR/usr/bin/
  74. cp /etc/resolv.conf $TARGET_DIR/etc
  75. chroot_deb $TARGET_DIR '/debootstrap/debootstrap --second-stage'
  76. elif [ ${APTCACHER} ] ; then
  77. debootstrap $DEBIAN_RELEASE $TARGET_DIR http://localhost:3142/ftp.fr.debian.org/debian/
  78. else
  79. debootstrap $DEBIAN_RELEASE $TARGET_DIR
  80. fi
  81. # mount proc, sys and dev
  82. mount -t proc chproc $TARGET_DIR/proc
  83. mount -t sysfs chsys $TARGET_DIR/sys
  84. mount -t devtmpfs chdev $TARGET_DIR/dev || mount --bind /dev $TARGET_DIR/dev
  85. mount -t devpts chpts $TARGET_DIR/dev/pts || mount --bind /dev/pts $TARGET_DIR/dev/pts
  86. # Configure debian apt repository
  87. cat <<EOT > $TARGET_DIR/etc/apt/sources.list
  88. deb http://ftp.fr.debian.org/debian $DEBIAN_RELEASE main contrib non-free
  89. deb http://security.debian.org/ $DEBIAN_RELEASE/updates main contrib non-free
  90. EOT
  91. cat <<EOT > $TARGET_DIR/etc/apt/apt.conf.d/71-no-recommends
  92. APT::Install-Suggests "0";
  93. EOT
  94. if [ ${APTCACHER} ] ; then
  95. cat <<EOT > $TARGET_DIR/etc/apt/apt.conf.d/01proxy
  96. Acquire::http::Proxy "http://localhost:3142";
  97. EOT
  98. fi
  99. chroot_deb $TARGET_DIR 'apt-get update'
  100. # Add useful packages
  101. chroot_deb $TARGET_DIR "apt-get install -y --force-yes openssh-server ntp parted locales vim-nox bash-completion rng-tools $PACKAGES"
  102. echo 'HRNGDEVICE=/dev/urandom' >> $TARGET_DIR/etc/default/rng-tools
  103. echo '. /etc/bash_completion' >> $TARGET_DIR/root/.bashrc
  104. # Use dhcp on boot
  105. cat <<EOT > $TARGET_DIR/etc/network/interfaces
  106. auto lo
  107. iface lo inet loopback
  108. allow-hotplug eth0
  109. iface eth0 inet dhcp
  110. allow-hotplug usb0
  111. iface usb0 inet dhcp
  112. EOT
  113. # Debootstrap optimisations from igorpecovnik
  114. # change default I/O scheduler, noop for flash media, deadline for SSD, cfq for mechanical drive
  115. cat <<EOT >> $TARGET_DIR/etc/sysfs.conf
  116. block/mmcblk0/queue/scheduler = noop
  117. #block/sda/queue/scheduler = cfq
  118. EOT
  119. # flash media tunning
  120. if [ -f "$TARGET_DIR/etc/default/tmpfs" ]; then
  121. sed -e 's/#RAMTMP=no/RAMTMP=yes/g' -i $TARGET_DIR/etc/default/tmpfs
  122. sed -e 's/#RUN_SIZE=10%/RUN_SIZE=128M/g' -i $TARGET_DIR/etc/default/tmpfs
  123. sed -e 's/#LOCK_SIZE=/LOCK_SIZE=/g' -i $TARGET_DIR/etc/default/tmpfs
  124. sed -e 's/#SHM_SIZE=/SHM_SIZE=128M/g' -i $TARGET_DIR/etc/default/tmpfs
  125. sed -e 's/#TMP_SIZE=/TMP_SIZE=1G/g' -i $TARGET_DIR/etc/default/tmpfs
  126. fi
  127. # Generate locales
  128. sed -i "s/^# fr_FR.UTF-8 UTF-8/fr_FR.UTF-8 UTF-8/" $TARGET_DIR/etc/locale.gen
  129. sed -i "s/^# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/" $TARGET_DIR/etc/locale.gen
  130. chroot_deb $TARGET_DIR "locale-gen en_US.UTF-8"
  131. # Update timezone
  132. echo 'Europe/Paris' > $TARGET_DIR/etc/timezone
  133. chroot_deb $TARGET_DIR "dpkg-reconfigure -f noninteractive tzdata"
  134. if [ "$DEBIAN_RELEASE" = "jessie" ] ; then
  135. # Add fstab for root
  136. chroot_deb $TARGET_DIR "echo '/dev/mmcblk0 / ext4 defaults 0 1' >> /etc/fstab"
  137. # Configure tty
  138. install -m 755 -o root -g root ${REP}/config/ttyS0.conf $TARGET_DIR/etc/init/ttyS0.conf
  139. chroot_deb $TARGET_DIR 'cp /lib/systemd/system/serial-getty@.service /etc/systemd/system/getty.target.wants/serial-getty@ttyS0.service'
  140. chroot_deb $TARGET_DIR 'sed -e s/"--keep-baud 115200,38400,9600"/"-L 115200"/g -i /etc/systemd/system/getty.target.wants/serial-getty@ttyS0.service'
  141. # specifics packets add and remove
  142. #chroot_deb $TARGET_DIR "debconf-apt-progress -- apt-get -y install libnl-3-dev busybox-syslogd software-properties-common python-software-properties"
  143. #chroot_deb $TARGET_DIR "apt-get -y remove rsyslog"
  144. # don't clear screen tty1
  145. #chroot_deb $TARGET_DIR 'sed -e s,"TTYVTDisallocate=yes","TTYVTDisallocate=no",g -i /etc/systemd/system/getty.target.wants/getty@tty1.service'
  146. # enable root login for latest ssh on jessie
  147. chroot_deb $TARGET_DIR "sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config"
  148. else
  149. # Configure tty
  150. echo T0:2345:respawn:/sbin/getty -L ttyS0 115200 vt100 >> $TARGET_DIR/etc/inittab
  151. fi
  152. # Good right on some directories
  153. chroot_deb $TARGET_DIR 'chmod 1777 /tmp/'
  154. chroot_deb $TARGET_DIR 'chgrp mail /var/mail/'
  155. chroot_deb $TARGET_DIR 'chmod g+w /var/mail/'
  156. chroot_deb $TARGET_DIR 'chmod g+s /var/mail/'
  157. # Set hostname
  158. echo $DEB_HOSTNAME > $TARGET_DIR/etc/hostname
  159. # Add firstrun and secondrun init script
  160. install -m 755 -o root -g root ${REP}/script/secondrun $TARGET_DIR/etc/init.d/
  161. install -m 755 -o root -g root ${REP}/script/firstrun $TARGET_DIR/etc/init.d/
  162. chroot_deb $TARGET_DIR "insserv firstrun >> /dev/null"
  163. if [ $INSTALL_KERNEL ] ; then
  164. cp ${INSTALL_KERNEL}/*.deb $TARGET_DIR/tmp/
  165. chroot_deb $TARGET_DIR 'dpkg -i /tmp/*.deb'
  166. rm $TARGET_DIR/tmp/*
  167. cp ${INSTALL_KERNEL}/boot.scr $TARGET_DIR/boot/
  168. chroot_deb $TARGET_DIR "ln -s /boot/dtb/$DTB /boot/board.dtb"
  169. fi
  170. if [ $INSTALL_YUNOHOST ] ; then
  171. chroot_deb $TARGET_DIR "apt-get install -y --force-yes git"
  172. chroot_deb $TARGET_DIR "git clone https://github.com/YunoHost/install_script /tmp/install_script"
  173. chroot_deb $TARGET_DIR "cd /tmp/install_script && ./autoinstall_yunohostv2"
  174. fi
  175. # Add 'olinux' for root password and force to change it at first login
  176. chroot_deb $TARGET_DIR '(echo olinux;echo olinux;) | passwd root'
  177. chroot_deb $TARGET_DIR 'chage -d 0 root'
  178. # Remove useless files
  179. chroot_deb $TARGET_DIR 'apt-get clean'
  180. rm $TARGET_DIR/etc/resolv.conf
  181. if [ ${CROSS} ] ; then
  182. rm $TARGET_DIR/usr/bin/qemu-arm-static
  183. fi
  184. if [ ${APTCACHER} ] ; then
  185. rm $TARGET_DIR/etc/apt/apt.conf.d/01proxy
  186. fi
  187. # Umount proc, sys, and dev
  188. umount -l $TARGET_DIR/dev/pts
  189. umount -l $TARGET_DIR/dev
  190. umount -l $TARGET_DIR/proc
  191. umount -l $TARGET_DIR/sys