bootrap-bullseye.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. # Check if the function exists
  31. case $1 in
  32. grmlnetwork)
  33. grmlnetwork()
  34. ;;
  35. install)
  36. install()
  37. ;;
  38. install2)
  39. install2()
  40. ;;
  41. bootloader)
  42. bootloader()
  43. ;;
  44. postinstall)
  45. postinstall()
  46. ;;
  47. *)
  48. echo "Valid functions are: grmlnetwork, install, postinstall" >&2
  49. ;;
  50. esac
  51. install(){
  52. #----------
  53. # Prepare disks
  54. # Parition disks -- pkg: parted
  55. parted $disk -s \
  56. mklabel msdos \
  57. mkpart primary ext4 512M 100% toggle 1 boot
  58. fdisk -l $disk
  59. # Format disks -- pkg: e2fsprogs dosfstools and to file system check
  60. mkfs.ext4 $disk1 && e2fsck $disk1
  61. # Prepare mount points and mount
  62. mkdir -p $mnt
  63. mount $disk1 $mnt
  64. # Create swapfile
  65. swapfile=$mnt/swapfile
  66. dd if=/dev/zero of=$swapfile bs=1M count=1024 status=progress # create 1GB file
  67. chmod 600 $swapfile #restric permissions
  68. mkswap $swapfile #format file
  69. #----------
  70. # Bootstrap -- pkg: debootstrap
  71. # Remark: Debootstrap does not install recommands!!
  72. debootstrap --variant=minbase --arch=amd64 bullseye $mnt http://ftp2.de.debian.org/debian/
  73. #----------
  74. # Configuration
  75. # Configure disk mounts
  76. # Or get UUID from blkid...
  77. cat >$mnt/etc/fstab <<EOL
  78. $disk1 / ext4 rw 0 0
  79. /swapfile none swap defaults 0 0
  80. EOL
  81. # Configure sources.list
  82. cat >/etc/apt/sources.list <<EOL
  83. deb http://ftp2.de.debian.org/debian bullseye main contrib non-free
  84. #deb-src http://ftp2.de.debian.org/debian bullseye main contrib non-free
  85. deb http://deb.debian.org/debian-security/ bullseye-security main contrib non-free
  86. #deb-src http://deb.debian.org/debian-security/ bullseye-security main contrib non-free
  87. deb http://ftp2.de.debian.org/debian bullseye-updates main contrib non-free
  88. #deb-src http://ftp2.de.debian.org/debian bullseye-updates main contrib non-free
  89. EOL
  90. # Configure hostname
  91. echo "127.0.0.1 $hostname" >> /etc/hosts
  92. echo $hostname > /etc/hostname
  93. #----------
  94. # Prepare chroot
  95. mount -o bind /dev $mnt/dev
  96. mount -o bind /dev/pts $mnt/dev/pts
  97. mount -t sysfs /sys $mnt/sys
  98. mount -t proc /proc $mnt/proc
  99. cp /proc/mounts $mnt/etc/mtab
  100. cp /etc/resolv.conf $mnt/etc/resolv.conf
  101. mkdir -p $mnt/installer
  102. cp $(dirname `realpath $0`)/*.sh $mnt/installer
  103. # Run script in chroot
  104. chroot $mnt /bin/bash $mnt/installer/bootrap-bullseye.sh install2
  105. # Install bootloader
  106. $0 bootloader
  107. }
  108. #----------
  109. # Function executed within chroot
  110. install2(){
  111. # Install basic system
  112. apt-get update
  113. apt-get install --yes \
  114. apt-utils dialog msmtp-mta \
  115. systemd-sysv locales tzdata haveged \
  116. linux-image-cloud-amd64 grub-pc \
  117. iproute2 netbase \
  118. ssh sudo \
  119. less vim-tiny bash-completion pwgen lsof \
  120. dnsutils iputils-ping curl
  121. # Upgrade and clean up
  122. apt-get upgrade --yes
  123. apt-get autoremove --yes
  124. apt-get clean --yes
  125. # Setup users
  126. pass=`pwgen --capitalize --numerals --ambiguous 12 1`
  127. useradd admin --create-home --shell /bin/bash
  128. echo "admin:$pass" | chpasswd
  129. echo 'root:sa' | chpasswd
  130. usermod -a -G sudo admin
  131. echo -e "\e[1;33;4;44mPassword for the user admin: $pass\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. # Leave chroot
  186. exit
  187. }
  188. bootloader(){
  189. # Install GRUB in /dev/vba
  190. chroot $mnt /bin/bash -c "grub-install $disk && update-grub"
  191. # Unmount
  192. umount $mnt/proc
  193. umount $mnt/sys
  194. umount $mnt/dev/pts
  195. umount $mnt/dev
  196. }
  197. postinstall(){
  198. ####----REBOOT into the new system, so we'll have dbus running
  199. localectl set-locale LANG=de_DE.UTF-8 # Default for LC_* variables not set.
  200. localectl set-locale LC_MESSAGES=en_US.UTF-8 # System messages.
  201. #localectl set-locale LC_RESPONSE=en_US.UTF-8 # How responses (such as Yes and No) appear
  202. update-locale
  203. timedatectl set-timezone Europe/Berlin
  204. }