bootstrap-bullseye.sh 6.3 KB

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