vbdm 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/bin/bash
  2. # VBDM (Virtual Block Device Manager)
  3. # Copyright Gradyn Wursten 2017
  4. # This software is licensed under the MIT license. See vbdm license for more info.
  5. echo "Virtual Block Device Manager 0.2"
  6. echo "Copyright Gradyn Wursten 2017: see vbdm license for more info."
  7. set -e
  8. set -o pipefail
  9. usage() {
  10. echo "Usage: vbdm (command) [arg1] [arg2]"
  11. echo " Commands:"
  12. echo " license: displays the MIT license with copyright notice"
  13. echo " create-ntfs (arg1: name of file, arg2: size in MiB): Creates an ntfs Virtual Block Device"
  14. echo " create-ext4 (arg1: name of file, arg2: size in MiB): Creates an ext4 Virtual Block Device"
  15. echo " delete (arg1: name of file): delete a Virtual Block Device"
  16. echo " mount (arg1: name of file): mount a Virtual Block Device"
  17. echo " unmount (arg1: name of file): unmount a Virtual Block Device"
  18. echo " dir (arg1: name of file): displays dir vitual block device is mounted (or would be mounted if it was mounted)"
  19. echo " make-real (arg1: name of file, arg2: device to clone to): Clones a Virtual Block Device onto an actual device"
  20. echo " make-virtual (arg1: device to clone from, arg2: name of file): Clones an actual device onto a Virtual Block Device"
  21. }
  22. if [[ "$@" == "" ]]; then
  23. usage
  24. exit 1
  25. fi
  26. if [[ "$1" == "create-ext4" ]]; then
  27. if [[ "$2" == "" || "$3" == "" ]]; then
  28. usage
  29. exit 1
  30. fi
  31. if [ -f $2 ]; then
  32. echo "File $2 allredy exists!"
  33. exit 2
  34. fi
  35. echo "Creating $2 with size $3M..."
  36. fallocate -l $3M $2
  37. echo "Creating ext4 filesystem on $2..."
  38. mkfs.ext4 $2
  39. echo "Mounting and setting permissions..."
  40. if [ -d /media/vbdm/$2 ]; then
  41. read -p "MOUNTPOINT ALLREDY EXISTS! I can try to unmount anything on it and delete it if you would like [y/n] " response
  42. if [ $response == "y" ]; then
  43. if grep -qs '/media/vbdm/$2' /proc/mounts; then
  44. umount /media/vbdm/$2
  45. fi
  46. sudo rmdir /media/vbdm/$2
  47. else
  48. echo "Cleaning up and aborting.."
  49. rm -r $2
  50. exit 1
  51. fi
  52. fi
  53. sudo mkdir /media/vbdm/$2
  54. sudo mount $2 /media/vbdm/$2
  55. sudo chown -R root:root /media/vbdm/$2
  56. sudo chmod -R 777 /media/vbdm/$2
  57. echo "Cleaning up..."
  58. sudo umount /media/vbdm/$2
  59. sudo rm -r /media/vbdm/$2
  60. echo "Done!"
  61. exit 1
  62. fi
  63. if [[ "$1" == "mount" ]]; then
  64. if [[ "$2" == "" ]]; then
  65. usage
  66. exit 1
  67. fi
  68. echo "Mounting $2 on /media/vbdm/$2..."
  69. sudo mkdir /media/vbdm/$2
  70. sudo mount $2 /media/vbdm/$2
  71. echo "Done!"
  72. exit 0
  73. fi
  74. if [[ "$1" == "unmount" ]]; then
  75. if [[ "$2" == "" ]]; then
  76. usage
  77. exit 1
  78. fi
  79. echo "Unmounting $2 from /media/vbdm/$2"
  80. sudo umount /media/vbdm/$2
  81. sudo rm -r /media/vbdm/$2
  82. echo "Done!"
  83. exit 0
  84. fi
  85. if [[ "$1" == "delete" ]]; then
  86. if [[ "$2" == "" ]]; then
  87. usage
  88. exit 1
  89. fi
  90. echo "Unmounting and deleting $2..."
  91. if grep -qs '/edia/vbdm/$2' /proc/mounts; then
  92. sudo umount /media/vbdm/$2
  93. fi
  94. rm $2
  95. if [ -f /media/vdbm/$2 ]; then
  96. rmdir /media/vbdm/$2
  97. fi
  98. echo "Done!"
  99. exit 0
  100. fi
  101. if [[ "$1" == "create-ntfs" ]]; then
  102. if [[ "$2" == "" || "$3" == "" ]]; then
  103. usage
  104. exit 1
  105. fi
  106. if [ -f $2 ]; then
  107. echo "file $2 allredy exists!"
  108. exit 2
  109. fi
  110. echo "creating $2 with size $3M..."
  111. fallocate -l $3M $2
  112. echo "Creating ntfs filesystem on $2..."
  113. #mkfs.ntfs will refuse to create partitions on a file without the force flag.
  114. #https://bugs.launchpad.net/nova/+bug/928749
  115. # -Q so it doesnt zero out the drive thats allredy zeros
  116. mkfs.ntfs -Q --force $2
  117. echo "Mounting and setting permissions..."
  118. if [ -d /media/vbdm/$2 ]; then
  119. read -p "MOUNTPOINT ALLREDY EXISTS! I can try to unmount anything on it and delete it if you would like [y/n] " response
  120. if [ $response == "y" ]; then
  121. if grep -qs '/media/vbdm/$2' /proc/mounts; then
  122. umount /media/vbdm/$2
  123. fi
  124. sudo rmdir /media/vbdm/$2
  125. else
  126. echo "Cleaning up and aborting.."
  127. rm -r $2
  128. exit 1
  129. fi
  130. fi
  131. sudo mkdir /media/vbdm/$2
  132. sudo mount $2 /media/vbdm/$2
  133. sudo chown -R root:root /media/vbdm/$2
  134. sudo chmod -R 777 /media/vbdm/$2
  135. echo "Cleaning up..."
  136. sudo umount /media/vbdm/$2
  137. sudo rmdir /media/vbdm/$2
  138. echo "Done!"
  139. exit 0
  140. fi
  141. if [[ $1 == "license" ]]; then
  142. echo ""
  143. echo "Copyright 2017 Gradyn Wursten and Sahibdeep Nann"
  144. echo 'Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:'
  145. echo 'The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.'
  146. echo 'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.'
  147. exit 0
  148. fi
  149. if [[ $1 == "dir" ]]; then
  150. if [[ $2 == "" ]]; then
  151. usage
  152. exit 1
  153. fi
  154. echo ""
  155. echo "/media/vbdm/$2"
  156. exit 0
  157. fi
  158. if [[ $1 == "make-real" ]]; then
  159. if [[ $2 == "" || $3 == "" ]]; then
  160. usage
  161. exit 1
  162. fi
  163. echo "Cloning $2 to $3..."
  164. dd if=$2 of=$3
  165. echo "Done!"
  166. fi
  167. if [[ $1 == "make-virtual" ]]; then
  168. if [[ $2 == "" || $3 == "" ]]; then
  169. usage
  170. exit 1
  171. fi
  172. echo "Cloning $3 to $2..."
  173. dd if=$3 of=$2
  174. echo "Done!"
  175. fi
  176. usage
  177. exit 1