debi.sh 16 KB

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