From 336ef1abb48b78116736abfcdb7c03c3caa5df12 Mon Sep 17 00:00:00 2001 From: Roland Date: Tue, 18 Jun 2024 16:17:54 -0600 Subject: [PATCH] Creating script which runs on first startup only. This is used to copy /etc/skel files to first user's home directory. --- .../wrolpi-first-startup.service | 12 ++++++++++++ pi-gen/stage2/04-wrolpi/03-run-chroot.sh | 4 ++-- wrolpi/scripts/first_startup.sh | 19 +++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 etc/raspberrypios/wrolpi-first-startup.service create mode 100755 wrolpi/scripts/first_startup.sh diff --git a/etc/raspberrypios/wrolpi-first-startup.service b/etc/raspberrypios/wrolpi-first-startup.service new file mode 100644 index 00000000..894eff67 --- /dev/null +++ b/etc/raspberrypios/wrolpi-first-startup.service @@ -0,0 +1,12 @@ +[Unit] +Description=Run script at first startup +After=multi-user.target + +[Service] +Type=oneshot +ExecStart=/opt/wrolpi/wrolpi/scripts/first_startup.sh +RemainAfterExit=true +StandardOutput=journal + +[Install] +WantedBy=multi-user.target diff --git a/pi-gen/stage2/04-wrolpi/03-run-chroot.sh b/pi-gen/stage2/04-wrolpi/03-run-chroot.sh index 934cdf87..eb74cb8a 100755 --- a/pi-gen/stage2/04-wrolpi/03-run-chroot.sh +++ b/pi-gen/stage2/04-wrolpi/03-run-chroot.sh @@ -12,9 +12,7 @@ alias la='ll -a' EOF # Create WROLPi user. This user will own the media directory, API, and App. -# The `pi` user will be the maintainer's user. useradd -md /home/wrolpi wrolpi -s "$(command -v bash)" -usermod -aG pi wrolpi # Change default postgresql port to 5432. sed -i 's/port = 5433/port = 5432/' /etc/postgresql/15/main/postgresql.conf @@ -50,6 +48,8 @@ chown wrolpi:wrolpi /media/wrolpi cp /opt/wrolpi/etc/raspberrypios/wrolpi-*.service /etc/systemd/system/ cp /opt/wrolpi/etc/raspberrypios/wrolpi.target /etc/systemd/system/ +# Enable first startup script. +systemctl enable wrolpi-first-startup.service # NetworkManager for the hotspot. systemctl enable NetworkManager diff --git a/wrolpi/scripts/first_startup.sh b/wrolpi/scripts/first_startup.sh new file mode 100755 index 00000000..5e492f8e --- /dev/null +++ b/wrolpi/scripts/first_startup.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +# This script is run once on the first startup of a Raspberry Pi. + +# Copy skeleton files to first users home directory. +USER_HOME=$(getent passwd 1000 | cut -d: -f6) +if [ "$(ls -A /etc/skel)" ]; then + echo "Files found in /etc/skel, copying to $USER_HOME..." + # Copy files from /etc/skel to the user's home directory + cp -r /etc/skel/. "$USER_HOME" + # Change ownership to the user with UID 1000 + chown -R 1000:1000 "$USER_HOME" + echo "Files copied successfully." +else + echo "No files found in /etc/skel." + exit 1 +fi + +# Do not run this script again. +systemctl disable wrolpi-first-startup.service