From 61265674852721d210e35965a9dfa81e1866b382 Mon Sep 17 00:00:00 2001 From: nacho Date: Thu, 31 Aug 2017 20:57:51 +0200 Subject: [PATCH] nc-automount: complete overhaul --- etc/nextcloudpi-config.d/nc-automount.sh | 121 +++++++++++------------ update.sh | 31 ++---- 2 files changed, 62 insertions(+), 90 deletions(-) diff --git a/etc/nextcloudpi-config.d/nc-automount.sh b/etc/nextcloudpi-config.d/nc-automount.sh index b44c2e289..18fba5b93 100644 --- a/etc/nextcloudpi-config.d/nc-automount.sh +++ b/etc/nextcloudpi-config.d/nc-automount.sh @@ -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 < /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 diff --git a/update.sh b/update.sh index d48996b02..3ac3365f5 100755 --- a/update.sh +++ b/update.sh @@ -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; @@ -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