Welcome to mirror list, hosted at ThFree Co, Russian Federation.

nc-automount.sh « SYSTEM « ncp « bin - github.com/nextcloud/nextcloudpi.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b85e87763193f138094222987258e3005f4a4f7d (plain)
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
#!/bin/bash

# Automount configuration for NextCloudPi
#
# Copyleft 2017 by Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com>
# GPL licensed (see end of file) * Use at your own risk!
#
# More at https://ownyourbits.com/
#


install()
{
  apt-get update
  apt-get install -y --no-install-recommends udiskie inotify-tools

  cat > /etc/udev/rules.d/99-udisks2.rules <<'EOF'
ENV{ID_FS_USAGE}=="filesystem|other|crypto", ENV{UDISKS_FILESYSTEM_SHARED}="1"
EOF

  cat > /usr/lib/systemd/system/nc-automount.service <<'EOF'
[Unit]
Description=Automount USB drives
Before=mysqld.service dphys-swapfile.service fail2ban.service smbd.service nfs-server.service

[Service]
Restart=always
ExecStartPre=/bin/bash -c "rmdir /media/* || true"
ExecStart=/usr/bin/udiskie -NTF

[Install]
WantedBy=multi-user.target
EOF

  cat > /usr/lib/systemd/system/nc-automount-links.service <<'EOF'
[Unit]
Description=Monitor /media for mountpoints and create USBdrive* symlinks
Before=nc-automount.service

[Service]
Restart=always
ExecStart=/usr/local/etc/nc-automount-links-mon

[Install]
WantedBy=multi-user.target
EOF

  cat > /usr/local/etc/nc-automount-links <<'EOF'
#!/bin/bash

ls -d /media/* &>/dev/null && {

  # remove old links
  for l in $( ls /media/ ); do
    test -L /media/"$l" && rm /media/"$l"
  done

  # create links
  i=0
  for d in $( ls -d /media/* 2>/dev/null ); do
    if [ $i -eq 0 ]; then
      test -e /media/USBdrive   || test -d "$d" && ln -sT "$d" /media/USBdrive
    else
      test -e /media/USBdrive$i || test -d "$d" && ln -sT "$d" /media/USBdrive$i
    fi
    i=$(( i + 1 ))
  done

}
EOF
  chmod +x /usr/local/etc/nc-automount-links

  cat > /usr/local/etc/nc-automount-links-mon <<'EOF'
#!/bin/bash
inotifywait --monitor --event create --event delete --format '%f %e' /media/ | \
  grep --line-buffered ISDIR | while read f; do
    echo $f
    sleep 0.5
    /usr/local/etc/nc-automount-links
done
EOF
  chmod +x /usr/local/etc/nc-automount-links-mon
}

configure()
{
  [[ $ACTIVE != "yes" ]] && {
    systemctl stop    nc-automount
    systemctl stop    nc-automount-links
    systemctl disable nc-automount
    systemctl disable nc-automount-links
    rm -rf /etc/systemd/system/{mariadb,nfs-server,dphys-swapfile,fail2ban}.service.d
    systemctl daemon-reload
    echo "automount disabled"
    return 0
  }
  systemctl enable  nc-automount
  systemctl enable  nc-automount-links
  systemctl start   nc-automount
  systemctl start   nc-automount-links

  # create delays in some units
  mkdir -p /etc/systemd/system/mariadb.service.d
  cat > /etc/systemd/system/mariadb.service.d/ncp-delay-automount.conf <<'EOF'
[Service]
ExecStartPre=/bin/sleep 20
Restart=on-failure
EOF

  mkdir -p /etc/systemd/system/nfs-server.service.d
  cat > /etc/systemd/system/nfs-server.service.d/ncp-delay-automount.conf <<'EOF'
[Service]
ExecStartPre=
ExecStartPre=/bin/bash -c "/bin/sleep 30; /usr/sbin/exportfs -r"
EOF

  mkdir -p /etc/systemd/system/dphys-swapfile.service.d
  cat > /etc/systemd/system/dphys-swapfile.service.d/ncp-delay-automount.conf <<'EOF'
[Service]
ExecStartPre=/bin/sleep 30
EOF

  mkdir -p /etc/systemd/system/fail2ban.service.d
  cat > /etc/systemd/system/fail2ban.service.d/ncp-delay-automount.conf <<'EOF'
[Service]
ExecStartPre=/bin/sleep 10
EOF

  systemctl daemon-reload
  echo "automount enabled"
}

# License
#
# This script is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this script; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA  02111-1307  USA