Skip to content

Commit

Permalink
generate hostname from mac addrs
Browse files Browse the repository at this point in the history
  • Loading branch information
nothub committed Nov 24, 2023
1 parent 64fbf7d commit a72df7a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 11 additions & 8 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions installer/late.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<EOF >"${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"
Expand Down

0 comments on commit a72df7a

Please sign in to comment.