fant0men

btrfs_umount.sh

Jun 28th, 2023 (edited)
985
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.83 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # This script is just meant to clean up the mountpoints of the Btrfs
  4. # (pseudo-RAID / JBOD) partition (data: single, metadata: raid1) on my
  5. # SD cards. For some reason it gets automatically remounted sometimes,
  6. # appending higher and higher numbers to the name of the mountpoint. So
  7. # this script will unmount all the ones except the first one.
  8.  
  9. regex="^\/run\/media\/${USER}\/SD_BTRFS([0-9]+)$"
  10.  
  11. mapfile -t lines < <(mount -t btrfs)
  12.  
  13. for (( i = 0; i < ${#lines[@]}; i++ )); do
  14.     line="${lines[${i}]}"
  15.  
  16.     mapfile -d' ' -t line_parts <<<"$line"
  17.     line_parts[-1]="${line_parts[-1]%$'\n'}"
  18.  
  19.     dev="${line_parts[0]}"
  20.     mnt_pt="${line_parts[2]}"
  21.  
  22.     if [[ ! -b $dev ]]; then
  23.         continue
  24.     fi
  25.  
  26.     if [[ $mnt_pt =~ $regex ]]; then
  27.         printf '\n%s\n' "${mnt_pt}: unmounting..."
  28.         sudo umount "$mnt_pt"
  29.     fi
  30. done
  31.  
  32. printf '\n'
  33.  
Advertisement
Add Comment
Please, Sign In to add comment