Root/ibrdtnd/files/mountcontainer.sh

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

Archive Download this file



interactive