-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3-mount-recording-source.sh
More file actions
256 lines (206 loc) · 7.67 KB
/
Copy path3-mount-recording-source.sh
File metadata and controls
256 lines (206 loc) · 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env bash
set -euo pipefail
# =============================================================================
# mount-smb-source.sh
# Mount a remote Windows SMB share as a recording source
# Rocky Linux 9.x
# Idempotent: safe to rerun
# =============================================================================
LOGFILE="/var/log/mount-smb-source.log"
SAMBA_USER="recordings"
MAX_ATTEMPTS=3
log() {
local msg
msg="$(date '+%Y-%m-%d %H:%M:%S') $1"
echo "$msg" | tee -a "$LOGFILE"
}
die() {
log "FATAL: $1"
exit 1
}
# ---- Pre-flight checks -----------------------------------------------------
[[ $(id -u) -eq 0 ]] || die "This script must be run as root."
# ---- Explanation and confirmation -------------------------------------------
echo ""
echo "======================================================================"
echo " Mount SMB Source What this script will do:"
echo "======================================================================"
echo ""
echo " 1. Install required packages if missing:"
echo " cifs-utils, samba-client"
echo " 2. Prompt for tenant number (determines mount at /mnt/<tenant>-recordings)"
echo " 3. Gather remote server connection details (IP, share, credentials)"
echo " 4. Test connectivity to the remote SMB share (up to ${MAX_ATTEMPTS} attempts)"
echo " 5. Create a credentials file at /root/.smb-<tenant>-recordings"
echo " Permissions locked to 0600 (root only)"
echo " 6. Add a CIFS mount entry to /etc/fstab with:"
echo " SMB version : 3.0"
echo " Mount options : noperm, _netdev (waits for network at boot)"
echo " UID/GID : ${SAMBA_USER}"
echo " 7. Mount the remote share"
echo ""
echo " Log file : ${LOGFILE}"
echo ""
echo "======================================================================"
echo ""
read -rp "Proceed? [y/N]: " CONFIRM
if [[ "${CONFIRM}" != "y" && "${CONFIRM}" != "Y" ]]; then
log "User declined. Exiting."
exit 0
fi
echo ""
# ---- Step 0: Ensure required packages are installed -------------------------
log "Step 0: Checking required packages"
PACKAGES=(cifs-utils samba-client)
INSTALL_NEEDED=()
for pkg in "${PACKAGES[@]}"; do
if ! rpm -q "$pkg" &>/dev/null; then
INSTALL_NEEDED+=("$pkg")
fi
done
if [[ ${#INSTALL_NEEDED[@]} -gt 0 ]]; then
log "Installing: ${INSTALL_NEEDED[*]}"
dnf install -y "${INSTALL_NEEDED[@]}"
log "Packages installed"
else
log "All required packages already installed"
fi
# ---- Step 1: Select tenant --------------------------------------------------
echo ""
while true; do
read -rp "Enter tenant number (e.g. 1 for t1, 2 for t2): " TENANT_NUM
if [[ "$TENANT_NUM" =~ ^[0-9]+$ ]] && (( TENANT_NUM >= 1 )); then
break
fi
echo "Invalid tenant number. Enter a positive integer."
done
TENANT_PREFIX="t${TENANT_NUM}"
MOUNT_POINT="/mnt/${TENANT_PREFIX}-recordings"
log "Tenant selected: $TENANT_PREFIX"
log "Mount point will be: $MOUNT_POINT"
if mountpoint -q "$MOUNT_POINT" 2>/dev/null; then
die "$MOUNT_POINT is already mounted. Unmount it first if you need to reconfigure."
fi
# ---- Step 2: Gather connection details --------------------------------------
echo ""
echo "Enter the connection details for the remote Windows server."
echo ""
ATTEMPT=0
while (( ATTEMPT < MAX_ATTEMPTS )); do
ATTEMPT=$((ATTEMPT + 1))
if (( ATTEMPT > 1 )); then
echo ""
echo "Attempt ${ATTEMPT} of ${MAX_ATTEMPTS}. Re-enter connection details."
echo ""
fi
read -rp "Server IP address: " SERVER_IP
read -rp "Share name [recordings]: " SHARE_NAME
SHARE_NAME="${SHARE_NAME:-recordings}"
read -rp "Domain (leave blank if not domain joined): " SMB_DOMAIN
read -rp "Username: " SMB_USERNAME
read -rsp "Password: " SMB_PASSWORD
echo ""
# Build the smbclient auth string
if [[ -n "$SMB_DOMAIN" ]]; then
SMB_AUTH_STRING="${SMB_DOMAIN}/${SMB_USERNAME}%${SMB_PASSWORD}"
else
SMB_AUTH_STRING="${SMB_USERNAME}%${SMB_PASSWORD}"
fi
# ---- Step 3: Test access ------------------------------------------------
log "Step 3: Testing access to //${SERVER_IP}/${SHARE_NAME} (attempt ${ATTEMPT})"
echo ""
echo "Testing connection to //${SERVER_IP}/${SHARE_NAME} ..."
if smbclient "//${SERVER_IP}/${SHARE_NAME}" \
-U "${SMB_AUTH_STRING}" \
-c "ls" \
2>&1 | tee /tmp/smbtest_output.tmp | grep -qE "blocks of size|blocks available"; then
echo ""
echo "Connection successful."
log "Connection test passed"
rm -f /tmp/smbtest_output.tmp
break
else
echo ""
echo "Connection failed. Output from smbclient:"
echo ""
cat /tmp/smbtest_output.tmp
rm -f /tmp/smbtest_output.tmp
log "Connection test failed (attempt ${ATTEMPT})"
if (( ATTEMPT >= MAX_ATTEMPTS )); then
die "Failed after ${MAX_ATTEMPTS} attempts. Fix the issue and rerun the script."
fi
fi
done
# ---- Step 4: Create credentials file ----------------------------------------
CREDS_FILE="/root/.smb-${TENANT_PREFIX}-recordings"
log "Step 4: Creating credentials file at $CREDS_FILE"
if [[ -n "$SMB_DOMAIN" ]]; then
cat > "$CREDS_FILE" <<EOF
username=${SMB_USERNAME}
password=${SMB_PASSWORD}
domain=${SMB_DOMAIN}
EOF
else
cat > "$CREDS_FILE" <<EOF
username=${SMB_USERNAME}
password=${SMB_PASSWORD}
EOF
fi
chmod 0600 "$CREDS_FILE"
log "Credentials file created with 0600 permissions"
# ---- Step 5: Create mount point ---------------------------------------------
log "Step 5: Creating mount point"
mkdir -p "$MOUNT_POINT"
log "Mount point $MOUNT_POINT created"
# ---- Step 6: Add fstab entry ------------------------------------------------
log "Step 6: Configuring fstab"
FSTAB_LINE="//${SERVER_IP}/${SHARE_NAME} ${MOUNT_POINT} cifs credentials=${CREDS_FILE},vers=3.0,uid=${SAMBA_USER},gid=${SAMBA_USER},file_mode=0664,dir_mode=0775,noperm,_netdev 0 0"
if grep -qF "${MOUNT_POINT}" /etc/fstab; then
log "fstab entry for $MOUNT_POINT already exists, skipping"
echo ""
echo "WARNING: An fstab entry for $MOUNT_POINT already exists."
echo "Review /etc/fstab manually if you need to update it."
else
echo "$FSTAB_LINE" >> /etc/fstab
log "fstab entry added"
fi
# ---- Step 7: Mount -----------------------------------------------------------
log "Step 7: Mounting $MOUNT_POINT"
if mountpoint -q "$MOUNT_POINT"; then
log "$MOUNT_POINT is already mounted, skipping"
else
mount "$MOUNT_POINT"
log "$MOUNT_POINT mounted successfully"
fi
# ---- Verification ------------------------------------------------------------
log "Running post setup verification"
VERIFY_PASS=true
if ! mountpoint -q "$MOUNT_POINT"; then
log "VERIFY FAIL: $MOUNT_POINT is not mounted"
VERIFY_PASS=false
fi
if [[ ! -f "$CREDS_FILE" ]]; then
log "VERIFY FAIL: credentials file $CREDS_FILE does not exist"
VERIFY_PASS=false
fi
CREDS_PERMS=$(stat -c '%a' "$CREDS_FILE" 2>/dev/null || echo "missing")
if [[ "$CREDS_PERMS" != "600" ]]; then
log "VERIFY FAIL: credentials file permissions are $CREDS_PERMS, expected 600"
VERIFY_PASS=false
fi
if [[ "$VERIFY_PASS" == true ]]; then
log "All verifications passed"
echo ""
echo "============================================="
echo " SMB source mount configured successfully"
echo "============================================="
echo " Remote share : //${SERVER_IP}/${SHARE_NAME}"
echo " Mount point : $MOUNT_POINT"
echo " Credentials : $CREDS_FILE"
echo " SMB user : $SMB_USERNAME"
echo " Log file : $LOGFILE"
echo "============================================="
else
log "Some verifications failed. Review the log at $LOGFILE"
exit 1
fi