[feat] scripts use posix sh, gen_pass
[dotfiles_afify.git] / .scripts / unmount_drives.sh
blob64e2e52b3bc9e9bca9b8201c3f9c5fa6772bc090
1 #!/bin/sh
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}' | grep -Eo '([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"
35 elif [ "$(uname)" = "Linux" ]; then
37 # df | awk '{print $1" "$6}' | grep "/mnt/" |\
38 chosen=$(\
39 lsblk --noheadings --raw -o NAME,SIZE,TYPE,MOUNTPOINT |\
40 awk '$1~/[[:digit:]]/ && $4~/\/mnt/;\
41 END {system("find /mnt/ -maxdepth 1 -type d\
42 -not -empty -name android*")}'|\
43 dmenu -i -p "Unmount volume" -l 10 )
45 partition=$(echo $chosen | awk '{print $1}') # sda#
46 file_type=$(echo $chosen | awk '{print $3}') # crypt | part
47 mount_point=$(echo $chosen | awk '{print $4}') # /mnt/usb1
48 android_d=$(echo $chosen | awk '/android/') # /mnt/.android
50 # android
51 if [ $android_d ]; then
52 fusermount -u $android_d\
53 && notify-send "Unmounted" "$android_d"\
54 || notify-send -u critical "Error Unmounting" "$android_d"
55 exit 0
58 ask_kill(){
59 resp=$(echo -e "no\nyes" | dmenu -p "( $active_ps ) ps running, Force kill ? ")
60 if [ "$resp" = "yes" ]; then
61 fuser -km $mount_point kill
62 # umount -l /dev/sdX lazy
63 unmount_func
64 else
65 exit
69 unmount_func(){
70 sync;dmenu -P -p "umount $mount_point | sudo " | sudo -S umount $mount_point;\
71 sudo fsck /dev/$partition \
72 && notify-send "Unmounted" "$mount_point"\
73 || ask_kill
76 # Unmount partition
77 if [ $mount_point ]; then
78 active_ps=$(fuser -vm $mount_point 2> /dev/null)
79 active_ps=$(echo $active_ps | grep [0-9] | wc -l)
80 if [ $active_ps -gt 0 ]; then
81 ask_kill
82 else
83 unmount_func
87 # If the drive is encrypted lock after umount
88 if [ "$file_type" = "crypt" ]; then
89 dmenu -P -p "luksClose | sudo " |\
90 sudo -S cryptsetup luksClose /dev/mapper/$partition\
91 && notify-send "Locked" "$partition"\
92 || notify-send -u critical "Error Locking" "$partition"