chuggerguy

browse linux drive image files

May 20th, 2025
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.27 KB | Software | 0 0
  1. #!/bin/bash
  2.  
  3. if [ "$(id -u)" != "0" ]; then
  4.   exec sudo "$0" "$@"
  5. fi
  6.  
  7. imgfile="$1"
  8. if [ ! -e $2 ]; then whereToMount="$2"; else whereToMount="mountPoint"; fi
  9. partition=`parted -s "$imgfile" unit B print | awk '{print $2}' | sed -n '/Start/,$p' | sed '1d' | sed '/^$/d' | sed 's/B//'`
  10. offset=($partition)
  11. numPartitions=${#offset[@]}
  12.  
  13. echo -e "\n$imgfile contains $numPartitions partitions\n"
  14.  
  15. for partNum in 0 $(($numPartitions-1)); do
  16.     offset="${offset[$partNum]}"
  17.     nextoffset="${offset[$partNum+1]}"
  18.     if [ ! -z $nextoffset ]; then
  19.         sizelimit=$(($nextoffset-$offset))
  20.     else
  21.         sizelimit=""
  22.     fi
  23.  
  24.     mkdir -p "$whereToMount/part$partNum"
  25.     mount -o loop,offset=$offset,sizelimit=$sizelimit $imgfile $whereToMount/part$partNum
  26. done
  27.  
  28. xdg-open $whereToMount 2>/dev/null 1>/dev/null
  29.  
  30. echo "Image should now be mounted and browsable"
  31. echo "Leave this terminal open while you browse the image"
  32. echo 'then come back and dismount the image by entering "Y"'
  33.     while [ "$response" != "y" ]; do
  34.         read -p "Dismount the image now? [Y/n]: " response
  35.         response=${response:-Y}
  36.         response="${response:0:1}"
  37.         response="${response,,}"
  38.     done
  39. umount $whereToMount/*
  40. rmdir $whereToMount/*
  41. rmdir $whereToMount
  42.  
  43. exit
  44.  
Advertisement
Add Comment
Please, Sign In to add comment