bootstrap-bookworm.sh 11 KB

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