bootstrap.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #!/bin/bash
  2. # Copyright 2012-2017 Aubort Jean-Baptiste <jean-baptiste.aubort@epfl.ch>
  3. VERSION=1.0.170706.1606
  4. HOSTNAME='debianzulu'
  5. DIR='./debian'
  6. DEV='/dev/sdb'
  7. PART="$DEV"1
  8. USER_1='fixme'
  9. PW_ROOT='fixme'
  10. PW_USER='fixme'
  11. ARCH='i386'
  12. VERSION_DEBIAN='stretch'
  13. REPO='http://ftp.ch.debian.org/debian'
  14. #
  15. # Create the chroot (in the host)
  16. #
  17. create_chroot(){
  18. umount_chroot
  19. echo "[*] Remove current directory: $DIR"
  20. rm -fr $DIR
  21. mkdir $DIR
  22. echo "[*] Installing base system: $DIR"
  23. debootstrap --arch=$ARCH $VERSION_DEBIAN $DIR $REPO
  24. }
  25. #
  26. # Install basic stuff (in the chroot)
  27. #
  28. install_chroot(){
  29. #
  30. # Prepare
  31. #
  32. mount_chroot
  33. ln -s /proc/mounts $DIR/etc/mtab
  34. #
  35. # Pre-Installation
  36. #
  37. cp ./conf/sources.list-$VERSION_DEBIAN $DIR/etc/apt/sources.list
  38. cp ./conf/init-apt-tmpfs $DIR/etc/init.d/apt-tmpfs
  39. cp ./conf/init-ramfs $DIR/etc/init.d/ramfs
  40. cp ./conf/debconf-selections $DIR/tmp/.
  41. chmod +x $DIR/etc/init.d/apt-tmpfs
  42. chmod +x $DIR/etc/init.d/ramfs
  43. # Network
  44. cp /etc/resolv.conf $DIR/etc/resolv.conf
  45. echo "$HOSTNAME" > $DIR/etc/hostname
  46. if [ $(cat $DIR/etc/hosts | grep '127.0.1.1' > /dev/null; echo $?) -eq 1 ]; then
  47. echo -e "127.0.1.1\t$HOSTNAME" >> $DIR/etc/hosts
  48. fi
  49. echo -e "auto lo eth0 wlan0\niface lo inet loopback" > $DIR/etc/network/interfaces
  50. # APT pinning
  51. #cp ./conf/chromium.pref $DIR/etc/apt/preferences.d/.
  52. if [ "$VERSION_DEBIAN" == "squeeze" ]; then
  53. cp ./conf/preferences $DIR/etc/apt/preferences
  54. fi
  55. #
  56. # Installation
  57. #
  58. echo "
  59. #!/bin/bash
  60. export HOME='/root'
  61. #
  62. # Install and configure programs
  63. #
  64. if [ \$(dpkg -l | grep deb-multimedia-keyring | grep ii > /dev/null; echo \$?) -eq 1 ]; then
  65. apt-get update
  66. apt-get -o Aptitude::CmdLine::Ignore-Trust-Violations=true --force-yes -y install deb-multimedia-keyring
  67. fi
  68. RET=1
  69. while [ \$RET -ne 0 ]; do
  70. gpg --recv 5A9BF3BB4E5E17B5 2>&1 | grep 'Total number processed: 1'; RET=\$?
  71. if [ \$RET -eq 0 ]; then gpg --export --armor 5A9BF3BB4E5E17B5 | apt-key add -; fi
  72. sleep 1
  73. done
  74. apt-get update
  75. apt-get --force-yes -y install debconf-utils
  76. debconf-set-selections < /tmp/debconf-selections
  77. apt-get --force-yes -y install $(cat conf/packages-$ARCH | tr '\n' ' ')
  78. apt-get --force-yes -y install $(cat conf/packages-base | tr '\n' ' ')
  79. apt-get --force-yes -y install $(cat conf/packages-$VERSION_DEBIAN | tr '\n' ' ')
  80. #
  81. # Configure packages
  82. #
  83. rm -f /etc/locale.gen
  84. dpkg-reconfigure -f noninteractive locales
  85. dpkg-reconfigure -f noninteractive keyboard-configuration
  86. echo 'Europe/Zurich' > /etc/timezone
  87. dpkg-reconfigure -f noninteractive tzdata
  88. apt-get --force-yes -y upgrade
  89. apt-get --force-yes -y dist-upgrade
  90. localepurge
  91. insserv -r avahi-daemon
  92. insserv -r hwclock.sh
  93. insserv apt-tmpfs
  94. insserv ramfs
  95. loadkeys fr_CH
  96. #easy_install web.py
  97. # Regenerate ssh host keys
  98. rm /etc/ssh/ssh_host_*
  99. dpkg-reconfigure openssh-server
  100. #
  101. # Users and groups
  102. #
  103. adduser --disabled-password $USER_1
  104. addgroup wheel
  105. addgroup powerdev #FIX for wheezy which does not have this group
  106. usermod -G sudo,bluetooth,wheel,cdrom,floppy,audio,dip,video,plugdev,powerdev,netdev,dialout $USER_1
  107. echo $USER_1:$PW_USER | chpasswd
  108. echo root:$PW_ROOT | chpasswd
  109. #TODO: HASH=\$(echo 1234 | openssl passwd -1 -stdin)
  110. #TODO: Use usermod -p HASH user
  111. #
  112. # Remove unneeded files
  113. #
  114. rm -rf /usr/share/icons/*/icon-theme.cache
  115. apt-get --purge --force-yes -y remove aspell aspell-en manpages ppp man-db info manpages groff-base cyrus-sasl2-doc tasksel tasksel-data ed nano dmidecode
  116. # Clean and self-destruction
  117. apt-get --force-yes -y autoremove
  118. apt-get clean
  119. rm \$0
  120. " > $DIR/tmp/install.sh
  121. chroot $DIR /usr/bin/env -i PATH="$PATH" HOME='/root' TERM="$TERM" /bin/bash /tmp/install.sh
  122. error_chroot
  123. #
  124. # Post-Installation
  125. #
  126. echo $VERSION > $DIR/home/$USER_1/zulu_version
  127. mkdir $DIR/home/$USER_1/scripts
  128. mkdir -p $DIR/home/$USER_1/.config/autostart
  129. mkdir -p $DIR/home/$USER_1/.cache/sessions
  130. mkdir -p $DIR/etc/X11/xorg.conf.d
  131. touch $DIR/home/$USER_1/.xsession-errors
  132. ln -s /usr/bin/thunar $DIR/usr/bin/nautilus
  133. cp ./conf/20-thinkpad.conf $DIR/etc/X11/xorg.conf.d/.
  134. cp ./conf/bashrc $DIR/home/$USER_1/.bashrc
  135. cp ./conf/bashrc $DIR/root/.bashrc
  136. cp ./conf/50-synaptics.conf $DIR/usr/share/X11/xorg.conf.d/50-synaptics.conf
  137. cp ./conf/rsyslog $DIR/etc/rsyslog.conf
  138. cp ./conf/inittab $DIR/etc/.
  139. cp ./conf/logrotate-rsyslog $DIR/etc/logrotate.d/rsyslog
  140. cp ./conf/fstab $DIR/etc/fstab
  141. cp ./conf/liveusb.conf $DIR/etc/sysctl.d/.
  142. cp ./conf/extlinux.conf $DIR/.
  143. cp ./conf/.conky* $DIR/home/$USER_1/. #FIXME: Desktop only
  144. cp ./conf/xscreensaver.desktop $DIR/home/$USER_1/.config/autostart/. #FIXME: Desktop only
  145. cp ./conf/xfce-session "$DIR/home/$USER_1/.cache/sessions/xfce4-session-$HOSTNAME:0"
  146. cp --preserve=all -r ./scripts/* $DIR/home/$USER_1/scripts/
  147. cp ./conf/rcS $DIR/etc/default/.
  148. sed -i 's/RAMRUN=no/RAMRUN=yes/' $DIR/etc/default/rcS
  149. sed -i 's/RAMLOCK=no/RAMLOCK=yes/' $DIR/etc/default/rcS
  150. sed -i 's/managed=false/managed=true/' $DIR/etc/NetworkManager/NetworkManager.conf
  151. sed -i 's/PermitRootLogin yes/PermitRootLogin no/' $DIR/etc/ssh/sshd_config
  152. sed -i "s/USER/$USER_1/" "$DIR/home/$USER_1/.cache/sessions/xfce4-session-$HOSTNAME:0"
  153. sed -i "s/HOSTNAME/$HOSTNAME/" "$DIR/home/$USER_1/.cache/sessions/xfce4-session-$HOSTNAME:0"
  154. if [ $(cat $DIR/etc/ssh/sshd_config | grep AllowUsers > /dev/null; echo $?) -eq 1 ]; then
  155. echo "AllowUsers $USER_1" >> $DIR/etc/ssh/sshd_config
  156. elif [ $(cat $DIR/etc/ssh/sshd_config | grep AllowUsers | grep $USER_1 > /dev/null; echo $?) -eq 1 ]; then
  157. sed -i "s/AllowUsers .*/\0 $USER_1/" $DIR/etc/ssh/sshd_config
  158. fi
  159. echo -e "[Desktop Entry]\nHidden=true\n" > $DIR/home/$USER_1/.config/autostart/xfce4-tips-autostart.desktop
  160. chown -R 1000:1000 $DIR/home/$USER_1
  161. patch -f -N $DIR/etc/init.d/sendsigs < conf/sendsigs.patch
  162. rm $DIR/etc/init.d/sendsigs.*
  163. touch $DIR/etc/udev/rules.d/70-persistent-net.rules
  164. if [ $(grep UseDNS $DIR/etc/ssh/sshd_config > /dev/null; echo $?) -ne 0 ]; then
  165. echo 'UseDNS no' >> $DIR/etc/ssh/sshd_config #Speedup login
  166. fi
  167. #
  168. # Distribution specific
  169. #
  170. if [[ "$VERSION_DEBIAN" == "squeeze" ]]; then
  171. cp ./conf/slim.conf $DIR/etc/.
  172. sed -i "s/USER/$USER_1/" $DIR/etc/slim.conf
  173. msgfmt -o $DIR/usr/share/locale/fr/LC_MESSAGES/libgnome-keyring.mo conf/libgnome-keyring.po #Bug Debian #591659
  174. else
  175. mkdir -p $DIR/var/lib/lightdm
  176. touch $DIR/var/lib/lightdm/.Xauthority
  177. chown 1000:1000 $DIR/var/lib/lightdm/.Xauthority
  178. cp ./conf/lightdm.conf $DIR/etc/lightdm/.
  179. cp ./conf/lightdm-autologin $DIR/etc/pam.d/.
  180. sed -i "s/USER/$USER_1/" $DIR/etc/lightdm/lightdm.conf
  181. fi
  182. #
  183. # Clean
  184. #
  185. rm -rf $DIR/tmp/*
  186. rm -rf $DIR/var/log/*
  187. rm -rf $DIR/var/cache/apt/archives/*
  188. umount_chroot
  189. }
  190. #
  191. # Chroot shell
  192. #
  193. error_chroot(){
  194. if [ $? -eq 126 ]; then
  195. echo -e "\033[1;31m[!] You probably are trying to chroot into a 64-bit system from a 32-bit host\033[0m"
  196. fi
  197. }
  198. mount_chroot(){
  199. mount -t proc proc $DIR/proc
  200. mount -t sysfs sysfs $DIR/sys
  201. mount -o bind /dev $DIR/dev
  202. mount -t devpts /dev/pts $DIR/dev/pts
  203. #mount -t tmpfs /dev/shm $DIR/dev/shm
  204. mount -t tmpfs /tmp $DIR/tmp
  205. }
  206. umount_chroot(){
  207. umount -f $DIR/{sys,proc,tmp,dev,dev/pts} &> /dev/null
  208. umount -l $DIR/{sys,proc,tmp,dev,dev/pts} &> /dev/null
  209. unlink $DIR/etc/mtab
  210. }
  211. #
  212. # Choose version
  213. #
  214. choose_version(){
  215. echo '[*] Versions:'
  216. echo -e "\t1) Debian 6 Squeeze"
  217. echo -e "\t2) Debian 7 Wheezy"
  218. echo -e "\t3) Debian 8 Jessie"
  219. echo -e "\t4) Debian 9 Stretch"
  220. echo ''
  221. read -p '[+] Choose a Debian version: ' version
  222. case $version in
  223. 1)
  224. VERSION_DEBIAN='squeeze'
  225. ;;
  226. 2)
  227. VERSION_DEBIAN='wheezy'
  228. ;;
  229. 3)
  230. VERSION_DEBIAN='jessie'
  231. ;;
  232. 4)
  233. VERSION_DEBIAN='stretch'
  234. ;;
  235. *)
  236. echo '[!] Invalid version'
  237. choose_version
  238. ;;
  239. esac
  240. }
  241. #
  242. # Choose architecture
  243. #
  244. choose_arch(){
  245. echo '[*] Architecture:'
  246. echo -e "\t1) 32-bit"
  247. echo -e "\t2) 64-bit"
  248. echo ''
  249. read -p '[+] Choose an architecture: ' arch
  250. case $arch in
  251. 1)
  252. ARCH='i386'
  253. ;;
  254. 2)
  255. ARCH='amd64'
  256. ;;
  257. *)
  258. echo '[!] Invalid architecture
  259. '
  260. choose_arch
  261. ;;
  262. esac
  263. }
  264. #
  265. # Main
  266. #
  267. clear
  268. echo
  269. echo ' --------------------------------'
  270. echo " Zulu Bootstrap $VERSION"
  271. echo ' --------------------------------'
  272. echo
  273. if [ "$USER" != "root" ]; then
  274. echo '[!] You must be root'
  275. exit
  276. fi
  277. exec 2> >(tee errors.log)
  278. echo -n '[*] Installing dependencies ... '
  279. apt-get -qq --force-yes -y install debootstrap extlinux parted pv bc gettext 1> /dev/null
  280. echo 'done.'
  281. echo
  282. choose_version
  283. echo
  284. choose_arch
  285. DIR="$DIR-$VERSION_DEBIAN-$ARCH"
  286. while true; do
  287. echo ''
  288. echo "[*] Menu for Debian $(echo $VERSION_DEBIAN | sed 's/^./\u&/') $ARCH: "
  289. echo -e "\t1) Create chroot\t(download from $REPO)"
  290. echo -e "\t2) Configure chroot\t(install packages, configure applications)"
  291. echo -e "\t3) Login to chroot\t(locally login to chroot)"
  292. echo -e "\t*) exit"
  293. echo ''
  294. read -p '[+] Choose an action: ' menu
  295. case $menu in
  296. 1)
  297. echo ''
  298. echo '[*] Create chroot'
  299. create_chroot
  300. echo
  301. echo '[*] FINISHED Create chroot!'
  302. ;;
  303. 2)
  304. echo ''
  305. echo '[*] Install chroot'
  306. install_chroot
  307. echo
  308. echo '[*] FINISHED Install chroot!'
  309. echo '[!] use ./scripts/duplicate/duplicate.sh to install'
  310. ;;
  311. 3)
  312. echo ''
  313. echo "[*] Connecting to chroot: $DIR"
  314. mount_chroot
  315. chroot $DIR /usr/bin/env -i PATH="$PATH" HOME='/root' TERM="$TERM" /bin/bash -i
  316. error_chroot
  317. umount_chroot
  318. echo '[*] FINISHED Connecting to chroot!'
  319. ;;
  320. *)
  321. umount_chroot
  322. exit
  323. ;;
  324. esac
  325. done