debi.sh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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. prompt_password
  254. elif [ "$network_console" = true ] && [ -z "$authorized_keys_url" ]; then
  255. prompt_password "Choose a password for the installer user of the SSH network console: "
  256. fi
  257. save_preseed='cat'
  258. if [ "$dry_run" != true ]; then
  259. [ "$(id -u)" -ne 0 ] && err 'root privilege is required'
  260. rm -rf "$installer_directory"
  261. mkdir -p "$installer_directory/initrd"
  262. cd "$installer_directory"
  263. save_preseed='tee -a initrd/preseed.cfg'
  264. fi
  265. $save_preseed << 'EOF'
  266. # Localization
  267. d-i debian-installer/language string en
  268. d-i debian-installer/country string US
  269. d-i debian-installer/locale string en_US.UTF-8
  270. d-i keyboard-configuration/xkb-keymap select us
  271. # Network configuration
  272. d-i netcfg/choose_interface select auto
  273. EOF
  274. if [ -n "$ip" ]; then
  275. echo 'd-i netcfg/disable_autoconfig boolean true' | $save_preseed
  276. echo "d-i netcfg/get_ipaddress string $ip" | $save_preseed
  277. [ -n "$netmask" ] && echo "d-i netcfg/get_netmask string $netmask" | $save_preseed
  278. [ -n "$gateway" ] && echo "d-i netcfg/get_gateway string $gateway" | $save_preseed
  279. [ -n "$dns" ] && echo "d-i netcfg/get_nameservers string $dns" | $save_preseed
  280. echo 'd-i netcfg/confirm_static boolean true' | $save_preseed
  281. fi
  282. if [ -n "$hostname" ]; then
  283. echo "d-i netcfg/hostname string $hostname" | $save_preseed
  284. hostname=debian
  285. domain=
  286. else
  287. hostname=$(cat /proc/sys/kernel/hostname)
  288. domain=$(cat /proc/sys/kernel/domainname)
  289. if [ "$domain" = '(none)' ]; then
  290. domain=
  291. else
  292. domain=" $domain"
  293. fi
  294. fi
  295. $save_preseed << EOF
  296. d-i netcfg/get_hostname string $hostname
  297. d-i netcfg/get_domain string$domain
  298. EOF
  299. echo 'd-i hw-detect/load_firmware boolean true' | $save_preseed
  300. if [ "$network_console" = true ]; then
  301. $save_preseed << EOF
  302. # Network console
  303. d-i anna/choose_modules string network-console
  304. d-i preseed/early_command string anna-install network-console
  305. EOF
  306. if [ -n "$authorized_keys_url" ]; then
  307. echo "d-i network-console/authorized_keys_url string $authorized_keys_url" | $save_preseed
  308. else
  309. $save_preseed << EOF
  310. d-i network-console/password password $password
  311. d-i network-console/password-again password $password
  312. EOF
  313. fi
  314. echo 'd-i network-console/start select Continue' | $save_preseed
  315. fi
  316. $save_preseed << EOF
  317. # Mirror settings
  318. d-i mirror/country string manual
  319. d-i mirror/protocol string $mirror_protocol
  320. d-i mirror/$mirror_protocol/hostname string $mirror_host
  321. d-i mirror/$mirror_protocol/directory string $mirror_directory
  322. d-i mirror/$mirror_protocol/proxy string
  323. d-i mirror/suite string $suite
  324. d-i mirror/udeb/suite string $suite
  325. EOF
  326. if [ "$skip_account_setup" != true ]; then
  327. password_hash=
  328. if command_exists mkpasswd; then
  329. password_hash=$(mkpasswd -m sha-512 "$password")
  330. elif command_exists busybox && busybox mkpasswd --help >/dev/null 2>&1; then
  331. password_hash=$(busybox mkpasswd -m sha512 "$password")
  332. elif command_exists python3; then
  333. password_hash=$(python3 -c 'import crypt, sys; print(crypt.crypt(sys.argv[1], crypt.mksalt(crypt.METHOD_SHA512)))' "$password")
  334. elif command_exists python; then
  335. password_hash=$(python -c 'import crypt, sys; print(crypt.crypt(sys.argv[1], crypt.mksalt(crypt.METHOD_SHA512)))' "$password" 2> /dev/null) || password_hash=
  336. fi
  337. $save_preseed << 'EOF'
  338. # Account setup
  339. EOF
  340. if [ -n "$authorized_keys_url" ]; then
  341. sshd_conf PasswordAuthentication no
  342. fi
  343. if [ "$username" = root ]; then
  344. if [ -z "$authorized_keys_url" ]; then
  345. sshd_conf PermitRootLogin yes
  346. else
  347. late_command "mkdir -m 0700 -p ~root/.ssh && busybox wget -O - \"$authorized_keys_url\" >> ~root/.ssh/authorized_keys"
  348. fi
  349. $save_preseed << 'EOF'
  350. d-i passwd/root-login boolean true
  351. d-i passwd/make-user boolean false
  352. EOF
  353. if [ -z "$password_hash" ]; then
  354. $save_preseed << EOF
  355. d-i passwd/root-password password $password
  356. d-i passwd/root-password-again password $password
  357. EOF
  358. else
  359. echo "d-i passwd/root-password-crypted password $password_hash" | $save_preseed
  360. fi
  361. else
  362. sshd_conf PermitRootLogin no
  363. if [ -n "$authorized_keys_url" ]; then
  364. 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"
  365. fi
  366. if [ "$sudo_with_password" = false ]; then
  367. late_command "echo \"$username ALL=(ALL:ALL) NOPASSWD:ALL\" > \"/etc/sudoers.d/90-user-$username\""
  368. fi
  369. $save_preseed << EOF
  370. d-i passwd/root-login boolean false
  371. d-i passwd/make-user boolean true
  372. d-i passwd/user-fullname string
  373. d-i passwd/username string $username
  374. EOF
  375. if [ -z "$password_hash" ]; then
  376. $save_preseed << EOF
  377. d-i passwd/user-password password $password
  378. d-i passwd/user-password-again password $password
  379. EOF
  380. else
  381. echo "d-i passwd/user-password-crypted password $password_hash" | $save_preseed
  382. fi
  383. fi
  384. fi
  385. $save_preseed << EOF
  386. # Clock and time zone setup
  387. d-i time/zone string $timezone
  388. d-i clock-setup/utc boolean true
  389. d-i clock-setup/ntp boolean true
  390. d-i clock-setup/ntp-server string $ntp
  391. EOF
  392. if [ "$skip_partitioning" != true ]; then
  393. $save_preseed << 'EOF'
  394. # Partitioning
  395. d-i partman-auto/method string regular
  396. EOF
  397. [ -n "$disk" ] && echo "d-i partman-auto/disk string $disk" | $save_preseed
  398. [ "$force_gpt" = true ] && echo 'd-i partman-partitioning/default_label string gpt' | $save_preseed
  399. echo "d-i partman/default_filesystem string $filesystem" | $save_preseed
  400. if [ -z "$efi" ]; then
  401. efi=false
  402. [ -d /sys/firmware/efi ] && efi=true
  403. fi
  404. $save_preseed << 'EOF'
  405. d-i partman-auto/expert_recipe string \
  406. naive :: \
  407. EOF
  408. if [ "$efi" = true ]; then
  409. $save_preseed << 'EOF'
  410. 538 538 1075 free \
  411. $iflabel{ gpt } \
  412. $reusemethod{ } \
  413. method{ efi } \
  414. format{ } \
  415. . \
  416. EOF
  417. else
  418. $save_preseed << 'EOF'
  419. 1 1 1 free \
  420. $iflabel{ gpt } \
  421. $reusemethod{ } \
  422. method{ biosgrub } \
  423. . \
  424. EOF
  425. fi
  426. $save_preseed << 'EOF'
  427. 2149 2150 -1 $default_filesystem \
  428. method{ format } \
  429. format{ } \
  430. use_filesystem{ } \
  431. $default_filesystem{ } \
  432. mountpoint{ / } \
  433. .
  434. EOF
  435. echo "d-i partman-auto/choose_recipe select naive" | $save_preseed
  436. $save_preseed << 'EOF'
  437. d-i partman-basicfilesystems/no_swap boolean false
  438. d-i partman/choose_partition select finish
  439. d-i partman/confirm boolean true
  440. EOF
  441. fi
  442. $save_preseed << 'EOF'
  443. # Base system installation
  444. EOF
  445. [ "$install_recommends" = false ] && echo "d-i base-installer/install-recommends boolean $install_recommends" | $save_preseed
  446. [ -n "$kernel" ] && echo "d-i base-installer/kernel/image string $kernel" | $save_preseed
  447. [ "$security_repository" = mirror ] && security_repository=$mirror_protocol://$mirror_host${mirror_directory%/*}/debian-security
  448. $save_preseed << EOF
  449. # Apt setup
  450. d-i apt-setup/services-select multiselect updates, backports
  451. d-i apt-setup/local0/repository string $security_repository $suite/updates main
  452. d-i apt-setup/local0/source boolean true
  453. EOF
  454. $save_preseed << 'EOF'
  455. # Package selection
  456. tasksel tasksel/first multiselect ssh-server
  457. EOF
  458. [ -n "$install" ] && echo "d-i pkgsel/include string $install" | $save_preseed
  459. [ -n "$upgrade" ] && echo "d-i pkgsel/upgrade select $upgrade" | $save_preseed
  460. $save_preseed << 'EOF'
  461. popularity-contest popularity-contest/participate boolean false
  462. # Boot loader installation
  463. d-i grub-installer/bootdev string default
  464. EOF
  465. [ "$force_efi_extra_removable" = true ] && echo "d-i grub-installer/force-efi-extra-removable boolean true" | $save_preseed
  466. [ -n "$kernel_params" ] && echo "d-i debian-installer/add-kernel-opts string$kernel_params" | $save_preseed
  467. $save_preseed << 'EOF'
  468. # Finishing up the installation
  469. EOF
  470. [ "$hold" != true ] && echo 'd-i finish-install/reboot_in_progress note' | $save_preseed
  471. [ "$bbr" = true ] && late_command '{ echo "net.core.default_qdisc=fq"; echo "net.ipv4.tcp_congestion_control=bbr"; } > /etc/sysctl.d/bbr.conf'
  472. [ -n "$in_target" ] && echo "d-i preseed/late_command string in-target sh -c '$in_target'" | $save_preseed
  473. [ "$power_off" = true ] && echo 'd-i debian-installer/exit/poweroff boolean true' | $save_preseed
  474. save_grub_cfg='cat'
  475. if [ "$dry_run" != true ]; then
  476. if [ -z "$architecture" ]; then
  477. architecture=amd64
  478. command_exists dpkg && architecture=$(dpkg --print-architecture)
  479. fi
  480. base_url="$mirror_protocol://$mirror_host$mirror_directory/dists/$suite/main/installer-$architecture/current/images/netboot/debian-installer/$architecture"
  481. firmware_url="https://cdimage.debian.org/cdimage/unofficial/non-free/firmware/$suite/current/firmware.cpio.gz"
  482. if command_exists wget; then
  483. wget "$base_url/linux" "$base_url/initrd.gz"
  484. [ "$firmware" = true ] && wget "$firmware_url"
  485. elif command_exists curl; then
  486. curl -f -L -O "$base_url/linux" -O "$base_url/initrd.gz"
  487. [ "$firmware" = true ] && curl -f -L -O "$firmware_url"
  488. elif command_exists busybox && busybox wget --help >/dev/null 2>&1; then
  489. busybox wget "$base_url/linux" "$base_url/initrd.gz"
  490. [ "$firmware" = true ] && busybox wget "$firmware_url"
  491. else
  492. err 'Could not find "wget" or "curl" or "busybox wget" command to download files'
  493. fi
  494. cd initrd
  495. gzip -d -c ../initrd.gz | cpio -i -d --no-absolute-filenames
  496. [ "$firmware" = true ] && gzip -d -c ../firmware.cpio.gz | cpio -i -d --no-absolute-filenames
  497. find . | cpio -o -H newc | gzip -9 > ../initrd.gz
  498. cd ..
  499. mkdir -p /etc/default/grub.d
  500. tee /etc/default/grub.d/zz-debi.cfg 1>&2 << EOF
  501. GRUB_DEFAULT=debi
  502. GRUB_TIMEOUT=$grub_timeout
  503. GRUB_TIMEOUT_STYLE=menu
  504. EOF
  505. if command_exists update-grub; then
  506. grub_cfg=/boot/grub/grub.cfg
  507. update-grub
  508. elif command_exists grub2-mkconfig; then
  509. tmp=$(mktemp)
  510. grep -vF zz_debi /etc/default/grub > "$tmp"
  511. cat "$tmp" > /etc/default/grub
  512. rm "$tmp"
  513. # shellcheck disable=SC2016
  514. echo 'zz_debi=/etc/default/grub.d/zz-debi.cfg; if [ -f "$zz_debi" ]; then . "$zz_debi"; fi' >> /etc/default/grub
  515. grub_cfg=/boot/grub2/grub.cfg
  516. grub2-mkconfig -o "$grub_cfg"
  517. else
  518. err 'Could not find "update-grub" or "grub2-mkconfig" command'
  519. fi
  520. save_grub_cfg="tee -a $grub_cfg"
  521. fi
  522. installer_directory="$boot_directory$installer"
  523. # shellcheck disable=SC2034
  524. mem=$(grep ^MemTotal: /proc/meminfo | { read -r x y z; echo "$y"; })
  525. [ $((mem / 1024)) -lt 483 ] && kernel_params="$kernel_params lowmem/low="
  526. $save_grub_cfg 1>&2 << EOF
  527. menuentry 'Debian Installer' --id debi {
  528. insmod part_msdos
  529. insmod part_gpt
  530. insmod ext2
  531. linux $installer_directory/linux$kernel_params
  532. initrd $installer_directory/initrd.gz
  533. }
  534. EOF