bootstrap-bookworm.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. # 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"
  31. disk1=$disk"1" # "p1" for nbd or nvme mounts
  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"
  35. netBroadcast="203.0.113.255"
  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
  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. fdisk -l $disk
  72. # Format disks -- pkg: e2fsprogs dosfstools and to file system check
  73. mkfs.ext4 $disk1 && e2fsck $disk1
  74. # Prepare mount points and mount
  75. mkdir -p $mnt
  76. mount $disk1 $mnt
  77. fi
  78. if [ "$partition" = "efi-crypt" ]
  79. then
  80. #----------
  81. # Prepare disks with following layout
  82. # - 301 MB partition for EFI --> p1
  83. # - 50 GB root partition for the OS (includes /boot) --> p2
  84. # - Remaining disk left to create a luks container --> p3
  85. parted $disk --script \
  86. mklabel gpt \
  87. mkpart EFI_system_partition fat32 1MiB 301MiB \
  88. set 1 esp on \
  89. set 1 boot on \
  90. align-check optimal 1 \
  91. mkpart Linux_system_parition ext4 301MiB 50GiB \
  92. align-check optimal 2 \
  93. mkpart Data_partion 50GiB 100% \
  94. align-check optimal 3 \
  95. unit MiB \
  96. print
  97. # Format disks -- pkg: e2fsprogs dosfstools and to file system check
  98. mkfs.fat -F 32 -n EFIBOOT $disk0 && fsck $disk0
  99. mkfs.ext4 $disk1 && fsck $disk1
  100. # Prepare mount points and mount
  101. mkdir -p $mnt $mnt"/boot/efi"
  102. mount $disk1 $mnt
  103. mount $disk0 $mnt"/boot/efi"
  104. fi
  105. # Create swapfile
  106. swapfile=$mnt/swapfile
  107. dd if=/dev/zero of=$swapfile bs=1M count=1024 status=progress # create 1GB file
  108. chmod 600 $swapfile #restric permissions
  109. mkswap $swapfile #format file
  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. #----------
  115. # Configuration
  116. # Configure disk mounts
  117. # Or get UUID from blkid...
  118. cat >$mnt/etc/fstab <<EOL
  119. $disk1 / ext4 rw 0 0
  120. /swapfile none swap defaults 0 0
  121. EOL
  122. # Configure sources.list
  123. cat >$mnt/etc/apt/sources.list <<EOL
  124. deb http://deb.debian.org/debian bookworm main non-free-firmware
  125. # deb-src http://deb.debian.org/debian bookworm main non-free-firmware
  126. deb http://deb.debian.org/debian-security/ bookworm-security main non-free-firmware
  127. # deb-src http://deb.debian.org/debian-security/ bookworm-security main non-free-firmware
  128. deb http://deb.debian.org/debian bookworm-updates main non-free-firmware
  129. # deb-src http://deb.debian.org/debian bookworm-updates main non-free-firmware
  130. EOL
  131. # Configure hostname
  132. echo "127.0.0.1 $hostname" >> $mnt/etc/hosts
  133. echo "$hostname" > $mnt/etc/hostname
  134. #----------
  135. # Prepare chroot
  136. mount -o bind /dev $mnt/dev
  137. mount -o bind /dev/pts $mnt/dev/pts
  138. mount -t sysfs /sys $mnt/sys
  139. mount -t proc /proc $mnt/proc
  140. cp /proc/mounts $mnt/etc/mtab
  141. cp /etc/resolv.conf $mnt/etc/resolv.conf
  142. mkdir -p $mnt/installer
  143. cp $(dirname `realpath $0`)/*.sh $mnt/installer
  144. # Run script in chroot
  145. chroot $mnt /bin/bash /installer/bootstrap-bullseye.sh install2
  146. # Install bootloader
  147. $0 bootloader
  148. }
  149. #----------
  150. # Function executed within chroot
  151. install2(){
  152. source /installer/config.sh
  153. # Install basic system
  154. apt-get update
  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 in the mbr
  247. if [ $partition="mbr-single" ]
  248. then
  249. chroot $mnt /bin/bash -c "grub-install $disk && update-grub"
  250. fi
  251. # Install grub in the efi partition
  252. if [ $partition="efi-crypt" ]
  253. then
  254. chroot $mnt /bin/bash -c \
  255. "grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=grub && update-grub"
  256. fi
  257. }
  258. unmount(){
  259. # Unmount if mounted
  260. ! mountpoint -q $mnt/proc || umount $mnt/proc
  261. ! mountpoint -q $mnt/sys || umount $mnt/sys
  262. ! mountpoint -q $mnt/dev/pts || umount $mnt/dev/pts
  263. ! mountpoint -q $mnt/dev || umount $mnt/dev
  264. ! mountpoint -q $mnt/root || umount $mnt/root
  265. ! mountpoint -q $mnt || umount $mnt
  266. # Delete mount-point if empty and not mounted
  267. [ -z "$(ls -A /mnt/)" ] && ! mountpoint -q $mnt && rm -R $mnt
  268. }
  269. postinstall(){
  270. ####----REBOOT into the new system, so we'll have dbus running
  271. localectl set-locale LANG=de_DE.UTF-8 # Default for LC_* variables not set.
  272. localectl set-locale LC_MESSAGES=en_US.UTF-8 # System messages.
  273. #localectl set-locale LC_RESPONSE=en_US.UTF-8 # How responses (such as Yes and No) appear
  274. update-locale
  275. timedatectl set-timezone Europe/Berlin
  276. }
  277. # Switch to functions...
  278. case $1 in
  279. grmlnetwork)
  280. echo Setup network in grml
  281. grmlnetwork
  282. ;;
  283. install)
  284. echo "Stage 1: Start installation"
  285. install
  286. ;;
  287. install2)
  288. echo "Stage 2: Start installation in chroot"
  289. install2
  290. ;;
  291. bootloader)
  292. echo "Stage 3: Install bootloader and unmount chroot"
  293. bootloader
  294. unmount
  295. echo "We're done and can reboot now"
  296. ;;
  297. postinstall)
  298. echo "Stage 4: Start post-installation in live system"
  299. postinstall
  300. ;;
  301. unmount)
  302. echo "Unmount chroot, e.g. in case installation fails"
  303. unmount
  304. ;;
  305. *)
  306. echo "Valid functions are: grmlnetwork, install, postinstall and unmount" >&2
  307. ;;
  308. esac