create_arm_debootstrap.sh 8.0 KB

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