From 389c0132fa55d2e6f558460f668329f251366401 Mon Sep 17 00:00:00 2001 From: Phuntsok Drak-pa Date: Tue, 8 Oct 2019 14:57:34 +0200 Subject: [PATCH] added utilities for easy device mount and unmount --- .config/fish/config.fish | 1 + .config/i3/config##yadm.j2 | 2 + .local/bin/dmount | 105 +++++++++++++++++++++++++++++++++++++ .local/bin/dumount | 61 +++++++++++++++++++++ 4 files changed, 169 insertions(+) create mode 100755 .local/bin/dmount create mode 100755 .local/bin/dumount diff --git a/.config/fish/config.fish b/.config/fish/config.fish index 6c20f44..5903fe0 100644 --- a/.config/fish/config.fish +++ b/.config/fish/config.fish @@ -19,3 +19,4 @@ end set -gx PATH $HOME/go/bin $HOME/.cargo/bin $HOME/.local/bin $HOME/.gem/ruby/2.6.0/bin $PATH set -gx PKG_CONFIG_PATH /usr/local/lib/pkgconfig/ $PKG_CONFIG_PATH +set -gx SUDO_ASKPASS ~/.local/bin/askpass diff --git a/.config/i3/config##yadm.j2 b/.config/i3/config##yadm.j2 index 80b5479..3b6eb6c 100644 --- a/.config/i3/config##yadm.j2 +++ b/.config/i3/config##yadm.j2 @@ -209,6 +209,8 @@ bindsym $mod+$alt+n exec $term ssh Naro # Utilities bindsym $mod+Shift+h exec $term htop +bindsym $mod+Ctrl+m exec dmount +bindsym $mod+Ctrl+u exec dumount # Music shortcuts bindsym $alt+XF86AudioRaiseVolume exec mpc next diff --git a/.local/bin/dmount b/.local/bin/dmount new file mode 100755 index 0000000..525f7b0 --- /dev/null +++ b/.local/bin/dmount @@ -0,0 +1,105 @@ +#!/usr/bin/fish +# -*- mode: fish -*- +# Gives a dmenu prompt to mount unmounted drives. +# If they’re in /etc/fstab, they’ll be mounted automatically. +# Otherwise, you’ll be prompted to give a mountpoint from already existing directories. +# If you input a novel directory, it will prompt you to create that directory. + +# Inspired from https://github.com/ihebchagra/dotfiles/blob/master/.local/bin/dmount + +begin + set -l LFS + set a (math (jmtpfs -l | wc -l) - 2) # test if any android device is connected + test $a -ge 0 && jmtpfs -l 2> /dev/null | tail -n $a | read -z anddrives + lsblk -rpo "name,type,size,mountpoint" | awk '$2=="part"&&$4==""{printf "%s (%s)\n",$1,$3}' | read -z usbdrives + blkid /dev/sr0 | awk '{print $1}' | sed 's/://' | read -z cddrives +end +set -g basemount /mnt +rm -f /tmp/drives +touch /tmp/drives +test -n "$usbdrives" && echo "USB" >> /tmp/drives +test -n "$cddrives" && echo "CD" >> /tmp/drives +test -n "$anddrives" && echo "Android" >> /tmp/drives + +function getmount -d "Get the directory in which the device will be mounted" + set -g mp (for d in $basemount + find $d -maxdepth 5 -type d + end | rofi -dmenu -i -p 'Type in mount point.') + if test -z $mp || test $mp = "" + return 1 + end + if test ! -d $mp + switch (printf "No\\nYes" | rofi -dmenu -i -p "$mp does not exist. Create it?") + case 'Yes' + mkdir -p $mp || sudo -A mkdir -p $mp + case '*' + return 1 + end + end +end + +function mountusb -d "Mount a USB device" + set -g chosen (echo $usbdrives | rofi -dmenu -i -p "Mount which drive?" | awk '{print $1}') + test -z $chosen && return 1 + set -g alreadymounted (lsblk -nrpo "name,type,mountpoint" | awk '$2=="part"&&$3!~/\/boot|\/home$|SWAP/&&length($3)>1{printf "-not ( -path *%s -prune ) \n",$3}') + getmount + test -z $mp && return 1 + switch (lsblk -no "fstype" $chosen) + case "vfat" + sudo -A mount -t vfat $chosen $mp -o rw,umask=0000 + case "ntfs" + sudo -A mount -t ntfs $chosen $mp -o rw,umask=0000 + case '*' + sudo -A mount $chosen $mp + set user (whoami) + sudo -A chown -R $user:users $mp + end && notify-send -a "dmount" "💻 USB mounting" "$chosen mounted to $mp." +end + +function mountandroid -d "Mount an Android device" + set chosen (echo $anddrives | rofi -dmenu -i -p "Which Android device?" | awk '{print $1 $2}' | sed 's/,$//') + set bus (echo $chosen | sed 's/,.*//') + getmount "$HOME -maxdepth 3 -type d" + jmtpfs -device=$chosen $mp + echo "OK" | rofi -dmenu -i -p "Press (Allow) on your phone screen" + set newchosen (jmtpfs -l | grep $bus | awk '{print $1 $2}' | sed 's/,$//') + echo $newchosen + sudo -A umount $mp + jmtpfs -device=$newchosen $mp + notify-send "🤖 Android Mounting" "Android device mounted to $mp." +end + +function mountcd + set chosen (echo $cddrives) + getmount "$HOME -maxdepth 3 -type d" + sudo -A mount $chosen $mp && notify-send "💿 CD mounting" "$chosen mounted." && exit 0 +end + +function asktype + cat /tmp/drives + switch (cat /tmp/drives | rofi -dmenu -i -p "Mount which drive?") + case "USB" + mountusb + case "Android" + mountandroid + case "CD" + mountcd + end +end + +switch (wc -l < /tmp/drives) + case 0 + notify-send "No USB drive or Android device or CD detected" + case 1 + switch (cat /tmp/drives) + case "USB" + mountusb + case "Android" + mountandroid + case CD + mountCD + end + case '*' + asktype +end +rm /tmp/drives diff --git a/.local/bin/dumount b/.local/bin/dumount new file mode 100755 index 0000000..3a30b43 --- /dev/null +++ b/.local/bin/dumount @@ -0,0 +1,61 @@ +#!/bin/fish +# -*- mode: fish -*- +# A dmenu prompt to unmount drives. +# Provides you with mounted partitions, select one to unmount. +# Drives mounted at /, /boot and /home will not be options to unmount. + +# From https://github.com/ihebchagra/dotfiles/blob/master/.local/bin/dumount + +set -g drives (lsblk -nrpo "name,type,size,mountpoint" | awk '$2=="part"&&$4!~/\/boot|\/home$|SWAP/&&length($4)>1{printf "%s (%s)\n",$4,$3}') +set -g androids (awk '/jmtpfs/ {print $2}' /etc/mtab) +set -g cds (awk '/sr0/ {print $2}' /etc/mtab) +set -g undrivefile /tmp/undrives +rm -f $undrivefile +touch $undrivefile +test -n "$drives" && echo "USB" >> $undrivefile +test -n "$cds" && echo "CD" >> $undrivefile +test -n "$androids" && echo "Android" >> $undrivefile + +function unmountusb + test -z "$drives" && rm $undrivefile && return 0 + set chosen (echo $drives | rofi -dmenu -i -p "Unmount which drive?" | awk '{print $1}') + test -z "$chosen" && rm $undrivefile && return 0 + sudo -A umount $chosen && notify-send "💻 USB unmounting" "$chosen unmounted." +end + +function unmountandroid + set chosen (echo $androids | rofi -dmenu -i -p "Unmount which device?") + test -z "$chosen" && rm $undrivefile && return 0 + sudo -A umount -l $chosen && notify-send "🤖 Android unmounting" "$chosen unmounted." +end + +function unmountcd + set chosen (echo "$cds" | rofi -dmenu -i -p "Unmount which CD?") + test -z "$chosen" && rm $undrivefile && return 0 + sudo -A umount -l $chosen && notify-send "💿 CD unmounting" "$chosen unmounted." +end + +function asktype + switch (cat $undrivefile | rofi -dmenu -i -p "Unmount which type of device?") + case 'USB' + unmountusb + case 'CD' + unmountcd + case 'Android' + unmountandroid + end +end + +switch (wc -l < $undrivefile) + case 0 + notify-send "No USB drive or Android device or CD to unmount" + case 1 + switch (cat $undrivefile) + case 'USB' + unmountusb + case 'CD' + unmountcd + case 'Android' + unmountandroid + end +end