+ifdef imagesize
+ ROOT_MB := $(shell printf '%i\n' "$$(( ${imagesize} - ${BOOT_MB} - 3 ))")
+endif
+
+PACKAGES += btrfs-progs bzip2 ca-certificates cron deborphan dnsutils file firmware-linux-free gzip htop ifupdown iputils-ping irqbalance isc-dhcp-client less make net-tools nmap ntpdate openssh-client pciutils psmisc rsync sshfs sudo traceroute unzip vim wget wireless-tools wpasupplicant xz-utils zip
+
ifdef packages
-PACKAGES := ${PACKAGES} $(subst ${comma_},${space},${packages})
+ PACKAGES += $(subst ${comma_},${space_},${packages})
+endif
+
+export DEBCONF RPICONFIG
+
+define DEBCONF :=
+${DEBCONF}
+
+keyboard-configuration keyboard-configuration/model select Generic 105-key (Intl) PC
+keyboard-configuration keyboard-configuration/variant select English (US) - English (Macintosh)
+
+console-setup console-setup/charmap47 select UTF-8
+console-setup console-setup/codeset47 select # Latin1 and Latin5 - western Europe and Turkic languages
+endef
+
+ifndef rpivmem
+ rpivmem := 128
+endif
+
+define RPICONFIG :=
+${RPICONFIG}
+
+dtoverlay=vc4-fkms-v3d,cma-${rpivmem}
+endef
+
+.PHONY: _debconf_selections _raspberry_config _keyboard
+_install: _debconf_selections
+_debconf_selections: ${SYSROOT}
+ printf '%s\n' "$${DEBCONF}" |chroot "$${SYSROOT}" debconf-set-selections
+
+ifdef keyboard
+ _config: _keyboard
endif
+_keyboard: ${CFGROOT}
+ -sed -Ei 's;^XKBLAYOUT=.*$$;XKBLAYOUT="${keyboard}";;' "$${CFGROOT}/etc/default/keyboard"
+
+ifdef raspi
+ _config: _raspberry_config _raspberry_initrd
+endif
+
+_raspberry_config: ${CFGROOT}
+ printf '%s\n' "$${RPICONFIG}" >>"$${CFGROOT}/boot/config.txt"
+
+_raspberry_initrd: ${CFGROOT}
+ printf '${fstype}\n' >>"$${CFGROOT}/etc/initramfs-tools/modules"
+ k="$$(printf '%s\n' "$${CFGROOT}"/lib/modules/* |sed 's;^.*/;;; /-/d')"; \
+ [ "${kernel}" ] && k="${kernel}"; \
+ chroot $${CFGROOT} update-initramfs -c -k "$${k##*/}"; \
+ printf 'initramfs initrd.img-%s\n' "$${k##*/}" >>"$${CFGROOT}/boot/config.txt"
+
+.PHONY: _timezone _locales
+
+ifdef timezone
+ export timezone
+ PACKAGES += tzdata
+ _config: _timezone
+endif
+
+ifdef locales
+ PACKAGES += locales
+ _config: _locales
+endif
+
+_timezone: ${CFGROOT}
+ printf '%s\n' "$${timezone}" >"$${CFGROOT}/etc/timezone"
+ ln -sf /usr/share/zoneinfo/"$${timezone}" "$${CFGROOT}/etc/localtime"
+
+_locales: ${CFGROOT}
+ for l in $(subst ${comma_},${space_},${locales}); do \
+ printf '%s %s\n' "$${l}" "$${l#*.}"; \
+ done >"$${CFGROOT}/etc/locale.gen"
+ printf 'LANG=%s\n' "$(firstword $(subst ${comma_},${space_},${locales}))" >"$${CFGROOT}/etc/default/locale"
+ chroot "$${CFGROOT}" locale-gen
+
+.PHONY: _rootpass _users _hostname
ifdef rootpass
-export rootpass
-.PHONY: _rootpass
-_config: _rootpass
-_rootpass: ${CFGROOT}
- chroot "$${CFGROOT}" usermod -p "$$(openssl passwd -salt "$$(($${RANDOM-0} + 10))" "$${rootpass}")" root
+ export rootpass
+ _config: _rootpass
endif
-ifdef wifi
-export wifi
-_config: ${CFGROOT}/etc/network/interfaces.d/wifi
+ifdef rootkey
+ export rootkey
+ _config: _rootkey
+endif
+
+ifdef users
+ _config: _users
+endif
+
+ifdef hostname
+ export hostname
+ _config: _hostname
+endif
+
+_rootpass: ${CFGROOT}
+ chroot "$${CFGROOT}" usermod -p "$$(openssl passwd "$${rootpass}")" root
+
+${rootkey}.pub:
+ [ -f "$@" ] || ssh-keygen -N '' -f "$${rootkey}"
+
+_rootkey: ${CFGROOT} ${rootkey}.pub
+ mkdir -m 700 "$${CFGROOT}/root/.ssh/"
+ cp "$${rootkey}.pub" "$${CFGROOT}/root/.ssh/authorized_keys"
+
+_users: ${CFGROOT}
+ for u in $(subst ${comma_},${space_},${users}); do \
+ chroot "$${CFGROOT}" useradd -s /bin/bash -p "$$(openssl passwd "")" "$$u"; \
+ done
+
+_hostname: ${CFGROOT}
+ printf '%s\n' "$${hostname}" >"$${CFGROOT}/etc/hostname"
+ sed -Ei "s;^127\.0\.0\.1.*$$;& $${hostname};" "$${CFGROOT}/etc/hosts"
+
+_timeouts: ${CFGROOT}
+ printf '%s\n' DefaultTimeoutStartSec=10s DefaultTimeoutStopSec=10s >>"${CFGROOT}/etc/systemd/system.conf"
-ifdef wifipass
-export wifipass
-${CFGROOT}/etc/network/interfaces.d/wifi: ${CFGROOT}
+
+.PHONY: _systemd_timeout
+_config: _systemd_timeout
+_systemd_timeout: ${CFGROOT}
+ mkdir -p "$${CFGROOT}/etc/systemd"
+ printf 'DefaultTimeout%sSec=%s\n' Start 10s Stop 10s >>"$${CFGROOT}/etc/systemd/system.conf"
+
+ifdef wifi
+ _config: ${CFGROOT}/etc/network/interfaces.d/wifi
+ ifdef wifipass
+ export wifi wifipass
+ ${CFGROOT}/etc/network/interfaces.d/wifi: ${CFGROOT}
printf 'auto wlan0\nallow-hotplug wlan0\n\niface wlan0 inet dhcp\n' >$@
printf ' wpa-ssid "%s"\n wpa-psk "%s"\n' "$$wifi" "$$wifipass" >>$@
-else
-${CFGROOT}/etc/network/interfaces.d/wifi: ${CFGROOT}
+ else
+ export wifi
+ ${CFGROOT}/etc/network/interfaces.d/wifi: ${CFGROOT}
printf 'auto wlan0\nallow-hotplug wlan0\n\niface wlan0 inet dhcp\n' >$@
- printf ' wireless-ssid "%s"\n' "$$wifi" >>$@
+ printf ' wireless-essid "%s"\n' "$$wifi" >>$@
+ endif
endif
+
+.PHONY: _fstab
+
+ifdef _fstab
+ _config: ${CFGROOT}/etc/fstab
+ export _fstab
+
+else
+ ifdef fsuuid
+ define _fstab :=
+UUID=${fsuuid} / btrfs auto,rw,subvol=${release} 0 0
+UUID=${fsuuid} /home btrfs auto,nofail,rw,subvol=home 0 0
+UUID=${fsuuid} /mnt/fsroot btrfs auto,nofail,rw 0 0
+LABEL=SWAP swap swap auto,nofail,sw 0 0
+LABEL=EFIBOOT /boot/efi vfat auto,nofail,ro 0 0
+endef
+ _config: ${CFGROOT}/etc/fstab
+ export _fstab
+
+ endif
endif
+
+${CFGROOT}/etc/fstab: ${CFGROOT}
+ printf '%s' "$${_fstab}" >"$@"
+
+
+_config: ${CFGROOT}/etc/sysctl.d/quiet_console
+
+${CFGROOT}/etc/sysctl.d/quiet_console: ${CFGROOT}
+ printf '%s\n' "kernel.printk=3 4 1 7" >"$@"