bootrap-bullseye.sh 5.6 KB

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