debi.sh 16 KB

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