[feat] vimrc, vifm, unmount_drives, backup
[dotfiles_afify.git] / .scripts / unmount_drives.sh
blobb4eb1a6293092ec931e09d075c95d0d6872ce2de
1 #!/usr/bin/env bash
3 #==============================================================================
4 # Name : unount_drives
5 # GitHub : Afify
6 # Copyright : MIT
7 # Version : 0.1
8 # Description : Unmount drives using dmenu.
9 # Notes :
10 # fusermount -u <mountpoint>>
11 # udisksctl mount -b /dev/sdb1
12 # udisksctl unmount -b /dev/sdd1
13 # udisksctl power-off -b /dev/sdd1
14 # $chosen:
15 # - awk first variable must contain digit (dont print /dev/sda )
16 # - and forth variable contains /mnt (means is mounted at /mnt)
17 # - for android case we find in /mnt for android* not empty directories
18 # this is not perfect solution to find mounted android devices.
19 # - if nothing is chosen exit 0
21 #==============================================================================
24 if [[ $(uname) == "OpenBSD" ]]; then
26 mounted=$(df | awk '{print $1" "$6}' | egrep -o '([cwsf]d[0-9]. /mnt.*)')
27 mount_point=$(echo "$mounted" | dmenu -l 10 | awk '{print $2}')
29 if [ $mount_point ]; then
30 dmenu -P -p "umount $mount_point | sudo " | sudo -S umount $mount_point\
31 && notify-send "Unmounted" "$mount_point"\
32 || notify-send -u critical "Error Unmounting" "$mount_point"
36 elif [[ $(uname) == "Linux" ]]; then
38 # df | awk '{print $1" "$6}' | grep "/mnt/" |\
39 chosen=$(\
40 lsblk --noheadings --raw -o NAME,SIZE,TYPE,MOUNTPOINT |\
41 awk '$1~/[[:digit:]]/ && $4~/\/mnt/;\
42 END {system("find /mnt/ -maxdepth 1 -type d\
43 -not -empty -name android*")}'|\
44 dmenu -i -p "Unmount volume" -l 10 )
46 partion=$(echo $chosen | awk '{print $1}') # sda#
47 file_type=$(echo $chosen | awk '{print $3}') # crypt | part
48 mount_point=$(echo $chosen | awk '{print $4}') # /mnt/usb1
49 android_d=$(echo $chosen | awk '/android/') # /mnt/.android
51 # android
52 if [ $android_d ]; then
53 fusermount -u $android_d\
54 && notify-send "Unmounted" "$android_d"\
55 || notify-send -u critical "Error Unmounting" "$android_d"
56 exit 0
59 ask_kill(){
60 resp=$(echo -e "no\nyes" | dmenu -p "( $active_ps ) ps running, Force kill ? ")
61 if [[ "$resp" == "yes" ]]; then
62 fuser -km $mount_point kill
63 # umount -l /dev/sdX lazy
64 unmount_func
65 else
66 exit
70 unmount_func(){
71 dmenu -P -p "umount $mount_point | sudo " | sudo -S umount $mount_point;\
72 sudo fsck /dev/$partion \
73 && notify-send "Unmounted" "$mount_point"\
74 || ask_kill
77 # Unmount partion
78 if [ $mount_point ]; then
79 active_ps=$(fuser -vm $mount_point 2> /dev/null)
80 active_ps=$(echo $active_ps | wc -l)
81 if [[ $active_ps -gt 0 ]]; then
82 ask_kill
83 else
84 unmount_func
88 # If the drive is encrypted lock after umount
89 if [ "$file_type" == "crypt" ]; then
90 dmenu -P -p "luksClose | sudo " |\
91 sudo -S cryptsetup luksClose /dev/mapper/$partion\
92 && notify-send "Locked" "$partion"\
93 || notify-send -u critical "Error Locking" "$partion"