@@ -5,6 +5,13 @@ set -Eeuo pipefail
5
5
group=" smb"
6
6
share=" /storage"
7
7
8
+ # Create shared directory
9
+ mkdir -p " $share " || { echo " Failed to create directory $share " ; exit 1; }
10
+
11
+ # Copy config file template
12
+ rm -f /etc/samba/smb.custom
13
+ cp /etc/samba/smb.conf /etc/samba/smb.custom
14
+
8
15
# Check if the smb group exists, if not, create it
9
16
if ! getent group " $group " & > /dev/null; then
10
17
groupadd " $group " || { echo " Failed to create group $group " ; exit 1; }
@@ -21,37 +28,36 @@ OldGID=$(getent group "$group" | cut -d: -f3)
21
28
22
29
# Change the UID and GID of the user and group if necessary
23
30
if [[ " $OldUID " != " $UID " ]]; then
24
- usermod -u " $UID " " $USER " || { echo " Failed to change UID for $USER " ; exit 1; }
31
+ usermod -o - u " $UID " " $USER " || { echo " Failed to change UID for $USER " ; exit 1; }
25
32
fi
26
33
27
34
if [[ " $OldGID " != " $GID " ]]; then
28
- groupmod -g " $GID " " $group " || { echo " Failed to change GID for group $group " ; exit 1; }
35
+ groupmod -o - g " $GID " " $group " || { echo " Failed to change GID for group $group " ; exit 1; }
29
36
fi
30
37
31
- # Change ownership of files and directories
32
- find / -path " $share " -prune -o -group " $OldGID " -exec chgrp -h " $group " {} \;
33
- find / -path " $share " -prune -o -user " $OldUID " -exec chown -h " $USER " {} \;
34
-
35
38
# Change Samba password
36
39
echo -e " $PASS \n$PASS " | smbpasswd -a -s " $USER " || { echo " Failed to change Samba password for $USER " ; exit 1; }
37
40
38
- rm -f /etc/samba/smb.custom
39
- cp /etc/samba/smb.conf /etc/samba/smb.custom
40
-
41
41
# Update force user and force group in smb.conf
42
42
sed -i " s/^\(\s*\)force user =.*/\1force user = $USER /" " /etc/samba/smb.custom"
43
43
sed -i " s/^\(\s*\)force group =.*/\1force group = $group /" " /etc/samba/smb.custom"
44
44
45
- # Verify if the RW variable is not equal to true (indicating read-only mode) and adjust settings accordingly
46
- if [[ " $RW " != " true" ]]; then
45
+ # Verify if the RW variable is equal to false (indicating read-only mode)
46
+ if [[ " $RW " == [Ff0]* ]]; then
47
+
48
+ # Adjust settings in smb.conf to set share to read-only
47
49
sed -i " s/^\(\s*\)writable =.*/\1writable = no/" " /etc/samba/smb.custom"
48
50
sed -i " s/^\(\s*\)read only =.*/\1read only = yes/" " /etc/samba/smb.custom"
49
- fi
50
51
51
- # Create shared directory and set permissions
52
- mkdir -p " $share " || { echo " Failed to create directory $share " ; exit 1; }
53
- chmod -R 0770 " $share " || { echo " Failed to set permissions for directory $share " ; exit 1; }
54
- chown -R " $USER :$group " " $share " || { echo " Failed to set ownership for directory $share " ; exit 1; }
52
+ else
53
+
54
+ # Set permissions for share directory if new (empty), leave untouched if otherwise
55
+ if [ -z " $( ls -A " $share " ) " ]; then
56
+ chmod 0770 " $share " || { echo " Failed to set permissions for directory $share " ; exit 1; }
57
+ chown " $USER :$group " " $share " || { echo " Failed to set ownership for directory $share " ; exit 1; }
58
+ fi
59
+
60
+ fi
55
61
56
62
# Start the Samba daemon with the following options:
57
63
# --foreground: Run in the foreground instead of daemonizing.
0 commit comments