bootstrap-bullseye.sh 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #!/bin/bash -e
  2. #----------
  3. # Interactive installation steps for Debian Bullseye from GRML using debootstrap
  4. # Design decisions
  5. # - Fokus on a simple setup, primarly for VMs
  6. # - One disk, one partion, swap-file in the same partion as safety net
  7. # - Use systemd whereever possible (network, ntp, cron, journald logging)
  8. # - Minimal number of packages & cloud kernel
  9. # - grub-pc, not efi
  10. # - random root and admin user password generation
  11. # - ssh on port 50101 limited to the admin user
  12. # Usage
  13. # # Boot grml
  14. # passwd root
  15. # grml-network
  16. # Start ssh
  17. # git clone https://git.in-ulm.de/ulpeters/bootstrap.git
  18. # cp config.sh.template config.sh # copy template
  19. # vi config.sh # update installation variables
  20. # bootstrap-bullseye.sh install # start installation
  21. # !! Note down the admin passwords and reboot
  22. # sudo /installer/bootstrap-bullseye.sh postinstall # run postinstall in the new system
  23. # Variables
  24. mnt="/mnt/root" # mountpoint for the new root filesystem
  25. hostname="somehost.example.com"
  26. disk="/dev/vda" # lsblk --list
  27. disk1=$disk"1"
  28. netDev="eth0" # ip link
  29. netAddress="203.0.113.66/24"
  30. netGateway="203.0.113.1"
  31. netBroadcast="203.0.113.255"
  32. netDNS1="192.0.2.10"
  33. netDNS2="198.51.100.10"
  34. netNTP="pool.ntp.org"
  35. extraPackages=""
  36. [ -f ./config.sh ] && source config.sh
  37. # Setup network in grml
  38. grmlnetwork(){
  39. ip link show # list interfaces
  40. ip addr add $netAddress dev $netDev
  41. ip link set $netDev up
  42. ip route add default via $netGateway
  43. echo nameserver $netDNS1 >> /etc/resolv.conf
  44. echo nameserver $netDNS2 >> /etc/resolv.conf
  45. }
  46. install(){
  47. #----------
  48. # Prepare disks
  49. # Parition disks -- pkg: parted
  50. parted $disk -s \
  51. mklabel msdos \
  52. mkpart primary ext4 512M 100% toggle 1 boot
  53. fdisk -l $disk
  54. # Format disks -- pkg: e2fsprogs dosfstools and to file system check
  55. mkfs.ext4 $disk1 && e2fsck $disk1
  56. # Prepare mount points and mount
  57. mkdir -p $mnt
  58. mount $disk1 $mnt
  59. # Create swapfile
  60. swapfile=$mnt/swapfile
  61. dd if=/dev/zero of=$swapfile bs=1M count=1024 status=progress # create 1GB file
  62. chmod 600 $swapfile #restric permissions
  63. mkswap $swapfile #format file
  64. #----------
  65. # Bootstrap -- pkg: debootstrap
  66. # Remark: Debootstrap does not install recommands!!
  67. debootstrap --variant=minbase --arch=amd64 bullseye $mnt http://ftp2.de.debian.org/debian/
  68. #----------
  69. # Configuration
  70. # Configure disk mounts
  71. # Or get UUID from blkid...
  72. cat >$mnt/etc/fstab <<EOL
  73. $disk1 / ext4 rw 0 0
  74. /swapfile none swap defaults 0 0
  75. EOL
  76. # Configure sources.list
  77. cat >$mnt/etc/apt/sources.list <<EOL
  78. deb http://ftp2.de.debian.org/debian bullseye main contrib non-free
  79. #deb-src http://ftp2.de.debian.org/debian bullseye main contrib non-free
  80. deb http://deb.debian.org/debian-security/ bullseye-security main contrib non-free
  81. #deb-src http://deb.debian.org/debian-security/ bullseye-security main contrib non-free
  82. deb http://ftp2.de.debian.org/debian bullseye-updates main contrib non-free
  83. #deb-src http://ftp2.de.debian.org/debian bullseye-updates main contrib non-free
  84. EOL
  85. # Configure hostname
  86. echo "127.0.0.1 $hostname" >> $mnt/etc/hosts
  87. echo "$hostname" > $mnt/etc/hostname
  88. #----------
  89. # Prepare chroot
  90. mount -o bind /dev $mnt/dev
  91. mount -o bind /dev/pts $mnt/dev/pts
  92. mount -t sysfs /sys $mnt/sys
  93. mount -t proc /proc $mnt/proc
  94. cp /proc/mounts $mnt/etc/mtab
  95. cp /etc/resolv.conf $mnt/etc/resolv.conf
  96. mkdir -p $mnt/installer
  97. cp $(dirname `realpath $0`)/*.sh $mnt/installer
  98. # Run script in chroot
  99. chroot $mnt /bin/bash /installer/bootstrap-bullseye.sh install2
  100. # Install bootloader
  101. $0 bootloader
  102. }
  103. #----------
  104. # Function executed within chroot
  105. install2(){
  106. source /installer/config.sh
  107. # Install basic system
  108. apt-get update
  109. apt-get install --yes \
  110. apt-utils dialog msmtp-mta \
  111. systemd-sysv locales tzdata haveged \
  112. linux-image-cloud-amd64 grub-pc \
  113. iproute2 netbase \
  114. ssh sudo molly-guard \
  115. less vim-tiny bash-completion pwgen lsof \
  116. dnsutils iputils-ping curl \
  117. $extraPackages
  118. # Upgrade and clean up
  119. apt-get upgrade --yes
  120. apt-get autoremove --yes
  121. apt-get clean --yes
  122. # Setup users and passwords
  123. [ -z $pwdAdmin ] && pwdAdmin=`pwgen --capitalize --numerals --ambiguous 12 1`
  124. useradd admin --create-home --shell /bin/bash
  125. echo "admin:$pwdAdmin" | chpasswd
  126. usermod -a -G sudo admin
  127. echo -e "\e[1;33;4;44mPassword for the user admin: $pwdAdmin\e[0m"
  128. pass=`pwgen --capitalize --numerals --ambiguous 12 1`
  129. [ -z $pwdRoot ] && pwdRoot=`pwgen --capitalize --numerals --ambiguous 12 1`
  130. echo "root:$pwdRoot" | chpasswd
  131. echo -e "\e[1;33;4;44mPassword for the user root: $pwdRoot\e[0m"
  132. # Harden SSHD
  133. sed -i -e 's/#Port 22/Port 50101/g' /etc/ssh/sshd_config
  134. sed -i -e 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/g' /etc/ssh/sshd_config
  135. # Allow admin to sudo without password
  136. echo AllowUsers admin >> /etc/ssh/sshd_config
  137. echo "admin ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/admin
  138. ## Configure network using systemd
  139. if [ -z $netAddress ]
  140. then
  141. ## Network OPTION 1 - DHCP
  142. cat >/etc/systemd/network/20-wired.network <<EOL
  143. [Match]
  144. Name=e*
  145. [Network]
  146. DHCP=ipv4
  147. IPv6PrivacyExtensions=false
  148. IPv6AcceptRA=false
  149. NTP=$netNTP
  150. EOL
  151. else
  152. ## Network OPTION 2 - static
  153. cat >/etc/systemd/network/20-wired.network <<EOL
  154. [Match]
  155. Name=$netDev
  156. [Network]
  157. Address=$netAddress
  158. Gateway=$netGateway
  159. Broadcast=$netBroadcast
  160. DNS=$netDNS1
  161. DNS=$netDNS2
  162. NTP=$netNTP
  163. EOL
  164. fi
  165. # Setup systemd resolver
  166. rm /etc/resolv.conf
  167. ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf
  168. systemctl enable systemd-networkd
  169. # to be checked why port 5353 is opened externally
  170. sed -i 's/#LLMNR=yes/LLMNR=no/' /etc/systemd/resolved.conf
  171. systemctl enable systemd-resolved
  172. # Limit journald logging to 1 month, 1 GB in total and split files per week
  173. cat >>/etc/systemd/journald.conf <<EOL
  174. # Custom settings
  175. MaxFileSec=1G
  176. MaxFileSec=1week
  177. MaxFileSec=1m
  178. EOL
  179. # Show errors in motd
  180. rm /etc/motd
  181. cat >/etc/update-motd.d/15-boot-errors<<EOL
  182. #!/bin/sh
  183. echo
  184. journalctl --boot --priority=3 --no-pager
  185. EOL
  186. chmod 755 /etc/update-motd.d/15-boot-errors
  187. # Setup keyboard layout
  188. cat >/etc/default/keyboard <<EOL
  189. XKBMODEL="pc105"
  190. XKBLAYOUT="de"
  191. XKBVARIANT="nodeadkeys"
  192. XKBOPTIONS=""
  193. BACKSPACE="guess"
  194. EOL
  195. # Leave chroot
  196. exit
  197. }
  198. bootloader(){
  199. # Install GRUB in /dev/vba
  200. chroot $mnt /bin/bash -c "grub-install $disk && update-grub"
  201. }
  202. unmount(){
  203. # Unmount if mounted
  204. ! mountpoint -q $mnt/proc || umount $mnt/proc
  205. ! mountpoint -q $mnt/sys || umount $mnt/sys
  206. ! mountpoint -q $mnt/dev/pts || umount $mnt/dev/pts
  207. ! mountpoint -q $mnt/dev || umount $mnt/dev
  208. ! mountpoint -q $mnt/root || umount $mnt/root
  209. ! mountpoint -q $mnt || umount $mnt
  210. # Delete mount-point if empty and not mounted
  211. [ -z "$(ls -A /mnt/)" ] && ! mountpoint -q $mnt && rm -R $mnt
  212. }
  213. postinstall(){
  214. ####----REBOOT into the new system, so we'll have dbus running
  215. localectl set-locale LANG=de_DE.UTF-8 # Default for LC_* variables not set.
  216. localectl set-locale LC_MESSAGES=en_US.UTF-8 # System messages.
  217. #localectl set-locale LC_RESPONSE=en_US.UTF-8 # How responses (such as Yes and No) appear
  218. update-locale
  219. timedatectl set-timezone Europe/Berlin
  220. }
  221. # Switch to functions...
  222. case $1 in
  223. grmlnetwork)
  224. echo Setup network in grml
  225. grmlnetwork
  226. ;;
  227. install)
  228. echo "Stage 1: Start installation"
  229. install
  230. ;;
  231. install2)
  232. echo "Stage 2: Start installation in chroot"
  233. install2
  234. ;;
  235. bootloader)
  236. echo "Stage 3: Install bootloader and unmount chroot"
  237. bootloader
  238. unmount
  239. echo "We're done and can reboot now"
  240. ;;
  241. postinstall)
  242. echo "Stage 4: Start post-installation in live system"
  243. postinstall
  244. ;;
  245. unmount)
  246. echo "Unmount chroot, e.g. in case installation fails"
  247. unmount
  248. ;;
  249. *)
  250. echo "Valid functions are: grmlnetwork, install, postinstall and unmount" >&2
  251. ;;
  252. esac