-
Notifications
You must be signed in to change notification settings - Fork 0
/
unionfs_example.sh
executable file
·178 lines (142 loc) · 4.63 KB
/
unionfs_example.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/bin/sh
####################
### The read only file system is a loop back file.
###
TESTYRO_MNT=/tmp/testy_ro
TESTYRO_BIN=testy_ro.bin
TESTYRO_LOOPDEV="" # this will be determined based upon losetup -f
###
### The read write file system is a tmpfs
TESTYRW_MNT=/tmp/testy_rw
###
### The union file system is a composite of the read-only and read-write
TESTYU_MNT=/tmp/testy_u
######### Start by testing to see anything exists first
# check if its already mounted. If so remove it.
if mount | grep $TESTYU_MNT > /dev/null; then
echo "fs is mounted, unmount it"
umount $TESTYU_MNT
fi
# check if its already mounted. If so remove it.
if mount | grep $TESTYRO_MNT > /dev/null; then
echo "fs is mounted, unmount it"
umount $TESTYRO_MNT
fi
# check if its already mounted. If so remove it.
if mount | grep $TESTYRW_MNT > /dev/null; then
echo "fs is mounted, unmount it"
umount $TESTYRW_MNT
fi
#### create the read only fs
###############################################################
# determine if loopback device is still mapped. If so remove it.
TESTYRO_LOOPDEV=`losetup -a | grep $TESTYRO_BIN | cut --delimiter=: -f 1`
if [ ! -z $TESTYRO_LOOPDEV ]; then
echo "loopback device exists"
losetup -d $TESTYRO_LOOPDEV
fi
# at this point, the loopback mapping is removed
# and the file is not mounted. The mount point may exist.
#Make a 1 MB file if it does not exist
if [ -f "$TESTYRO_BIN" ]; then
echo "$TESTYRO_BIN exists"
else
echo "$TESTYRO_BIN does not exist. Make it."
dd if=/dev/zero of=$TESTYRO_BIN bs=512 count=2K
fi
# Find the free loopback file
TESTYRO_LOOPDEV=`losetup -f`
# Associate the loopback file with the file
losetup $TESTYRO_LOOPDEV $TESTYRO_BIN
#Format the file with 1% reserved block count
mkfs -t ext2 -m 1 -v $TESTYRO_LOOPDEV
# make the mount mount point
if [ -d "$TESTYRO_MNT" ]; then
echo "$TESTYRO_MNT exists. OK"
else
echo "$TESTYRO_MNT does not exist"
mkdir $TESTYRO_MNT
fi
# mount the read only fs
if [ -d "$TESTYRO_MNT" ]; then
echo "$TESTYRO_MNT exists. OK. Lets mount it."
mount -o rw $TESTYRO_LOOPDEV $TESTYRO_MNT
cat > $TESTYRO_MNT/read-only << EOF
This is a read-only file.
What does it look like on the readonly file system.
EOF
umount $TESTYRO_MNT
mount -o ro $TESTYRO_LOOPDEV $TESTYRO_MNT
if [ $? -eq 0 ]; then
echo "All good. FS created and mounted."
else
echo "mount failed."
fi
else
echo "$TESTYRO_MNT does not exist still"
echo "remove the loopback mapping and quit"
losetup -d $TESTYRO_LOOPDEV
exit 1
fi
#### create the read write fs
###############################################################
######### Start by testing to see anything exists first
# make the mount mount point
if [ -d "$TESTYRW_MNT" ]; then
echo "$TESTYRW_MNT exists. OK"
else
echo "$TESTYRW_MNT does not exist"
mkdir $TESTYRW_MNT
fi
# mount the read write fs
if [ -d "$TESTYRW_MNT" ]; then
echo "$TESTYRW_MNT exists. OK. Lets mount it."
mount -t tmpfs -o size=1M,nr_inodes=1k,mode=700 tmpfs $TESTYRW_MNT
if [ $? -eq 0 ]; then
echo "All good. FS created and mounted."
cat > $TESTYRW_MNT/read-write << EOF
This is a read-write file.
What does it look like on the readonly file system.
EOF
else
echo "mount failed."
fi
else
echo "$TESTYRW_MNT does not exist still"
exit 1
fi
#### Create the UnionFS mount
###############################################################
# make the mount mount point
if [ -d "$TESTYU_MNT" ]; then
echo "$TESTYU_MNT exists. OK"
else
echo "$TESTYU_MNT does not exist"
mkdir $TESTYU_MNT
fi
# mount the union fs
if [ -d "$TESTYU_MNT" ]; then
echo "$TESTYU_MNT exists. OK. Lets mount it."
mount -t unionfs -o dirs=/$TESTYRW_MNT=rw:$TESTYRO_MNT=ro $TESTYU_MNT $TESTYU_MNT
if [ $? -eq 0 ]; then
echo "All good. FS created and mounted."
else
echo "mount failed."
fi
else
echo "$TESTYU_MNT does not exist still"
exit 1
fi
####### Final check to see if everything is mounted
############################################################
if mount | grep $TESTYU_MNT > /dev/null; then
echo "$TESTYU_MNT is mounted. Good."
fi
# check if its already mounted. If so remove it.
if mount | grep $TESTYRO_MNT > /dev/null; then
echo "$TESTYRO_MNT is mounted. Good."
fi
# check if its already mounted. If so remove it.
if mount | grep $TESTYRW_MNT > /dev/null; then
echo "$TESTYRW_MNT is mounted. Good."
fi