bootstrap-bookworm.sh 7.9 KB

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