Skip to content

Instantly share code, notes, and snippets.

@cauethenorio
Last active June 21, 2019 03:03
Show Gist options
  • Select an option

  • Save cauethenorio/86cf8ff276a03fb945792f7777121d03 to your computer and use it in GitHub Desktop.

Select an option

Save cauethenorio/86cf8ff276a03fb945792f7777121d03 to your computer and use it in GitHub Desktop.

Revisions

  1. cauethenorio revised this gist Jun 21, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions install-automount.sh
    Original file line number Diff line number Diff line change
    @@ -48,9 +48,9 @@ FS_LABEL=\`lsblk -o name,label,UUID | grep \${PART} | awk '{print \$2"-"\$3}'\`
    if [ -z \${FS_LABEL} ]
    then
    /usr/bin/pmount --umask 000 --noatime -w --sync /dev/\${PART} /media/\${PART}
    /usr/bin/pmount --umask 000 --noatime -w /dev/\${PART} /media/\${PART}
    else
    /usr/bin/pmount --umask 000 --noatime -w --sync /dev/\${PART} /media/\${FS_LABEL}_\${PART}
    /usr/bin/pmount --umask 000 --noatime -w /dev/\${PART} /media/\${FS_LABEL}
    fi
    EOM

  2. cauethenorio revised this gist Jun 21, 2019. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions install-automount.sh
    Original file line number Diff line number Diff line change
    @@ -43,14 +43,14 @@ EOM
    cat > /usr/local/bin/automount <<-EOM
    #!/bin/bash
    PART=$1
    FS_LABEL=`lsblk -o name,label,UUID | grep ${PART} | awk '{print $2"-"$3}'`
    PART=\$1
    FS_LABEL=\`lsblk -o name,label,UUID | grep \${PART} | awk '{print \$2"-"\$3}'\`
    if [ -z ${FS_LABEL} ]
    if [ -z \${FS_LABEL} ]
    then
    /usr/bin/pmount --umask 000 --noatime -w --sync /dev/${PART} /media/${PART}
    /usr/bin/pmount --umask 000 --noatime -w --sync /dev/\${PART} /media/\${PART}
    else
    /usr/bin/pmount --umask 000 --noatime -w --sync /dev/${PART} /media/${FS_LABEL}_${PART}
    /usr/bin/pmount --umask 000 --noatime -w --sync /dev/\${PART} /media/\${FS_LABEL}_\${PART}
    fi
    EOM

  3. cauethenorio revised this gist Jun 21, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion install-automount.sh
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    set -e


    f [[ $EUID -ne 0 ]]; then
    if [[ $EUID -ne 0 ]]; then
    echo "This script must be run as root"
    exit 1
    fi
  4. cauethenorio created this gist Jun 21, 2019.
    60 changes: 60 additions & 0 deletions install-automount.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    #! /bin/bash

    # Source
    # https://www.raspberrypi.org/forums/viewtopic.php?t=192291
    # https://raspberrypi.stackexchange.com/questions/66169/auto-mount-usb-stick-on-plug-in-without-uuid

    set -e


    f [[ $EUID -ne 0 ]]; then
    echo "This script must be run as root"
    exit 1
    fi


    # This script mounts drives to /media/usb*, so make sure those
    # folders aren't occupied.
    # If you want a cleaner look, don't create any folders.
    apt-get install pmount


    # Udev rule
    cat > /etc/udev/rules.d/usbstick.rules <<- EOM
    ACTION=="add", KERNEL=="sd[a-z][0-9]", TAG+="systemd", ENV{SYSTEMD_WANTS}="usbstick-handler@%k"
    EOM


    # Systemd service
    cat > /lib/systemd/system/[email protected] <<-EOM
    [Unit]
    Description=Mount USB sticks
    BindsTo=dev-%i.device
    After=dev-%i.device
    [Service]
    Type=oneshot
    RemainAfterExit=yes
    ExecStart=/usr/local/bin/automount %I
    ExecStop=/usr/bin/pumount /dev/%I
    EOM


    cat > /usr/local/bin/automount <<-EOM
    #!/bin/bash
    PART=$1
    FS_LABEL=`lsblk -o name,label,UUID | grep ${PART} | awk '{print $2"-"$3}'`
    if [ -z ${FS_LABEL} ]
    then
    /usr/bin/pmount --umask 000 --noatime -w --sync /dev/${PART} /media/${PART}
    else
    /usr/bin/pmount --umask 000 --noatime -w --sync /dev/${PART} /media/${FS_LABEL}_${PART}
    fi
    EOM

    chmod +x /usr/local/bin/automount


    echo "Reboot to continue"