-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_mount.sh
79 lines (62 loc) · 1.46 KB
/
add_mount.sh
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
#!/bin/bash
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <mount_path> <remote_nfs>"
exit 1
fi
path="${1}"
remote="${2}"
# Escape the mount path for use in systemd unit names
epath=$(systemd-escape --path "${path}")
# Create mount point if it does not exist
mkdir -p "${path}"
# Create the mount unit file
mfile="/etc/systemd/system/${epath}.mount"
cat > "${mfile}" << EOF
[Unit]
Description=NFS mount for ${path}
Requires=network-online.target
After=network-online.target
[Mount]
What=${remote}
Where=${path}
Type=nfs
Options=rsize=8192,wsize=8192,timeo=21,nofail
[Install]
WantedBy=multi-user.target
EOF
# Check if mount unit file creation was successful
if [ $? -ne 0 ]; then
echo "Failed to create mount unit file."
exit 1
fi
# Create the automount unit file
afile="/etc/systemd/system/${epath}.automount"
cat > "${afile}" << EOF
[Unit]
Description=Automount for ${path}
[Automount]
Where=${path}
TimeoutIdleSec=10
[Install]
WantedBy=multi-user.target
EOF
# Check if automount unit file creation was successful
if [ $? -ne 0 ]; then
echo "Failed to create automount unit file."
exit 2
fi
# Reload systemd manager configuration
systemctl daemon-reload
# Enable and start the mount unit
systemctl enable "${epath}.mount"
if [ $? -ne 0 ]; then
echo "Failed to enable mount unit."
exit 3
fi
systemctl start "${epath}.mount"
if [ $? -ne 0 ]; then
echo "Failed to start mount unit."
exit 4
fi
echo "NFS mount setup completed successfully."
exit 0