bootstrap-bullseye.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #!/bin/bash -e
  2. #----------
  3. # Interactive installation steps for Debian Bullseye from GRML using debootstrap
  4. # Design decisions
  5. # - Fokus on a simple setup, primarly for VMs
  6. # - One disk, one partion, swap-file in the same partion as safety net
  7. # - Use systemd whereever possible (network, ntp, cron, journald logging)
  8. # - Minimal number of packages & cloud kernel
  9. # - grub-pc, not efi
  10. # - random root and admin user password generation
  11. # - ssh on port 50101 limited to the admin user
  12. # Usage
  13. # Boot grml and clone repo
  14. # cp config.sh.template config.sh # copy template
  15. # vi config.sh # update installation variables
  16. # bootstrap-bullseye.sh install # start installation
  17. # !! Note down the admin passwords and reboot
  18. # sudo /installer/bootstrap-bullseye.sh postinstall # run postinstall in the new system
  19. # Variables
  20. mnt="/mnt/root" # mountpoint for the new root filesystem
  21. hostname="somehost.example.com"
  22. disk="/dev/vda" # lsblk --list
  23. disk1=$disk"1"
  24. netDev="eth0" # ip link
  25. netAddress="203.0.113.66/24"
  26. netGateway="203.0.113.1"
  27. netBroadcast="203.0.113.255"
  28. netDNS1="192.0.2.10"
  29. netDNS2="198.51.100.10"
  30. netNTP="pool.ntp.org"
  31. [ -f ./config.sh ] && source config.sh
  32. # Setup network in grml
  33. grmlnetwork(){
  34. ip link show # list interfaces
  35. ip addr add $netAddress dev $netDev
  36. ip link set $netDev up
  37. ip route add default via §netGateway
  38. echo nameserver $netDNS1 >> /etc/resolv.conf
  39. echo nameserver $netDNS2 >> /etc/resolv.conf
  40. }
  41. install(){
  42. #----------
  43. # Prepare disks
  44. # Parition disks -- pkg: parted
  45. parted $disk -s \
  46. mklabel msdos \
  47. mkpart primary ext4 512M 100% toggle 1 boot
  48. fdisk -l $disk
  49. # Format disks -- pkg: e2fsprogs dosfstools and to file system check
  50. mkfs.ext4 $disk1 && e2fsck $disk1
  51. # Prepare mount points and mount
  52. mkdir -p $mnt
  53. mount $disk1 $mnt
  54. # Create swapfile
  55. swapfile=$mnt/swapfile
  56. dd if=/dev/zero of=$swapfile bs=1M count=1024 status=progress # create 1GB file
  57. chmod 600 $swapfile #restric permissions
  58. mkswap $swapfile #format file
  59. #----------
  60. # Bootstrap -- pkg: debootstrap
  61. # Remark: Debootstrap does not install recommands!!
  62. debootstrap --variant=minbase --arch=amd64 bullseye $mnt http://ftp2.de.debian.org/debian/
  63. #----------
  64. # Configuration
  65. # Configure disk mounts
  66. # Or get UUID from blkid...
  67. cat >$mnt/etc/fstab <<EOL
  68. $disk1 / ext4 rw 0 0
  69. /swapfile none swap defaults 0 0
  70. EOL
  71. # Configure sources.list
  72. cat >$mnt/etc/apt/sources.list <<EOL
  73. deb http://ftp2.de.debian.org/debian bullseye main contrib non-free
  74. #deb-src http://ftp2.de.debian.org/debian bullseye main contrib non-free
  75. deb http://deb.debian.org/debian-security/ bullseye-security main contrib non-free
  76. #deb-src http://deb.debian.org/debian-security/ bullseye-security main contrib non-free
  77. deb http://ftp2.de.debian.org/debian bullseye-updates main contrib non-free
  78. #deb-src http://ftp2.de.debian.org/debian bullseye-updates main contrib non-free
  79. EOL
  80. # Configure hostname
  81. echo "127.0.0.1 $hostname" >> $mnt/etc/hosts
  82. echo "$hostname" > $mnt/etc/hostname
  83. #----------
  84. # Prepare chroot
  85. mount -o bind /dev $mnt/dev
  86. mount -o bind /dev/pts $mnt/dev/pts
  87. mount -t sysfs /sys $mnt/sys
  88. mount -t proc /proc $mnt/proc
  89. cp /proc/mounts $mnt/etc/mtab
  90. cp /etc/resolv.conf $mnt/etc/resolv.conf
  91. mkdir -p $mnt/installer
  92. cp $(dirname `realpath $0`)/*.sh $mnt/installer
  93. # Run script in chroot
  94. chroot $mnt /bin/bash /installer/bootstrap-bullseye.sh install2
  95. # Install bootloader
  96. $0 bootloader
  97. }
  98. #----------
  99. # Function executed within chroot
  100. install2(){
  101. # Install basic system
  102. apt-get update
  103. apt-get install --yes \
  104. apt-utils dialog msmtp-mta \
  105. systemd-sysv locales tzdata haveged \
  106. linux-image-cloud-amd64 grub-pc \
  107. iproute2 netbase \
  108. ssh sudo \
  109. less vim-tiny bash-completion pwgen lsof \
  110. dnsutils iputils-ping curl
  111. # Upgrade and clean up
  112. apt-get upgrade --yes
  113. apt-get autoremove --yes
  114. apt-get clean --yes
  115. # Setup users and passwords
  116. [ -z $pwdAdmin ] && pwdAdmin=`pwgen --capitalize --numerals --ambiguous 12 1`
  117. useradd admin --create-home --shell /bin/bash
  118. echo "admin:$pwdAdmin" | chpasswd
  119. usermod -a -G sudo admin
  120. echo -e "\e[1;33;4;44mPassword for the user admin: $pass\e[0m"
  121. pass=`pwgen --capitalize --numerals --ambiguous 12 1`
  122. [ -z $pwdRoot ] && pwdRoot=`pwgen --capitalize --numerals --ambiguous 12 1`
  123. echo "root:$pwdRoot" | chpasswd
  124. echo -e "\e[1;33;4;44mPassword for the user root: $pass\e[0m"
  125. # Harden SSHD
  126. echo AllowUsers admin >> /etc/ssh/sshd_config
  127. sed -i -e 's/#Port 22/Port 50101/g' /etc/ssh/sshd_config
  128. sed -i -e 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/g' /etc/ssh/sshd_config
  129. ## Configure network using systemd
  130. if [ -z $netAddress ]
  131. then
  132. ## Network OPTION 1 - DHCP
  133. cat >/etc/systemd/network/20-wired.network <<EOL
  134. [Match]
  135. Name=e*
  136. [Network]
  137. DHCP=ipv4
  138. IPv6PrivacyExtensions=false
  139. IPv6AcceptRA=false
  140. NTP=$netNTP
  141. EOL
  142. else
  143. ## Network OPTION 2 - static
  144. cat >/etc/systemd/network/20-wired.network <<EOL
  145. [Match]
  146. Name=$netDev
  147. [Network]
  148. Address=$netAddress
  149. Gateway=$netGateway
  150. Broadcast=$netBroadcast
  151. DNS=$netDNS1
  152. DNS=$netDNS2
  153. NTP=$netNTP
  154. EOL
  155. fi
  156. # Setup systemd resolver
  157. rm /etc/resolv.conf
  158. ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf
  159. systemctl enable systemd-networkd
  160. # to be checked why port 5353 is opened externally
  161. sed -i 's/#LLMNR=yes/LLMNR=no/' /etc/systemd/resolved.conf
  162. systemctl enable systemd-resolved
  163. # Limit journald logging to 1 month, 1 GB in total and split files per week
  164. cat >>/etc/systemd/journald.conf <<EOL
  165. # Custom settings
  166. MaxFileSec=1G
  167. MaxFileSec=1week
  168. MaxFileSec=1m
  169. EOL
  170. # Show errors in motd
  171. rm /etc/motd
  172. cat >/etc/update-motd.d/15-boot-errors<<EOL
  173. #!/bin/sh
  174. echo
  175. journalctl --boot --priority=3 --no-pager
  176. EOL
  177. chmod 755 /etc/update-motd.d/15-boot-errors
  178. # Leave chroot
  179. exit
  180. }
  181. bootloader(){
  182. # Install GRUB in /dev/vba
  183. chroot $mnt /bin/bash -c "grub-install $disk && update-grub"
  184. }
  185. unmount(){
  186. # Unmount if mounted
  187. ! mountpoint -q $mnt/proc || umount $mnt/proc
  188. ! mountpoint -q $mnt/sys || umount $mnt/sys
  189. ! mountpoint -q $mnt/dev/pts || umount $mnt/dev/pts
  190. ! mountpoint -q $mnt/dev || umount $mnt/dev
  191. ! mountpoint -q $mnt/root || umount $mnt/root
  192. ! mountpoint -q $mnt || umount $mnt
  193. # Delete mount-point if empty and not mounted
  194. [ -z "$(ls -A /mnt/)" ] && ! mountpoint -q /mnt && rm -R $mnt
  195. }
  196. postinstall(){
  197. ####----REBOOT into the new system, so we'll have dbus running
  198. localectl set-locale LANG=de_DE.UTF-8 # Default for LC_* variables not set.
  199. localectl set-locale LC_MESSAGES=en_US.UTF-8 # System messages.
  200. #localectl set-locale LC_RESPONSE=en_US.UTF-8 # How responses (such as Yes and No) appear
  201. update-locale
  202. timedatectl set-timezone Europe/Berlin
  203. }
  204. # Switch to functions...
  205. case $1 in
  206. grmlnetwork)
  207. echo Setup network in grml
  208. grmlnetwork
  209. ;;
  210. install)
  211. echo "Stage 1: Start installation"
  212. install
  213. ;;
  214. install2)
  215. echo "Stage 2: Start installation in chroot"
  216. install2
  217. ;;
  218. bootloader)
  219. echo "Stage 3: Install bootloader and unmount chroot"
  220. bootloader
  221. unmount
  222. echo "We're done and can reboot now"
  223. ;;
  224. postinstall)
  225. echo "Stage 4: Start post-installation in live system"
  226. postinstall
  227. ;;
  228. unmount)
  229. echo "Unmount chroot, e.g. in case installation fails"
  230. unmount
  231. ;;
  232. *)
  233. echo "Valid functions are: grmlnetwork, install, postinstall and unmount" >&2
  234. ;;
  235. esac