Skip to content

Commit

Permalink
nc-automount: complete overhaul
Browse files Browse the repository at this point in the history
  • Loading branch information
nachoparker committed Sep 1, 2017
1 parent b25fd33 commit c4d2e41
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 90 deletions.
121 changes: 56 additions & 65 deletions etc/nextcloudpi-config.d/nc-automount.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,87 +36,78 @@ IMPORTANT: halt or umount the drive before extracting" \

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
apt-get update
apt-get install -y --no-install-recommends udiskie

exit 1
cat > /etc/udev/rules.d/99-udisks2.rules <<'EOF'
ENV{ID_FS_USAGE}=="filesystem|other|crypto", ENV{UDISKS_FILESYSTEM_SHARED}="1"
EOF
chmod +x /usr/local/etc/blknum

systemctl daemon-reload
}
cat > /usr/lib/systemd/system/nc-automount.service <<'EOF'
[Unit]
Description=Automount USB drives
Before=mysqld.service
cleanup() { :; }
[Service]
Restart=always
ExecStart=/usr/bin/udiskie -NTF
ExecStartPost=/bin/sleep 8
ExecStartPost=/usr/local/etc/nc-automount-links
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
[Install]
WantedBy=multi-user.target
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"
cat > /usr/local/etc/nc-automount-links <<'EOF'
#!/bin/bash
# Get a label if present, otherwise specify one
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
ls -d /media/* &>/dev/null && {
# 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}"
# remove old links
for l in `ls /media/`; do
test -L /media/"$l" && rm /media/"$l"
done
# Exit
LABEL="exit"
# create links
i=0
for d in `ls -d /media/*`; do
[ $i -eq 0 ] && \
ln -sT "$d" /media/USBdrive || \
ln -sT "$d" /media/USBdrive$i
i=$(( i + 1 ))
done
}
EOF
chmod +x /usr/local/etc/nc-automount-links

[[ "$ACTIVE_" != "yes" ]] && {
rm -f /etc/udev/rules.d/50-automount.rules
sed -i '/# Rules for automounting both/,+11d' /etc/fstab
}
# adjust when mariaDB starts
local DBUNIT=/lib/systemd/system/mariadb.service
grep -q sleep $DBUNIT || sed -i "/^ExecStart=/iExecStartPre=/bin/sleep 10" $DBUNIT
}

# mount whatever is currently plugged in
udevadm control --reload-rules && udevadm trigger
configure()
{
[[ $ACTIVE_ != "yes" ]] && {
systemctl stop nc-automount
systemctl disable nc-automount
echo "automount disabled"
return 0
}
systemctl enable nc-automount
systemctl start nc-automount
echo "automount enabled"
}

[[ "$ACTIVE_" != "yes" ]] && echo "automount is now inactive" || echo "automount is now active"
cleanup()
{
apt-get autoremove -y
apt-get clean
rm /var/lib/apt/lists/* -r
rm -f /home/pi/.bash_history
systemctl disable ssh
}


# License
#
# This script is free software; you can redistribute it and/or modify it
Expand Down
31 changes: 6 additions & 25 deletions update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ cp etc/library.sh /usr/local/etc/

source /usr/local/etc/library.sh

# fix automount, reinstall if its old version
AMFILE=/usr/local/etc/nextcloudpi-config.d/nc-automount.sh
grep -q nc-automount.service $AMFILE || rm $AMFILE

# copy all files in bin and etc
for file in bin/* etc/*; do
[ -f "$file" ] || continue;
Expand Down Expand Up @@ -62,31 +66,8 @@ test -f /root/.my.cnf || echo -e "[client]\npassword=ownyourbits" > /root/.my.cn
chown www-data /var/www/nextcloud/.htaccess
rm -rf /var/www/nextcloud/.well-known

# fix automount
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

# fix ncp-notify-update
cat > /usr/local/bin/ncp-notify-update <<'EOF'
# fix ncp-notify-update
cat > /usr/local/bin/ncp-notify-update <<'EOF'
#!/bin/bash
VERFILE=/usr/local/etc/ncp-version
LATEST=/var/run/.ncp-latest-version
Expand Down

0 comments on commit c4d2e41

Please sign in to comment.