bootstrap-bookworm.sh 11 KB

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