bootstrap-bullseye.sh 7.5 KB

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