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
|
#!/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!
#
# Usage:
#
# ./installer.sh nc-automount.sh <IP> (<img>)
#
# See installer.sh instructions for details
#
# More at https://ownyourbits.com/
#
ACTIVE_=no
DESCRIPTION="Automount USB drives by plugging them in"
show_info()
{
whiptail --yesno \
--backtitle "NextCloudPi configuration" \
--title "Automount notes" \
"Plugged in USB drives will be automounted under /media
on boot or at the moment of insertion.
Format your drive as ext4 in order to move NC datafolder or database
VFAT or NTFS is not recommended for this task, as it does not suport permissions
Drives with multiple partitions are not supported
IMPORTANT: halt or umount the drive before extracting" \
20 90
}
install()
{
cat > /usr/local/etc/blknum <<'EOF'
#!/bin/bash
# we perform a cleanup with the first one
ls -d /dev/USBdrive* &>/dev/null || {
rmdir /media/USBdrive*
for f in `ls /media/`; do
test -L $f && rm $f
done
exit 0
}
for i in `seq 1 1 8`; do
test -e /media/USBdrive$i && continue
echo $i
exit 0
done
exit 1
EOF
chmod +x /usr/local/etc/blknum
systemctl daemon-reload
}
cleanup() { :; }
configure()
{
# FSTAB
[[ "$ACTIVE_" == "yes" ]] && {
grep -q /media/USBdrive8 /etc/fstab || cat >> /etc/fstab <<EOF
# Rules for automounting both at boot and upon USB plugin. Rely on udev rules
# Don't delete manually. Instead deactivate nc-automount
/dev/USBdrive /media/USBdrive auto defaults,noatime,auto,nofail 0 2
/dev/USBdrive1 /media/USBdrive1 auto defaults,noatime,auto,nofail 0 2
/dev/USBdrive2 /media/USBdrive2 auto defaults,noatime,auto,nofail 0 2
/dev/USBdrive3 /media/USBdrive3 auto defaults,noatime,auto,nofail 0 2
/dev/USBdrive4 /media/USBdrive4 auto defaults,noatime,auto,nofail 0 2
/dev/USBdrive5 /media/USBdrive5 auto defaults,noatime,auto,nofail 0 2
/dev/USBdrive6 /media/USBdrive6 auto defaults,noatime,auto,nofail 0 2
/dev/USBdrive7 /media/USBdrive7 auto defaults,noatime,auto,nofail 0 2
/dev/USBdrive8 /media/USBdrive8 auto defaults,noatime,auto,nofail 0 2
EOF
}
# UDEV
cat > /etc/udev/rules.d/50-automount.rules <<'EOF'
# Need to be a block device
KERNEL!="sd[a-z][0-9]", GOTO="exit"
# Import some useful filesystem info as variables
IMPORT{program}="/sbin/blkid -o udev -p %N"
# Need to be a filesystem
ENV{ID_FS_TYPE}!="vfat|ntfs|ext4|iso9660", GOTO="exit"
# Create symlink that will be understood by fstab, and a directory in /media
ACTION!="remove", PROGRAM="/usr/local/etc/blknum", RUN+="/bin/mkdir -p /media/USBdrive%c", SYMLINK+="USBdrive%c"
# Get a label if present, otherwise specify one
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
# Link with label name if exists
ACTION=="add", ENV{ID_FS_LABEL}!="", ENV{ID_FS_LABEL}!="USBdrive*", RUN+="/bin/rm /media/%E{ID_FS_LABEL}", RUN+="/bin/ln -sT /media/USBdrive%c /media/%E{ID_FS_LABEL}"
# Exit
LABEL="exit"
EOF
[[ "$ACTIVE_" != "yes" ]] && {
rm -f /etc/udev/rules.d/50-automount.rules
sed -i '/ # Rules for automounting both/,+11d' /etc/fstab
}
# mount whatever is currently plugged in
udevadm control --reload-rules && udevadm trigger
[[ "$ACTIVE_" != "yes" ]] && echo "automount is now inactive" || echo "automount is now active"
}
# 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
|