| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708 |
- #!/bin/sh
- # shellcheck shell=dash
- set -eu
- err() {
- echo "Error: $1." 1>&2
- exit 1
- }
- command_exists() {
- command -v "$1" > /dev/null 2>&1
- }
- # Sets variable:
- late_command=
- in_target() {
- local command=
- for argument in "$@"; do
- command="$command $argument"
- done
- if [ -n "$command" ]; then
- [ -z "$late_command" ] && late_command='true'
- late_command="$late_command;$command"
- fi
- }
- in_target_backup() {
- in_target "if [ ! -e \"$1.backup\" ]; then cp \"$1\" \"$1.backup\"; fi"
- }
- configure_sshd() {
- # !isset($sshd_config_backup)
- [ -z ${sshd_config_backup+1s} ] && in_target_backup /etc/ssh/sshd_config
- sshd_config_backup=
- in_target sed -Ei \""s/^#?$1 .+/$1 $2/"\" /etc/ssh/sshd_config
- }
- prompt_password() {
- local prompt=
- if [ $# -gt 0 ]; then
- prompt=$1
- elif [ "$username" = root ]; then
- prompt="Choose a password for the root user: "
- else
- prompt="Choose a password for user $username: "
- fi
- stty -echo
- trap 'stty echo' EXIT
- while [ -z "$password" ]; do
- echo -n "$prompt" > /dev/tty
- read -r password < /dev/tty
- echo > /dev/tty
- done
- stty echo
- trap - EXIT
- }
- download() {
- if command_exists wget; then
- wget -O "$2" "$1"
- elif command_exists curl; then
- curl -fL "$1" -o "$2"
- elif command_exists busybox && busybox wget --help > /dev/null 2>&1; then
- busybox wget -O "$2" "$1"
- else
- err 'Cannot find "wget", "curl" or "busybox wget" to download files'
- fi
- }
- ip=
- netmask=
- gateway=
- dns='8.8.8.8 8.8.4.4'
- hostname=
- network_console=false
- suite=buster
- daily_d_i=false
- mirror_protocol=http
- mirror_host=deb.debian.org
- mirror_directory=/debian
- security_repository=http://security.debian.org/debian-security
- account_setup=true
- username=debian
- password=
- authorized_keys_url=
- sudo_with_password=false
- timezone=UTC
- ntp=0.debian.pool.ntp.org
- disk_partitioning=true
- disk=
- force_gpt=true
- efi=
- filesystem=ext4
- kernel=
- cloud_kernel=false
- bpo_kernel=false
- install_recommends=true
- install='ca-certificates libpam-systemd'
- upgrade=
- kernel_params=
- bbr=false
- hold=false
- power_off=false
- architecture=
- boot_directory=
- firmware=false
- force_efi_extra_removable=true
- grub_timeout=5
- dry_run=false
- while [ $# -gt 0 ]; do
- case $1 in
- --cdn|--aws)
- mirror_protocol=https
- [ "$1" = '--aws' ] && mirror_host=cdn-aws.deb.debian.org
- security_repository=mirror
- ;;
- --china)
- dns='223.5.5.5 223.6.6.6'
- mirror_protocol=https
- mirror_host=mirrors.aliyun.com
- ntp=ntp.aliyun.com
- security_repository=mirror
- ;;
- --ip)
- ip=$2
- shift
- ;;
- --netmask)
- netmask=$2
- shift
- ;;
- --gateway)
- gateway=$2
- shift
- ;;
- --dns)
- dns=$2
- shift
- ;;
- --hostname)
- hostname=$2
- shift
- ;;
- --network-console)
- network_console=true
- ;;
- --version)
- case $2 in
- 9|stretch)
- suite=stretch
- ;;
- 10|buster)
- suite=buster
- ;;
- 11|bullseye)
- suite=bullseye
- daily_d_i=true
- ;;
- *)
- err "Unsupported version: $2"
- esac
- shift
- ;;
- --suite)
- suite=$2
- case $2 in
- bullseye|testing|sid|unstable)
- daily_d_i=true
- esac
- shift
- ;;
- --release-d-i)
- daily_d_i=false
- ;;
- --daily-d-i)
- daily_d_i=true
- ;;
- --mirror-protocol)
- mirror_protocol=$2
- shift
- ;;
- --https)
- mirror_protocol=https
- ;;
- --mirror-host)
- mirror_host=$2
- shift
- ;;
- --mirror-directory)
- mirror_directory=${2%/}
- shift
- ;;
- --security-repository)
- security_repository=$2
- shift
- ;;
- --no-user|--no-account-setup)
- account_setup=false
- ;;
- --user|--username)
- username=$2
- shift
- ;;
- --password)
- password=$2
- shift
- ;;
- --authorized-keys-url)
- authorized_keys_url=$2
- shift
- ;;
- --sudo-with-password)
- sudo_with_password=true
- ;;
- --timezone)
- timezone=$2
- shift
- ;;
- --ntp)
- ntp=$2
- shift
- ;;
- --no-part|--no-disk-partitioning)
- disk_partitioning=false
- ;;
- --disk)
- disk=$2
- shift
- ;;
- --no-force-gpt)
- force_gpt=false
- ;;
- --bios)
- efi=false
- ;;
- --efi)
- efi=true
- ;;
- --filesystem)
- filesystem=$2
- shift
- ;;
- --kernel)
- kernel=$2
- shift
- ;;
- --cloud-kernel)
- cloud_kernel=true
- ;;
- --bpo-kernel)
- bpo_kernel=true
- ;;
- --no-install-recommends)
- install_recommends=false
- ;;
- --install)
- install=$2
- shift
- ;;
- --no-upgrade)
- upgrade=none
- ;;
- --safe-upgrade)
- upgrade=safe-upgrade
- ;;
- --full-upgrade)
- upgrade=full-upgrade
- ;;
- --ethx)
- kernel_params="$kernel_params net.ifnames=0 biosdevname=0"
- ;;
- --bbr)
- bbr=true
- ;;
- --hold)
- hold=true
- ;;
- --power-off)
- power_off=true
- ;;
- --architecture)
- architecture=$2
- shift
- ;;
- --boot-directory)
- boot_directory=$2
- shift
- ;;
- --firmware)
- firmware=true
- ;;
- --no-force-efi-extra-removable)
- force_efi_extra_removable=false
- ;;
- --grub-timeout)
- grub_timeout=$2
- shift
- ;;
- --dry-run)
- dry_run=true
- ;;
- *)
- err "Unknown option: \"$1\""
- esac
- shift
- done
- [ -z "$architecture" ] && {
- architecture=$(dpkg --print-architecture 2> /dev/null) || {
- case $(uname -m) in
- x86_64)
- architecture=amd64
- ;;
- aarch64)
- architecture=arm64
- ;;
- i386)
- architecture=i386
- ;;
- *)
- err 'No "--architecture" specified'
- esac
- }
- }
- [ -z "$kernel" ] && {
- kernel="linux-image-$architecture"
- [ "$cloud_kernel" = true ] && {
- [ "$architecture" != amd64 ] && [ "$architecture" != arm64 ] &&
- err 'Cloud kernel is only available for amd64 (x86_64) and arm64 (aarch64) architectures'
- kernel="linux-image-cloud-$architecture"
- }
- [ "$bpo_kernel" = true ] && {
- [ "$suite" != buster ] && [ "$suite" != stretch ] &&
- err 'Backports kernel is only available for 10 (buster) and 9 (stretch)'
- install="$kernel/$suite-backports $install"
- }
- }
- [ -n "$authorized_keys_url" ] && ! download "$authorized_keys_url" /dev/null &&
- err "Failed to download SSH authorized public keys from \"$authorized_keys_url\""
- installer="debian-$suite"
- installer_directory="/boot/$installer"
- save_preseed='cat'
- [ "$dry_run" = false ] && {
- [ "$(id -u)" -ne 0 ] && err 'root privilege is required'
- rm -rf "$installer_directory"
- mkdir -p "$installer_directory"
- cd "$installer_directory"
- save_preseed='tee -a preseed.cfg'
- }
- if [ "$account_setup" = true ]; then
- prompt_password
- elif [ "$network_console" = true ] && [ -z "$authorized_keys_url" ]; then
- prompt_password "Choose a password for the installer user of the SSH network console: "
- fi
- $save_preseed << 'EOF'
- # Localization
- d-i debian-installer/language string en
- d-i debian-installer/country string US
- d-i debian-installer/locale string en_US.UTF-8
- d-i keyboard-configuration/xkb-keymap select us
- # Network configuration
- d-i netcfg/choose_interface select auto
- EOF
- [ -n "$ip" ] && {
- echo 'd-i netcfg/disable_autoconfig boolean true' | $save_preseed
- echo "d-i netcfg/get_ipaddress string $ip" | $save_preseed
- [ -n "$netmask" ] && echo "d-i netcfg/get_netmask string $netmask" | $save_preseed
- [ -n "$gateway" ] && echo "d-i netcfg/get_gateway string $gateway" | $save_preseed
- [ -z "${ip%%*:*}" ] && [ -n "${dns%%*:*}" ] && dns='2001:4860:4860::8888 2001:4860:4860::8844'
- [ -n "$dns" ] && echo "d-i netcfg/get_nameservers string $dns" | $save_preseed
- echo 'd-i netcfg/confirm_static boolean true' | $save_preseed
- }
- if [ -n "$hostname" ]; then
- echo "d-i netcfg/hostname string $hostname" | $save_preseed
- hostname=debian
- domain=
- else
- hostname=$(cat /proc/sys/kernel/hostname)
- domain=$(cat /proc/sys/kernel/domainname)
- if [ "$domain" = '(none)' ]; then
- domain=
- else
- domain=" $domain"
- fi
- fi
- $save_preseed << EOF
- d-i netcfg/get_hostname string $hostname
- d-i netcfg/get_domain string$domain
- EOF
- echo 'd-i hw-detect/load_firmware boolean true' | $save_preseed
- [ "$network_console" = true ] && {
- $save_preseed << 'EOF'
- # Network console
- d-i anna/choose_modules string network-console
- d-i preseed/early_command string anna-install network-console
- EOF
- if [ -n "$authorized_keys_url" ]; then
- echo "d-i network-console/authorized_keys_url string $authorized_keys_url" | $save_preseed
- else
- $save_preseed << EOF
- d-i network-console/password password $password
- d-i network-console/password-again password $password
- EOF
- fi
- echo 'd-i network-console/start select Continue' | $save_preseed
- }
- $save_preseed << EOF
- # Mirror settings
- d-i mirror/country string manual
- d-i mirror/protocol string $mirror_protocol
- d-i mirror/$mirror_protocol/hostname string $mirror_host
- d-i mirror/$mirror_protocol/directory string $mirror_directory
- d-i mirror/$mirror_protocol/proxy string
- d-i mirror/suite string $suite
- EOF
- [ "$account_setup" = true ] && {
- password_hash=$(mkpasswd -m sha-256 "$password" 2> /dev/null) ||
- password_hash=$(openssl passwd -5 "$password" 2> /dev/null) ||
- password_hash=$(busybox mkpasswd -m sha256 "$password" 2> /dev/null) || {
- for python in python3 python python2; do
- password_hash=$("$python" -c 'import crypt, sys; print(crypt.crypt(sys.argv[1], crypt.mksalt(crypt.METHOD_SHA256)))' "$password" 2> /dev/null) && break
- done
- }
- $save_preseed << 'EOF'
- # Account setup
- EOF
- [ -n "$authorized_keys_url" ] && configure_sshd PasswordAuthentication no
- if [ "$username" = root ]; then
- if [ -z "$authorized_keys_url" ]; then
- configure_sshd PermitRootLogin yes
- else
- in_target "mkdir -m 0700 -p ~root/.ssh && busybox wget -O- \"$authorized_keys_url\" >> ~root/.ssh/authorized_keys"
- fi
- $save_preseed << 'EOF'
- d-i passwd/root-login boolean true
- d-i passwd/make-user boolean false
- EOF
- if [ -z "$password_hash" ]; then
- $save_preseed << EOF
- d-i passwd/root-password password $password
- d-i passwd/root-password-again password $password
- EOF
- else
- echo "d-i passwd/root-password-crypted password $password_hash" | $save_preseed
- fi
- else
- configure_sshd PermitRootLogin no
- [ -n "$authorized_keys_url" ] &&
- in_target "sudo -u $username mkdir -m 0700 -p ~$username/.ssh && busybox wget -O - \"$authorized_keys_url\" | sudo -u $username tee -a ~$username/.ssh/authorized_keys"
- [ "$sudo_with_password" = false ] &&
- in_target "echo \"$username ALL=(ALL:ALL) NOPASSWD:ALL\" > \"/etc/sudoers.d/90-user-$username\""
- $save_preseed << EOF
- d-i passwd/root-login boolean false
- d-i passwd/make-user boolean true
- d-i passwd/user-fullname string
- d-i passwd/username string $username
- EOF
- if [ -z "$password_hash" ]; then
- $save_preseed << EOF
- d-i passwd/user-password password $password
- d-i passwd/user-password-again password $password
- EOF
- else
- echo "d-i passwd/user-password-crypted password $password_hash" | $save_preseed
- fi
- fi
- }
- $save_preseed << EOF
- # Clock and time zone setup
- d-i time/zone string $timezone
- d-i clock-setup/utc boolean true
- d-i clock-setup/ntp boolean true
- d-i clock-setup/ntp-server string $ntp
- EOF
- [ "$disk_partitioning" = true ] && {
- $save_preseed << 'EOF'
- # Partitioning
- d-i partman-auto/method string regular
- EOF
- if [ -n "$disk" ]; then
- echo "d-i partman-auto/disk string $disk" | $save_preseed
- else
- # shellcheck disable=SC2016
- echo 'd-i partman/early_command string debconf-set partman-auto/disk "$(list-devices disk | head -n 1)"' | $save_preseed
- fi
- [ "$force_gpt" = true ] && echo 'd-i partman-partitioning/default_label string gpt' | $save_preseed
- echo "d-i partman/default_filesystem string $filesystem" | $save_preseed
- [ -z "$efi" ] && {
- efi=false
- [ -d /sys/firmware/efi ] && efi=true
- }
- $save_preseed << 'EOF'
- d-i partman-auto/expert_recipe string \
- naive :: \
- EOF
- if [ "$efi" = true ]; then
- $save_preseed << 'EOF'
- 106 106 106 free \
- $iflabel{ gpt } \
- $reusemethod{ } \
- method{ efi } \
- format{ } \
- . \
- EOF
- else
- $save_preseed << 'EOF'
- 1 1 1 free \
- $iflabel{ gpt } \
- $reusemethod{ } \
- method{ biosgrub } \
- . \
- EOF
- fi
- $save_preseed << 'EOF'
- 1075 1076 -1 $default_filesystem \
- method{ format } \
- format{ } \
- use_filesystem{ } \
- $default_filesystem{ } \
- mountpoint{ / } \
- .
- EOF
- echo 'd-i partman-auto/choose_recipe select naive' | $save_preseed
- $save_preseed << 'EOF'
- d-i partman-basicfilesystems/no_swap boolean false
- d-i partman/choose_partition select finish
- d-i partman/confirm boolean true
- d-i partman/confirm_nooverwrite boolean true
- EOF
- }
- $save_preseed << EOF
- # Base system installation
- d-i base-installer/kernel/image string $kernel
- EOF
- [ "$install_recommends" = false ] && echo "d-i base-installer/install-recommends boolean $install_recommends" | $save_preseed
- [ "$security_repository" = mirror ] && security_repository=$mirror_protocol://$mirror_host${mirror_directory%/*}/debian-security
- $save_preseed << EOF
- # Apt setup
- d-i apt-setup/services-select multiselect updates, backports
- d-i apt-setup/local0/repository string $security_repository $suite/updates main
- d-i apt-setup/local0/source boolean true
- EOF
- $save_preseed << 'EOF'
- # Package selection
- tasksel tasksel/first multiselect ssh-server
- EOF
- [ -n "$install" ] && echo "d-i pkgsel/include string $install" | $save_preseed
- [ -n "$upgrade" ] && echo "d-i pkgsel/upgrade select $upgrade" | $save_preseed
- $save_preseed << 'EOF'
- popularity-contest popularity-contest/participate boolean false
- # Boot loader installation
- d-i grub-installer/bootdev string default
- EOF
- [ "$force_efi_extra_removable" = true ] && echo 'd-i grub-installer/force-efi-extra-removable boolean true' | $save_preseed
- [ -n "$kernel_params" ] && echo "d-i debian-installer/add-kernel-opts string$kernel_params" | $save_preseed
- $save_preseed << 'EOF'
- # Finishing up the installation
- EOF
- [ "$hold" = false ] && echo 'd-i finish-install/reboot_in_progress note' | $save_preseed
- [ "$bbr" = true ] && in_target '{ echo "net.core.default_qdisc=fq"; echo "net.ipv4.tcp_congestion_control=bbr"; } > /etc/sysctl.d/bbr.conf'
- [ -n "$late_command" ] && echo "d-i preseed/late_command string in-target sh -c '$late_command'" | $save_preseed
- [ "$power_off" = true ] && echo 'd-i debian-installer/exit/poweroff boolean true' | $save_preseed
- save_grub_cfg='cat'
- [ "$dry_run" = false ] && {
- base_url="$mirror_protocol://$mirror_host$mirror_directory/dists/$suite/main/installer-$architecture/current/images/netboot/debian-installer/$architecture"
- [ "$daily_d_i" = true ] && base_url="https://d-i.debian.org/daily-images/$architecture/daily/netboot/debian-installer/$architecture"
- firmware_url="https://cdimage.debian.org/cdimage/unofficial/non-free/firmware/$suite/current/firmware.cpio.gz"
- download "$base_url/linux" linux
- download "$base_url/initrd.gz" initrd.gz
- [ "$firmware" = true ] && download "$firmware_url" firmware.cpio.gz
- gzip -d initrd.gz
- # cpio reads a list of file names from the standard input
- echo preseed.cfg | cpio -o -H newc -A -F initrd
- gzip -9 initrd
- mkdir -p /etc/default/grub.d
- tee /etc/default/grub.d/zz-debi.cfg 1>&2 << EOF
- GRUB_DEFAULT=debi
- GRUB_TIMEOUT=$grub_timeout
- GRUB_TIMEOUT_STYLE=menu
- EOF
- if command_exists update-grub; then
- grub_cfg=/boot/grub/grub.cfg
- update-grub
- elif command_exists grub2-mkconfig; then
- tmp=$(mktemp)
- grep -vF zz_debi /etc/default/grub > "$tmp"
- cat "$tmp" > /etc/default/grub
- rm "$tmp"
- # shellcheck disable=SC2016
- echo 'zz_debi=/etc/default/grub.d/zz-debi.cfg; if [ -f "$zz_debi" ]; then . "$zz_debi"; fi' >> /etc/default/grub
- grub_cfg=/boot/grub2/grub.cfg
- grub2-mkconfig -o "$grub_cfg"
- else
- err 'Could not find "update-grub" or "grub2-mkconfig" command'
- fi
- save_grub_cfg="tee -a $grub_cfg"
- }
- [ -z "$boot_directory" ] && {
- if grep -q '\s/boot\s' /proc/mounts; then
- boot_directory=/
- else
- boot_directory=/boot/
- fi
- }
- installer_directory="$boot_directory$installer"
- # shellcheck disable=SC2034
- mem=$(grep ^MemTotal: /proc/meminfo | { read -r x y z; echo "$y"; })
- [ $((mem / 1024)) -le 512 ] && kernel_params="$kernel_params lowmem/low=1"
- initrd="$installer_directory/initrd.gz"
- [ "$firmware" = true ] && initrd="$initrd $installer_directory/firmware.cpio.gz"
- $save_grub_cfg 1>&2 << EOF
- menuentry 'Debian Installer' --id debi {
- insmod part_msdos
- insmod part_gpt
- insmod ext2
- linux $installer_directory/linux$kernel_params
- initrd $initrd
- }
- EOF
|