@@ -7,6 +7,10 @@ FSX_OZFS_EXISTS=$1
77FSX_OPENZFS_DNS_NAME=" /home"
88FSX_L_DNS_NAME=" /fsx"
99
10+ # Look for shared_users.txt in parent directory
11+ SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
12+ SHARED_USER_FILE=" ${SCRIPT_DIR} /../shared_users.txt"
13+
1014# Function to check mount
1115check_mount ()
1216{
@@ -36,56 +40,152 @@ wait_for_mount()
3640 done
3741}
3842
43+ # Function to setup home directory for a user with OpenZFS
44+ setup_user_home_openzfs ()
45+ {
46+ local username=$1
47+
48+ echo " Setting up OpenZFS home directory for user: $username "
49+
50+ # Create user directory on OpenZFS
51+ ansible localhost -b -m ansible.builtin.file -a " path='$FSX_OPENZFS_DNS_NAME /$username ' state=directory owner=$username group=$username mode=0755"
52+
53+ # Set home directory to /home/username
54+ ansible localhost -b -m ansible.builtin.user -a " name=$username home='$FSX_OPENZFS_DNS_NAME /$username ' move_home=yes"
55+ echo " Home directory set to $FSX_OPENZFS_DNS_NAME /$username "
56+
57+ # Maintain access to /fsx/username
58+ if wait_for_mount " $FSX_L_DNS_NAME " ; then
59+ sudo mkdir -p " $FSX_L_DNS_NAME /$username "
60+ sudo chown " $username :$username " " $FSX_L_DNS_NAME /$username "
61+ else
62+ echo " Warning: FSx Lustre mount not available, skipping $FSX_L_DNS_NAME /$username setup"
63+ fi
64+ }
65+
66+ # Function to setup home directory for a user with FSx Lustre only
67+ setup_user_home_fsx_lustre ()
68+ {
69+ local username=$1
70+ local fsx_home=$2
71+
72+ echo " Setting up FSx Lustre home directory for user: $username at $fsx_home "
73+
74+ if [ -d " $fsx_home " ]; then
75+ sudo usermod -d " $fsx_home " " $username "
76+ elif [ -d " $FSX_L_DNS_NAME " ]; then
77+ # Create the directory
78+ sudo mkdir -p " $fsx_home "
79+ sudo chown " $username :$username " " $fsx_home "
80+
81+ # Try to change home directory with move
82+ if ! sudo usermod -m -d " $fsx_home " " $username " ; then
83+ echo " Warning: Could not move home directory for $username . Setting home without moving files."
84+
85+ # If user has existing home, copy contents
86+ if [ -d " /home/$username " ]; then
87+ sudo rsync -a " /home/$username /" " $fsx_home /"
88+ fi
89+ sudo chown -R " $username :$username " " $fsx_home "
90+
91+ sudo usermod -d " $fsx_home " " $username "
92+ else
93+ echo " Home directory moved successfully to $fsx_home "
94+ fi
95+ fi
96+ }
97+
3998if [ -z " $FSX_OZFS_EXISTS " ]; then
4099 echo " Error: Missing parameter. Usage: $0 <1|0> (1 if OpenZFS exists, 0 otherwise)"
41100 exit 1
42101fi
43102
44103# Check if OpenZFS is mounted
45104if [ $FSX_OZFS_EXISTS -eq 1 ]; then
46- echo " OpenZFS is mounted. Looping to ensure FSxOZFS is mounted ."
47-
105+ echo " OpenZFS is mounted. Setting up home directories on OpenZFS ."
106+
48107 if wait_for_mount " $FSX_OPENZFS_DNS_NAME " ; then
49- ansible localhost -b -m ansible.builtin.file -a " path='$FSX_OPENZFS_DNS_NAME /ubuntu' state=directory owner=ubuntu group=ubuntu mode=0755"
50-
51- echo " OpenZFS is mounted at $FSX_OPENZFS_DNS_NAME "
52- # Set home directory to /home/ubuntu
53- ansible localhost -b -m ansible.builtin.user -a " name=ubuntu home='$FSX_OPENZFS_DNS_NAME /ubuntu' move_home=yes"
54- echo " Home directory set to $FSX_OPENZFS_DNS_NAME /ubuntu"
55-
56- # Maintain access to /fsx/ubuntu
57- if wait_for_mount " $FSX_L_DNS_NAME " ; then
58- sudo mkdir -p " $FSX_L_DNS_NAME /ubuntu"
59- sudo chown ubuntu:ubuntu " $FSX_L_DNS_NAME /ubuntu"
108+ # Setup ubuntu user first
109+ echo " Setting up home directory for default ubuntu user..."
110+ setup_user_home_openzfs " ubuntu"
111+
112+ # Process additional users from shared_users.txt if it exists
113+ if [[ -f $SHARED_USER_FILE ]]; then
114+ echo " Found $SHARED_USER_FILE , processing additional users..."
115+ echo " Contents of $SHARED_USER_FILE :"
116+ cat " $SHARED_USER_FILE "
117+
118+ while IFS=" ," read -r username uid home; do
119+ # Trim whitespace from all fields
120+ username=$( echo " $username " | xargs)
121+ uid=$( echo " $uid " | xargs)
122+ home=$( echo " $home " | xargs)
123+
124+ # Skip empty lines or lines that are just whitespace
125+ if [[ -z " $username " ]]; then
126+ echo " Skipping empty or invalid line"
127+ continue
128+ fi
129+
130+ # Verify user exists before trying to set up home
131+ if ! id -u " $username " > /dev/null 2>&1 ; then
132+ echo " WARNING: User $username does not exist, skipping home setup"
133+ continue
134+ fi
135+
136+ echo " Processing home directory for user: '$username '"
137+ setup_user_home_openzfs " $username "
138+ done < " $SHARED_USER_FILE "
139+
140+ echo " All users from $SHARED_USER_FILE processed successfully"
60141 else
61- echo " Warning: FSx mount not available, skipping $FSX_L_DNS_NAME / ubuntu setup "
142+ echo " No $SHARED_USER_FILE found, only ubuntu user configured "
62143 fi
63144 fi
64145else
65- echo " OpenZFS is not mounted. Skipped OZFS check loop, and looping for FSxL only."
66- echo " Using FSxL file system as home..."
67-
146+ echo " OpenZFS is not mounted. Using FSx Lustre file system as home..."
147+
68148 if ! wait_for_mount " $FSX_L_DNS_NAME " ; then
69149 echo " Warning: FSx mount not available. Exiting."
70150 exit 1
71151 fi
72- if [ -d " $FSX_L_DNS_NAME /ubuntu" ]; then
73- sudo usermod -d " $FSX_L_DNS_NAME /ubuntu" ubuntu
74- elif [ -d " $FSX_L_DNS_NAME " ]; then
75- # Create the directory (race condition: if it doesn't get detected)
76- sudo mkdir -p " $FSX_L_DNS_NAME /ubuntu"
77- sudo chown ubuntu:ubuntu " $FSX_L_DNS_NAME /ubuntu"
78-
79- # Try to change home directory with move (race condition)
80- if ! sudo usermod -m -d " $FSX_L_DNS_NAME /ubuntu" ubuntu; then
81- echo " Warning: Could not move home directory. Setting home without moving files."
82-
83- sudo rsync -a /home/ubuntu/ " $FSX_L_DNS_NAME /ubuntu/"
84- sudo chown -R ubuntu:ubuntu " $FSX_L_DNS_NAME /ubuntu"
85-
86- sudo usermod -d " $FSX_L_DNS_NAME /ubuntu" ubuntu
87- else
88- echo " Home directory moved successfully to $FSX_L_DNS_NAME /ubuntu"
89- fi
152+
153+ # Setup ubuntu user first
154+ echo " Setting up home directory for default ubuntu user..."
155+ setup_user_home_fsx_lustre " ubuntu" " $FSX_L_DNS_NAME /ubuntu"
156+
157+ # Process additional users from shared_users.txt if it exists
158+ if [[ -f $SHARED_USER_FILE ]]; then
159+ echo " Found $SHARED_USER_FILE , processing additional users..."
160+ echo " Contents of $SHARED_USER_FILE :"
161+ cat " $SHARED_USER_FILE "
162+
163+ while IFS=" ," read -r username uid home; do
164+ # Trim whitespace from all fields
165+ username=$( echo " $username " | xargs)
166+ uid=$( echo " $uid " | xargs)
167+ home=$( echo " $home " | xargs)
168+
169+ # Skip empty lines or lines that are just whitespace
170+ if [[ -z " $username " ]] || [[ -z " $home " ]]; then
171+ echo " Skipping empty or invalid line"
172+ continue
173+ fi
174+
175+ # Verify user exists before trying to set up home
176+ if ! id -u " $username " > /dev/null 2>&1 ; then
177+ echo " WARNING: User $username does not exist, skipping home setup"
178+ continue
179+ fi
180+
181+ echo " Processing home directory for user: '$username ' at '$home '"
182+ setup_user_home_fsx_lustre " $username " " $home "
183+ done < " $SHARED_USER_FILE "
184+
185+ echo " All users from $SHARED_USER_FILE processed successfully"
186+ else
187+ echo " No $SHARED_USER_FILE found, only ubuntu user configured"
90188 fi
91- fi
189+ fi
190+
191+ echo " Home directory setup completed for all users"
0 commit comments