debi.sh 16 KB

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