debi.sh 16 KB

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