bootrap_bullseye.sh 5.1 KB

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