1
0

mountcontainer.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/bin/sh
  2. #
  3. CONTAINER=`/sbin/uci -q get ibrdtn.storage.container`
  4. CPATH=`/sbin/uci -q get ibrdtn.storage.path`
  5. check_var() {
  6. if [ -z "$1" ]; then
  7. echo "$2"
  8. exit 1
  9. fi
  10. }
  11. check_path() {
  12. if [ ! -d "$1" ]; then
  13. echo "$2"
  14. return 1
  15. fi
  16. }
  17. check_file() {
  18. if [ ! -f "$1" ]; then
  19. echo "$2"
  20. exit 1
  21. fi
  22. }
  23. container_mount() {
  24. CONTAINER=`/sbin/uci -q get ibrdtn.storage.container`
  25. CPATH=`/sbin/uci -q get ibrdtn.storage.path`
  26. if [ -z "`mount | grep ' on $CPATH '`" ]; then
  27. # try to mount the container
  28. /bin/mount -o loop $CONTAINER $CPATH
  29. return $?
  30. fi
  31. return 0
  32. }
  33. container_reinitialize() {
  34. SIZE=`/sbin/uci get -q ibrdtn.storage.container_size`
  35. CONTAINER=`/sbin/uci -q get ibrdtn.storage.container`
  36. # try to rebuild the container
  37. if [ -n "$SIZE" ]; then
  38. /bin/rm -f $CONTAINER
  39. /usr/share/ibrdtn/mkcontainer.sh $CONTAINER $SIZE
  40. if [ $? -eq 0 ]; then
  41. container_mount
  42. return $?
  43. fi
  44. return 1
  45. fi
  46. return 1
  47. }
  48. check_var $CONTAINER "Storage container not set in uci.\nuse: uci set ibrdtn.storage.container=<container-file>"
  49. check_var $CPATH "Storage container mount path not set in uci.\nuse: uci set ibrdtn.storage.path=<mount-path>"
  50. check_path $CPATH "Storage container mount path does not exist."
  51. if [ $? -gt 0 ]; then
  52. /bin/mkdir -p $CPATH
  53. if [ $? -gt 0 ]; then
  54. echo "can not create container mount path."
  55. exit 1
  56. fi
  57. fi
  58. if [ "$1" == "-u" ]; then
  59. /bin/umount $CPATH
  60. exit 0
  61. fi
  62. if [ -n "`/bin/mount | grep $CPATH`" ]; then
  63. echo "Container already mounted"
  64. exit 0
  65. fi
  66. if [ ! -f "$CONTAINER" ]; then
  67. echo "Storage container file $CONTAINER does not exist."
  68. container_reinitialize
  69. exit $?
  70. fi
  71. # try to mount the container
  72. container_mount
  73. if [ $? -gt 0 ]; then
  74. echo -n "can not mount container file. checking... "
  75. /usr/sbin/e2fsck -p $CONTAINER
  76. if [ $? -gt 0 ]; then
  77. echo " error"
  78. echo "Container file $CONTAINER broken. Try to reinitialize the container."
  79. container_reinitialize
  80. if [ $? -eq 0 ]; then
  81. echo "container ready!"
  82. exit 0
  83. else
  84. exit 1
  85. fi
  86. fi
  87. echo "done"
  88. container_mount
  89. if [ $? -gt 0 ]; then
  90. echo "mount failed!"
  91. exit 1
  92. fi
  93. fi
  94. echo "container ready!"
  95. exit 0