#!/usr/bin/env bash uses_sysd_resolve() { local rootfs_dir="$1" _res local resconf="${rootfs_dir}/etc/resolv.conf" sysd_resolvedir="${rootfs_dir}/run/systemd/resolve" if [[ -d "$sysd_resolvedir" ]]; then return 0; fi if [[ -f "${rootfs_dir}/etc/systemd/resolved.conf" ]]; then return 0; fi if grep -q "nameserver 127.0.0." "$resconf"; then return 0; fi if grep -qi "systemd-resolved" "$resconf"; then return 0; fi if grep -qi "stub" "$resconf"; then return 0; fi return 1 } _inj_ns() { local pdir="$(dirname "$1")" [[ ! -d "$pdir" ]] && echo -en " >> Parent folder $pdir not found. Creating it... " && mkdir -pv "$pdir" touch "$1" echo "nameserver 185.130.44.20" >> "$1" echo "nameserver 1.1.1.1" >> "$1" echo "nameserver 2a07:e00::333" >> "$1" } fix_resolve() { local rootfs_dir="$1" local res_dir="${rootfs_dir}/run/systemd/resolve" local res_stub="${res_dir}/stub-resolv.conf" resolv_conf="${rootfs_dir}/etc/resolv.conf" if uses_sysd_resolve "$rootfs_dir"; then echo -e " >> System at '${rootfs_dir}' appears to use systemd-resolved. Initialising ${res_stub} ..." _inj_ns "$res_stub" else echo -e " >> System at '${rootfs_dir}' doesn't appear to use systemd-resolved. Writing directly to $resolv_conf ..." _inj_ns "$resolv_conf" fi } bind_mounts() { cd "$1" if [[ ! -d proc ]] || [[ -z "$(ls proc)" ]]; then echo -e " [...] ${1}/proc not mounted. Mounting now." echo -e " -> $(mount -v -t proc proc proc/) \n" else echo -e " [+++] ${1}/proc is non-empty - must be already mounted. Skipping.\n" fi if [[ ! -d sys ]] || [[ -z "$(ls sys)" ]]; then echo -e " [...] ${1}/sys not mounted. Mounting now." echo -e " -> $(mount -v --bind /sys sys/) \n" else echo -e " [+++] ${1}/sys is non-empty - must be already mounted. Skipping.\n" fi if [[ ! -d dev ]] || [[ -z "$(ls dev)" ]] || ! grep -q "${1}/dev" <<< "$(mount)"; then echo -e " [...] ${1}/dev not mounted. Mounting now." echo -e " -> $(mount -v --bind /dev dev/) \n" else echo -e " [+++] ${1}/dev is non-empty - must be already mounted. Skipping.\n" fi cd - &>/dev/null } umount_all() { local bdir="$1" cd / umount -v "${bdir}/sys" umount -v "${bdir}/proc" umount -v "${bdir}/dev" if grep -q "${bdir}/boot" <<< "$(mount)"; then umount -v "${bdir}/boot"; fi if grep -q "${bdir}/home" <<< "$(mount)"; then umount -v "${bdir}/home"; fi if grep -q "${bdir}/root" <<< "$(mount)"; then umount -v "${bdir}/root"; fi if grep -q "${bdir}/var/log" <<< "$(mount)"; then umount -v "${bdir}/var/log"; fi if grep -q "${bdir}/var" <<< "$(mount)"; then umount -v "${bdir}/var"; fi if grep -q "${bdir}/usr" <<< "$(mount)"; then umount -v "${bdir}/usr"; fi } if (( $# < 1 )); then echo -e "Usage: $0 [chroot_dir]" echo echo -e "Runs post-setup commands within a newly created debootstrap chroot," echo -e "to ensure a good user experience e.g. via generating and configuring" echo -e "locales as to prevent locale errors.\n" exit 1 fi CHR_OUT="$1" LANGUAGE=C.UTF-8 LANG=C.UTF-8 LC_ALL=C.UTF-8 LC_CTYPE=C.UTF-8 echo -e "\n >>> Generating default locale config at ${CHR_OUT}/etc/default/locale\n" cat > "${CHR_OUT}/etc/default/locale" <<"EOF" LANGUAGE=en_US.UTF-8 LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 EOF echo -e "\n >>> Mounting proc/sys/dev inside of ${CHR_OUT}\n" bind_mounts "$CHR_OUT" echo -e "\n >>> Setting up DNS in ${CHR_OUT}\n" fix_resolve "$CHR_OUT" echo -e "\n >>> Ensuring 'locales' is installed in ${CHR_OUT} - then cleaning up apt using autoremove + clean\n" chroot "$CHR_OUT" /bin/bash -c 'apt update -qy; apt install -qy locales; apt autoremove -qy; apt clean -qy;' echo -e "\n >>> Enabling en_US and en_GB in ${CHR_OUT}/etc/locale.gen\n" sed -i "/^#.* en_US.*/s/^# //g" "${CHR_OUT}/etc/locale.gen" sed -i "/^#.* en_GB.*/s/^# //g" "${CHR_OUT}/etc/locale.gen" echo -e "\n >>> Running locale-gen inside of ${CHR_OUT}\n" chroot "$CHR_OUT" /bin/bash -c 'locale-gen' echo -e "\n >>> Generating default locale config at ${CHR_OUT}/etc/default/locale\n" cat > "${CHR_OUT}/etc/default/locale" <<"EOF" LANGUAGE=en_US.UTF-8 LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 EOF umount_all "$CHR_OUT" echo -e "\n [+++] FINISHED POST-SETUP for chroot dir: $CHR_OUT \n"