From a72df7a282b9e7a7df06468516e39acff206efd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20H=C3=BCbner?= Date: Fri, 24 Nov 2023 18:57:54 +0100 Subject: [PATCH] generate hostname from mac addrs --- README.md | 5 +++++ build.sh | 19 +++++++++++-------- installer/late.sh | 17 +++++++++++++++++ 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d42f85f..30cda44 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,11 @@ echo "ssh-ed25519 AAAA... bar" >> configs/authorized_keys If the `-p` flag is not set, a random admin password will be generated. +### Hostname + +If the `-n` flag is not set, a hostname will be generated at installation. +The hostname will be based on the installed machines mac addresses. + ## Debug in VM While running the installer, press `ctrl`+`alt`+`f4` to show the installers log output. diff --git a/build.sh b/build.sh index 3e060b4..a59031a 100755 --- a/build.sh +++ b/build.sh @@ -22,8 +22,8 @@ usage() { username="janitor" password="$(pwgen -ns 16 1)" password_mask="false" -hostname="machine" -domain="example.org" +hostname="undefined" +domain="home.arpa" iso_url="https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-12.2.0-amd64-netinst.iso" sign_key="DA87E80D6294BE9B" out_file="debian-12.2.0-amd64-auto.iso" @@ -85,17 +85,20 @@ cp -a installer/* "${workdir}" # generate password hash salt="$(pwgen -ns 16 1)" -hash="$(mkpasswd -m sha-512 -S "${salt}" "${password}")" +passhash="$(mkpasswd -m sha-512 -S "${salt}" "${password}")" if test "${password_mask}" == "true"; then password="$(echo "${password}" | tr '[:print:]' 'x')" fi # replace tokens -find "${workdir}" -type f -exec sed -i "s#@USERNAME@#${username}#" {} \; -sed -i "s#@PASSHASH@#${hash}#" "${workdir}/preseed.cfg" -sed -i "s#@HOSTNAME@#${hostname}#" "${workdir}/preseed.cfg" -sed -i "s#@DOMAIN@#${domain}#" "${workdir}/preseed.cfg" -sed -i "s#@PACKAGES@#${apt_pkgs[*]}#" "${workdir}/preseed.cfg" +replace_token() { + find "${workdir}" -type f -exec sed -i "s#${1}#${2}#" {} \; +} +replace_token "@USERNAME@" "${username}" +replace_token "@PASSHASH@" "${passhash}" +replace_token "@HOSTNAME@" "${hostname}" +replace_token "@DOMAIN@" "${domain}" +replace_token "@PACKAGES@" "${apt_pkgs[*]}" # clear existing output iso file if test -f "${out_file}"; then diff --git a/installer/late.sh b/installer/late.sh index 5170879..deef93d 100755 --- a/installer/late.sh +++ b/installer/late.sh @@ -4,6 +4,23 @@ set -eu prefix="/target" admin="@USERNAME@" +hostname="@HOSTNAME@" +domain="@DOMAIN@" + +if test "${hostname}" == "undefined"; then + # generate hostname from mac addresses + hostname="DEB$(cat /sys/class/net/*/address | tr -d '\n' | sha256sum | tr '[:lower:]' '[:upper:]' | head -c 5)" + echo "${hostname}" >"${prefix}/etc/hostname" + cat <"${prefix}/etc/hosts" +127.0.0.1 localhost +127.0.1.1 ${hostname}.${domain} ${hostname} + +# The following lines are desirable for IPv6 capable hosts +::1 localhost ip6-localhost ip6-loopback +ff02::1 ip6-allnodes +ff02::2 ip6-allrouters +EOF +fi # custom configs cp -a "/cdrom/configs/bashrc.bash" "${prefix}/etc/skel/.bashrc"