fant0men

Btrfs unmount script

Aug 2nd, 2020 (edited)
1,382
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # This script will automatically unmount any duplicated mount points for the
  3. # device specified. This script is meant for the pseudo-RAID (data: single,
  4. # metadata: raid1) Btrfs file system on my SD cards.
  5.  
  6. device=$(readlink -f "$1")
  7.  
  8. type=$(file -b "$device")
  9.  
  10. block_regex='^block special'
  11. num_regex='[[:digit:]]$'
  12.  
  13. if [[ -z $device ]]; then
  14.     echo -e "Usage: $(basename "$0") [dev]\n"
  15.     exit
  16. elif [[ ! $type =~ $block_regex ]]; then
  17.     echo 'Not a device file:'
  18.     echo -e "${device}\n"
  19.     exit
  20. fi
  21.  
  22. mount | grep "^${device} " | cut -d' ' -f3 | while read mountpoint; do
  23.     if [[ $mountpoint =~ $num_regex ]]; then
  24.         echo "${mountpoint}: unmounting..."
  25.         sudo umount "$mountpoint" || exit
  26.     fi
  27. done
  28.  
  29. echo
  30.  
RAW Paste Data