bootrap-bullseye.sh 6.0 KB

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