k98kurz

tcmount: script to mount/unmount truecrypt volumes easily

Aug 19th, 2015
243
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. if [[ "$1" != "m" && "$1" != "u" ]] || [ ${#2} -lt 1 ] || [[ "$1" != "u" && ${#3} -lt 1 ]]; then
  3.     echo "tcmount - mount or unmount a TrueCrypt volume"
  4.     echo "usage: $0 [m|u] mapmountname [path/to/file/or/device]"
  5.     echo "    m - mounts a volume container file/device"
  6.     echo "    u - unmounts a volume"
  7.     echo "mapmountname will be used to mkdirs in /media/`whoami` and /dev/mapper"
  8.     echo
  9.     exit 1
  10. fi
  11.  
  12. mountit () {
  13.     local uid=`id -u`
  14.     local user=`whoami`
  15.     local device=`sudo losetup -f`
  16.     local passwd=
  17.     local tcvol="$2"
  18.     local mapping=$1
  19.     if [ ! -e "$tcvol" ]; then echo "error: volume "$tcvol" does not exist"; exit 1; fi
  20.     if [ -d "$tcvol" ]; then echo "error: volume "$tcvol" is directory"; exit 1; fi
  21.     if [ ! -d "/home/$user/.tcmount" ]; then mkdir "/home/$user/.tcmount"; fi
  22.     if [ -e "/home/$user/$mapping" ]; then echo "error: mapping already exists for $mapping"; exit 1; fi
  23.     echo "preparing to mount $tcvol to /media/$user/$mapping"
  24.     echo $device > /home/$user/.tcmount/$mapping
  25.     sudo mkdir /media/$user/$mapping
  26.     sudo losetup $device "$tcvol"
  27.     sudo tcplay --map=$mapping --device=$device
  28.     sudo mount -o uid=$uid /dev/mapper/$mapping /media/$user/$mapping
  29.     exit 0
  30. }
  31.  
  32. unmountit () {
  33.     local uid=`id -u`
  34.         local user=`whoami`
  35.     local mapping=$1
  36.     if [ ! -d "/home/$user/.tcmount" ]; then echo "error: ~/.tcmount directory missing"; exit 1; fi
  37.     if [ ! -f "/home/$user/.tcmount/$mapping" ]; then echo "error: ~/.tcmount/$mapping file (contains loop name) is missing"; exit 1; fi
  38.     local device=`cat /home/$user/.tcmount/$mapping`
  39.     if [ ! -d "/media/$user/$mapping" ]; then echo "error: /media/$user/$mapping is not a directory or does not exist"; exit 1; fi
  40.     echo "preparing to unmount /media/$user/$mapping"
  41.     sudo umount /media/$user/$mapping
  42.     sudo tcplay -u /dev/mapper/$mapping
  43.     rm /home/$user/.tcmount/$mapping
  44.     sudo rmdir /media/$user/$mapping
  45.     exit 0
  46. }
  47.  
  48. if [ "$1" == "m" ]; then
  49.     mountit $2 "$3"
  50. elif [ "$1" == "u" ]; then
  51.     unmountit $2
  52. fi
RAW Paste Data