debi.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. #!/bin/sh
  2. # shellcheck shell=dash
  3. set -euo pipefail
  4. err() {
  5. echo "Error: $1." 1>&2
  6. exit 1
  7. }
  8. command_exists() {
  9. command -v "$1" > /dev/null 2>&1
  10. }
  11. in_target=
  12. late_command() {
  13. local cmd
  14. for arg in "$@"; do
  15. cmd="$cmd $arg"
  16. done
  17. if [ -n "$cmd" ]; then
  18. [ -z "$in_target" ] && in_target='true'
  19. in_target="$in_target;$cmd"
  20. fi
  21. }
  22. in_target_backup() {
  23. late_command "if [ ! -e \"$1.backup\" ]; then cp \"$1\" \"$1.backup\"; fi"
  24. }
  25. sshd_conf() {
  26. [ -z ${backed_sshd+1} ] && in_target_backup /etc/ssh/sshd_config
  27. backed_sshd=
  28. late_command sed -Ei \""s/^#?$1 .+/$1 $2/"\" /etc/ssh/sshd_config
  29. }
  30. prompt_password() {
  31. stty -echo
  32. echo -n "Choose a password for the new user $username: " > /dev/tty
  33. read -r password < /dev/tty
  34. stty echo
  35. echo > /dev/tty
  36. }
  37. ip=
  38. netmask=
  39. gateway=
  40. dns='8.8.8.8 8.8.4.4'
  41. hostname=
  42. network_console=false
  43. suite=buster
  44. mirror_protocol=http
  45. mirror_host=deb.debian.org
  46. mirror_directory=/debian
  47. security_repository=http://security.debian.org/debian-security
  48. skip_account_setup=false
  49. username=debian
  50. password=
  51. authorized_keys_url=
  52. sudo_with_password=false
  53. timezone=UTC
  54. ntp=0.debian.pool.ntp.org
  55. skip_partitioning=false
  56. disk=
  57. force_gpt=true
  58. efi=
  59. filesystem=ext4
  60. kernel=
  61. install_recommends=true
  62. install='ca-certificates libpam-systemd'
  63. upgrade=
  64. kernel_params=
  65. bbr=false
  66. hold=false
  67. power_off=false
  68. architecture=
  69. boot_directory=/boot/
  70. firmware=false
  71. force_efi_extra_removable=false
  72. grub_timeout=5
  73. dry_run=false
  74. while [ $# -gt 0 ]; do
  75. case $1 in
  76. --cdn|--aws)
  77. mirror_protocol=https
  78. [ "$1" = '--aws' ] && mirror_host=cdn-aws.deb.debian.org
  79. security_repository=mirror
  80. ;;
  81. --china)
  82. dns='223.5.5.5 223.6.6.6'
  83. mirror_protocol=https
  84. mirror_host=mirrors.aliyun.com
  85. ntp=ntp.aliyun.com
  86. security_repository=mirror
  87. ;;
  88. --ip)
  89. ip=$2
  90. shift
  91. ;;
  92. --netmask)
  93. netmask=$2
  94. shift
  95. ;;
  96. --gateway)
  97. gateway=$2
  98. shift
  99. ;;
  100. --dns)
  101. dns=$2
  102. shift
  103. ;;
  104. --hostname)
  105. hostname=$2
  106. shift
  107. ;;
  108. --network-console)
  109. network_console=true
  110. ;;
  111. --suite)
  112. suite=$2
  113. shift
  114. ;;
  115. --mirror-protocol)
  116. mirror_protocol=$2
  117. shift
  118. ;;
  119. --https)
  120. mirror_protocol=https
  121. ;;
  122. --mirror-host)
  123. mirror_host=$2
  124. shift
  125. ;;
  126. --mirror-directory)
  127. mirror_directory=${2%/}
  128. shift
  129. ;;
  130. --security-repository)
  131. security_repository=$2
  132. shift
  133. ;;
  134. --skip-account-setup)
  135. skip_account_setup=true
  136. ;;
  137. --user|--username)
  138. username=$2
  139. shift
  140. ;;
  141. --password)
  142. password=$2
  143. shift
  144. ;;
  145. --authorized-keys-url)
  146. authorized_keys_url=$2
  147. shift
  148. ;;
  149. --sudo-with-password)
  150. sudo_with_password=true
  151. ;;
  152. --timezone)
  153. timezone=$2
  154. shift
  155. ;;
  156. --ntp)
  157. ntp=$2
  158. shift
  159. ;;
  160. --skip-partitioning)
  161. skip_partitioning=true
  162. ;;
  163. --disk)
  164. disk=$2
  165. shift
  166. ;;
  167. --no-force-gpt)
  168. force_gpt=false
  169. ;;
  170. --bios)
  171. efi=false
  172. ;;
  173. --efi)
  174. efi=true
  175. ;;
  176. --filesystem)
  177. filesystem=$2
  178. shift
  179. ;;
  180. --kernel)
  181. kernel=$2
  182. shift
  183. ;;
  184. --cloud-kernel)
  185. kernel=linux-image-cloud-amd64
  186. ;;
  187. --no-install-recommends)
  188. install_recommends=false
  189. ;;
  190. --install)
  191. install=$2
  192. shift
  193. ;;
  194. --no-upgrade)
  195. upgrade=none
  196. ;;
  197. --safe-upgrade)
  198. upgrade=safe-upgrade
  199. ;;
  200. --full-upgrade)
  201. upgrade=full-upgrade
  202. ;;
  203. --eth)
  204. kernel_params=' net.ifnames=0 biosdevname=0'
  205. ;;
  206. --bbr)
  207. bbr=true
  208. ;;
  209. --hold)
  210. hold=true
  211. ;;
  212. --power-off)
  213. power_off=true
  214. ;;
  215. --architecture)
  216. architecture=$2
  217. shift
  218. ;;
  219. --boot-partition)
  220. boot_directory=/
  221. ;;
  222. --firmware)
  223. firmware=true
  224. ;;
  225. --force-efi-extra-removable)
  226. force_efi_extra_removable=true
  227. ;;
  228. --grub-timeout)
  229. grub_timeout=$2
  230. shift
  231. ;;
  232. --dry-run)
  233. dry_run=true
  234. ;;
  235. *)
  236. err "Illegal option $1"
  237. esac
  238. shift
  239. done
  240. installer="debian-$suite"
  241. installer_directory="/boot/$installer"
  242. save_preseed='cat'
  243. if [ "$dry_run" != true ]; then
  244. [ "$(id -u)" -ne 0 ] && err 'root privilege is required'
  245. rm -rf "$installer_directory"
  246. mkdir -p "$installer_directory/initrd"
  247. cd "$installer_directory"
  248. save_preseed='tee -a initrd/preseed.cfg'
  249. fi
  250. $save_preseed << 'EOF'
  251. # Localization
  252. d-i debian-installer/language string en
  253. d-i debian-installer/country string US
  254. d-i debian-installer/locale string en_US.UTF-8
  255. d-i keyboard-configuration/xkb-keymap select us
  256. # Network configuration
  257. d-i netcfg/choose_interface select auto
  258. EOF
  259. if [ -n "$ip" ]; then
  260. echo 'd-i netcfg/disable_autoconfig boolean true' | $save_preseed
  261. echo "d-i netcfg/get_ipaddress string $ip" | $save_preseed
  262. [ -n "$netmask" ] && echo "d-i netcfg/get_netmask string $netmask" | $save_preseed
  263. [ -n "$gateway" ] && echo "d-i netcfg/get_gateway string $gateway" | $save_preseed
  264. [ -n "$dns" ] && echo "d-i netcfg/get_nameservers string $dns" | $save_preseed
  265. echo 'd-i netcfg/confirm_static boolean true' | $save_preseed
  266. fi
  267. $save_preseed << 'EOF'
  268. d-i netcfg/get_hostname string debian
  269. d-i netcfg/get_domain string
  270. EOF
  271. if [ -n "$hostname" ]; then
  272. echo "d-i netcfg/hostname string $hostname" | $save_preseed
  273. fi
  274. echo 'd-i hw-detect/load_firmware boolean true' | $save_preseed
  275. while [ -z "$password" ]; do
  276. prompt_password
  277. done
  278. if [ "$network_console" = true ]; then
  279. $save_preseed << EOF
  280. # Network console
  281. d-i anna/choose_modules string network-console
  282. d-i preseed/early_command string anna-install network-console
  283. d-i network-console/password password $password
  284. d-i network-console/password-again password $password
  285. EOF
  286. [ -n "$authorized_keys_url" ] && echo "d-i network-console/authorized_keys_url string $authorized_keys_url" | $save_preseed
  287. echo 'd-i network-console/start select Continue' | $save_preseed
  288. fi
  289. $save_preseed << EOF
  290. # Mirror settings
  291. d-i mirror/country string manual
  292. d-i mirror/protocol string $mirror_protocol
  293. d-i mirror/$mirror_protocol/hostname string $mirror_host
  294. d-i mirror/$mirror_protocol/directory string $mirror_directory
  295. d-i mirror/$mirror_protocol/proxy string
  296. d-i mirror/suite string $suite
  297. d-i mirror/udeb/suite string $suite
  298. EOF
  299. if [ "$skip_account_setup" != true ]; then
  300. password_hash=
  301. if command_exists mkpasswd; then
  302. password_hash=$(mkpasswd -m sha-512 "$password")
  303. elif command_exists busybox && busybox mkpasswd --help >/dev/null 2>&1; then
  304. password_hash=$(busybox mkpasswd -m sha512 "$password")
  305. elif command_exists python3; then
  306. password_hash=$(python3 -c 'import crypt, sys; print(crypt.crypt(sys.argv[1], crypt.mksalt(crypt.METHOD_SHA512)))' "$password")
  307. elif command_exists python; then
  308. password_hash=$(python -c 'import crypt, sys; print(crypt.crypt(sys.argv[1], crypt.mksalt(crypt.METHOD_SHA512)))' "$password" 2> /dev/null) || password_hash=
  309. fi
  310. $save_preseed << 'EOF'
  311. # Account setup
  312. EOF
  313. if [ -n "$authorized_keys_url" ]; then
  314. sshd_conf PasswordAuthentication no
  315. fi
  316. if [ "$username" = root ]; then
  317. if [ -z "$authorized_keys_url" ]; then
  318. sshd_conf PermitRootLogin yes
  319. else
  320. late_command "mkdir -m 0700 -p ~root/.ssh && busybox wget -O - \"$authorized_keys_url\" >> ~root/.ssh/authorized_keys"
  321. fi
  322. $save_preseed << 'EOF'
  323. d-i passwd/root-login boolean true
  324. d-i passwd/make-user boolean false
  325. EOF
  326. if [ -z "$password_hash" ]; then
  327. $save_preseed << EOF
  328. d-i passwd/root-password password $password
  329. d-i passwd/root-password-again password $password
  330. EOF
  331. else
  332. echo "d-i passwd/root-password-crypted password $password_hash" | $save_preseed
  333. fi
  334. else
  335. sshd_conf PermitRootLogin no
  336. if [ -n "$authorized_keys_url" ]; then
  337. late_command "sudo -u $username mkdir -m 0700 -p ~$username/.ssh && busybox wget -O - \"$authorized_keys_url\" | sudo -u $username tee -a ~$username/.ssh/authorized_keys"
  338. fi
  339. if [ "$sudo_with_password" = false ]; then
  340. late_command "echo \"$username ALL=(ALL:ALL) NOPASSWD:ALL\" > \"/etc/sudoers.d/90-user-$username\""
  341. fi
  342. $save_preseed << EOF
  343. d-i passwd/root-login boolean false
  344. d-i passwd/make-user boolean true
  345. d-i passwd/user-fullname string
  346. d-i passwd/username string $username
  347. EOF
  348. if [ -z "$password_hash" ]; then
  349. $save_preseed << EOF
  350. d-i passwd/user-password password $password
  351. d-i passwd/user-password-again password $password
  352. EOF
  353. else
  354. echo "d-i passwd/user-password-crypted password $password_hash" | $save_preseed
  355. fi
  356. fi
  357. fi
  358. $save_preseed << EOF
  359. # Clock and time zone setup
  360. d-i time/zone string $timezone
  361. d-i clock-setup/utc boolean true
  362. d-i clock-setup/ntp boolean true
  363. d-i clock-setup/ntp-server string $ntp
  364. EOF
  365. if [ "$skip_partitioning" != true ]; then
  366. $save_preseed << 'EOF'
  367. # Partitioning
  368. d-i partman-auto/method string regular
  369. EOF
  370. [ -n "$disk" ] && echo "d-i partman-auto/disk string $disk" | $save_preseed
  371. [ "$force_gpt" = true ] && echo 'd-i partman-partitioning/default_label string gpt' | $save_preseed
  372. echo "d-i partman/default_filesystem string $filesystem" | $save_preseed
  373. if [ -z "$efi" ]; then
  374. efi=false
  375. [ -d /sys/firmware/efi ] && efi=true
  376. fi
  377. $save_preseed << 'EOF'
  378. d-i partman-auto/expert_recipe string \
  379. naive :: \
  380. EOF
  381. if [ "$efi" = true ]; then
  382. $save_preseed << 'EOF'
  383. 538 538 1075 free \
  384. $iflabel{ gpt } \
  385. $reusemethod{ } \
  386. method{ efi } \
  387. format{ } \
  388. . \
  389. EOF
  390. else
  391. $save_preseed << 'EOF'
  392. 1 1 1 free \
  393. $iflabel{ gpt } \
  394. $reusemethod{ } \
  395. method{ biosgrub } \
  396. . \
  397. EOF
  398. fi
  399. $save_preseed << 'EOF'
  400. 2149 2150 -1 $default_filesystem \
  401. method{ format } \
  402. format{ } \
  403. use_filesystem{ } \
  404. $default_filesystem{ } \
  405. mountpoint{ / } \
  406. .
  407. EOF
  408. echo "d-i partman-auto/choose_recipe select naive" | $save_preseed
  409. $save_preseed << 'EOF'
  410. d-i partman-basicfilesystems/no_swap boolean false
  411. d-i partman/choose_partition select finish
  412. d-i partman/confirm boolean true
  413. EOF
  414. fi
  415. $save_preseed << 'EOF'
  416. # Base system installation
  417. EOF
  418. [ "$install_recommends" = false ] && echo "d-i base-installer/install-recommends boolean $install_recommends" | $save_preseed
  419. [ -n "$kernel" ] && echo "d-i base-installer/kernel/image string $kernel" | $save_preseed
  420. [ "$security_repository" = mirror ] && security_repository=$mirror_protocol://$mirror_host${mirror_directory%/*}/debian-security
  421. $save_preseed << EOF
  422. # Apt setup
  423. d-i apt-setup/services-select multiselect updates, backports
  424. d-i apt-setup/local0/repository string $security_repository $suite/updates main
  425. d-i apt-setup/local0/source boolean true
  426. EOF
  427. $save_preseed << 'EOF'
  428. # Package selection
  429. tasksel tasksel/first multiselect ssh-server
  430. EOF
  431. [ -n "$install" ] && echo "d-i pkgsel/include string $install" | $save_preseed
  432. [ -n "$upgrade" ] && echo "d-i pkgsel/upgrade select $upgrade" | $save_preseed
  433. $save_preseed << 'EOF'
  434. popularity-contest popularity-contest/participate boolean false
  435. # Boot loader installation
  436. d-i grub-installer/bootdev string default
  437. EOF
  438. [ "$force_efi_extra_removable" = true ] && echo "d-i grub-installer/force-efi-extra-removable boolean true" | $save_preseed
  439. [ -n "$kernel_params" ] && echo "d-i debian-installer/add-kernel-opts string$kernel_params" | $save_preseed
  440. $save_preseed << 'EOF'
  441. # Finishing up the installation
  442. EOF
  443. [ "$hold" != true ] && echo 'd-i finish-install/reboot_in_progress note' | $save_preseed
  444. [ "$bbr" = true ] && late_command '{ echo "net.core.default_qdisc=fq"; echo "net.ipv4.tcp_congestion_control=bbr"; } > /etc/sysctl.d/bbr.conf'
  445. [ -n "$in_target" ] && echo "d-i preseed/late_command string in-target dash -c '$in_target'" | $save_preseed
  446. [ "$power_off" = true ] && echo 'd-i debian-installer/exit/poweroff boolean true' | $save_preseed
  447. save_grub_cfg='cat'
  448. if [ "$dry_run" != true ]; then
  449. if [ -z "$architecture" ]; then
  450. architecture=amd64
  451. command_exists dpkg && architecture=$(dpkg --print-architecture)
  452. fi
  453. base_url="$mirror_protocol://$mirror_host$mirror_directory/dists/$suite/main/installer-$architecture/current/images/netboot/debian-installer/$architecture"
  454. firmware_url="https://cdimage.debian.org/cdimage/unofficial/non-free/firmware/$suite/current/firmware.cpio.gz"
  455. if command_exists wget; then
  456. wget "$base_url/linux" "$base_url/initrd.gz"
  457. [ "$firmware" = true ] && wget "$firmware_url"
  458. elif command_exists curl; then
  459. curl -f -L -O "$base_url/linux" -O "$base_url/initrd.gz"
  460. [ "$firmware" = true ] && curl -f -L -O "$firmware_url"
  461. elif command_exists busybox && busybox wget --help >/dev/null 2>&1; then
  462. busybox wget "$base_url/linux" "$base_url/initrd.gz"
  463. [ "$firmware" = true ] && busybox wget "$firmware_url"
  464. else
  465. err 'Could not find "wget" or "curl" or "busybox wget" command to download files'
  466. fi
  467. cd initrd
  468. gzip -d -c ../initrd.gz | cpio -i -d --no-absolute-filenames
  469. [ "$firmware" = true ] && gzip -d -c ../firmware.cpio.gz | cpio -i -d --no-absolute-filenames
  470. find . | cpio -o -H newc | gzip -9 > ../initrd.gz
  471. cd ..
  472. mkdir -p /etc/default/grub.d
  473. tee /etc/default/grub.d/zz-debi.cfg 1>&2 << EOF
  474. GRUB_DEFAULT=debi
  475. GRUB_TIMEOUT=$grub_timeout
  476. GRUB_TIMEOUT_STYLE=menu
  477. EOF
  478. if command_exists update-grub; then
  479. grub_cfg=/boot/grub/grub.cfg
  480. update-grub
  481. elif command_exists grub2-mkconfig; then
  482. tmp=$(mktemp)
  483. grep -vF zz_debi /etc/default/grub > "$tmp"
  484. cat "$tmp" > /etc/default/grub
  485. rm "$tmp"
  486. # shellcheck disable=SC2016
  487. echo 'zz_debi=/etc/default/grub.d/zz-debi.cfg; if [ -f "$zz_debi" ]; then . "$zz_debi"; fi' >> /etc/default/grub
  488. grub_cfg=/boot/grub2/grub.cfg
  489. grub2-mkconfig -o "$grub_cfg"
  490. else
  491. err 'Could not find "update-grub" or "grub2-mkconfig" command'
  492. fi
  493. save_grub_cfg="tee -a $grub_cfg"
  494. fi
  495. installer_directory="$boot_directory$installer"
  496. # shellcheck disable=SC2034
  497. mem=$(grep ^MemTotal: /proc/meminfo | { read -r x y z; echo "$y"; })
  498. [ $((mem / 1024)) -lt 483 ] && kernel_params="$kernel_params lowmem/low="
  499. $save_grub_cfg 1>&2 << EOF
  500. menuentry 'Debian Installer' --id debi {
  501. insmod part_msdos
  502. insmod part_gpt
  503. insmod ext2
  504. linux $installer_directory/linux$kernel_params
  505. initrd $installer_directory/initrd.gz
  506. }
  507. EOF