create_arm_debootstrap.sh 6.9 KB

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