bootrap-bullseye.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. prepare) prepare() ;;
  34. install) install() ;;
  35. bootloader) bootloader() ;;
  36. postinstall) postinstall() ;;
  37. *) echo "Valid functions are: grmlnetwork, prepare, install, bootloader, postinstall" >&2 ;;
  38. esac
  39. prepare(){
  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. install(){
  83. #----------
  84. # Chroot
  85. mount -o bind /dev $mnt/dev
  86. mount -o bind /dev/pts $mnt/dev/pts
  87. mount -t sysfs /sys $mnt/sys
  88. mount -t proc /proc $mnt/proc
  89. cp /proc/mounts $mnt/etc/mtab
  90. cp /etc/resolv.conf $mnt/etc/resolv.conf
  91. chroot $mnt /bin/bash
  92. # Install basic system
  93. apt-get update
  94. apt-get install --yes \
  95. apt-utils dialog msmtp-mta \
  96. systemd-sysv locales tzdata haveged \
  97. linux-image-cloud-amd64 grub-pc \
  98. iproute2 netbase \
  99. ssh sudo \
  100. less vim-tiny bash-completion pwgen lsof \
  101. dnsutils iputils-ping curl
  102. # Upgrade and clean up
  103. apt-get upgrade --yes
  104. apt-get autoremove --yes
  105. apt-get clean --yes
  106. # Setup users
  107. pass=`pwgen --capitalize --numerals --ambiguous 12 1`
  108. useradd admin --create-home --shell /bin/bash
  109. echo "admin:$pass" | chpasswd
  110. echo 'root:sa' | chpasswd
  111. usermod -a -G sudo admin
  112. echo -e "\e[1;33;4;44mPassword for the user admin: $pass\e[0m"
  113. # Harden SSHD
  114. echo AllowUsers admin >> /etc/ssh/sshd_config
  115. sed -i -e 's/#Port 22/Port 50101/g' /etc/ssh/sshd_config
  116. sed -i -e 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/g' /etc/ssh/sshd_config
  117. ## Configure network using systemd
  118. if [ ! -z $netAddress ]
  119. then
  120. ## Network OPTION 1 - DHCP
  121. cat >/etc/systemd/network/20-wired.network <<EOL
  122. [Match]
  123. Name=e*
  124. [Network]
  125. DHCP=ipv4
  126. IPv6PrivacyExtensions=false
  127. IPv6AcceptRA=false
  128. NTP=$netNTP
  129. EOL
  130. else
  131. ## Network OPTION 2 - static
  132. cat >/etc/systemd/network/20-wired.network <<EOL
  133. [Match]
  134. Name=$netDev
  135. [Network]
  136. Address=$netAddress
  137. Gateway=$netGateway
  138. Broadcast=$netBroadcast
  139. DNS=$netDNS1
  140. DNS=$netDNS2
  141. NTP=$netNTP
  142. EOL
  143. fi
  144. # Setup systemd resolver
  145. rm /etc/resolv.conf
  146. ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf
  147. systemctl enable systemd-networkd
  148. # to be checked why port 5353 is opened externally
  149. sed -i 's/#LLMNR=yes/LLMNR=no/' /etc/systemd/resolved.conf
  150. systemctl enable systemd-resolved
  151. # Limit journald logging to 1 month, 1 GB in total and split files per week
  152. cat >>/etc/systemd/journald.conf <<EOL
  153. # Custom settings
  154. MaxFileSec=1G
  155. MaxFileSec=1week
  156. MaxFileSec=1m
  157. EOL
  158. # Show errors in motd
  159. rm /etc/motd
  160. cat >/etc/update-motd.d/15-boot-errors<<EOL
  161. #!/bin/sh
  162. echo
  163. journalctl --boot --priority=3 --no-pager
  164. EOL
  165. chmod 755 /etc/update-motd.d/15-boot-errors
  166. # Leave chroot
  167. exit
  168. }
  169. bootloader(){
  170. # Install GRUB in /dev/vba
  171. chroot $mnt /bin/bash -c "grub-install $disk && update-grub"
  172. # Unmount
  173. umount $mnt/proc
  174. umount $mnt/sys
  175. umount $mnt/dev/pts
  176. umount $mnt/dev
  177. }
  178. postinstall(){
  179. ####----REBOOT into the new system, so we'll have dbus running
  180. localectl set-locale LANG=de_DE.UTF-8 # Default for LC_* variables not set.
  181. localectl set-locale LC_MESSAGES=en_US.UTF-8 # System messages.
  182. #localectl set-locale LC_RESPONSE=en_US.UTF-8 # How responses (such as Yes and No) appear
  183. update-locale
  184. timedatectl set-timezone Europe/Berlin
  185. }