bootrap-bullseye.sh 5.3 KB

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