From b4695722b4cf68bb1feb4b3d816155e6ea425249 Mon Sep 17 00:00:00 2001 From: Dimitris Karakasilis Date: Tue, 10 Sep 2024 11:34:34 +0300 Subject: [PATCH] Fix home dir ownership as a last step (#1036) because users might be created in various stages (like "network") Signed-off-by: Dimitris Karakasilis --- .../kairos-overlay-files/collection.yaml | 2 +- .../system/oem/00_home_dir_owner_fix.yaml | 17 +++---------- .../files/usr/bin/fix-home-dir-ownership | 24 +++++++++++++++++++ 3 files changed, 28 insertions(+), 15 deletions(-) create mode 100755 packages/static/kairos-overlay-files/files/usr/bin/fix-home-dir-ownership diff --git a/packages/static/kairos-overlay-files/collection.yaml b/packages/static/kairos-overlay-files/collection.yaml index 7fdb989..d8e1169 100644 --- a/packages/static/kairos-overlay-files/collection.yaml +++ b/packages/static/kairos-overlay-files/collection.yaml @@ -1,4 +1,4 @@ packages: - name: "kairos-overlay-files" category: "static" - version: "1.1.48" + version: "1.1.49" diff --git a/packages/static/kairos-overlay-files/files/system/oem/00_home_dir_owner_fix.yaml b/packages/static/kairos-overlay-files/files/system/oem/00_home_dir_owner_fix.yaml index bf50741..bda227e 100644 --- a/packages/static/kairos-overlay-files/files/system/oem/00_home_dir_owner_fix.yaml +++ b/packages/static/kairos-overlay-files/files/system/oem/00_home_dir_owner_fix.yaml @@ -1,17 +1,6 @@ name: "Fix home directory permissions (kairos issue #2797)" stages: - initramfs.after: - - name: "Fix permissions" - if: '[ ! -f "/usr/local/.kairos/skip-home-directory-ownership-fix" ]' + network.after: + - name: "Fix home dir ownership" commands: - - | - # Iterate over users in /etc/passwd and chown their directories - awk -F: '$3 >= 1000 && $6 ~ /^\/home\// {print $1, $6}' /etc/passwd | while read -r user homedir; do - if [ -d "$homedir" ]; then # Check if the home directory exists - echo "Changing ownership of $homedir to $user" - chown -R "$user":"$user" "$homedir" - else - echo "Directory $homedir does not exist for user $user" - fi - done - echo "https://github.com/kairos-io/kairos/issues/2843" > /usr/local/.kairos/skip-home-directory-ownership-fix + - "/usr/bin/fix-home-dir-ownership" diff --git a/packages/static/kairos-overlay-files/files/usr/bin/fix-home-dir-ownership b/packages/static/kairos-overlay-files/files/usr/bin/fix-home-dir-ownership new file mode 100755 index 0000000..32cedcf --- /dev/null +++ b/packages/static/kairos-overlay-files/files/usr/bin/fix-home-dir-ownership @@ -0,0 +1,24 @@ +#!/bin/bash + +set -e + +SENTINEL_FILE="/usr/local/.kairos/skip-home-directory-ownership-fix" + +if [ -f $SENTINEL_FILE ]; then + echo "Skipping ownership fix because sentinel file was found: $SENTINEL_FILE" + exit 0 +fi + +# Iterate over users in /etc/passwd and chown their directories +awk -F: '$3 >= 1000 && $6 ~ /^\/home\// {print $1, $6}' /etc/passwd | while read -r user homedir; do + if [ -d "$homedir" ]; then # Check if the home directory exists + echo "Changing ownership of $homedir to $user" + chown -R "$user":"$user" "$homedir" + else + echo "Directory $homedir does not exist for user $user" + fi +done + +# Write the sentinel file +mkdir -p "$(dirname $SENTINEL_FILE)" +echo "https://github.com/kairos-io/kairos/issues/2843" > $SENTINEL_FILE