bootstrap-bullseye.sh 6.9 KB

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