bootstrap-bullseye.sh 7.6 KB

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