bootstrap-bookworm.sh 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. # - support for grub-pc and grub-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. # config-get-netconf-eth0.sh # get running grml network config
  20. # vi config.sh # update installation variables
  21. # bootstrap-bullseye.sh install # start installation
  22. # !! Note down the admin passwords and reboot
  23. # sudo /installer/bootstrap-bullseye.sh postinstall # run postinstall in the new system
  24. # Variables
  25. mnt="/mnt/root" # mountpoint for the root filesystem during installation
  26. hostname="somehost.example.com"
  27. partition="mbr-single" # mbr-single or efi-crypt
  28. disk="/dev/vda" # lsblk --list
  29. disk1=$disk"1"
  30. disk0=$disk"p1" # efi partion, only relevant if partion="efi"
  31. netDev="eth0" # ip link
  32. netAddress="203.0.113.66/24"
  33. netGateway="203.0.113.1"
  34. netBroadcast="203.0.113.255"
  35. netDNS1="192.0.2.10"
  36. netDNS2="198.51.100.10"
  37. netNTP="pool.ntp.org"
  38. extraPackages=""
  39. [ -f ./config.sh ] && source config.sh
  40. # Setup network in grml
  41. grmlnetwork(){
  42. ip link show # list interfaces
  43. ip addr add $netAddress dev $netDev
  44. ip link set $netDev up
  45. ip route add default via $netGateway
  46. echo nameserver $netDNS1 >> /etc/resolv.conf
  47. echo nameserver $netDNS2 >> /etc/resolv.conf
  48. }
  49. install(){
  50. # Wipe existing partition table
  51. dd if=/dev/zero of=$disk bs=512 count=34
  52. # Parition disks -- pkg: parted
  53. # Prepare partition tables and partitions
  54. # -parted --script does not accept blanks in partition names
  55. if [ "$partition" = "mbr-single" ]
  56. then
  57. #----------
  58. # Prepare disks with a single partition
  59. parted $disk --script \
  60. mklabel msdos \
  61. mkpart primary ext4 512M 100% toggle 1 boot
  62. fdisk -l $disk
  63. # Format disks -- pkg: e2fsprogs dosfstools and to file system check
  64. mkfs.ext4 $disk1 && e2fsck $disk1
  65. # Prepare mount points and mount
  66. mkdir -p $mnt
  67. mount $disk1 $mnt
  68. fi
  69. if [ "$partition" = "efi-crypt" ]
  70. then
  71. #----------
  72. # Prepare disks with following layout
  73. # - 301 MB partition for EFI --> p1
  74. # - 50 GB root partition for the OS (includes /boot) --> p2
  75. # - Remaining disk left to create a luks container --> p3
  76. parted $disk --script \
  77. mklabel gpt \
  78. mkpart EFI_system_partition fat32 1MiB 301MiB \
  79. set 1 esp on \
  80. set 1 boot on \
  81. align-check optimal 1 \
  82. mkpart Linux_system_parition ext4 301MiB 50GiB \
  83. align-check optimal 2 \
  84. mkpart Data_partion 50GiB 100% \
  85. align-check optimal 3 \
  86. unit MiB \
  87. print
  88. # Format disks -- pkg: e2fsprogs dosfstools and to file system check
  89. mkfs.fat -F 32 -n EFIBOOT $disk0 && fsck $disk0
  90. mkfs.ext4 $disk1 && fsck $disk1
  91. # Prepare mount points and mount
  92. mkdir -p $mnt $mnt"/boot/efi"
  93. mount $disk1 $mnt
  94. mount $disk0 $mnt"/boot/efi"
  95. fi
  96. # Create swapfile
  97. swapfile=$mnt/swapfile
  98. dd if=/dev/zero of=$swapfile bs=1M count=1024 status=progress # create 1GB file
  99. chmod 600 $swapfile #restric permissions
  100. mkswap $swapfile #format file
  101. #----------
  102. # Bootstrap -- pkg: debootstrap
  103. # Remark: Debootstrap does not install recommands!!
  104. debootstrap --variant=minbase --arch=amd64 bookworm $mnt http://ftp2.de.debian.org/debian/
  105. #----------
  106. # Configuration
  107. # Configure disk mounts
  108. # Or get UUID from blkid...
  109. cat >$mnt/etc/fstab <<EOL
  110. $disk1 / ext4 rw 0 0
  111. /swapfile none swap defaults 0 0
  112. EOL
  113. # Configure sources.list
  114. cat >$mnt/etc/apt/sources.list <<EOL
  115. deb http://deb.debian.org/debian bookworm main non-free-firmware
  116. # deb-src http://deb.debian.org/debian bookworm main non-free-firmware
  117. deb http://deb.debian.org/debian-security/ bookworm-security main non-free-firmware
  118. # deb-src http://deb.debian.org/debian-security/ bookworm-security main non-free-firmware
  119. deb http://deb.debian.org/debian bookworm-updates main non-free-firmware
  120. # deb-src http://deb.debian.org/debian bookworm-updates main non-free-firmware
  121. EOL
  122. # Configure hostname
  123. echo "127.0.0.1 $hostname" >> $mnt/etc/hosts
  124. echo "$hostname" > $mnt/etc/hostname
  125. #----------
  126. # Prepare chroot
  127. mount -o bind /dev $mnt/dev
  128. mount -o bind /dev/pts $mnt/dev/pts
  129. mount -t sysfs /sys $mnt/sys
  130. mount -t proc /proc $mnt/proc
  131. cp /proc/mounts $mnt/etc/mtab
  132. cp /etc/resolv.conf $mnt/etc/resolv.conf
  133. mkdir -p $mnt/installer
  134. cp $(dirname `realpath $0`)/*.sh $mnt/installer
  135. # Run script in chroot
  136. chroot $mnt /bin/bash /installer/bootstrap-bullseye.sh install2
  137. # Install bootloader
  138. $0 bootloader
  139. }
  140. #----------
  141. # Function executed within chroot
  142. install2(){
  143. source /installer/config.sh
  144. # Install basic system
  145. apt-get update
  146. apt-get install --yes \
  147. apt-utils dialog msmtp-mta \
  148. systemd-sysv locales tzdata haveged \
  149. linux-image-cloud-amd64 grub-pc \
  150. iproute2 netbase \
  151. ssh sudo molly-guard \
  152. less vim-tiny bash-completion pwgen lsof \
  153. dnsutils iputils-ping curl \
  154. $extraPackages
  155. # Upgrade and clean up
  156. apt-get upgrade --yes
  157. apt-get autoremove --yes
  158. apt-get clean --yes
  159. # Setup users and passwords
  160. [ -z $pwdAdmin ] && pwdAdmin=`pwgen --capitalize --numerals --ambiguous 12 1`
  161. useradd admin --create-home --shell /bin/bash
  162. echo "admin:$pwdAdmin" | chpasswd
  163. usermod -a -G sudo admin
  164. echo -e "\e[1;33;4;44mPassword for the user admin: $pwdAdmin\e[0m"
  165. pass=`pwgen --capitalize --numerals --ambiguous 12 1`
  166. [ -z $pwdRoot ] && pwdRoot=`pwgen --capitalize --numerals --ambiguous 12 1`
  167. echo "root:$pwdRoot" | chpasswd
  168. echo -e "\e[1;33;4;44mPassword for the user root: $pwdRoot\e[0m"
  169. # Harden SSHD
  170. sed -i -e 's/#Port 22/Port 50101/g' /etc/ssh/sshd_config
  171. sed -i -e 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/g' /etc/ssh/sshd_config
  172. # https://infosec.mozilla.org/guidelines/openssh.html
  173. # Allow admin to sudo without password
  174. echo AllowUsers admin >> /etc/ssh/sshd_config
  175. echo "admin ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/admin
  176. ## Configure network using systemd
  177. if [ -z $netAddress ]
  178. then
  179. ## Network OPTION 1 - DHCP
  180. cat >/etc/systemd/network/20-wired.network <<EOL
  181. [Match]
  182. Name=e*
  183. [Network]
  184. DHCP=ipv4
  185. IPv6PrivacyExtensions=false
  186. IPv6AcceptRA=false
  187. NTP=$netNTP
  188. EOL
  189. else
  190. ## Network OPTION 2 - static
  191. cat >/etc/systemd/network/20-wired.network <<EOL
  192. [Match]
  193. Name=$netDev
  194. [Network]
  195. Address=$netAddress
  196. Gateway=$netGateway
  197. Broadcast=$netBroadcast
  198. DNS=$netDNS1
  199. DNS=$netDNS2
  200. NTP=$netNTP
  201. EOL
  202. fi
  203. ## Setup systemd resolver
  204. apt-get install --yes systemd-resolved
  205. echo -e "\n# Disable local name resolution" >> /etc/systemd/resolved.conf
  206. echo "LLMNR=no" >> /etc/systemd/resolved.conf
  207. echo "MulticastDNS=no" >> /etc/systemd/resolved.conf
  208. systemctl enable systemd-networkd
  209. systemctl enable systemd-resolved
  210. # Limit journald logging to 1 month, 1 GB in total and split files per week
  211. mkdir -p /etc/systemd/journald.conf.d/
  212. cat >/etc/systemd/journald.conf.d/retention.conf <<EOL
  213. MaxRetentionSec=1month
  214. SystemMaxUse=1G
  215. MaxFileSec=1week
  216. EOL
  217. # Show errors in motd
  218. rm /etc/motd
  219. cat >/etc/update-motd.d/15-boot-errors<<EOL
  220. #!/bin/sh
  221. echo
  222. journalctl --boot --priority=3 --no-pager
  223. EOL
  224. chmod 755 /etc/update-motd.d/15-boot-errors
  225. # Setup keyboard layout
  226. cat >/etc/default/keyboard <<EOL
  227. XKBMODEL="pc105"
  228. XKBLAYOUT="de"
  229. XKBVARIANT="nodeadkeys"
  230. XKBOPTIONS=""
  231. BACKSPACE="guess"
  232. EOL
  233. # Leave chroot
  234. exit
  235. }
  236. bootloader(){
  237. # Install grub in the mbr
  238. if [ $partition="mbr-single" ]
  239. then
  240. chroot $mnt /bin/bash -c "grub-install $disk && update-grub"
  241. fi
  242. # Install grub in the efi partition
  243. if [ $partition="efi-crypt" ]
  244. then
  245. chroot $mnt /bin/bash -c \
  246. "grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=grub && update-grub"
  247. fi
  248. }
  249. unmount(){
  250. # Unmount if mounted
  251. ! mountpoint -q $mnt/proc || umount $mnt/proc
  252. ! mountpoint -q $mnt/sys || umount $mnt/sys
  253. ! mountpoint -q $mnt/dev/pts || umount $mnt/dev/pts
  254. ! mountpoint -q $mnt/dev || umount $mnt/dev
  255. ! mountpoint -q $mnt/root || umount $mnt/root
  256. ! mountpoint -q $mnt || umount $mnt
  257. # Delete mount-point if empty and not mounted
  258. [ -z "$(ls -A /mnt/)" ] && ! mountpoint -q $mnt && rm -R $mnt
  259. }
  260. postinstall(){
  261. ####----REBOOT into the new system, so we'll have dbus running
  262. localectl set-locale LANG=de_DE.UTF-8 # Default for LC_* variables not set.
  263. localectl set-locale LC_MESSAGES=en_US.UTF-8 # System messages.
  264. #localectl set-locale LC_RESPONSE=en_US.UTF-8 # How responses (such as Yes and No) appear
  265. update-locale
  266. timedatectl set-timezone Europe/Berlin
  267. }
  268. # Switch to functions...
  269. case $1 in
  270. grmlnetwork)
  271. echo Setup network in grml
  272. grmlnetwork
  273. ;;
  274. install)
  275. echo "Stage 1: Start installation"
  276. install
  277. ;;
  278. install2)
  279. echo "Stage 2: Start installation in chroot"
  280. install2
  281. ;;
  282. bootloader)
  283. echo "Stage 3: Install bootloader and unmount chroot"
  284. bootloader
  285. unmount
  286. echo "We're done and can reboot now"
  287. ;;
  288. postinstall)
  289. echo "Stage 4: Start post-installation in live system"
  290. postinstall
  291. ;;
  292. unmount)
  293. echo "Unmount chroot, e.g. in case installation fails"
  294. unmount
  295. ;;
  296. *)
  297. echo "Valid functions are: grmlnetwork, install, postinstall and unmount" >&2
  298. ;;
  299. esac