bootstrap-bookworm.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. # Alternatively bootstrap from a docker container, e.g. if no recent debootstrap is available
  115. docker run -it --rm --cap-add=SYS_CHROOT --name debootstrap -v $mnt:$mnt -e mnt=$mnt \debian /bin/sh -c \
  116. "apt-get update && apt-get install --yes debootstrap && debootstrap --variant=minbase --arch=amd64 bookworm $mnt http://ftp2.de.debian.org/debian/"
  117. # Configure disk mounts
  118. # Or get UUID from blkid...
  119. [ "$partition" = "efi-crypt" ] && \
  120. echo "$disk0 /boot/efi/ vfat rw 0 0" >> $mnt/etc/fstab
  121. echo "$disk1 / ext4 rw 0 0" >> $mnt/etc/fstab
  122. echo "/swapfile none swap defaults 0 0" >> $mnt/etc/fstab
  123. # Configure sources.list
  124. cat >$mnt/etc/apt/sources.list <<EOL
  125. deb http://deb.debian.org/debian bookworm main non-free-firmware
  126. # deb-src http://deb.debian.org/debian bookworm main non-free-firmware
  127. deb http://deb.debian.org/debian-security/ bookworm-security main non-free-firmware
  128. # deb-src http://deb.debian.org/debian-security/ bookworm-security main non-free-firmware
  129. deb http://deb.debian.org/debian bookworm-updates main non-free-firmware
  130. # deb-src http://deb.debian.org/debian bookworm-updates main non-free-firmware
  131. EOL
  132. # Configure hostname
  133. echo "127.0.0.1 $hostname" >> $mnt/etc/hosts
  134. echo "$hostname" > $mnt/etc/hostname
  135. #----------
  136. # Prepare chroot
  137. mount -o bind /dev $mnt/dev
  138. mount -o bind /dev/pts $mnt/dev/pts
  139. mount -t sysfs /sys $mnt/sys
  140. mount -t proc /proc $mnt/proc
  141. cp /proc/mounts $mnt/etc/mtab
  142. cp /etc/resolv.conf $mnt/etc/resolv.conf
  143. mkdir -p $mnt/installer
  144. cp $(dirname `realpath $0`)/*.sh $mnt/installer
  145. # Run script in chroot
  146. chroot $mnt /bin/bash /installer/bootstrap-bookworm.sh install2
  147. # Install bootloader
  148. $0 bootloader
  149. }
  150. #----------
  151. # Function executed within chroot
  152. install2(){
  153. source /installer/config.sh
  154. # Install basic system
  155. apt-get update
  156. apt-get install --yes \
  157. apt-utils dialog msmtp-mta \
  158. systemd-sysv locales tzdata haveged \
  159. $kernelPkg $grubPkg \
  160. iproute2 netbase \
  161. ssh sudo molly-guard \
  162. less vim-tiny bash-completion pwgen lsof \
  163. dnsutils iputils-ping curl \
  164. $extraPackages
  165. # Upgrade and clean up
  166. apt-get upgrade --yes
  167. apt-get autoremove --yes
  168. apt-get clean --yes
  169. # Setup users and passwords
  170. [ -z $pwdAdmin ] && pwdAdmin=`pwgen --capitalize --numerals --ambiguous 12 1`
  171. useradd admin --create-home --shell /bin/bash
  172. echo "admin:$pwdAdmin" | chpasswd
  173. usermod -a -G sudo admin
  174. echo -e "\e[1;33;4;44mPassword for the user admin: $pwdAdmin\e[0m"
  175. pass=`pwgen --capitalize --numerals --ambiguous 12 1`
  176. [ -z $pwdRoot ] && pwdRoot=`pwgen --capitalize --numerals --ambiguous 12 1`
  177. echo "root:$pwdRoot" | chpasswd
  178. echo -e "\e[1;33;4;44mPassword for the user root: $pwdRoot\e[0m"
  179. # Harden SSHD
  180. sed -i -e 's/#Port 22/Port 50101/g' /etc/ssh/sshd_config
  181. sed -i -e 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/g' /etc/ssh/sshd_config
  182. # https://infosec.mozilla.org/guidelines/openssh.html
  183. # Allow admin to sudo without password
  184. echo AllowUsers admin >> /etc/ssh/sshd_config
  185. echo "admin ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/admin
  186. ## Configure network using systemd
  187. if [ -z $netAddress ]
  188. then
  189. ## Network OPTION 1 - DHCP
  190. cat >/etc/systemd/network/20-wired.network <<EOL
  191. [Match]
  192. Name=e*
  193. [Network]
  194. DHCP=ipv4
  195. IPv6PrivacyExtensions=false
  196. IPv6AcceptRA=false
  197. NTP=$netNTP
  198. EOL
  199. else
  200. ## Network OPTION 2 - static
  201. cat >/etc/systemd/network/20-wired.network <<EOL
  202. [Match]
  203. Name=$netDev
  204. [Network]
  205. Address=$netAddress
  206. Gateway=$netGateway
  207. Broadcast=$netBroadcast
  208. DNS=$netDNS1
  209. DNS=$netDNS2
  210. NTP=$netNTP
  211. EOL
  212. fi
  213. ## Setup systemd resolver
  214. apt-get install --yes systemd-resolved
  215. echo -e "\n# Disable local name resolution" >> /etc/systemd/resolved.conf
  216. echo "LLMNR=no" >> /etc/systemd/resolved.conf
  217. echo "MulticastDNS=no" >> /etc/systemd/resolved.conf
  218. systemctl enable systemd-networkd
  219. systemctl enable systemd-resolved
  220. # Limit journald logging to 1 month, 1 GB in total and split files per week
  221. mkdir -p /etc/systemd/journald.conf.d/
  222. cat >/etc/systemd/journald.conf.d/retention.conf <<EOL
  223. MaxRetentionSec=1month
  224. SystemMaxUse=1G
  225. MaxFileSec=1week
  226. EOL
  227. # Show errors in motd
  228. rm /etc/motd
  229. cat >/etc/update-motd.d/15-boot-errors<<EOL
  230. #!/bin/sh
  231. echo
  232. journalctl --boot --priority=3 --no-pager
  233. EOL
  234. chmod 755 /etc/update-motd.d/15-boot-errors
  235. # Setup keyboard layout
  236. cat >/etc/default/keyboard <<EOL
  237. XKBMODEL="pc105"
  238. XKBLAYOUT="de"
  239. XKBVARIANT="nodeadkeys"
  240. XKBOPTIONS=""
  241. BACKSPACE="guess"
  242. EOL
  243. # Leave chroot
  244. exit
  245. }
  246. bootloader(){
  247. # Install grub in the mbr
  248. echo "$grubPkg selected as bootloader"
  249. if [ $partition="mbr-single" ]
  250. then
  251. chroot $mnt /bin/bash -c "grub-install $disk && update-grub"
  252. fi
  253. # Install grub in the efi partition
  254. if [ $partition="efi-crypt" ]
  255. then
  256. chroot $mnt /bin/bash -c \
  257. "grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=grub && update-grub"
  258. fi
  259. }
  260. unmount(){
  261. # Unmount if mounted
  262. ! mountpoint -q $mnt/proc || umount $mnt/proc
  263. ! mountpoint -q $mnt/sys || umount $mnt/sys
  264. ! mountpoint -q $mnt/dev/pts || umount $mnt/dev/pts
  265. ! mountpoint -q $mnt/dev || umount $mnt/dev
  266. ! mountpoint -q $mnt/boot/efi || umount $mnt/boot/efi
  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. [ -d $mnt/boot/efi ] && [ -z "$(ls -A $mnt/boot/efi)" ] && ! mountpoint -q $mnt/boot/efi && rm -R $mnt/boot/efi
  271. }
  272. postinstall(){
  273. ####----REBOOT into the new system, so we'll have dbus running
  274. localectl set-locale LANG=de_DE.UTF-8 # Default for LC_* variables not set.
  275. localectl set-locale LC_MESSAGES=en_US.UTF-8 # System messages.
  276. #localectl set-locale LC_RESPONSE=en_US.UTF-8 # How responses (such as Yes and No) appear
  277. update-locale
  278. timedatectl set-timezone Europe/Berlin
  279. }
  280. # Switch to functions...
  281. case $1 in
  282. grmlnetwork)
  283. echo Setup network in grml
  284. grmlnetwork
  285. ;;
  286. install)
  287. echo "Stage 1: Start installation"
  288. install
  289. ;;
  290. install2)
  291. echo "Stage 2: Start installation in chroot"
  292. install2
  293. ;;
  294. bootloader)
  295. echo "Stage 3: Install bootloader and unmount chroot"
  296. bootloader
  297. unmount
  298. echo "We're done and can reboot now"
  299. ;;
  300. postinstall)
  301. echo "Stage 4: Start post-installation in live system"
  302. postinstall
  303. ;;
  304. unmount)
  305. echo "Unmount chroot, e.g. in case installation fails"
  306. unmount
  307. ;;
  308. *)
  309. echo "Valid functions are: grmlnetwork, install, postinstall and unmount" >&2
  310. ;;
  311. esac